类设计
1.1 父类抽象类
成员属性: id(编号) name(姓名) sex(性别) birthday(生日) age(年龄-由生日计算得出)构造方法: 无参构造 全参构造 成员方法: toString()抽象方法: getType():由各子类实现,返回各自的"类型"字符串。 getWork():由各子类实现,返回各自的"工作"字符串。
1.2 子类Student
构造方法 无参构造 全参构造(super调用父类全参构造)重写抽象方法 重写getType() 重写getWork()
1.3 子类Teacher
构造方法 无参构造 全参构造(super调用父类全参构造)重写抽象方法 重写getType() 重写getWork()
1.4 工具类
全局变量 学员ID值(添加学员信息时,编号由此ID加1生成)教师ID值(添加教师信息时,编号由此ID加1生成)全局方法 根据生日计算年龄的方法 打印一个Person对象的方法;打印一个ArrayList<? extends Person>集合的方法
1.5 启动类
定义启动类:MainApp启动程序
程序设计
2.1 父类Person类
public abstract class Person {
private int id
;
private String name
;
private String gender
;
private String birthday
;
private int age
;
public Person() {
}
public Person(int id
, String name
, String gender
, String birthday
) {
this.id
= id
;
this.name
= name
;
this.gender
= gender
;
this.birthday
= birthday
;
}
public abstract String
getType();
public abstract String
getWork();
public int getId() {
return id
;
}
public void setId(int id
) {
this.id
= id
;
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public String
getGender() {
return gender
;
}
public void setGender(String gender
) {
this.gender
= gender
;
}
public String
getBirthday() {
return birthday
;
}
public void setBirthday(String birthday
) {
this.birthday
= birthday
;
}
public int getAge() {
age
= Utils
.birthday(this.getBirthday());
return age
;
}
public void setAge(int age
) {
this.age
= age
;
}
@Override
public String
toString() {
return id
+ "\t\t" +
name
+ "\t\t" +
gender
+ "\t\t" +
birthday
+ "\t" +
this.getAge() + "\t\t" +
"我是一名" + getType() + "我的工作是" + getWork();
}
}
2.2 学生类
public class Stuent extends Person {
public Stuent() {
}
public Stuent(int id
, String name
, String gender
, String birthday
) {
super(id
, name
, gender
, birthday
);
}
@Override
public String
getType() {
return "学习Java";
}
@Override
public String
getWork() {
return "学生";
}
}
2.3 老师类
public class Teacher extends Person {
public Teacher() {
}
public Teacher(int id
, String name
, String gender
, String birthday
) {
super(id
, name
, gender
, birthday
);
}
@Override
public String
getType() {
return "教师";
}
@Override
public String
getWork() {
return "讲课";
}
}
2.4 工具类
import java
.text
.ParseException
;
import java
.text
.SimpleDateFormat
;
import java
.util
.ArrayList
;
import java
.util
.Calendar
;
import java
.util
.Date
;
public class Utils {
public static int stuID
;
public static int teaID
;
public static int birthday(String birthday
) {
SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
Date birthdayDate
=null
;
try {birthdayDate
= sdf
.parse(birthday
);
} catch (ParseException e
) { return -1;
}
Calendar c
= Calendar
.getInstance();
c
.setTime(birthdayDate
);
c
.get(Calendar
.YEAR
);
Calendar c1
= Calendar
.getInstance();
c1
.get(Calendar
.MONTH
);
if (c1
.before(c
)) {
return -1;
}
if (c
.get(Calendar
.MONTH
) > c1
.get(Calendar
.MONTH
)) {
return c1
.get(Calendar
.YEAR
) - c
.get(Calendar
.YEAR
) - 1;
} else if (c
.get(Calendar
.MONTH
) < c1
.get(Calendar
.MONTH
)) {
return c1
.get(Calendar
.YEAR
) - c
.get(Calendar
.YEAR
);
} else{
if (c
.get(Calendar
.DAY_OF_MONTH
) > c1
.get(Calendar
.DAY_OF_MONTH
)) {
return c1
.get(Calendar
.YEAR
) - c
.get(Calendar
.YEAR
) - 1;
} else if (c
.get(Calendar
.DAY_OF_MONTH
) < c1
.get(Calendar
.DAY_OF_MONTH
)) {
return c1
.get(Calendar
.YEAR
) - c
.get(Calendar
.YEAR
);
} else{
return c1
.get(Calendar
.YEAR
) - c
.get(Calendar
.YEAR
);
}
}
}
public static void PersonList(ArrayList
<? extends Person> personlist
) {
System
.out
.println("***************************************************");
System
.out
.println("编号\t\t姓名\t\t性别\t\t生日\t\t\t年龄\t\t描述");
for (int i
= 0; i
< personlist
.size(); i
++) {
System
.out
.println(personlist
.get(i
));
}
}
public static void printPerson(Person person
) {
System
.out
.println("***************************************************");
System
.out
.println("编号\t\t姓名\t\t性别\t\t生日\t\t\t年龄\t\t描述");
System
.out
.println(person
);
}
}
2.5 启动类
import java
.util
.ArrayList
;
import java
.util
.Scanner
;
public class MainApp {
public static void main(String
[] args
) {
Scanner sc
= new Scanner(System
.in
);
ArrayList
<Stuent> stuents
= new ArrayList<>();
ArrayList
<Teacher> teachers
= new ArrayList<>();
while (true) {
System
.out
.println("1.学员信息管理 2.教师信息管理 3.退出");
int a
= sc
.nextInt();
switch (a
) {
case 1:
studentManage(stuents
, sc
);
break;
case 2:
teacherManage(teachers
, sc
);
break;
case 3:
System
.out
.println("谢谢使用");
System
.exit(0);
default:
System
.out
.println("输入有误");
break;
}
}
}
private static void teacherManage(ArrayList
<Teacher> teachers
, Scanner sc
) {
while (true) {
System
.out
.println("教师管理系统");
System
.out
.println("1.添加教师信息 2.修改教师信息 3.删除教师信息 4.查询教师 5.返回");
int a
= sc
.nextInt();
switch (a
) {
case 1:
addTeacher(teachers
, sc
);
break;
case 2:
updateTeacher(teachers
, sc
);
break;
case 3:
delTeacher(teachers
, sc
);
break;
case 4:
findTeacher(teachers
);
break;
case 5:
return;
}
}
}
private static void addTeacher(ArrayList
<Teacher> teachers
, Scanner sc
) {
System
.out
.println("输入姓名");
String name
= sc
.next();
System
.out
.println("输入性别");
String gender
= sc
.next();
System
.out
.println("输入生日");
String birthday
= sc
.next();
teachers
.add(new Teacher(++Utils
.teaID
, name
, gender
, birthday
));
System
.out
.println("添加成功");
}
private static void updateTeacher(ArrayList
<Teacher> teachers
, Scanner sc
) {
System
.out
.println("请输入要改教师的id");
int updateid
= sc
.nextInt();
for (int i
= 0; i
< teachers
.size(); i
++) {
Teacher tea
= teachers
.get(i
);
if (tea
.getId() == updateid
) {
Utils
.printPerson(tea
);
System
.out
.println("其输入新的姓名(保留输入0)");
String newName
= sc
.next();
System
.out
.println("输入新的性别(保留输入0)");
String newGender
= sc
.next();
System
.out
.println("输入新的生日(保留输入0)");
String newbirthday
= sc
.next();
if (!"0".equals(newName
)) {
tea
.setName(newName
);
}
if (!"0".equals(newGender
)) {
tea
.setGender(newGender
);
}
if (!"0".equals(newbirthday
)) {
tea
.setBirthday(newbirthday
);
}
System
.out
.println("修改成功");
return;
}
}
System
.out
.println("没有找到" + updateid
+ "id");
}
private static void delTeacher(ArrayList
<Teacher> teachers
, Scanner sc
) {
System
.out
.println("请输入要删除学教师的的id");
int delid
= sc
.nextInt();
for (int i
= 0; i
< teachers
.size(); i
++) {
Teacher tea
= teachers
.get(i
);
if (delid
== tea
.getId()) {
Utils
.printPerson(tea
);
System
.out
.println("是否要删除这条信息(y/n)");
String input
= sc
.next();
if ("y".equals(input
)) {
teachers
.remove(i
);
System
.out
.println("删除成功");
return;
} else if ("n".equals(input
)) {
System
.out
.println("操作被取消");
return;
}
}
}
System
.out
.println("没有找到id为" + delid
+ "的成员");
}
private static void findTeacher(ArrayList
<Teacher> teachers
) {
if (teachers
.size() == 0) {
System
.out
.println("查询不到数据");
} else {
Utils
.PersonList(teachers
);
}
}
private static void studentManage(ArrayList
<Stuent> stuents
, Scanner sc
) {
while (true) {
System
.out
.println("学生管理系统");
System
.out
.println("1.添加学员 2.修改学员 3.删除学员 4.查询学员 5.返回");
int a
= sc
.nextInt();
switch (a
) {
case 1:
addStudent(stuents
, sc
);
break;
case 2:
updateStudent(stuents
, sc
);
break;
case 3:
delStudent(stuents
, sc
);
break;
case 4:
findStudent(stuents
);
break;
case 5:
return;
}
}
}
private static void addStudent(ArrayList
<Stuent> stuents
, Scanner sc
) {
System
.out
.println("输入姓名");
String name
= sc
.next();
System
.out
.println("输入性别");
String gender
= sc
.next();
System
.out
.println("输入生日");
String birthday
= sc
.next();
stuents
.add(new Stuent(++Utils
.stuID
, name
, gender
, birthday
));
System
.out
.println("添加成功");
}
private static void updateStudent(ArrayList
<Stuent> stuents
, Scanner sc
) {
System
.out
.println("请输入要改学生的id");
int updateid
= sc
.nextInt();
for (int i
= 0; i
< stuents
.size(); i
++) {
Stuent stu
= stuents
.get(i
);
if (stu
.getId() == updateid
) {
Utils
.printPerson(stu
);
System
.out
.println("其输入新的姓名(保留输入0)");
String newName
= sc
.next();
System
.out
.println("输入新的性别(保留输入0)");
String newGender
= sc
.next();
System
.out
.println("输入新的生日(保留输入0)");
String newbirthday
= sc
.next();
if (!"0".equals(newName
)) {
stu
.setName(newName
);
}
if (!"0".equals(newGender
)) {
stu
.setGender(newGender
);
}
if (!"0".equals(newbirthday
)) {
stu
.setBirthday(newbirthday
);
}
System
.out
.println("修改成功");
return;
}
}
System
.out
.println("没有找到" + updateid
+ "id");
}
private static void delStudent(ArrayList
<Stuent> stuents
, Scanner sc
) {
System
.out
.println("请输入要删除学生的的学号");
int delid
= sc
.nextInt();
for (int i
= 0; i
< stuents
.size(); i
++) {
Stuent stu
= stuents
.get(i
);
if (delid
== stu
.getId()) {
Utils
.printPerson(stu
);
System
.out
.println("是否要删除这条信息(y/n)");
String input
= sc
.next();
if ("y".equals(input
)) {
stuents
.remove(i
);
System
.out
.println("删除成功");
return;
} else if ("n".equals(input
)) {
System
.out
.println("操作被取消");
return;
}
}
}
System
.out
.println("没有找到id为" + delid
+ "的成员");
}
private static void findStudent(ArrayList
<Stuent> stuents
) {
System
.out
.println("查询结果");
if (stuents
.size() == 0) {
System
.out
.println("没有数据");
} else {
Utils
.PersonList(stuents
);
}
}
}