package com;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static List<StudentEntity> list = new LinkedList<>();
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int status;
boolean flag = true;
while (flag) {
System.out.println("-------欢迎使用学生管理系统-------");
System.out.println();
System.out.println("------- 1.添加学生 -------");
System.out.println("------- 2.修改学生 -------");
System.out.println("------- 3.删除学生 -------");
System.out.println("------- 4.学生列表 -------");
System.out.println("------- 5.学生年龄概况 -------");
System.out.println("------- 6.学生查找(通过姓名) -------");
System.out.println("------- 7.学生查找(通过学号) -------");
System.out.println("------- 8.退出系统 -------");
System.out.println("请输入要操作的序号:");
status = sc.nextInt();
switch (status) {
case 1: {
add();
break;
}
case 2: {
alter();
break;
}
case 3: {
delete();
break;
}
case 4: {
printList();
break;
}
case 5: {
printAgeList();
break;
}
case 6: {
findByName();
break;
}
case 7: {
findById();
break;
}
case 8: {
System.out.println("系统已经退出!");
flag = false;
break;
}
default: {
System.out.println("输入无效!请重新输入");
break;
}
}
}
}
public static void add() {
StudentEntity enty = new StudentEntity();
System.out.println("----------添加学生信息------------");
String id;
while(true) {
System.out.println("请输入学生学号(-1退出):");
while (true) {
id = sc.next();
if(id.equals("-1")){
return;
}
try {
Integer.parseInt(id);
} catch (Exception e) {
System.out.println("学号输入有误!请重新输入");
continue;
}
boolean flag = true;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId().equals(id)) {
System.out.println("学号重复!请重新输入");
flag = false;
break;
}
}
if (flag) {
enty.setId(id);
break;
}
}
System.out.println("请输入学生姓名:");
enty.setName(sc.next());
System.out.println("请输入学生年龄:");
while (true) {
String age = sc.next();
try {
int i = Integer.parseInt(age);
if (i < 0 || i > 130) {
System.out.println("年龄输入有误!请重新输入");
continue;
}
enty.setAge(age);
break;
} catch (Exception e) {
System.out.println("年龄输入有误!请重新输入");
}
}
System.out.println("请输入学生电话:");
while (true) {
String phone = sc.next();
if (phone.length() != 11) {
System.out.println("电话号码输入有误!请重新输入");
continue;
}
try {
BigInteger bigInteger = new BigInteger(phone);
} catch (Exception e) {
System.out.println("电话号码输入有误!请重新输入");
continue;
}
enty.setPhone(phone);
break;
}
list.add(enty);
System.out.println("添加成功!");
}
}
public static void delete() {
System.out.println("----------删除学生信息------------");
String id;
int index;
boolean flag;
while (true) {
System.out.println("请输入需要删除的学生学号(-1退出):");
flag = false;
id = sc.next();
if (id.equals("-1"))
return;
index = -1;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId().equals(id)) {
index = i;
flag = true;
break;
}
}
while (flag) {
System.out.println("请再次确认是否修改? Y/N");
String ans = sc.next();
if (ans.equals("Y")) {
list.remove(index);
System.out.println("删除成功!");
break;
} else if (ans.equals("N")) {
break;
} else
System.out.println("输入的数据非法,请重新输入");
flag = true;
}
if (!flag) {
System.out.println("删除失败!没有找到该学生。当前学生人数:" + list.size());
System.out.println("请重新输入");
}
}
}
public static void alter() {
StudentEntity tempStu = new StudentEntity();
System.out.println("----------修改学生信息------------");
int index = -1;
boolean flag;
while (true) {
System.out.println("请输入要修改的学生学号(-1退出):");
String id = sc.next();
flag = false;
loop:
while (true) {
if (id.equals("-1"))
return;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId().equals(id)) {
tempStu.setId(id);
index = i;
flag = true;
break loop;
}
}
System.out.println("修改失败!没有该学生。当前系统内学生人数:" + list.size());
id = sc.next();
}
System.out.println("请输入学生姓名:");
tempStu.setName(sc.next());
System.out.println("请输入学生年龄:");
while (true) {
String age = sc.next();
try {
int j = Integer.parseInt(age);
if (j < 0 || j > 130) {
System.out.println("年龄输入有误!请重新输入");
continue;
}
tempStu.setAge(age);
break;
} catch (Exception e) {
System.out.println("年龄输入有误!请重新输入");
}
}
System.out.println("请输入学生电话:");
while (true) {
String phone = sc.next();
if (phone.length() != 11) {
System.out.println("电话号码输入有误!请重新输入");
break;
}
try {
BigInteger bigInteger = new BigInteger(phone);
} catch (Exception e) {
System.out.println("电话号码输入有误!请重新输入");
break;
}
tempStu.setPhone(phone);
break;
}
while (flag) {
System.out.println("请再次确认是否修改? Y/N");
String ans = sc.next();
if (ans.equals("Y")) {
list.get(index).setName(tempStu.getName());
list.get(index).setAge(tempStu.getAge());
list.get(index).setPhone(tempStu.getPhone());
System.out.println("修改成功!");
} else if (ans.equals("N")) {
break;
} else {
System.out.println("输入的数据非法,请重新输入");
}
}
}
}
public static void printList() {
System.out.println("学生列表:");
Iterator<StudentEntity> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
public static void printAgeList() {
System.out.println("------------学生年龄概况-----------");
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < list.size(); i++) {
if (map.containsKey(list.get(i).getAge())) {
map.put(list.get(i).getAge(), map.get(list.get(i).getAge()) + 1);
} else {
map.put(list.get(i).getAge(), 1);
}
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("年龄:" + entry.getKey() + " 人数:" + entry.getValue());
}
}
public static void findByName() {
String name;
while (true) {
System.out.println("请输入需要查找的学生姓名(-1退出):");
name = sc.next();
if (name.equals("-1"))
return;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(name)) {
System.out.println(list.get(i));
return;
}
}
System.out.println("查找失败!没有该学生");
}
}
public static void findById() {
String id;
while (true) {
System.out.println("请输入需要查找的学生学号(-1退出):");
id = sc.next();
if (id.equals("-1"))
return;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId().equals(id)) {
System.out.println(list.get(i));
return;
}
}
System.out.println("查找失败!没有该学生");
}
}
}
class StudentEntity {
private String id;
private String name;
private String age;
private String phone;
public StudentEntity(String id, String name, String age, String phone) {
this.id = id;
this.name = name;
this.age = age;
this.phone = phone;
}
public StudentEntity(){
}
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;
}
@Override
public String toString() {
return "学号 = " + id + ", 姓名 = " + name + ", 年龄 = " + age + ", 电话号码 = " + phone;
}
}
标签:String,管理系统,list,System,学生,println,时尚,id,out
From: https://www.cnblogs.com/tenset/p/17778215.html