简介
该项目是我大一学习javaSE阶段写过的一个新手项目。适合处在学习JavaSE阶段的朋友作为阶段性练手项目,主要是练习一下基本的流程控制结构,循环结构,编写类、List集合的简单使用,IO的文件简单读取写入。
功能介绍
在控制台实现,登录,注册,学生信息增删查改,通过输出流把用户信息,学生信息写入文本文件,进行持久化存储,通过输入流把信息读取到内存打印到控制台实现查询。
目录结构:
运行示例
项目代码:
实现流程
定义学生类,用户类
public class Student { String id;//学号 String name;//姓名 String phone;//电话 String major;//专业 String address;//住址 public Student(){} public Student(String id,String name,String phone,String major,String address){ this.id=id; this.name=name; this.phone=phone; this.major=major; this.address=address; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; }
public class Userinfo { String account;//账号 String password;//密码 public Userinfo(){} public Userinfo(String account, String password) { super(); this.account = account; this.password = password; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }
功能代码
public class StudentDemo { public static void main(String[] args) throws IOException { String fileName = "学生信息.txt"; String fileName1 = "用户信息.txt"; // 登录 DL(fileName1); // 管理 GL(fileName); } // 登录界面 public static void DL(String fileName1) throws IOException { while (true) { boolean flag = false; System.out.println("**********xxxx学生管理系统**********"); System.out.println("\t\t1.登录"); System.out.println("\t\t2.注册"); System.out.print("选择:"); Scanner sc = new Scanner(System.in); String choose = sc.nextLine(); switch (choose) { case "1": denglu(fileName1); flag = true; break; case "2": zhuce(fileName1); break; default: System.out.println("错误!!!"); break; } if (flag) { break; } } } // 用户注册 public static void zhuce(String fileName1) throws IOException { ArrayList<Userinfo> array = new ArrayList<Userinfo>(); Scanner sc = new Scanner(System.in); dlReader(fileName1, array); while (true) { boolean flag = false; System.out.print("注册账号:"); String account = sc.nextLine(); // 判断是否已经注册过!!!! /// for (int i = 0; i < array.size(); i++) { Userinfo u1 = array.get(i); if (u1.getAccount().equals(account)) { System.out.println("该用户已注册!!!"); flag = true; break; } } if (flag) { zhuce(fileName1);// 不要放for循环里前否则会多次循环!!!!!!! break; } /// System.out.print("注册密码:"); String password = sc.nextLine(); System.out.print("确认密码:"); String password1 = sc.nextLine(); if (password.equals(password1)) { Userinfo u = new Userinfo(); u.setAccount(account); u.setPassword(password1); array.add(u); dlWriter(fileName1, array); System.out.println("注册成功!"); break; } else { System.out.println("密码不一致请重新注册!!!"); } } } // 用户登录 public static void denglu(String fileName1) throws IOException { ArrayList<Userinfo> array = new ArrayList<Userinfo>(); Scanner sc = new Scanner(System.in); dlReader(fileName1, array); while (true) { boolean flag = false; System.out.print("登录账号:"); String account = sc.nextLine(); System.out.print("登录密码:"); String password = sc.nextLine(); for (int i = 0; i < array.size(); i++) { Userinfo u = array.get(i); if (u.getAccount().equals(account) && u.getPassword().equals(password)) { System.out.println("登录成功!"); flag = true; break; } } if (!flag) { System.out.println("账号或密码错误!!!"); } if (flag) { break; } } } // 登录界面-读 public static void dlReader(String fileName1, ArrayList<Userinfo> array) throws IOException { BufferedReader br = new BufferedReader(new FileReader(fileName1)); String line; while ((line = br.readLine()) != null) { String[] readDate = line.split("\t"); Userinfo u = new Userinfo(); u.setAccount(readDate[0]); u.setPassword(readDate[1]); array.add(u); } br.close(); } // 登录界面-写 public static void dlWriter(String fileName1, ArrayList<Userinfo> array) throws IOException { BufferedWriter bw = new BufferedWriter(new FileWriter(fileName1)); for (int i = 0; i < array.size(); i++) { Userinfo u = array.get(i); StringBuilder sb = new StringBuilder(); sb.append(u.getAccount()).append("\t").append(u.getPassword()); bw.write(sb.toString()); bw.newLine(); bw.flush(); } bw.close(); } // 管理界面 public static void GL(String fileName) throws IOException { while (true) { System.out.println("**********xxxx学生管理系统**********"); System.out.println("\t\t1.增"); System.out.println("\t\t2.删"); System.out.println("\t\t3.查"); System.out.println("\t\t4.改"); System.out.println("\t\t5.退"); System.out.print("选择:"); Scanner sc = new Scanner(System.in); String choose = sc.nextLine(); switch (choose) { case "1": add(fileName); break; case "2": remove(fileName); break; case "3": fine(fileName); break; case "4": update(fileName); break; case "5": System.out.println("退出成功!"); System.exit(0); break; default: System.out.println("错误!!!"); break; } } } // 增 public static void add(String fileName) throws IOException { ArrayList<Student> array = new ArrayList<Student>(); Scanner sc = new Scanner(System.in); boolean flag = false; System.out.print("学号:"); String id = sc.nextLine(); glReader(fileName, array); for (int i = 0; i < array.size(); i++) { Student s = array.get(i); if (s.getId().equals(id)) { System.out.println("学号存在!!!"); flag = true; break; } } if (flag) { add(fileName); } System.out.print("姓名:"); String name = sc.nextLine(); System.out.print("电话:"); String phone = sc.nextLine(); System.out.print("专业:"); String major = sc.nextLine(); System.out.print("地址:"); String address = sc.nextLine(); Student s = new Student(); s.setId(id); s.setName(name); s.setPhone(phone); s.setMajor(major); s.setAddress(address); array.add(s); glWriter(fileName, array); System.out.println("添加成功!"); } // 删 public static void remove(String fileName) throws IOException { ArrayList<Student> array = new ArrayList<Student>(); Scanner sc = new Scanner(System.in); glReader(fileName, array); while (true) { boolean flag = false; System.out.print("删除学号:"); String id = sc.nextLine(); for (int i = 0; i < array.size(); i++) { Student s = array.get(i); if (s.getId().equals(id)) { System.out.println("学号存在!"); array.remove(i); System.out.println("删除成功!"); flag = true; break; } else if ((i == array.size() - 1) && (!s.getId().equals(id))) { System.out.println("学号不存在!!!"); } } if (flag) { break; } } glWriter(fileName, array); } // 查 public static void fine(String fileName) throws IOException { ArrayList<Student> array = new ArrayList<Student>(); Scanner sc = new Scanner(System.in); System.out.println("\t\t1.全部查询"); System.out.println("\t\t2.关键字查询"); System.out.print("选择:"); String choose = sc.nextLine(); switch (choose) { case "1": qbfine(fileName, array); break; case "2": gjfine(fileName, array); break; default: System.out.println("错误!!!"); break; } } // 全部查询 public static void qbfine(String fileName, ArrayList<Student> array) throws IOException { glReader(fileName, array); System.out.println("-------------------------------------------------------------------------"); System.out.println("【学号】 【姓名】 【电话】 【专业】 【地址】"); if (array.size() != 0) { for (int i = 0; i < array.size(); i++) { Student s = array.get(i); System.out.println(s.getId() + " " + s.getName() + "\t " + s.getPhone() + "\t " + s.getMajor() + "\t" + s.getAddress()); } } else { System.out.println("无信息!!!"); } System.out.println("-------------------------------------------------------------------------"); } // 关键字查询 public static void gjfine(String fileName, ArrayList<Student> array) throws IOException { glReader(fileName, array); Scanner sc = new Scanner(System.in); System.out.println("关键字:"); String gj = sc.nextLine(); System.out.println("-------------------------------------------------------------------------"); System.out.println("【学号】 【姓名】 【电话】 【专业】 【地址】"); if (array.size() != 0) { for (int i = 0; i < array.size(); i++) { Student s = array.get(i); if (s.getId().equals(gj) || s.getName().equals(gj) || s.getPhone().equals(gj) || s.getMajor().equals(gj) || s.getAddress().equals(gj)) { System.out.println(s.getId() + " " + s.getName() + "\t " + s.getPhone() + "\t " + s.getMajor() + "\t" + s.getAddress()); } else if (i == array.size() - 1 && !s.getId().equals(gj) || s.getName().equals(gj) || s.getPhone().equals(gj) || s.getMajor().equals(gj) || s.getAddress().equals(gj)) { System.out.println("\t\t\t\t无信息!!!"); } } } else { System.out.println("\t\t\t\t无信息!!!"); } System.out.println("-------------------------------------------------------------------------"); } // 改 public static void update(String fileName) throws IOException { ArrayList<Student> array = new ArrayList<Student>(); Scanner sc = new Scanner(System.in); glReader(fileName, array); while (true) { boolean flag = false; System.out.println("修改学号:"); String id = sc.nextLine(); for (int i = 0; i < array.size(); i++) { Student s = array.get(i); if (s.getId().equals(id)) { System.out.print("新姓名:"); String name = sc.nextLine(); System.out.print("新电话:"); String phone = sc.nextLine(); System.out.print("新专业:"); String major = sc.nextLine(); System.out.print("新地址:"); String address = sc.nextLine(); s.setName(name); s.setPhone(phone); s.setMajor(major); s.setAddress(address); array.set(i, s); System.out.println("修改成功!"); flag = true; break; } else if ((i == array.size() - 1) && (!s.getId().equals(id))) { System.out.println("学号不存在!!!"); } } if (flag) { break; } } glWriter(fileName, array); } // 管理界面-读 public static void glReader(String fileName, ArrayList<Student> array) throws IOException { BufferedReader br = new BufferedReader(new FileReader(fileName)); String line; while ((line = br.readLine()) != null) { String[] date = line.split(","); Student s = new Student(); s.setId(date[0]); s.setName(date[1]); s.setPhone(date[2]); s.setMajor(date[3]); s.setAddress(date[4]); array.add(s); } br.close(); } // 管理界面-写 public static void glWriter(String fileName, ArrayList<Student> array) throws IOException { BufferedWriter bw = new BufferedWriter(new FileWriter(fileName)); for (int i = 0; i < array.size(); i++) { Student s = array.get(i); StringBuilder sb = new StringBuilder(); sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getPhone()).append(",") .append(s.getMajor()).append(",").append(s.getAddress()); bw.write(sb.toString()); bw.newLine(); bw.flush(); } bw.close(); }