java控制台学生管理系统

package com.小项目;

学完java基础了,写了一个小项目,算是对前面知识点的回顾吧,代码如下,有什么不对的地方请大神指正;
public class Student {
private Integer id;
private String name;
private Integer age;
private Character sex;
private double math;
private double physics;
private double english;
private double java;
private double sum;
private double avg;


public Integer getId() {
return id;
}


public void setId(Integer id) {
this.id = id;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public Integer getAge() {
return age;
}


public void setAge(Integer age) {
this.age = age;
}


public Character getSex() {
return sex;
}


public void setSex(Character sex) {
this.sex = sex;
}


public double getMath() {
return math;
}


public void setMath(double math) {
this.math = math;
}


public double getPhysics() {
return physics;
}


public void setPhysics(double physics) {
this.physics = physics;
}


public double getEnglish() {
return english;
}


public void setEnglish(double english) {
this.english = english;
}


public double getJava() {
return java;
}


public void setJava(double java) {
this.java = java;
}


public double getSum() {
return sum;
}


public void setSum(double sum) {
this.sum = this.math + this.physics + this.english + this.java;
}


public double getAvg() {
return avg;
}


public void setAvg(double avg) {
this.avg = this.sum / 4;
}


public Student(Integer id, String name, Integer age, Character sex,
double math, double physics, double english, double java,
double sum, double avg) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.math = math;
this.physics = physics;
this.english = english;
this.java = java;
this.sum = sum;
this.avg = avg;
}


public Student() {
super();
}


@Override
public String toString() {
return id + "\t" + name + "\t" + sex + "\t" + age + "\t" + math + "\t"
+ physics + "\t" + english + "\t" + java + "\t" + sum + "\t"
+ avg;
}


}

package com.小项目;


import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;


