学生管理系统代码实现
Database
下面展示一些 内联代码片
。
import java.util.ArrayList;
import java.util.List;
public class Database implements java.io.Serializable{
List<Accounts>accountsList=new ArrayList<>();
List<Student>studentList=new ArrayList<>();
AccountsInOut accountsInOut=new AccountsInOut();
InAndOut inAndOut=new InAndOut();
public Database(){
try{
accountsList=accountsInOut.inPut();
studentList=inAndOut.inPut();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
public void accountOut(List<Accounts>list){
accountsInOut.outPut(list);
}
public void studentOut(List<Student>list){
inAndOut.outPut(list);
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
public List<Accounts> getAccountsList() {
return accountsList;
}
public void setAccountsList(List<Accounts> accountsList) {
this.accountsList = accountsList;
}
}
Dao
StudentDao
下面展示一些 内联代码片
。
import system.Database;
import system.Student;
import java.util.List;
public class StudentDao {
Database database;
public StudentDao(Database database){
this.database=database;
}
public void add(Student student){
database.getStudentList().add(student);
}
public void show(){
List<Student> list=database.getStudentList();
int n=1;
for(Student student:list){
System.out.println("编号"+n+" 学号:"
+student.getNum()+" 姓名:"
+student.getName()
+" 专业:"+student.getMajor()
+" 课程1成绩:"+student.getClass1Score()
+" 课程2成绩:"+student.getClass2Score()
+" 课程3成绩:"+student.getClass3Score()
+" 课程4成绩:"+student.getClass4Score()
+" 总分:"+student.getSum());
n++;
}
}
public void delete(Student student){
Student student1=null;
for(Student each:database.getStudentList()){
if(student.getNum().equals(each.getNum())){
student1=each;
}
}
database.getStudentList().remove(student1);
}
public Student searchByNum(String num){
Student student=null;
for(Student each:database.getStudentList()){
if(num.equals(each.getNum())){
student=each;
}
}
return student;
}
public Student searchByName(String name){
Student student=null;
for(Student each:database.getStudentList()){
if(name.equals(each.getName())){
student=each;
}
}
return student;
}
/**
* 双向冒泡排序
* @param studentList
* @return
*/
public List<Student> doubleBubbleSort(List<Student> studentList){
List<Student> list=studentList;
Student student=null;
int left=0,right=studentList.size()-1;
while(left<right)
{
for(int i=left+1;i<=right;i++){
if(list.get(left).getSum()<list.get(i).getSum()){
student=list.get(i);
list.set(i,list.get(left));
list.set(left,student);
}
}
left++;
for(int i=right-1;i>=left;i--){
if(list.get(right).getSum()>list.get(i).getSum()){
student=list.get(i);
list.set(i,list.get(right));
list.set(right,student);
}
}
right--;
}
return list;
}
/**
* 快速排序
* @param studentList
* @return
*/
public List<Student> quickSort(List<Student> studentList){
List<Student> list=studentList;
quickSort1(list,0,list.size()-1);
return list;
}
public void quickSort1(List<Student> studentList,int left,int right){
if(left<right){
int i=left,j=right;
Student student=studentList.get(left);
double x=student.getSum();
while(i<j){
while((i<j)&&(studentList.get(j).getSum()<x)){
j--;
}
if(i<j){
studentList.set(i,studentList.get(j));
i++;
}
while((i<j)&&(studentList.get(i).getSum()>x)){
i++;
}
if(i<j){
studentList.set(j,studentList.get(i));
j--;
}
}
studentList.set(i,student);
quickSort1(studentList,left,i-1);
quickSort1(studentList,i+1,right);
}
}
/**
* 希尔排序
* @param studentList
* @return
*/
public List<Student> shellSort(List<Student> studentList){
List<Student> list=studentList;
Student student=null;
int j;
for (int gap = list.size() / 2; gap > 0; gap /= 2) {
for (int i = gap; i < list.size(); i++) {
student=list.get(i);
double tmp=student.getSum();
for (j = i; j >= gap && tmp>list.get(j-gap).getSum(); j -= gap) {
list.set(j,list.get(j-gap));
}
list.set(j,student);
}
}
return list;
}
/**
* 堆排序
* @param studentList
* @return
*/
public List<Student> heapSort(List<Student> studentList){
List<Student> list=studentList;
int len = list.size();
buildMaxHeap(list, len);
for (int i = len - 1; i > 0; i--) {
swap(list, 0, i);
len--;
heapify(list, 0, len);
}
return list;
}
private void buildMaxHeap(List<Student> studentList, int len) {
for (int i = (int) Math.floor(len / 2); i >= 0; i--) {
heapify(studentList, i, len);
}
}
private void heapify(List<Student> studentList, int i, int len) {
int left = 2 * i + 1;
int right = 2 * i + 2;
int largest = i;
if (left < len && studentList.get(left).getSum() < studentList.get(largest).getSum()) {
largest = left;
}
if (right < len && studentList.get(right).getSum() < studentList.get(largest).getSum()) {
largest = right;
}
if (largest != i) {
swap(studentList, i, largest);
heapify(studentList, largest, len);
}
}
private void swap(List<Student> studentList, int i, int j) {
Student student=studentList.get(i);
studentList.set(i,studentList.get(j));
studentList.set(j,student);
}
/**
* 排名赋值并打印
* @param studentList
*/
public void showPlace(List<Student> studentList){
for(int i=0;i<studentList.size();i++){
studentList.get(i).setPlace(i+1);
}
int i=1;
for(Student each:studentList){
System.out.println(i+"."+" 姓名 "+each.getName()+" 专业 "+each.getMajor()+" 总分 "+each.getSum()+" 名次 "+each.getPlace());
i++;
}
}
};
AccountDao
下面展示一些 内联代码片
。
import system.Accounts;
import system.Database;
public class AccountsDao {
Database database;
public AccountsDao(Database database){
this.database=database;
}
public void add(Accounts accounts){
database.getAccountsList().add(accounts);
}
}
Student
stydents
public class Student implements java.io.Serializable{
//学号、姓名、专业、四门课成绩、总分、名次
private String num;
private String name;
private String major;
private double class1Score;
private double class2Score;
private double class3Score;
private double class4Score;
private double sum;
private int place;
public Student(String num, String name, String major, double class1Score, double class2Score, double class3Score, double class4Score) {
this.num = num;
this.name = name;
this.major = major;
this.class1Score = class1Score;
this.class2Score = class2Score;
this.class3Score = class3Score;
this.class4Score = class4Score;
this.sum=class1Score+class2Score+class3Score+class4Score;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public double getClass1Score() {
return class1Score;
}
public void setClass1Score(double class1Score) {
this.class1Score = class1Score;
}
public double getClass2Score() {
return class2Score;
}
public void setClass2Score(double class2Score) {
this.class2Score = class2Score;
}
public double getClass3Score() {
return class3Score;
}
public void setClass3Score(double class3Score) {
this.class3Score = class3Score;
}
public double getClass4Score() {
return class4Score;
}
public void setClass4Score(double class4Score) {
this.class4Score = class4Score;
}
public double getSum() {
return sum;
}
public void setSum(double sum) {
this.sum = sum;
}
public int getPlace() {
return place;
}
public void setPlace(int place) {
this.place = place;
}
}
Studentsys
下面展示一些 内联代码片
。
import DAO.AccountsDao;
import DAO.StudentDao;
import java.util.List;
import java.util.Scanner;
public class StudentSys {
Database database;
StudentDao studentDao;
AccountsDao accountsDao;
Scanner scanner=new Scanner(System.in);
public StudentSys(Database database){
this.database=database;
studentDao=new StudentDao(database);
accountsDao=new AccountsDao(database);
}
public void loginPage(){
System.out.println("学生成绩档案管理系统");
System.out.println("1.登录");
System.out.println("2.注册");
System.out.println("3.退出系统");
System.out.println("请输入你的选择:");
String input=scanner.next();
switch (input){
case "1":login();
break;
case "2":register();loginPage();
break;
case "3":System.out.println("系统已经退出");
break;
default:{
System.out.println("非法输入,请重新输入");
loginPage();
}
}
}
/**
* 登录
*/
public void login(){
Accounts accounts1=null;
Boolean find=false;
System.out.println("请输入账号");
String userName=scanner.next();
List<Accounts> list=database.getAccountsList();
for(Accounts accounts:list){
if(userName.equals(accounts.getUserName()))
{
accounts1=accounts;
find=true;
break;
}
}
if(find==true){
System.out.println("请输入密码");
String passWord=scanner.next();
if(passWord.equals(accounts1.getPassWord())){
System.out.println("登录成功");
mainPage();
}
else{
System.out.println("密码错误,请重新登录");
login();
}
}
else {
System.out.println("账号不存在");
loginPage();
}
}
/**
* 注册
*/
public void register(){
System.out.println("请输入账号");
String userName=scanner.next();
System.out.println("请输入密码");
String passWord=scanner.next();
accountsDao.add(new Accounts(userName,passWord));
System.out.println("注册成功");
database.accountOut(database.getAccountsList());
}
/**
* 主界面
*/
public void mainPage(){
System.out.println("1.新增学生信息");
System.out.println("2.浏览学生信息");
System.out.println("3.查找学生信息");
System.out.println("4.删除学生信息");
System.out.println("5.退出登录");
System.out.println("请输入你的选择:");
String choose=scanner.next();
switch (choose){
case "1":add();mainPage();
break;
case "2":showInformation();mainPage();
break;
case "3":search();
break;
case "4":delete();mainPage();
break;
case "5":database.studentOut(database.getStudentList());loginPage();
break;
default:System.out.println("非法输入,请重新选择");mainPage();
}
}
/**
* 新增学生信息
*/
public void add(){
System.out.println("学生学号:");
String num=scanner.next();
System.out.println("学生姓名:");
String name=scanner.next();
System.out.println("学生专业:");
String major=scanner.next();
System.out.println("课程1成绩:");
double class1Score=scanner.nextDouble();
System.out.println("课程2成绩:");
double class2Score=scanner.nextDouble();
System.out.println("课程3成绩:");
double class3Score=scanner.nextDouble();
System.out.println("课程4成绩:");
double class4Score=scanner.nextDouble();
studentDao.add(new Student(num,name,major,class1Score,class2Score,class3Score,class4Score));
}
/**
* 展示学生信息
*/
public void showInformation(){
List<Student> list=null;
studentDao.show();
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("请输入你的选择");
String choose=scanner.next();
switch (choose){
case "1":
list=studentDao.doubleBubbleSort(database.getStudentList());
studentDao.showPlace(list);
break;
case "2":
list=studentDao.shellSort(database.getStudentList());
studentDao.showPlace(list);
break;
case "3":
list=studentDao.quickSort(database.getStudentList());
studentDao.showPlace(list);
break;
case "4":
list=studentDao.heapSort(database.getStudentList());
studentDao.showPlace(list);
break;
case "5":
break;
}
}
/**
* 查找学生信息
*/
public void search(){
studentDao.show();
System.out.println("1.按学号查找");
System.out.println("2.按姓名查找");
System.out.println("3.返回上一级");
System.out.println("请输入选项:");
String choose=scanner.next();
switch (choose){
case "1":searchByNum();
break;
case "2":searchByName();
break;
case "3":mainPage();
break;
default:
System.out.println("非法输入,请重新输入");
search();
}
}
/**
* 删除学生信息
*/
public void delete(){
studentDao.show();
System.out.println("要删除的学号:");
String num=scanner.next();
Student student=studentDao.searchByNum(num);
studentDao.delete(student);
System.out.println("删除成功");
}
public void searchByName(){
System.out.println("请输入要查找的姓名:");
String name=scanner.next();
Student student;
student=studentDao.searchByName(name);
System.out.println(" 学号:"
+student.getNum()+" 姓名:"
+student.getName()
+" 专业:"+student.getMajor()
+" 课程1成绩:"+student.getClass1Score()
+" 课程2成绩:"+student.getClass2Score()
+" 课程3成绩:"+student.getClass3Score()
+" 课程4成绩:"+student.getClass4Score());
System.out.println("是否修改成绩(Y\\N)");
String choose=scanner.next();
if(choose.equals("Y")){
System.out.println("修改第几门课成绩:");
String input=scanner.next();
double score;
switch (input){
case "1":score=scanner.nextDouble();student.setClass1Score(score);
break;
case "2":score=scanner.nextDouble();student.setClass2Score(score);
break;
case "3":score=scanner.nextDouble();student.setClass3Score(score);
break;
case "4":score=scanner.nextDouble();student.setClass4Score(score);
break;
default:System.out.println("输入错误");
searchByName();
}
student.setSum(student.getClass1Score()+student.getClass2Score()+student.getClass3Score()+student.getClass4Score());
mainPage();
}
else{
mainPage();
}
}
public void searchByNum(){
System.out.println("请输入要查找的学号:");
String num=scanner.next();
Student student;
student=studentDao.searchByNum(num);
System.out.println(" 学号:"
+student.getNum()+" 姓名:"
+student.getName()
+" 专业:"+student.getMajor()
+" 课程1成绩:"+student.getClass1Score()
+" 课程2成绩:"+student.getClass2Score()
+" 课程3成绩:"+student.getClass3Score()
+" 课程4成绩:"+student.getClass4Score());
System.out.println("是否修改成绩(Y\\N)");
String choose=scanner.next();
if(choose.equals("Y")){
System.out.println("修改第几门课成绩:");
String input=scanner.next();
double score;
switch (input){
case "1":score=scanner.nextDouble();student.setClass1Score(score);
break;
case "2":score=scanner.nextDouble();student.setClass2Score(score);
break;
case "3":score=scanner.nextDouble();student.setClass3Score(score);
break;
case "4":score=scanner.nextDouble();student.setClass4Score(score);
break;
default:System.out.println("输入错误");
searchByNum();
}
student.setSum(student.getClass1Score()+student.getClass2Score()+student.getClass3Score()+student.getClass4Score());
mainPage();
}
else{
mainPage();
}
}
}
Account
Accounts
下面展示一些 内联代码片
。
public class Accounts implements java.io.Serializable{
private String userName;
private String passWord;
public Accounts(String userName, String passWord) {
this.userName = userName;
this.passWord = passWord;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
AccountSys
下面展示一些 内联代码片
。
import java.io.*;
import java.util.List;
public class AccountsInOut {
public void outPut(List<Accounts> list)
{
try {
FileOutputStream outputStream = new FileOutputStream("D:\\学生成绩管理系统数据文件\\Accounts.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(list);
objectOutputStream.flush();
objectOutputStream.close();
outputStream.close();
}catch (Exception e){
}
}
public List<Accounts> inPut()
{
try
{
FileInputStream inputStream=new FileInputStream("D:\\学生成绩管理系统数据文件\\Accounts.txt");
ObjectInputStream objectInputStream=new ObjectInputStream(inputStream);
List<Accounts>list=(List<Accounts>)objectInputStream.readObject();
objectInputStream.close();
inputStream.close();
return list;
}catch(IOException i)
{
return null;
}catch(ClassNotFoundException c)
{
return null;
}catch (Exception e){
return null;
}
}
}
InandOut
下面展示一些 内联代码片
。
import java.io.*;
import java.util.List;
public class InAndOut {
public void outPut(List<Student>list)
{
try {
FileOutputStream outputStream = new FileOutputStream("D:\\学生成绩管理系统数据文件\\test.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(list);
objectOutputStream.flush();
objectOutputStream.close();
outputStream.close();
}catch (Exception e){
}
}
public List<Student> inPut()
{
try
{
FileInputStream inputStream=new FileInputStream("D:\\学生成绩管理系统数据文件\\test.txt");
ObjectInputStream objectInputStream=new ObjectInputStream(inputStream);
List<Student>list=(List<Student>)objectInputStream.readObject();
objectInputStream.close();
inputStream.close();
return list;
}catch(IOException i)
{
return null;
}catch(ClassNotFoundException c)
{
return null;
}catch (Exception e){
return null;
}
}
}
Main
下面展示一些 内联代码片
。
public class Main {
public static void main(String[]args){
Database database = new Database();
StudentSys studentSys=new StudentSys(database);
studentSys.loginPage();
}
}