设计Person类,有姓名、地址、电话号码和电子邮件等属性。其子类为Student和Employee,其中Employee类又有教员类Faculty和职员类staff。学生类有班级状态(大一、大二、大三或大四),这些状态为常量。Employee类有办公室、工资和受聘日期。定义一个名为MyDate的类,包含year、month和day数据域。Faculty类有办公时间和级别。Staff类有职务称号。重写每个类中的toString方法,显示相应的类名和人名。 编写测试类Test1,要求随机产生不同的对象,并将这些对象存储到数组中,并用方法printMessages(Person[] persons)将对象的信息输出。 版本2: 在上面实现类的基础上,为每个类增加一个将当前对象序列化到指定文件的方法writeToFile(File f)。为Student类创建一个新的子类Postgraduate,有研究方向和导师姓名两个新增数据域。编写测试类Test2,要求随机产生不同的对象,并将这些对象序列化到指定的文件中,并用方法printMessages(Person[] persons)将对象的信息输出。 版本3: 在版本2的基础上设计实现一个具有GUI界面的人员信息管理系统,要求实现基本的人员增、删、改、查的功能,人员信息列表应采用TableView组件。
时间: 2024-03-16 19:43:15 浏览: 292
好的,我来逐步帮你回答这些问题。
首先,我们需要设计一个Person类,代码如下:
```java
public class Person implements Serializable {
private String name;
private String address;
private String phoneNumber;
private String email;
public Person(String name, String address, String phoneNumber, String email) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
this.email = email;
}
@Override
public String toString() {
return "Person: " + name;
}
public void writeToFile(File f) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(this);
oos.close();
}
}
```
这个类包含了姓名、地址、电话号码和电子邮件等属性,还有一个名为MyDate的类,我们可以在这个类中定义。同时,这个类实现了Serializable接口,为后面的序列化操作做准备。
接下来,我们需要设计子类Student和Employee,其中Employee类又有教员类Faculty和职员类Staff。代码如下:
```java
public class Student extends Person {
public static final int FRESHMAN = 1;
public static final int SOPHOMORE = 2;
public static final int JUNIOR = 3;
public static final int SENIOR = 4;
private int status;
public Student(String name, String address, String phoneNumber, String email, int status) {
super(name, address, phoneNumber, email);
this.status = status;
}
@Override
public String toString() {
return "Student: " + super.name;
}
public void writeToFile(File f) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(this);
oos.close();
}
}
public class Employee extends Person {
private String office;
private double salary;
private MyDate dateHired;
public Employee(String name, String address, String phoneNumber, String email, String office, double salary, MyDate dateHired) {
super(name, address, phoneNumber, email);
this.office = office;
this.salary = salary;
this.dateHired = dateHired;
}
@Override
public String toString() {
return "Employee: " + super.name;
}
public void writeToFile(File f) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(this);
oos.close();
}
}
public class Faculty extends Employee {
private String officeHours;
private String rank;
public Faculty(String name, String address, String phoneNumber, String email, String office, double salary, MyDate dateHired, String officeHours, String rank) {
super(name, address, phoneNumber, email, office, salary, dateHired);
this.officeHours = officeHours;
this.rank = rank;
}
@Override
public String toString() {
return "Faculty: " + super.name;
}
public void writeToFile(File f) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(this);
oos.close();
}
}
public class Staff extends Employee {
private String title;
public Staff(String name, String address, String phoneNumber, String email, String office, double salary, MyDate dateHired, String title) {
super(name, address, phoneNumber, email, office, salary, dateHired);
this.title = title;
}
@Override
public String toString() {
return "Staff: " + super.name;
}
public void writeToFile(File f) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(this);
oos.close();
}
}
```
这些子类继承了Person类的属性,同时还有自己的特有属性。同时,这些类也实现了Serializable接口,并且重写了toString方法和writeToFile方法。
接着,我们需要为每个类创建一个名为MyDate的类,代码如下:
```java
public class MyDate implements Serializable {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public String toString() {
return month + "/" + day + "/" + year;
}
}
```
这个类包含了年、月、日三个属性,并且也实现了Serializable接口。
接下来,我们需要编写测试类Test1,随机产生不同的对象,并将这些对象存储到数组中,并用方法printMessages(Person[] persons)将对象的信息输出。代码如下:
```java
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class Test1 {
public static void main(String[] args) throws IOException {
Person[] persons = new Person[10];
Random rand = new Random();
for (int i = 0; i < 10; i++) {
int type = rand.nextInt(4);
String name = "Person " + i;
String address = "Address " + i;
String phoneNumber = "Phone " + i;
String email = "Email " + i;
switch (type) {
case 0:
int status = rand.nextInt(4) + 1;
persons[i] = new Student(name, address, phoneNumber, email, status);
break;
case 1:
String office = "Office " + i;
double salary = rand.nextDouble() * 10000;
MyDate dateHired = new MyDate(2021, rand.nextInt(12) + 1, rand.nextInt(28) + 1);
persons[i] = new Employee(name, address, phoneNumber, email, office, salary, dateHired);
break;
case 2:
String office2 = "Office " + i;
double salary2 = rand.nextDouble() * 10000;
MyDate dateHired2 = new MyDate(2021, rand.nextInt(12) + 1, rand.nextInt(28) + 1);
String officeHours = "9am-5pm";
String rank = "Professor";
persons[i] = new Faculty(name, address, phoneNumber, email, office2, salary2, dateHired2, officeHours, rank);
break;
case 3:
String office3 = "Office " + i;
double salary3 = rand.nextDouble() * 10000;
MyDate dateHired3 = new MyDate(2021, rand.nextInt(12) + 1, rand.nextInt(28) + 1);
String title = "Secretary";
persons[i] = new Staff(name, address, phoneNumber, email, office3, salary3, dateHired3, title);
break;
}
}
for (Person p : persons) {
System.out.println(p.toString());
}
File file = new File("person.dat");
persons[0].writeToFile(file);
}
}
```
这个类随机生成10个不同的对象,并将这些对象存储到数组中。然后,我们遍历这个数组,输出每个对象的信息。最后,我们将第一个对象序列化到文件中。
接着,我们需要在版本2的基础上为Student类创建一个新的子类Postgraduate,有研究方向和导师姓名两个新增数据域。代码如下:
```java
public class Postgraduate extends Student {
private String researchDirection;
private String advisorName;
public Postgraduate(String name, String address, String phoneNumber, String email, int status, String researchDirection, String advisorName) {
super(name, address, phoneNumber, email, status);
this.researchDirection = researchDirection;
this.advisorName = advisorName;
}
@Override
public String toString() {
return "Postgraduate: " + super.name;
}
public void writeToFile(File f) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(this);
oos.close();
}
}
```
这个类继承了Student类,同时还有自己的特有属性。同时,这个类也实现了Serializable接口,并且重写了toString方法和writeToFile方法。
最后,我们需要编写测试类Test2,随机生成不同的对象,并将这些对象序列化到指定的文件中,并用方法printMessages(Person[] persons)将对象的信息输出。代码如下:
```java
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Random;
public class Test2 {
public static void main(String[] args) throws IOException {
Person[] persons = new Person[10];
Random rand = new Random();
for (int i = 0; i < 10; i++) {
int type = rand.nextInt(5);
String name = "Person " + i;
String address = "Address " + i;
String phoneNumber = "Phone " + i;
String email = "Email " + i;
switch (type) {
case 0:
int status = rand.nextInt(4) + 1;
persons[i] = new Student(name, address, phoneNumber, email, status);
break;
case 1:
String office = "Office " + i;
double salary = rand.nextDouble() * 10000;
MyDate dateHired = new MyDate(2021, rand.nextInt(12) + 1, rand.nextInt(28) + 1);
persons[i] = new Employee(name, address, phoneNumber, email, office, salary, dateHired);
break;
case 2:
String office2 = "Office " + i;
double salary2 = rand.nextDouble() * 10000;
MyDate dateHired2 = new MyDate(2021, rand.nextInt(12) + 1, rand.nextInt(28) + 1);
String officeHours = "9am-5pm";
String rank = "Professor";
persons[i] = new Faculty(name, address, phoneNumber, email, office2, salary2, dateHired2, officeHours, rank);
break;
case 3:
String office3 = "Office " + i;
double salary3 = rand.nextDouble() * 10000;
MyDate dateHired3 = new MyDate(2021, rand.nextInt(12) + 1, rand.nextInt(28) + 1);
String title = "Secretary";
persons[i] = new Staff(name, address, phoneNumber, email, office3, salary3, dateHired3, title);
break;
case 4:
int status2 = rand.nextInt(4) + 1;
String researchDirection = "Research " + i;
String advisorName = "Advisor " + i;
persons[i] = new Postgraduate(name, address, phoneNumber, email, status2, researchDirection, advisorName);
break;
}
}
for (Person p : persons) {
System.out.println(p.toString());
File file = new File(p.getClass().getName() + "_" + p.hashCode() + ".dat");
p.writeToFile(file);
}
}
}
```
这个类随机生成10个不同的对象,并将这些对象序列化到不同的文件中。同时,我们遍历这个数组,输出每个对象的信息。
最后,我们需要在版本3的基础上设计实现一个具有GUI界面的人员信息管理系统,要求实现基本的人员增、删、改、查的功能,人员信息列表应采用TableView组件。在这里,我建议你使用JavaFX来实现GUI界面,因为JavaFX提供了丰富的UI组件和布局方式,可以很方便地构建出一个美观且易用的界面。
阅读全文