此程序主要是为了演示管理学生信息,用于展示信息,为初级教学,这个教学呢,主要是让我门对程序开发有一些逻辑上的认识,对于初学者来讲可以着重去看看。
如遇到不解可咨询,Email [email protected]
package Student;
/**
* @Author xgp
* @Date 2022/7/22 10:04
* @PackageName:Student
* @ClassName: StudentEntity
* @Description: TODO
* @Version 1.0
*/
public class StudentEntity {
private String id;
private String name;
private String age;
private String phone;
@Override
public String toString() {
return "StudentEntity{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age='" + age + '\'' +
", phone='" + phone + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
static List list = new ArrayList();
public static void add() {
System.out.println("------------添加信息------------");
StudentEntity studentEntity = new StudentEntity();
System.out.print("学号:");
while (true) {
Scanner scanner = new Scanner(System.in);
String id = scanner.next();
long count = list.stream().filter(item -> item.getId().equals(id)).count();
if (count > 0) {
System.out.print("学号不能重复,请重新输入:");
} else {
studentEntity.setId(id);
break;
}
}
System.out.print("姓名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
System.out.print("年龄:");
while(true){
scanner = new Scanner(System.in);
String age = scanner.next();
try {
int i = Integer.parseInt(age);
studentEntity.setAge(age);
break;
}catch (Exception e){
System.out.print("年龄非数字,请重新输入:");
}
}
System.out.print("手机:");
scanner = new Scanner(System.in);
String phone = scanner.next();
studentEntity.setName(name);
studentEntity.setPhone(phone);
System.out.println(studentEntity);
list.add(studentEntity);
System.out.println("------------添加成功------------");
}
public static void edit() {
System.out.println("------------编辑信息------------");
System.out.print("输入要编辑的学号(-1退出):");
boolean flag = true;
while (flag) {
Scanner scanner = new Scanner(System.in);
String editId = scanner.next();
if ("-1".equals(editId)) {
System.out.println("------------操作取消------------");
break;
}
StudentEntity studentEntity = new StudentEntity();
int index = -1;
for (int i = 0; i < list.size(); i++) {
StudentEntity item = list.get(i);
studentEntity.setId(item.getId());
studentEntity.setName(item.getName());
studentEntity.setAge(item.getAge());
studentEntity.setPhone(item.getPhone());
if (editId.equals(studentEntity.getId())) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("输入学号不存在,请重新输入(-1退出):");
} else {
System.out.print("姓名(" + studentEntity.getName() + "):");
scanner = new Scanner(System.in);
String name = scanner.nextLine();
if (name.length() > 0) {
studentEntity.setName(name);
}
System.out.print("年龄(" + studentEntity.getAge() + "):");
scanner = new Scanner(System.in);
String age = scanner.nextLine();
if (age.length() > 0) {
studentEntity.setAge(age);
}
System.out.print("手机号(" + studentEntity.getPhone() + "):");
scanner = new Scanner(System.in);
String phone = scanner.nextLine();
if (phone.length() > 0) {
studentEntity.setPhone(phone);
}
System.out.print("确认是否编辑(Y/N):");
while (true) {
scanner = new Scanner(System.in);
String status = scanner.next();
if ("N".equals(status)) {
System.out.println("------------操作取消------------");
flag = false;
break;
}else if("Y".equals(status)) {
System.out.println("------------修改成功------------");
list.set(index,studentEntity);
flag = false;
break;
}else {
System.out.print("输入错误,请重新输入,确认是否编辑(Y/N):");
}
}
}
}
}
public static void del() {
System.out.println("------------删除信息------------");
System.out.print("输入要删除的学号(-1退出):");
boolean flag = true;
while (flag) {
Scanner scanner = new Scanner(System.in);
String delId = scanner.next();
if ("-1".equals(delId)) {
System.out.println("------------操作取消------------");
break;
}
int index = -1;
for (int i = 0; i < list.size(); i++) {
StudentEntity item = list.get(i);
if(delId.equals(item.getId())){
index = i;
break;
}
}
if(index == -1){
System.out.print("输入学号不存在,请重新输入要删除的学号(-1退出):");
}else {
System.out.print("确认是否编辑(Y/N):");
while (true) {
scanner = new Scanner(System.in);
String status = scanner.next();
if ("N".equals(status)) {
System.out.println("------------操作取消------------");
flag = false;
break;
}else if("Y".equals(status)) {
System.out.println("------------删除成功------------");
list.remove(index);
flag = false;
break;
}else {
System.out.print("输入错误,请重新输入,确认是否编辑(Y/N):");
}
}
}
}
}
public static void list() {
System.out.println("------------学生列表------------");
System.out.print("序号\t\t\t学号\t\t\t姓名\t\t\t年龄\t\t\t手机");
System.out.println();
if (list.size() == 0) {
System.out.println("------------暂时未有学生信息,请添加------------");
}
for (int i = 0; i < list.size(); i++) {
StudentEntity item = list.get(i);
System.out.printf("%-12s", (i + 1));
System.out.printf("%-12s", item.getId());
System.out.printf("%-12s", item.getName());
System.out.printf("%-12s", item.getAge());
System.out.printf("%-12s", item.getPhone());
System.out.println();
}
}
public static void ageDistributed() {
System.out.println("------------学生年龄分布------------");
System.out.print("序号\t\t0-20\t20-40\t40-60\t60以上");
System.out.println();
if (list.size() == 0) {
System.out.println("------------暂时未有学生信息,请添加------------");
}else {
long one = list.stream().filter(item ->
(Integer.parseInt(item.getAge()) >= 0 &&
Integer.parseInt(item.getAge()) < 20)).count();
long two = list.stream().filter(item ->
(Integer.parseInt(item.getAge()) >= 20 &&
Integer.parseInt(item.getAge()) < 40)).count();
long three = list.stream().filter(item ->
(Integer.parseInt(item.getAge()) >= 40 &&
Integer.parseInt(item.getAge()) < 60)).count();
long four = list.stream().filter(item ->
(Integer.parseInt(item.getAge()) >= 60 )).count();
System.out.printf("%-8s", ( 1));
System.out.printf("%-8s", one);
System.out.printf("%-8s", two);
System.out.printf("%-8s", three);
System.out.printf("%-8s", four);
System.out.println();
}
}
public static void listByName() {
System.out.println("------------学生查询------------");
System.out.print("输入要查询学生的姓名(-1退出):");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
if("-1".equals(name)){
System.out.println("------------操作取消------------");
return;
}
List entities = list.stream().filter(item -> item.getName().equals(name)).collect(Collectors.toList());
getByName(name, entities);
}
public static void listById() {
System.out.println("------------学生查询------------");
System.out.print("输入要查询学生的姓名(-1退出):");
Scanner scanner = new Scanner(System.in);
String id = scanner.next();
if("-1".equals(id)){
System.out.println("------------操作取消------------");
return;
}
List entities = list.stream().filter(item -> item.getId().equals(id)).collect(Collectors.toList());
getByName(id, entities);
}
public static void main(String[] args) {
Boolean flag = true;
while (flag) {
System.out.println("------------学生信息管理系统------------");
System.out.println();
System.out.println("\t\t\t1.添加学生");
System.out.println("\t\t\t2.修改学生");
System.out.println("\t\t\t3.删除学生");
System.out.println("\t\t\t4.学生列表");
System.out.println("\t\t\t5.退出");
System.out.println("\t\t\t6.学生年龄分布");
System.out.println("\t\t\t7.学生姓名查找");
System.out.println("\t\t\t8.学生学号查找");
System.out.print("\t\t请选择(1-8):");
Scanner scanner = new Scanner(System.in);
String status = scanner.next();
switch (status) {
case "1":
add();
break;
case "2":
edit();
break;
case "3":
del();
break;
case "4":
list();
break;
case "6":
ageDistributed();
break;
case "7":
listByName();
break;
case "8":
listById();
break;
case "5":
System.out.println("------------系统已退出------------");
flag = false;
break;
default:
System.out.println("输入参数错误,请输入1 - 5");
}
}
}