学生管理系统(Java)
现有需求需要用Java写一个学生管理需求,可将该需求分解为如下几部分: 1.先创建一个学生类,该学生类包含学生学号,学生姓名,学生年龄,学生居住地址。
public class Student {
private String stuname;
private String name;
private String age;
private String arr;
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
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 getArr() {
return arr;
}
public void setArr(String arr) {
this.arr = arr;
}
public Student() {
}
public Student(String stuname, String name, String age, String arr) {
this.stuname = stuname;
this.name = name;
this.age = age;
this.arr = arr;
}
}
2.写一个main方法输出学生管理系统的页面,以及传入需要选择的选项。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Student> arrayList=new ArrayList<>();
while (true){
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("请输入你的选择:");
int a=scanner.nextInt();
switch (a){
case 1:
addStu(arrayList);
break;
case 2:
delStu(arrayList);
break;
case 3:
uptStu(arrayList);
break;
case 4:
showStu(arrayList);
break;
case 5:
System.out.println("感谢使用本系统");
System.exit(0);
}
}
3.写一个addStu方法,用于添加学生信息。
private static void addStu(ArrayList<Student> arrayList) {
Scanner scanner = new Scanner(System.in);
Student s = new Student();
System.out.println("请输入学生学号:");
String stunumber = scanner.nextLine();
if (checknum(stunumber, arrayList)) {
System.out.println("已结有该学生学号,请重新输入!");
} else {
System.out.println("请输入学生姓名:");
String stuName = scanner.nextLine();
System.out.println("请输入学生年龄:");
String stuAge = scanner.nextLine();
System.out.println("请输入学生地址:");
String stuArr = scanner.nextLine();
s.setStuname(stunumber);
s.setName(stuName);
s.setAge(stuAge);
s.setArr(stuArr);
System.out.println("你确定要保存吗(y/n)?");
String can = scanner.nextLine();
if (can.equals("y")) {
arrayList.add(s);
System.out.println("添加成功!!!");
} else {
System.out.println("保存失败!!!");
}
}
}
4.写一个showStu方法,用于查看所学生信息。
private static void showStu(ArrayList<Student> arrayList){
if (arrayList.size()==0){
System.out.println("系统中无信息,请输入信息。");
}else {
for (int i=0;i<arrayList.size();i++){
Student s=arrayList.get(i);
System.out.println("---学号------姓名------年龄------地址---");
System.out.println(" "+s.getStuname()+" "+s.getName()+" "+s.getAge()+"岁 "+s.getArr()+" ");
}
}
}
5.写一个delStu方法,用于删除学生信息。
private static void delStu(ArrayList<Student> arrayList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除学生的学号:");
String idstu = scanner.nextLine();
for (int i = 0; i < arrayList.size(); i++) {
Student student = arrayList.get(i);
if (student.getStuname().equals(idstu)) {
arrayList.remove(i);
System.out.println("删除成功!");
break;
}
}
}
6.写一个uptStu方法,用于修改学生信息。
private static void uptStu(ArrayList<Student> arrayList) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入要修改学生的学号");
String stunum = scanner.nextLine();
for (int i = 0; i < arrayList.size(); i++) {
Student student = arrayList.get(i);
if (student.getStuname().equals(stunum)) {
System.out.println("请输入新的姓名");
String name = scanner.nextLine();
System.out.println("请输入新的年龄");
String age = scanner.nextLine();
System.out.println("请输入新的地址");
String arr = scanner.nextLine();
student.setStuname(stunum);
student.setName(name);
student.setAge(age);
student.setArr(arr);
arrayList.set(i, student);
System.out.println("修改成功");
break;
}
}
}
7.写一个checknum方法,用于判断添加的学生信息是否已经存在。
private static boolean checknum(String stunumber, ArrayList<Student> arrayList) {
Boolean string = false;
for (int i = 0; i < arrayList.size(); i++) {
Student student = arrayList.get(i);
if (stunumber.equals(student.getStuname())) {
string = true;
break;
} else {
string = false;
break;
}
}
return string;
}
8.整体代码实现。
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Student> arrayList = new ArrayList<>();
while (true) {
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("请输入你的选择:");
int a = scanner.nextInt();
switch (a) {
case 1:
addStu(arrayList);
break;
case 2:
delStu(arrayList);
break;
case 3:
uptStu(arrayList);
break;
case 4:
showStu(arrayList);
break;
case 5:
System.out.println("感谢使用本系统");
System.exit(0);
}
}
}
private static void addStu(ArrayList<Student> arrayList) {
Scanner scanner = new Scanner(System.in);
Student s = new Student();
System.out.println("请输入学生学号:");
String stunumber = scanner.nextLine();
if (checknum(stunumber, arrayList)) {
System.out.println("已结有该学生学号,请重新输入!");
} else {
System.out.println("请输入学生姓名:");
String stuName = scanner.nextLine();
System.out.println("请输入学生年龄:");
String stuAge = scanner.nextLine();
System.out.println("请输入学生地址:");
String stuArr = scanner.nextLine();
s.setStuname(stunumber);
s.setName(stuName);
s.setAge(stuAge);
s.setArr(stuArr);
System.out.println("你确定要保存吗(y/n)?");
String can = scanner.nextLine();
if (can.equals("y")) {
arrayList.add(s);
System.out.println("添加成功!!!");
} else {
System.out.println("保存失败!!!");
}
}
}
private static void showStu(ArrayList<Student> arrayList) {
if (arrayList.size() == 0) {
System.out.println("系统中无信息,请输入信息。");
} else {
for (int i = 0; i < arrayList.size(); i++) {
Student s = arrayList.get(i);
if (i == 0) {
System.out.println("---学号------姓名------年龄------地址---");
System.out.println(" " + s.getStuname() + " " + s.getName() + " " + s.getAge() + "岁 " + s.getArr() + " ");
} else {
System.out.println(" " + s.getStuname() + " " + s.getName() + " " + s.getAge() + "岁 " + s.getArr() + " ");
}
}
}
}
private static void delStu(ArrayList<Student> arrayList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除学生的学号:");
String idstu = scanner.nextLine();
for (int i = 0; i < arrayList.size(); i++) {
Student student = arrayList.get(i);
if (student.getStuname().equals(idstu)) {
arrayList.remove(i);
System.out.println("删除成功!");
break;
}
}
}
private static boolean checknum(String stunumber, ArrayList<Student> arrayList) {
Boolean string = false;
for (int i = 0; i < arrayList.size(); i++) {
Student student = arrayList.get(i);
if (stunumber.equals(student.getStuname())) {
string = true;
break;
} else {
string = false;
break;
}
}
return string;
}
private static void uptStu(ArrayList<Student> arrayList) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入要修改学生的学号");
String stunum = scanner.nextLine();
for (int i = 0; i < arrayList.size(); i++) {
Student student = arrayList.get(i);
if (student.getStuname().equals(stunum)) {
System.out.println("请输入新的姓名");
String name = scanner.nextLine();
System.out.println("请输入新的年龄");
String age = scanner.nextLine();
System.out.println("请输入新的地址");
String arr = scanner.nextLine();
student.setStuname(stunum);
student.setName(name);
student.setAge(age);
student.setArr(arr);
arrayList.set(i, student);
System.out.println("修改成功");
break;
}
}
}
}
|