学生成绩档案管理系统源代码
package StddentsSystem;
public class Student {
int ID;
String name, major;
double grade, grade1, grade2, grade3, grade4;
int rank;
public Student(int ID, String name, String major, double grade1, double grade2, double grade3, double grade4) {
this.ID = ID;
this.name = name;
this.major = major;
this.grade = grade1+grade2+grade3+grade4;
this.grade1 = grade1;
this.grade2 = grade2;
this.grade3 = grade3;
this.grade4 = grade4;
}
public int getID() {
return ID;
}
public String getName() {
return name;
}
public String getMajor() {
return major;
}
public double getGrade() {
return grade;
}
public double getGrade1() {
return grade1;
}
public double getGrade2() {
return grade2;
}
public double getGrade3() {
return grade3;
}
public double getGrade4() {
return grade4;
}
public int getRank() {
Database database=new Database();
Dao dao=new Dao(database);
dao.rank();
return rank;
}
public void setID(int ID) {
this.ID = ID;
}
public void setName(String name) {
this.name = name;
}
public void setMajor(String major) {
this.major = major;
}
public void setGrade(double grade) {
this.grade = grade;
}
public void setGrade1(double grade1) {
this.grade1 = grade1;
}
public void setGrade2(double grade2) {
this.grade2 = grade2;
}
public void setGrade3(double grade3) {
this.grade3 = grade3;
}
public void setGrade4(double grade4) {
this.grade4 = grade4;
}
public void setRank(int rank) {
this.rank = rank;
}
}
package StddentsSystem;
import java.util.ArrayList;
import java.util.List;
public class Database {
List<Student> student=new ArrayList<>();
public Database(){
Student student1=new Student(1001,"小明","软件工程",86,75,89,78);
Student student2=new Student(1002,"小华","人工智能",65,87,99,52);
Student student3=new Student(1003,"张三","物联网",78,99,99,32);
student.add(student1);
student.add(student2);
student.add(student3);
}
public List<Student> getStudent() {
return student;
}
public void setStudent(List<Student> student) {
this.student = student;
}
}
package StddentsSystem;
import java.util.ArrayList;
import java.util.List;
import StddentsSystem.Database;
import StddentsSystem.Student;
public class Dao {
private Database database;
public Dao(Database database) {
this.database = database;
}
public void insert(Student student) {
List<Student> students = database.getStudent();
students.add(student);
database.setStudent(students);
}
public void delet(Student student){
List<Student> students=database.getStudent();
students.remove(student);
database.setStudent(students);
}
public void modify1(int number,double grade){
List<Student> students=database.getStudent();
students.get(number-1).setGrade1(grade);
database.setStudent(students);
}
public void modify2(int number,double grade){
List<Student> students=database.getStudent();
students.get(number-1).setGrade2(grade);
database.setStudent(students);
}
public void modify3(int number,double grade){
List<Student> students=database.getStudent();
students.get(number-1).setGrade3(grade);
database.setStudent(students);
}
public void modify4(int number,double grade){
List<Student> students=database.getStudent();
students.get(number-1).setGrade4(grade);
database.setStudent(students);
}
public Student findByID(int n){
List<Student> students=database.getStudent();
List<Student> findByID=new ArrayList<>();
for(int i=0;i<students.size();i++){
if(students.get(i).getID()==n){
findByID.add(students.get(i));
}
}
return findByID.get(0);
}
public Student findByName(String name){
List<Student> students=database.getStudent();
List<Student> findByName=new ArrayList<>();
for(int i=0;i<students.size();i++){
if(students.get(i).getName().equals(name)){
findByName.add(students.get(i));
}
}
return findByName.get(0);
}
public void rank(){
List<Student> students=database.getStudent();
Student student0;
for(int i=0;i<students.size();i++){
for(int j=i+1;j<students.size();j++){
if(students.get(i).getGrade()<students.get(j).getGrade()){
student0=students.get(i);
students.set(i,students.get(j));
students.set(j,student0);
}
}
}
for(int i=0;i<students.size();i++){
students.get(i).setRank(i+1);
}
database.setStudent(students);
}
public void rank1(){
List<Student> students=database.getStudent();
Student student0;
for(int i=0,j=students.size()-1;j-i>1;i++,j--){
for(int k=i+1;k<students.size();k++){
if(students.get(i).getGrade()<students.get(k).getGrade()){
student0=students.get(i);
students.set(i,students.get(k));
students.set(k,student0);
}
}
for(int k=j-1;j>=0;j--){
if(students.get(j).getGrade()>students.get(k).getGrade()){
student0=students.get(j);
students.set(j,students.get(k));
students.set(k,student0);
}
}
}
for(int i=0;i<students.size();i++){
students.get(i).setRank(i+1);
}
database.setStudent(students);
}
}
public class StudentsSystem {
Database database=new Database();
private Dao dao=new Dao(database);
Scanner scanner=new Scanner(java.lang.System.in);
public StudentsSystem(Database database){
}
public void start(){
System.out.println("主菜单");
System.out.println("1、增加");
System.out.println("2、删除");
System.out.println("3、修改");
System.out.println("4、查询");
System.out.println("5、退出");
int choice=scanner.nextInt();
switch (choice){
case 1:insert();break;
case 2:delet();break;
case 3:modify();break;
case 4:find();break;
case 5:System.exit(0);break;
default:
System.out.println("编号错误,返回主菜单");
start();
}
}
public void insert(){
System.out.println("请输入学生信息");
System.out.println("学号:");
int ID=scanner.nextInt();
System.out.println("姓名:");
String Name=scanner.next();
System.out.println("专业:");
String Major=scanner.next();
System.out.println("成绩1:");
double Grade1=scanner.nextDouble();
System.out.println("成绩2:");
double Grade2=scanner.nextDouble();
System.out.println("成绩3:");
double Grade3=scanner.nextDouble();
System.out.println("成绩4:");
double Grade4=scanner.nextDouble();
Student newStudent=new Student(ID,Name,Major,Grade1,Grade2,Grade3,Grade4);
dao.insert(newStudent);
System.out.println("添加成功,返回主菜单");
start();
}
public void delet(){
List<Student> students=database.getStudent();
dao.rank();
for(int i=0;i<students.size();i++){
System.out.println(i+1+"、学号:"+students.get(i).getID()+" "+"姓名:"+students.get(i).getName()+" "+"专业:"+students.get(i).getMajor()+" "+"总分:"+students.get(i).getGrade()+" "+"排名:"+students.get(i).getRank());
}
System.out.println("请选择编号");
int choice=scanner.nextInt();
dao.delet(students.get(choice-1));
System.out.println("删除成功,返回主菜单");
start();
}
public void modify(){
List<Student> students=database.getStudent();
dao.rank();
for(int i=0;i<students.size();i++){
System.out.println(i+1+".学号:"+students.get(i).getID()+" "+"姓名:"+students.get(i).getName()+" "+"专业:"+students.get(i).getMajor()+" "+"总分:"+students.get(i).getGrade()+" "+"排名:"+students.get(i).getRank());
}
System.out.println("请选择编号");
int choice1=scanner.nextInt();
System.out.println("请输入课程1成绩");
double newGrade=scanner.nextDouble();
dao.modify1(choice1,newGrade);
System.out.println("请输入课程2成绩");
newGrade=scanner.nextDouble();
dao.modify2(choice1,newGrade);
System.out.println("请输入课程3成绩");
newGrade=scanner.nextDouble();
dao.modify3(choice1,newGrade);
System.out.println("请输入课程4成绩");
newGrade=scanner.nextDouble();
dao.modify4(choice1,newGrade);
System.out.println("修改成功,返回主菜单");
start();
}
public void find(){
System.out.println("1、学号查询");
System.out.println("2、姓名查询");
int choice=scanner.nextInt();
switch(choice){
case 1:findByID();
case 2:findByName();
default:
System.out.println("编号错误,返回主菜单");
start();
}
}
public void findByID(){
System.out.println("请输入学号");
int ID=scanner.nextInt();
dao.rank();
Student findByID=dao.findByID(ID);
System.out.println("学号:"+findByID.getID()+" "+"姓名:"+findByID.getName()+" "+"专业:"+findByID.getMajor()+" "+"总分:"+findByID.getGrade()+" "+"排名:"+findByID.getRank());
System.out.println("查询成功,返回主菜单");
start();
}
public void findByName(){
System.out.println("请输入姓名");
String Name=scanner.next();
dao.rank();
Student findByName=dao.findByName(Name);
System.out.println("学号:"+findByName.getID()+" "+"姓名:"+findByName.getName()+" "+"专业:"+findByName.getMajor()+" "+"总分:"+findByName.getGrade()+" "+"排名:"+findByName.getRank());
System.out.println("。查询成功,返回主菜单");
start();
}
}
package StddentsSystem;
public class test {
public static void main(String[] args) {
Database database=new Database();
StudentsSystem studentsSystem=new StudentsSystem(database);
studentsSystem.start();
}
}