java题目,io流问题
java题目,io流问题编写一个简单的名片管理系统,用于管理名片的信息(主要包括姓名、公司、职务、电话、QQ等信息)实现如下的功能:1)添加一张新名片2)显示系统中所有的...
java题目,io流问题编写一个简单的名片管理系统,用于管理名片的信息(主要包括姓名、公司、职务、电话、QQ等信息)实现如下的功能:
1)添加一张新名片
2)显示系统中所有的名片信息
3)删除某个人的名片
要求:
数据要存储到文件中 展开
1)添加一张新名片
2)显示系统中所有的名片信息
3)删除某个人的名片
要求:
数据要存储到文件中 展开
3个回答
展开全部
我这里有一个简单的学生管理系统,你只需要把Student学生类修改成名片类就可以了。你需要新建立一个java文件名为HW4.java,复制粘贴以下代码,编译运行就可以了。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class Student implements Comparable<Student>, Serializable{
/**
* Serializable UID: ensures serialize/de-serialize process will be successful.
*/
private static final long serialVersionUID = -3515442620523776933L;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int number;
private int age;
private double weight;
private String name;
public Student(int number, int age, double weight, String name) {
this.number = number;
this.age = age;
this.weight = weight;
this.name = name;
}
@Override
public int compareTo(Student o) {
if (this.age == o.age) {
return (int)(this.weight - o.weight);
}
return this.age - o.age;
}
}
class StudentSortByAge implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge();
}
}
class StudentSortByWeight implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return (int)(o1.getWeight() - o2.getWeight());
}
}
public class HW4 {
//passing one and only one instance of scanner instance reduce complexity of program.
public static void main(String[] args) {
System.out.println("\nWelcome to the System, Choose options below: ");
printPrompt();
Student[] students = null;
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextInt()) {
switch (scanner.nextInt()) {
case 1:
System.out.println("Print Student Information");
if (students == null) {
System.out.println("Please Initilise N students first");
} else {
printStudents(students);
}
printPrompt();
break;
case 2:
System.out.println("Enter numebr of students you want to create: ");
int number = scanner.nextInt();
students = initilise(number, scanner);
printPrompt();
break;
case 3:
System.out.println("Add a new student");
printPrompt();
if (students == null) {
System.out.println("Please Initilise N students first");
} else {
int newLength = students.length + 1;
students = Arrays.copyOf(students, newLength);
students[newLength - 1] = createStudent(scanner);
System.out.println("New student has been added.");
printPrompt();
}
break;
case 4:
System.out.println("Sorting by Age, Weight in Asce order");
if (students == null) {
System.out.println("Please Initilise N students first");
} else {
Student[] sortedStudents = students;
Arrays.sort(sortedStudents);
printStudents(sortedStudents);
}
break;
case 5:
System.out.println("Calcaulte Min, Max, Ave of Age and Weight");
if (students == null) {
System.out.println("Please Initilise N students first");
} else {
Student[] sortedAgeStudents = students;
Arrays.sort(sortedAgeStudents, new StudentSortByAge());
Student[] sortedWeightStudents = students;
Arrays.sort(sortedWeightStudents, new StudentSortByWeight());
int averageAge = 0;
double averageWeight = 0.0;
for (Student student : sortedAgeStudents) {
averageAge += student.getAge();
averageWeight += student.getWeight();
}
averageAge = averageAge / sortedAgeStudents.length;
averageWeight = averageWeight / sortedWeightStudents.length;
System.out.printf("Min Age: %d, Max Age: %d, Ave Age: %d\n", sortedAgeStudents[0].getAge(), sortedAgeStudents[sortedAgeStudents.length - 1].getAge(), averageAge);
System.out.printf("Min Weight: %f, Max Weight: %f, Ave Weight: %f\n", sortedWeightStudents[0].getWeight(), sortedWeightStudents[sortedWeightStudents.length - 1].getWeight(), averageWeight);
}
break;
case 6:
System.out.println("Write Student data into file");
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("studentsData"), true))) {
oos.writeObject(students);
printPrompt();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 7:
System.out.println("Read studentData from file");
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("studentsData")))) {
students = (Student[]) (ois.readObject());
printPrompt();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
scanner.close();
System.out.println("Quit");
break;
}
}
}
public static void printPrompt() {
System.out.println("1: Display current students");
System.out.println("2: Initilise N students");
System.out.println("3: Add new student");
System.out.println("4: Sorting by Age, Weight in Asce order");
System.out.println("5: Calcaulte Min, Max, Ave of Age and Weight");
System.out.println("6: Write Student data into file");
System.out.println("7: Read studentData from file");
}
public static Student[] initilise(int n, Scanner scanner) {
Student[] students = new Student[n];
for (int i = 0; i < n; i ++) {
students[i] = createStudent(scanner);
}
System.out.println("Initilisation of students complete.");
return students;
}
public static void printStudents(Student[] students) {
for (Student student : students) {
System.out.printf("Student: %s, Number: %d, Age: %d, Weight: %f\n", student.getName(), student.getNumber(), student.getAge(), student.getWeight());
}
}
public static void printSortedStudents(Student[] students) {
for (Student student : students) {
System.out.printf("Age: %d, Weight: %f, Student: %s, Number: %d\n", student.getAge(), student.getWeight(), student.getName(), student.getNumber());
}
}
public static Student createStudent(Scanner scanner) {
int studentNumber = 0, studentAge = 0;
double studentWeight = 0.0;
String studentName = null;
System.out.println("Enter Student Number: ");
while(scanner.hasNext()) {
studentNumber = scanner.nextInt();
System.out.println("Enter Student Age: ");
studentAge = scanner.nextInt();
System.out.println("Enter Student Weight: ");
//nextDouble仅仅读取double的值,在double值后的'\n'将会被nextLine()所读取,所以读取studentName时需要再读取一次nextLine()
studentWeight = scanner.nextDouble();
System.out.println("Enter Student Name: ");
scanner.nextLine();
studentName = scanner.nextLine();
break;
}
return new Student(studentNumber, studentAge, studentWeight, studentName);
}
}
2018-08-02 · 知道合伙人软件行家
关注
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/*
我的思路很简单,一个功能类,一个名片类!
1.检测本地磁盘是否有数据,如果没有默认建立一个名片模板
2.如果有,读入数据提出关键字作为学生对象参数使用!
3.使用集合TreeMap保存学生对象,以便操作!
4.功能类对读取后的集合内部数据进行增删改查动作!
5.第一个为功能类,第二个为名片类!
6.每步都有详细中文注释,不足之处还请指点!
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Scanner;
public class CardDemo {
static String path = "K:\\IO测试";// 1.名片路径!
static File file = new File(new File(path), "名片.txt");// 2.文件!
static BufferedReader br = null;// 3.读!
static BufferedWriter bw = null;//4.写!
static Student<String, Integer> stu = null;// 5.学生类!
static TreeMap<String, Student<String, Integer>> hs = null;// 6.集合!
static String name = null, phone = null, qq = null, company = null, post = null;
static Integer age = null;// 7.参数!
static Scanner sc = new Scanner(System.in);// 8.键盘录入!
public static void main(String[] args) throws IOException {
hs = new TreeMap<String, Student<String, Integer>>();// 初始化集合!
init();
}
public static void init() throws IOException {// 初始化
if (!file.exists()) {//如果本地文件不存在,就需要创建模板!
sop("数据不存在,启动模板创建功能!");
bw = new BufferedWriter(new FileWriter(file));
newFile();// 创建模板!
} else {//如果存在就读取
sop("数据存在,开始读取!");
br = new BufferedReader(new FileReader(file));
readFile();//读取方法!
}
}
// 如果本地文件存在,就读取+切割,提取关键数据,创建学生对象,与集合关联!
private static void readFile() throws IOException {
hs.clear();// 1.清空集合,准备保存对象使用!
String[] srr = new String[6];// 2.临时字符串数组,存入个人信息使用的!
int i = 0;// 数组,游标!
for (String str = br.readLine(); str != null; str = br.readLine()) {// 3.循环读取数据文件!
if (!(str.contains("|")))// 如果读取的这一行文本不含有|就跳过去!
continue;
// 循环切割提取关键字作为参数,新建学生对象封装在集合中!
srr[i++] = str.substring(str.indexOf(":")).replaceAll("[\\s:]+", "");
if (i >= 6) {
// 捕获关键数据,作为学生对象参数,使用!
stu = new Student<>(srr[0], Integer.parseInt(srr[1]), srr[2], srr[3], srr[4], srr[5]);
hs.put(srr[0], stu);
i = 0;
}
}
if (br != null) br.close();// 4.读取完毕,关流!
for (Iterator<String> it = hs.keySet().iterator(); it.hasNext();) {
System.out.println(hs.get(it.next()));// 5.循环读取结束,查看集合内容!
}
// 6.查看完毕是否进行修改或者添加?
sop("是否进行下一步操作?" + "\r\n0:保存退出系统" + "\r\n1:删除修改添加");
if (sc.nextLine().equalsIgnoreCase("1"))
setFile();
}
private static void newFile() throws IOException {// 如果没有,就新建一个模板!
for (int i = 1; i < 10; i++) {// 循环创建模板!
name = new String(i + ":张三");
phone = new String("13900123456" + i);
qq = "" + 6655238 + i;
age = 19 + i;
hs.put(name, new Student<String, Integer>(name, age, "公司", "职务", phone, qq));
}
writeFile();// 模板建立完毕!写入数据!
}
private static void setFile() throws IOException {//查找,添加与删除
if (hs.size() < 1) {
sop("内容为空,不进行操作,请返回!");
return;
}
sop("你想怎么操作?删除还是修改?\r\n0:删除动作\r\n1:修改添加");
name = sc.nextLine();// 判断删除还是修改?
if (name.equalsIgnoreCase("0")) {// 1.删除动作!
sop("请输入想要删除的姓名:");
name = sc.nextLine();
sop("以下为被删除名片:\r\n"+hs.remove(name));
} else {// 2.增加与修改!
sop("输入姓名:");
name = sc.nextLine();
sop("输入年龄:");
age = Integer.valueOf(sc.nextLine().replaceAll("[^\\d]", ""));
sop("输入公司:");
company = sc.nextLine();
sop("输入职务:");
post = sc.nextLine();
sop("输入电话:");
phone = sc.nextLine().replaceAll("[^\\d]", "");
sop("输入QQ号:");
qq = sc.nextLine().replaceAll("[^\\d]", "");
hs.put(name, new Student<String, Integer>(name, age, company, post, phone, qq));
}
sop("操作完成是否保存?\r\n0:放弃\r\n1:保存");
if (sc.nextLine().contains("1"))
writeFile();
}
private static void writeFile() throws IOException {// 保存:写出去!
if (hs.size() < 1) {
sop("内容为空,不进行操作,请返回!");
return;
}
if (bw == null) {// 写之前判断输出流是否关联到文件!
bw = new BufferedWriter(new FileWriter(file));
}
String tem = null;// 循环迭代,并调用写入方法,把集合内容写入数据库!
for (Iterator<String> it = hs.keySet().iterator(); it.hasNext();) {
tem = hs.get(it.next()).toString();
bw.write(tem);
bw.newLine();
bw.flush();
}
if (bw != null)bw.close();// 关流动作!
sop("操作完成是否进行下一步动作?\r\n0:退出\r\n1:继续");
if (sc.nextLine().contains("1"))
init();
}
private static <T> void sop(T t) {// 打印机!
System.out.println(t);
}
}//下面是名片类!
public class Student<T extends String, E extends Integer> implements Comparable<Student<T, E>>{
private T name;// 姓名!
private E age;// 年龄!
private T company;// 公司!
private T post;// 职务!
private T phone;// 电话!
private T QQ;// QQ!
public Student() {
}
public Student(T name, E age, T company, T post, T phone, T QQ) {
this.name = name;
this.age = age;
this.company = company;
this.post = post;
this.phone = phone;
this.QQ = QQ;
}
public T getName() {return name;}
public void setName(T name) {this.name = name;}
public E getAge() { return age;}
public void setAge(E age) { this.age = age;}
public T getCompany() { return company;}
public void setCompany(T company) {
this.company = company;
}
public T getPost() {return post;}
public void setPost(T post) {this.post = post;}
public T getPhone() {return phone;}
public void setPhone(T phone) { this.phone = phone; }
public T getQQ() {return QQ;}
public void setQQ(T qQ) {QQ = qQ;}
public int compareTo(Student<T, E> s) {
return this.name.compareTo(s.getName());
}
public int hashCode() {
return this.hashCode();
}
public boolean equals(Object obj) {
if (!(obj instanceof Student)) {
return false;
}
Student<T, E> s = (Student) obj;
return this.name.equals(s.getName()) && this.age == s.getAge();
}
public String toString() {
String str = "---------------------------------\r\n";
str += "|--姓名:\t\t\t" + this.getName() + "\r\n";
str += "|--年龄:\t\t\t" + this.getAge() + "\r\n";
str += ("|--公司:\t\t\t" + this.getCompany()) + "\r\n";
str += "|--职务:\t\t\t" + this.getPost() + "\r\n";
str += "|--电话:\t\t\t" + this.getPhone() + "\r\n";
str += "|--腾讯:\t\t\t" + this.getQQ() + "\r\n";
str += "---------------------------------";
return str;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询