数据结构实践1——学生档案管理系统
目录
算法
排序算法自己曾经的总结,这里不再赘述:链接: https://blog.csdn.net/m0_46113894/article/details/109520450.
文件的存储与读取:
- 使用BufferReader缓冲流读取,并使用装饰模式,使用输入流与文件流构造
- 使用BufferWriter写文件,构造时注意:文件流第二个参数写false,这样写的时候不会接着文件的末尾加上去,而是重写文件。
- 采用特定文本格式存储,每次读取一行,使用String的split方法根据逗号分隔字符串,借此提取出字符串中的信息,并顺序的存入Array List列表中
- 写的时候顺序遍历列表,通过构造该格式的字符串再一行一行地写入文件
new BufferedReader(new InputStreamReader(new FileInputStream(fileName), “UTF-8”));
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, false)));
代码
pojo
Student类
package StudentInfoManage.pojo;
/**
* @author LiuWei
*/
public class Student {
private int id;
private String name;
private String major;
private int cpp;
private int network;
private int dataStructure;
private int english;
private int total;
private int ranking;
private static int studentNum=1;
public Student(){
}
/**
* 新增学生所需构造函数,id自动生成,提供格式与存储格式相同
* @param name
* @param major
* @param cpp
* @param network
* @param dataStructure
* @param english
*/
public Student(String name, String major, int cpp, int network, int dataStructure, int english) {
this.id=studentNum++;
this.name = name;
this.major = major;
this.cpp = cpp;
this.network = network;
this.dataStructure = dataStructure;
this.english = english;
this.total =this.cpp+this.network+this.dataStructure+this.english;
this.ranking=0;
}
/**
* 文件读取用,所有参数为String,无id,总分,排名
* @param name
* @param major
* @param cpp
* @param network
* @param dataStructure
* @param english
*/
public Student( String name, String major, String cpp, String network, String dataStructure, String english) {
this.id=studentNum++;
this.name = name;
this.major = major;
this.cpp =Integer.parseInt(cpp);
this.network =Integer.parseInt( network);
this.dataStructure = Integer.parseInt(dataStructure);
this.english = Integer.parseInt(english);
this.total =this.cpp+this.network+this.dataStructure+this.english;
this.ranking = 0;
}
/**
* 提供所有参数,用于读取文件中学生信息完全都有的情况,所有参数为String
* @param id
* @param name
* @param major
* @param cpp
* @param network
* @param dataStructure
* @param english
* @param total
* @param ranking
*/
public Student(String id, String name, String major, String cpp, String network, String dataStructure, String english, String total, String ranking) {
this.id=Integer.parseInt(id);
//遇到更大的id自动按大号生成
if(this.id>studentNum){
studentNum=this.id+1;
}
this.name = name;
this.major = major;
this.cpp =Integer.parseInt(cpp);
this.network =Integer.parseInt( network);
this.dataStructure = Integer.parseInt(dataStructure);
this.english = Integer.parseInt(english);
this.total =this.cpp+this.network+this.dataStructure+this.english;
this.ranking = Integer.parseInt(ranking);
}
/**
* 用于读取文件的不同情况
* @param id
* @param name
* @param major
* @param cpp
* @param network
* @param dataStructure
* @param english
*/
public Student(String id ,String name, String major, String cpp, String network, String dataStructure, String english) {
this.id=Integer.parseInt(id);
this.name = name;
this.major = major;
this.cpp =Integer.parseInt(cpp);
this.network =Integer.parseInt( network);
this.dataStructure = Integer.parseInt(dataStructure);
this.english = Integer.parseInt(english);
this.total =this.cpp+this.network+this.dataStructure+this.english;
this.ranking = 0;
}
public int getId() {
return id;
}
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 int getCpp() {
return cpp;
}
public void setCpp(int cpp) {
this.cpp = cpp;
}
public int getNetwork() {
return network;
}
public void setNetwork(int network) {
this.network = network;
}
public int getDataStructure() {
return dataStructure;
}
public void setDataStructure(int dataStructure) {
this.dataStructure = dataStructure;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getRanking() {
return ranking;
}
public void setRanking(int ranking) {
this.ranking = ranking;
}
public String getAllInfo(){
return new String(id+","+name+","+major+","+cpp+","+network+","+dataStructure+","+english+","+total+","+ranking);
}
public void setAllScore(int cpp,int network,int dataStructure,int english) {
this.cpp = cpp;
this.network=network;
this.dataStructure=dataStructure;
this.english=english;
this.total=cpp+network+dataStructure+english;
}
public void showAll(){
System.out.println(id+" "+name+" "+major+" "+cpp+" "+network+" "+ dataStructure+" "+english+" "+total+" "+ranking);
}
}
Account类
package StudentInfoManage.pojo;
/**
* @author lw
*/
public class Account {
/**账户编号*/
private int accountId;
/**账户名称*/
private String accountName;
/**密码*/
private String password;
static int accountNum=1;
public Account( String accountName, String password) {
this.accountId =accountNum++ ;
this.accountName = accountName;
this.password = password;
}
public Account() {
}
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
data
StudentInfo类
package StudentInfoManage.data;
import StudentInfoManage.pojo.Account;
import StudentInfoManage.pojo.Student;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author LiuWei
* 该类负责读取文件,将信息保存在List中
*/
public class StudentInfo {
/**学生类表*/
private List<Student> students = new ArrayList<>();
private List<Account> accountList=new ArrayList<>();
BufferedReader br;
BufferedWriter out;
String fileName="src/StudentInfoManage/StudentInfo.txt";
/**构造函数
* 这里读取文件初始化数据
*/
public StudentInfo() throws IOException {
accountList.add(new Account("admin","0000"));
try {
//初始化输入输出流,注意Reader和Writer只能同时有一个,windows系统只能同时选其一中,否则文件会被清空
br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
String contents = null;
String[] splitInfo = null;
//循环读取所有的文本
do {
contents = br.readLine();
if (contents != null) {
splitInfo = contents.split(",");
//System.out.println(Arrays.toString(splitInfo));
} else {
break;
}
if (splitInfo != null && splitInfo.length == 7) {
students.add(new Student(splitInfo[0], splitInfo[1], splitInfo[2], splitInfo[3], splitInfo[4], splitInfo[5], splitInfo[6]));
} else if (splitInfo.length == 9) {
students.add(new Student(splitInfo[0], splitInfo[1], splitInfo[2], splitInfo[3], splitInfo[4], splitInfo[5], splitInfo[6], splitInfo[7], splitInfo[8]));
}
} while (contents != null);
//System.out.println(students.size());
} catch (UnsupportedEncodingException unsupportedEncodingException) {
unsupportedEncodingException.printStackTrace();
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
br.close();
}
public void setStudents(List<Student> students) {
this.students = students;
}
/**
* 根据线性表重写文件,更新学生信息
*/
public void update() {
//每次更新重新打开输出流,保证每次重写
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, false)));
for (Student ele : students) {
try {
out.write(ele.getAllInfo() + '\n');
} catch (IOException e) {
e.printStackTrace();
}
}
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public List<Student> getStudents () {
return students;
}
public List<Account> getAccountList(){
return accountList;
}
}
DAO
StudentDAO类
package StudentInfoManage.dao;
import StudentInfoManage.data.StudentInfo;
import StudentInfoManage.pojo.Student;
import java.util.List;
import java.util.Scanner;
/**
* @author LiuWei
*/
public class StudentDAO {
private StudentInfo database;
public StudentDAO(StudentInfo database) {
this.database = database;
sortStudent();
database.update();
}
/**
* 新增信息
* @param student 学生对象
*/
public void addStudent(Student student){
database.getStudents().add(student);
sortStudent();
database.update();
}
/**
* 更新课程成绩
* @param studentId
* @param cpp
* @param net
* @param data
* @param eng
*/
public boolean updateStudent(int studentId,int cpp,int net,int data ,int eng){
if(cpp<0||net<0||data<0||eng<0){
return false;
}
for (Student student : database.getStudents()) {
if (student.getId()==studentId){
student.setAllScore(cpp,net,data,eng);
sortStudent();
database.update();
return true;
}
}
return false;
}
/**
* 根据id查找
* @param studentId id
* @return 返回学生对象
*/
public Student searchByStudentId(int studentId){
for (Student student : database.getStudents()) {
if (student.getId()==studentId){
return student;
}
}
return null;
}
/**
* 根据姓名查找
* @param studentName 姓名
* @return 返回学生对象
*/
public Student searchByStudentName(String studentName){
for (Student student : database.getStudents()) {
if (student.getName().equals(studentName)){
return student;
}
}
return null;
}
/**
* 显示所有学生信息
*/
public List<Student> getAll(){
return database.getStudents();
}
/**
* 计算学生的排名
*/
public void sortStudent(){
List <Student>list= database.getStudents();
System.out.println("请选择排序方式:");
System.out.println("1.双向冒泡排序");
System.out.println("2.快速排序");
System.out.println("3.希尔排序");
System.out.println("4.堆排序");
String choice=new Scanner(System.in).next();
switch (choice){
case "1":duplexSort(list);break;
case "3":hillSort(list);break;
case "4":heapSort(list);break;
default:quickSort(list,0,list.size()-1);
}
for(int i=0;i< list.size();i++){
if(i>0&&list.get(i).getTotal()==list.get(i-1).getTotal()){
list.get(i).setRanking(list.get(i-1).getRanking());
}else {
list.get(i).setRanking(i+1);
}
}
}
public boolean deleteStudent(Student stu){
List <Student> list=database.getStudents();
for (Student student : list) {
if (student.getId()==stu.getId()){
list.remove(student);
quickSort(list,0,list.size()-1);
database.update();
return true;
}
}
return false;
}
/**
* 双向冒泡排序
* @param list 待排序线性表
*/
public void duplexSort( List <Student>list){
int left=0,right = list.size()-1;
while(left<right){
//左侧扫描
for(int j = left+1; j <= right; j++) {
if(list.get(left).getTotal()<list.get(j).getTotal()) {
Student temp=list.get(left);
list.set(left,list.get(j));
list.set(j,temp);
}
}
left++;
if(left>=right){
break;
}
//右侧扫描
for(int j = right-1; j >= left; j--) {
if(list.get(right).getTotal()>list.get(j).getTotal()) {
Student temp=list.get(right);
list.set(right,list.get(j));
list.set(j,temp);
}
}
right--;
}
}
/**
* 快速排序
* @param list 待排序线性表
* @param left 左边界
* @param right 右边界
*/
public void quickSort( List <Student>list,int left,int right){
if (left > right) {
return;
}
int i = left;
int j = right;
Student t = list.get(left);
while (i != j){
while (list.get(j).getTotal() <= t.getTotal()&& (j > i)) {
j--;
}
while (list.get(i).getTotal() >= t.getTotal()&& (j > i)) {
i++;
}
//交换位置
if (i < j) {
Student temp=list.get(i);
list.set(i,list.get(j));
list.set(j,temp);
}
}
list.set(left,list.get(i));
list.set(i,t);
//递归左子序列
quickSort(list,left, --i);
//递归右子序列
quickSort(list,++j, right);
return;
}
/**
* 希尔排序
* @param list 待排序线性表
*/
public void hillSort( List <Student>list){
int d, i, j;
int n=list.size()-1;
//多个短序列循环整合
for (d=n/2;d>=1;d=d/2) {
//每个序列内循环插入每个元素
for ( i = d; i <= n; i++) {
//存放待插入元素
Student temp= list.get(i);
//寻找插入位置
for (j = i-d; j>=0&&temp.getTotal() >list.get(j).getTotal(); j-=d) {
//待插入元素向后移动
list.set(j+d,list.get(j));
}
list.set(j+d,temp);
}
}
}
/**
* 堆排序
* @param list
*/
public void heapSort( List <Student>list){
int i = 0;
int n=list.size()-1;
//初始建堆,从最后一个分支结点至根结点
for (i = n/2+1; i >= 0; i--) {
Sift(list, i, n);
}
//System.out.println(list.get(0).getAllInfo());
//重复执行移走堆顶及重建堆的操作
for (i=0; i<=n; i++) {
// System.out.println(list.get(0).getAllInfo());
//交换0和末尾(n-i)的位置
Student temp=list.get(0);
list.set(0,list.get(n-i));
list.set(n-i,temp);
Sift(list, 0, n-i-1);
}
}
/**
* 创建小根堆
* @param list
* @param k 当前位置
* @param m 最大下标
*/
void Sift(List<Student> list, int k, int m) {
//i指向被筛选结点,j指向结点i的左孩子
int i = k, j = 2 * i+1;
while (j <= m) {
//比较i的左右孩子,j指向两者中的较小者
if (j < m &&list.get(j).getTotal() > list.get(j+1).getTotal()) {
j++;
}
//若根结点已经小于左右孩子中的较小者
if (list.get(i).getTotal() < list.get(j).getTotal()) {
break;
} else {
Student temp=list.get(i);
list.set(i,list.get(j));
list.set(j,temp);
//被筛结点位于原来结点j的位置
i = j; j = 2 * i+1;
}
}
}
}
AccountDAO类
package StudentInfoManage.dao;
import StudentInfoManage.data.StudentInfo;
import StudentInfoManage.pojo.Account;
/**
* @author lw
*/
public class AccountDao {
/**数据库*/
private StudentInfo StudentInfo;
/**
* 构造函数传入同一个数据库
*/
public AccountDao( StudentInfo studentInfo) {
this.StudentInfo = studentInfo;
}
/**
* 根据用户名查找用户
* @param accountName 用户名称
* @return 账户对象
*/
public Account findByName(String accountName){
Account account = null;
for (Account user : StudentInfo.getAccountList()) {
//比较账户名
if(user.getAccountName().equals(accountName)){
account = user;
break;
}
}
return account;
}
/**
* 新增用户
* @param account
*/
public void insert(Account account){
StudentInfo.getAccountList().add(account);
}
}
service
StudentSystem类
package StudentInfoManage.service;
import StudentInfoManage.dao.AccountDao;
import StudentInfoManage.dao.StudentDAO;
import StudentInfoManage.data.StudentInfo;
import StudentInfoManage.pojo.Account;
import StudentInfoManage.pojo.Student;
import java.util.Scanner;
/**
* @author LiuWei
*/
public class StudentSystem {
private Scanner scanner = new Scanner(System.in);
private StudentDAO studentDAO;
private AccountDao accountDao;
public StudentSystem(StudentInfo studentinfo) {
this.accountDao=new AccountDao(studentinfo);
this.studentDAO = new StudentDAO(studentinfo);
}
public void start(){
System.out.println("1. 登录");
System.out.println("2. 注册");
System.out.println("3. 退出系统");
System.out.println("请选择:");
String choice = scanner.next();
switch (choice){
case "1":
//登录
login();
mainMenu();
break;
case "2":
regist();
break;
//注册
case "3":
System.out.println("退出了系统!");
break;
default:
System.out.println("无效的选项,请重新输入!");
start();
}
}
public void mainMenu(){
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("6. 退出系统");
String choice = scanner.next();
switch (choice){
case "1":
addStudent();
mainMenu();
break;
case "2":
showAllStudentInfo();
mainMenu();
break;
case "3":
deleStudent();
mainMenu();
break;
case "4":
searchStudent();
mainMenu();
break;
case "5":
updateStudent();
mainMenu();
break;
case "6":
System.out.println("退出了系统!");
break;
default:
System.out.println("无效的选项,请重新输入!");
mainMenu();
}
}
/**
* 登录
*/
public void login(){
System.out.println("请输入用户名:");
String userName = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
//通过DAO根据用户名查询数据库中对象
Account account = accountDao.findByName(userName);
//判断用户是否存在
if(account != null){
//将数据库中找到的账户,再根据其密码和输入的密码比较
if(account.getPassword().equals(password)){
System.out.println("登录成功!");
}else{
System.out.println("密码错误!");
login();
}
}else{
System.out.println("用户名不存在!请重新输入");
login();
}
}
/**
* 注册
*/
public void regist() {
System.out.println("请输入注册的用户名:");
String userName = scanner.next();
System.out.println("请输入注册的密码:");
String password = scanner.next();
System.out.println("请再次输入确认密码:");
String passwordConfirm = scanner.next();
//判断是否密码相同
if (password.equals(passwordConfirm)) {
String confirmCode=getVerificationCode();
System.out.println("请输入验证码");
System.out.println("********");
System.out.println("* "+confirmCode+" *");
System.out.println("********");
String userCode=scanner.next();
if (userCode.equals(confirmCode)){
//判断用户名是否被注册
Account account = accountDao.findByName(userName);
if (account != null) {
System.out.println("该用户名已被注册!输入0返回上一层菜单,其他任意键继续注册");
String result = scanner.next();
if (result.equals("0")) {
start();
} else {
regist();
}
} else {
//根据注册的信息创建一个账户对象
Account registAccount = new Account(userName,password);
//将注册的信息写入数据库
accountDao.insert(registAccount);
System.out.println("注册成功!");
//返回起始菜单
start();
}
}
else {
System.err.println("验证码不正确!请重新输入");
regist();
}
}
else {
System.err.println("两次输入的密码不一致!请重新输入");
regist();
}
}
/**
* 生成验证码
* @return 验证码String
*/
public String getVerificationCode(){
String code="";
for (int i = 0; i < 4; i++) {
code+=(int)(Math.random()*10);
}
return code;
}
/**
* 新增学生
*/
public void addStudent(){
System.out.println("请输入学生姓名");
String name=scanner.next();
System.out.println("请输入专业");
String major=scanner.next();
System.out.println("请输入c++成绩");
int cpp=toInt(scanner.next());
System.out.println("请输入计算机网络成绩");
int net=toInt(scanner.next());
System.out.println("请输入数据结构成绩");
int data=toInt(scanner.next());
System.out.println("请输入英语成绩");
int eng=toInt(scanner.next());
if(cpp<0||net<0||data<0||eng<0){
System.err.println("创建失败");
return;
}
studentDAO.addStudent(new Student(name,major,cpp,net,data,eng));
System.out.println("创建成功!");
}
/**
* 显示所有信息
*/
public void showAllStudentInfo(){
System.out.println("id "+" 姓名 "+" 专业 "+" c++ "+" 计算机网络 "+"数据结构"+" 英语 "+" 总分 "+" 名次 ");
for (Student stu:studentDAO.getAll()){
stu.showAll();
}
}
/**
* 根据id查找学生,修改四门课成绩
*/
public void updateStudent(){
System.out.println("请输入要修改学生的id");
int id=toInt(scanner.next());
System.out.println("请输入c++成绩");
int cpp=toInt(scanner.next());
System.out.println("请输入计算机网络成绩");
int net=toInt(scanner.next());
System.out.println("请输入数据结构成绩");
int data=toInt(scanner.next());
System.out.println("请输入英语成绩");
int eng=toInt(scanner.next());
if(studentDAO.updateStudent(id,cpp,net,data,eng)){
System.out.println("修改成功");
}else {
System.err.println("修改失败");
}
}
/**
* 搜索学生
*/
public void searchStudent(){
System.out.println("1. 根据姓名查找");
System.out.println("2. 根据id查找");
System.out.println("3. 返回上一级");
String choice = scanner.next();
switch (choice){
case "1":
System.out.println("请输入姓名");
String name=scanner.next();
Student stua=studentDAO.searchByStudentName(name);
if(stua==null){
System.out.println("未查询到");
}else {
stua.showAll();
start();
}
break;
case "2":
System.out.println("请输入id");
int id=toInt(scanner.next());
Student stub=studentDAO.searchByStudentId(id);
if(stub==null){
System.out.println("未查询到");
}else {
stub.showAll();
start();
}
break;
case "3":
start();
break;
default:
System.err.println("无效的选项,请重新输入!");
searchStudent();
}
}
/**
* 删除学生
*/
public void deleStudent(){
System.out.println("请输入学生的id");
int id=toInt(scanner.next());
Student stu=studentDAO.searchByStudentId(id);
if(stu!=null&& studentDAO.deleteStudent(stu)){
System.out.println("删除成功");
}else {
System.out.println("未查询到该学生");
}
}
/**
* 返回字符串的对应数字,如果输入的不是数字返回-1,同时catch异常防止转换函数抛出异常
* @param a 输入的字符串
* @return 相应的数字
*/
public int toInt(String a){
try {
int result=Double.valueOf(a).intValue();
return result<0?0:result;
}catch (Exception e){
return -1;
}
}
}
Main
package StudentInfoManage;
import StudentInfoManage.dao.StudentDAO;
import StudentInfoManage.data.StudentInfo;
import StudentInfoManage.service.StudentSystem;
import java.io.IOException;
/**
* @author LiuWei
*/
public class Main {
public static void main(String[] args) {
StudentInfo database= null;
try {
database = new StudentInfo();
} catch (IOException e) {
e.printStackTrace();
}
StudentSystem sys=new StudentSystem(database);
sys.start();
}
}
版权声明:本文为m0_46113894原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。