public class MangeStudent {
private ArrayList array_list = null;
private static int namber = 0;
private static final String user_name = "www";
private static final String user_password = "123qwe";
private static final String ensure_botton = "确定";
private static final String cancel_botton = "取消";


MangeStudent() {
// Student s=new Student();
array_list = new ArrayList();
}


// 得到学生信息;
public Student get_studentInfo() {
Integer student_id = 0;
Integer student_age = 0;
double student_math = 0.0;
double student_physics = 0.0;
double student_english = 0.0;
double student_java = 0.0;
double student_sum = 0.0;
double student_avg = 0.0;
Scanner sc1 = new Scanner(System.in);
System.out.println("请输入学生学号");
try {
student_id = sc1.nextInt();
} catch (InputMismatchException e) {
Scanner sce = new Scanner(System.in);
System.out.println("输入有误,请输入整数");
student_id = sce.nextInt();


}


Scanner sc2 = new Scanner(System.in);
System.out.println("请输入学生姓名");
String student_name = sc2.nextLine();


Scanner sc3 = new Scanner(System.in);
System.out.println("请输入学生性别");
Character student_sex = sc3.nextLine().charAt(0);


Scanner sc4 = new Scanner(System.in);
System.out.println("请输入学生年龄");
try {
student_age = sc4.nextInt();
} catch (InputMismatchException e) {
Scanner sce = new Scanner(System.in);
System.out.println("输入有误,请输入整数");
student_age = sce.nextInt();
}
Scanner sc5 = new Scanner(System.in);
System.out.println("请输入学生数学成绩");
try {
student_math = sc5.nextDouble();
} catch (InputMismatchException e) {
Scanner sce = new Scanner(System.in);
System.out.println("输入有误,请输入数字");
student_math = sce.nextDouble();


}
Scanner sc6 = new Scanner(System.in);
System.out.println("请输入学生物理成绩");
try {
student_physics = sc6.nextDouble();
} catch (InputMismatchException e) {
Scanner sce = new Scanner(System.in);
System.out.println("输入有误,请输入数字");
student_physics = sce.nextDouble();


}
Scanner sc7 = new Scanner(System.in);
System.out.println("请输入学生英语成绩");
try {
student_english = sc7.nextDouble();
} catch (InputMismatchException e) {
Scanner sce = new Scanner(System.in);
System.out.println("输入有误,请输入数字");
student_english = sce.nextDouble();


}
Scanner sc8 = new Scanner(System.in);
System.out.println("请输入学生java成绩");
try {
student_java = sc8.nextDouble();
} catch (InputMismatchException e) {
Scanner sce = new Scanner(System.in);
System.out.println("输入有误,请输入数字");
student_java = sce.nextDouble();


}
student_sum = student_math + student_physics + student_english
+ student_java;
student_avg = student_sum / 4;
// 这儿发现调用Student的getSum()和getAvg()方法不可行,因为调用的是初始值;
return new Student(student_id, student_name, student_age, student_sex,
student_math, student_physics, student_english, student_java,
student_sum, student_avg);


}


// 添加学生
public void add_student(Student s) {
array_list.add(s);
}


// 遍历集合查询学生
public void selet_ArrayList() {


System.out.println("查询结果:\n学生学号\t姓名\t性别\t年龄\t数学"
+ "\t物理\t英语\tjava\t总分\t平均分");
for (int i = 0; i < array_list.size(); i++) {
Student s = array_list.get(i);
System.out.println(s);
}
}


// 根据学生学号删除指定位置上的学生
public void delete_student(Integer id) {


for (int i = 0; i < array_list.size(); i++) {


Student s = array_list.get(i);
if (id.equals(s.getId())) {
System.out.println("要删除学生的信息:" + array_list.get(i));
System.out.println("确定要删除这个学生吗?确定请输入“确定”,取消请输入“取消”,");
Scanner in = new Scanner(System.in);
String cin_botton = in.nextLine();
if (cin_botton.equals(ensure_botton)) {
array_list.remove(i);
System.out.println("删除学生成功");
} else if (cin_botton.equals(cancel_botton)) {
System.out.println("已取消删除操作");
break;
} else {
System.out.println("输入有误");
}
}


}


}


// 判断这个学生是否存在
public boolean exsit_student(Integer id) {


for (int i = 0; i < array_list.size(); i++) {


Student s = array_list.get(i);
if (id.equals(s.getId())) {
return true;
}


}
return false;


}


// 根据学生学号改对应学生信息
public void set_student(Integer id, String name, Integer age,
Character sex, double math, double physics, double english,
double java) {
for (int i = 0; i < array_list.size(); i++) {
Student s = array_list.get(i);
if (id.equals(s.getId())) {
s.setId(id);
s.setName(name);
s.setAge(age);
s.setSex(sex);
s.setMath(math);
s.setPhysics(physics);
s.setEnglish(english);
s.setJava(java);
System.out.println("修改后的这个学生信息:" + array_list.get(i));
System.out.println("修改完成,请继续");
} else {
System.out.println("您输入的学生学号系统中不存在,请输入的学号跟修改的学号一样哦");
}


}


}


// 查api,直接访问ArrayList中的每个元素是不行的,ArrayList没有比较的方法,只能放在数组中排序了,根据年龄进行排序
public void sort_student_age() {
Student[] s = new Student[array_list.size()];
for (int i = 0; i < array_list.size(); i++) {


s[i] = array_list.get(i);


}
for (int i = 0; i < s.length - 1; i++) {
for (int j = i + 1; j < s.length; j++) {
if (s[i].getAge() < s[j].getAge()) {
Student stu = s[i];
s[i] = s[j];
s[j] = stu;
}
}
}
System.out.println("排序结果:\n学生学号\t姓名\t性别\t年龄\t数学"
+ "\t物理\t英语\tjava\t总分\t平均分");
for (Student stu : s) {
System.out.println(stu);
}


}


// 根据总分排序
public void sort_student_sum() {
Student[] s = new Student[array_list.size()];
for (int i = 0; i < array_list.size(); i++) {


s[i] = array_list.get(i);


}
for (int i = 0; i < s.length - 1; i++) {
for (int j = i + 1; j < s.length; j++) {
if (s[i].getSum() < s[j].getSum()) {
Student stu = s[i];
s[i] = s[j];
s[j] = stu;
}
}
}
System.out.println("排序结果:\n学生学号\t姓名\t性别\t年龄\t数学"
+ "\t物理\t英语\tjava\t总分\t平均分");
for (Student stu : s) {
System.out.println(stu);
}


}


// 根据平均分排序
public void sort_student_avg() {
Student[] s = new Student[array_list.size()];
for (int i = 0; i < array_list.size(); i++) {


s[i] = array_list.get(i);


}
for (int i = 0; i < s.length - 1; i++) {
for (int j = i + 1; j < s.length; j++) {
if (s[i].getAvg() < s[j].getAvg()) {
Student stu = s[i];
s[i] = s[j];
s[j] = stu;
}
}
}
System.out.println("排序结果:\n学生学号\t姓名\t性别\t年龄\t数学"
+ "\t物理\t英语\tjava\t总分\t平均分");
for (Student stu : s) {
System.out.println(stu);
}


}


// public double get_student_total_score(){
// return 0;
//
//
// }


// 获取用户输入,看是否输入正确;
public boolean user_cin() {


Scanner sc = new Scanner(System.in);
System.out.println("请输入学生管理员用户名");
String name = sc.nextLine();
String regex = "\\w+";
boolean b1 = name.matches(regex);
if (b1) {
System.out.println("输入用户名符合要求,继续");
} else {
System.out.println("输入的用户名不符合要求");
}
Scanner sc1 = new Scanner(System.in);
System.out.println("请输入密码");
String password = sc1.nextLine();
if (!name.equals(user_name) || !password.equals(user_password)) {
System.out.println("用户名或密码错误");
return false;
}
if (name.equals(user_name) && password.equals(user_password)) {
return true;
}
return false;


}


// 输入正确后进入系统
public void enter_system() {


System.out.println("欢迎进入系统,请开始您的表演");
System.out.println("请输入您的操作\n 1,添加学生;\n 2,删除学生  \n 3,修改学生\n "
+ "4,查询学生\n 5,根据年龄排序\n 6,根据总分排序\n 7,根据平均分排序\n 8,退出系统");
System.out.println("请输入你的操作");
Scanner sc2 = new Scanner(System.in);
try {
namber = sc2.nextInt();
} catch (Exception e) {
System.out.println("请输入整数");
}
if (namber == 1) {
add_student(get_studentInfo());
} else if (namber == 2) {
Scanner sc3 = new Scanner(System.in);
System.out.println("请输入要删除学生的学号");
int student_id = sc3.nextInt();
if (exsit_student(student_id)) {
delete_student(student_id);
} else {
System.out.println("找不到相应的学生,请重新输入");
}
} else if (namber == 3) {
Scanner sc4 = new Scanner(System.in);
System.out.println("请输入要修改学生的学号");
Integer student_id = sc4.nextInt();
if (exsit_student(student_id)) {


Student s = get_studentInfo();
System.out.println("新输入的学生学号:" + s.getId());


set_student(s.getId(), s.getName(), s.getAge(), s.getSex(),
s.getMath(), s.getPhysics(), s.getEnglish(),
s.getJava());
} else {
System.out.println("找不到相应的学生,请重新输入");
}


} else if (namber == 4) {
selet_ArrayList();
} else if (namber == 5) {
sort_student_age();
} else if (namber == 6) {
sort_student_sum();
} else if (namber == 7) {
sort_student_avg();
} else if (namber == 8) {
System.out.println("感谢使用学生管理系统");
System.exit(1);
} else {
System.out.println("输入有误,重新出入");
}


}


}

package com.小项目;


public class Test {


public static void main(String[] args) {
MangeStudent m = new MangeStudent();
if (m.user_cin()) {
while (true) {
m.enter_system();
}


}


}
}

你可能感兴趣的:(java控制台学生管理系统)