学生成绩管理系统(代码实现
- 1.开发语言及工具
- 1.开发语言
- 2.开发工具
- 2.代码实现
- 1.类
- Databese数据库
- Student
- StudentDao
- ManageSystem
- Main
- 运行结果
- 新增信息
- 查找信息
- 排序输出名次
- 修改成绩
1.开发语言及工具
1.开发语言
JAVA
2.开发工具
Idea
2.代码实现
1.类
Databese数据库
数据库,储存学生信息
package ManageSystem;
import java.util.ArrayList;
import java.util.List;
/**
* 数据库,储存信息
*/
public class Database {
private List<Student> studentsList=new ArrayList<>();
public Database(){
Student student1=new Student("Kobe",2100,"软件工程",90,90,90,90,360);
studentsList.add(student1);
Student student=new Student("阿杜",3000,"体育",80,80,80,80,320);
studentsList.add(student);
Student student2=new Student("老詹",1000,"体育",85,85,85,85,340);
studentsList.add(student2);
Student student3=new Student("哈登",2000,"软件工程",80,85,80,85,330);
studentsList.add(student3);
}
public List<Student> getStudentsList(){
return studentsList;
}
public void setStudentsList(List<Student> studentsList){
this.studentsList=studentsList;
}
}
Student
学生类
package ManageSystem;
/**
* 学生类
*/
public class Student {
private String Name;//姓名
private int Number;//学号
private String Specialty;//专业
private int no1;
private int no2;
private int no3;
private int no4;
private int Sum;//总分
public Student(String Name,int Number,String Specialty,int no1,int no2,int no3,int no4,int sum){
this.Name=Name;
this.Number=Number;
this.Specialty=Specialty;
this.no1=no1;
this.no2=no2;
this.no3=no3;
this.no4=no4;
this.Sum=sum;
}
public String getName(){
return Name;
}
public void setName(String Name){
this.Name=Name;
}
public int getNumber(){
return Number;
}
public void setNumber(int Number){
this.Number=Number;
}
public String getSpecialty(){
return Specialty;
}
public void setSpecialty(String Specialty){
this.Specialty=Specialty;
}
public int getNo1(){
return no1;
}
public void setNo1(int no1){
this.no1=no1;
}
public int getNo2(){
return no2;
}
public void setNo2(int no2){
this.no2=no2;
}
public int getNo3(){
return no3;
}
public void setNo3(int no3){
this.no3=no3;
}
public int getNo4(){
return no4;
}
public void setNo4(int no4){
this.no4=no4;
}
public int getSum(){
return Sum;
}
public void setSum(int Sum){
this.Sum=Sum;
}
}
StudentDao
学生数据访问方法
package ManageSystem;
/**
* 学生数据访问方法
*/
public class StudentDao {
private Database database;
public StudentDao(Database database){
this.database=database;
}
/**
*学生信息打印输出
*/
public void Print(){
System.out.println("姓名" + "学号" + "专业" + "各科成绩"+"总分");
for (Student student : database.getStudentsList()) {
System.out.print(student.getName()+" ");
System.out.print(student.getNumber()+" ");
System.out.print(student.getSpecialty()+" ");
System.out.print(student.getNo1()+" ");
System.out.print(student.getNo2()+" ");
System.out.print(student.getNo3()+" ");
System.out.print(student.getNo4()+" ");
System.out.println(student.getSum()+" ");
}
}
/**
*新增学生信息
*/
public void Insert(Student student1){
database.getStudentsList().add(student1);
Print();
}
/**
*对学生信息排序
*/
public Student SortBynumber(){//学号排序
Student student=null;
for (int j=0;j<database.getStudentsList().size();j++){
if(database.getStudentsList()!=null) {
for (int i=0;i<database.getStudentsList().size()-1;i++){
student=database.getStudentsList().get(i);
database.getStudentsList().set(i,database.getStudentsList().get(i+1));
database.getStudentsList().set(i+1,student);
}
}
}
Print();
return student;
}
public Student Sort1(){//按总成绩冒泡排序排序
Student student=null;
for (Student student1:database.getStudentsList()) {
if (database.getStudentsList() != null) {
for (int i = 0; i < database.getStudentsList().size() - 1; i++) {
if (database.getStudentsList().get(i).getSum() < database.getStudentsList().get(i + 1).getSum()) {
student = database.getStudentsList().get(i);
database.getStudentsList().set(i, database.getStudentsList().get(i + 1));
database.getStudentsList().set(i + 1, student);
}
}
}
}
PrintByNo();
return student;
}
//希尔排序
public Student ShellSort()
{
Student student1=null;
int gap,j;
for(Student student:database.getStudentsList()) {
for (gap = database.getStudentsList().size()/ 2; gap > 0; gap /= 2) //控制步长
for (j = gap; j < database.getStudentsList().size(); ++j) //++j操作是很巧妙地,这步操作省去了对数组进行分组的一层循环
{
student1= database.getStudentsList().get(j);
int prev = j - gap;
while (prev >= 0 && database.getStudentsList().get(j).getSum()< database.getStudentsList().get(prev).getSum()) { //这里是直接插入排序的核心步骤
database.getStudentsList().set(j,database.getStudentsList().get(prev));
prev -= gap;
}
database.getStudentsList().set(prev+gap,student1);
}
}
return student1;
}
//快速排序
public Student quick_sort( int left,int right)
{
Student student1=null;
int i = left;
int j = right;
for (Student student:database.getStudentsList()) {
student=database.getStudentsList().get(i);
//i>=j时,就说明已经把x排到正确的位置了
if (i < j) {
while (i < j) {
while (i < j && database.getStudentsList().get(j).getSum() > student1.getSum())//从右往左找第一个小于x的数
j--;
if (i < j)
/*赋值之后,把i往后移动一个位置,因为i前面的数肯定
比x小,故在下次查找大于x得值时,不用参与比较*/
database.getStudentsList().set(i++, database.getStudentsList().get(j));
while (i < j && database.getStudentsList().get(i).getSum() < student1.getSum())//从左往右找第一个大于x的数
i++;
if (i < j)
database.getStudentsList().set(j--, database.getStudentsList().get(i));//赋值之后,把j往前移动一个位置,因为j后面的数肯定比x大
}
database.getStudentsList().set(i,student1);//此时i和j指向的是同一个位置,故也可以写arr[j]=x
//递归调用的对基准值两边的序列进行快速排序
quick_sort( left, i - 1);//i==j时,就不用进入此函数了,故必须i<j
quick_sort(i + 1, right);//i==j时,就不用进入此函数了
}
}
PrintByNo();
return student1;
}
/**
* 删除学生信息
*/
//按名删除
public Student DeleteByname(String name){
Student student1=null;
for(Student student:database.getStudentsList()){
for(int i=0;i<database.getStudentsList().size();i++){
if(student1.getName()==name){
student1=database.getStudentsList().remove(i);
}
}
}
Print();
return student1;
}
/**
* 查找学生信息
*/
public Student SearchByname(String name){
Student student1=null;
for(Student student:database.getStudentsList()){
if(student.getName().equals(name)){
student1=student;
System.out.println("姓名:"+student1.getName());
System.out.println("学号:"+student1.getNumber());
System.out.println("专业:"+student1.getSpecialty());
System.out.println("第一成绩:"+student1.getNo1());
System.out.println("第二成绩:"+student1.getNo2());
System.out.println("第三成绩:"+student1.getNo3());
System.out.println("第四成绩:"+student1.getNo4());
System.out.println("总成绩:"+student1.getSum());
}
}
return student1;
}
/**
* 修改成绩
*/
public Student Change(int num,int no1,int no2,int no3,int no4,int sum){
Student student=null;
for(int i=0;i<database.getStudentsList().size();i++){
student=database.getStudentsList().get(i);
if(student.getNumber()==num){
student.setNo1(no1);
student.setNo2(no2);
student.setNo3(no3);
student.setNo4(no4);
student.setSum(sum);
}
}
Print();
return student;
}
/**
* 排名输出
*/
public void PrintByNo(){
System.out.println("排名"+"姓名"+"学号"+"专业"+"第一成绩"+"第二成绩"+"第三成绩"+"第四成绩"+"总分");
for (int i=0;i<database.getStudentsList().size();i++){
System.out.print((i+1)+database.getStudentsList().get(i).getName()+" ");
System.out.print(database.getStudentsList().get(i).getNumber()+" "+database.getStudentsList().get(i).getSpecialty()+" ");
System.out.print(database.getStudentsList().get(i).getNo1()+" "+database.getStudentsList().get(i).getNo2()+" ");
System.out.print(database.getStudentsList().get(i).getNo3()+" "+database.getStudentsList().get(i).getNo4()+" ");
System.out.println(database.getStudentsList().get(i).getSum()+" ");
}
}
}
ManageSystem
管理系统类,实现功能
package ManageSystem;
import java.util.Scanner;
/**
* 学生管理系统
*/
public class ManageSystem {
private StudentDao studentDao;
Scanner scanner=new Scanner(System.in);
private int Id=1;
private int password=0;
public ManageSystem(Database database){
studentDao=new StudentDao(database);
}
/**
* 登录界面,进入系统
*/
public void Start(){
System.out.print("用户名:");
int num=scanner.nextInt();
if(Id==num){
InputPassword();
/**
* 进入系统界面
*/
}else {
System.out.println("用户不存在!");
Start();
}
}
public void InputPassword() {
System.out.print("密码:");
int pass = scanner.nextInt();
if (password==pass) {
Visit();
}else {
System.out.println("密码错误");
Start();
}
}
/**
* 访问系统,实现相应功能
*/
public void Visit(){
System.out.println("1.学生基本信息");
System.out.println("2.信息管理");
int choice=scanner.nextInt();
switch (choice) {
case 1:
{//输出信息
Student student = studentDao.SortBynumber();
System.out.println("输入1返回上一层");
int i=scanner.nextInt();
if(i==1) Visit();
}
break;
case 2: {
System.out.println("1.新增信息");
System.out.println("2.查找信息");
System.out.println("3.排序信息");
System.out.println("4.修改成绩");
System.out.println("5.删除信息");
int choice1 = scanner.nextInt();
switch (choice1) {
case 1: {
System.out.println("姓名:");
String name = scanner.next();
System.out.println("学号:");
int number = scanner.nextInt();
System.out.println("专业:");
String specialty = scanner.next();
System.out.println("各科成绩;");
int no1 = scanner.nextInt();
int no2 = scanner.nextInt();
int no3 = scanner.nextInt();
int no4 = scanner.nextInt();
System.out.println("总分;");
int sum = scanner.nextInt();
Student student1 = new Student(name, number, specialty, no1, no2, no3, no4, sum);
studentDao.Insert(student1);
}
break;
case 2: {
System.out.println("输入姓名:");
String name = scanner.next();
Student student1 = studentDao.SearchByname(name);
}
break;
case 3: {
System.out.println("1.冒泡排序");
System.out.println("2.希尔排序");
int i = scanner.nextInt();
switch (i) {
case 1:
{Student student1 = studentDao.Sort1();}
break;
case 2:
Student student2 = studentDao.ShellSort();
break;
case 3:{
String name=scanner.next();
Student student=studentDao.DeleteByname(name);
}
}
Student student1 = studentDao.Sort1();
}
break;
case 4: {
System.out.println("输入学号:");
int number = scanner.nextInt();
System.out.println("输入修改的各科成绩:");
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int d = scanner.nextInt();
int sum = a + b + c + d;
Student student1 = studentDao.Change(number, a, b, c, d, sum);
}
break;
}
}
}
}
}
Main
调用数据库,实现运行
package ManageSystem;
/**
* 主类
*/
public class Main {
public static void main(String []args){
Database database=new Database();
ManageSystem manageSystem=new ManageSystem(database);
manageSystem.Start();
}
}
运行结果
新增信息
查找信息
排序输出名次
修改成绩