这是一个用JavaSE写的学生管理系统,功能非常简单,只能实现简单的增删改查,没有使用数据库和前端页面,这里使用txt文档作为数据库,使用控制台作为前端。
entity:实体类
student学生类:
package com.abc.entity;
public class Student {
private String id;//学生学号
private String name;//学生姓名
private String gender;//性别
private int age;//学生年龄
private double score;//java成绩
private String password;//密码
public Student() {
}
//此构造器用于学生登录
public Student(String name, String password) {
this.name = name;
this.password = password;
}
//学生修改个人信息使用
public Student(String id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
//用于查看学生信息
public Student(String id, String name, String gender, int age, double score) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.score = score;
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Student [name=" + name + ", password=" + password + "]";
}
}
teacher教师类:
package com.abc.entity;
/**
* 这是一个实体类
* 这里把教师当作是管理员,教师具有对学生信息进行增加、删除、修改、查询的操作(1.查看学生信息 2.添加学生信息 3.修改学生信息 4.退出)
* 学生的权限(1.查看学生信息 2.修改个人信息 3.退出)
*
*/
public class Teacher {
private String name;//用户名
private String password;//密码
//无参构造
public Teacher() {
}
//有参构造,目的是为了给这些属性赋值
public Teacher(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Teacher [name=" + name + ", password=" + password + "]";
}
}
Dao层:就是对txt文件读写的操作
package com.abc.dao;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import com.abc.entity.Student;
public class StudentLoginDao {
// 集合里面存放的是Student对象,所以泛型<Student>,现在把这个集合当作是一个空的容器,用来接收从studentLogin.txt里面读取出来的信息
private static ArrayList<Student> list = new ArrayList<Student>();
// 将teacher.txt数据读取出来,添加到集合中,封装一个方法来读取(封装一个方法可以提高代码的重用性)。由于studentLogin.txt是纯文本文件,所以可以使用字符流来读取数据,这样效率会更高
public void init() throws Exception {
BufferedReader br = new BufferedReader(
new FileReader(new File("E:\\JAVA\\workspace\\studentManager\\src\\studentLogin.txt")));
String s;
// readLine()每次读取一行数据
while ((s = br.readLine()) != null) {
String[] strs = s.split(" ");// 用空格来切割字符串,目的是为了获取到用户名和密码[张三,123456],第一个是用户名,第2个是密码
Student student = new Student(strs[0], strs[1]);// 将切割好的数组通过下标取出来赋值给Student对象,循环一次放一次数据(此语句调用了双参构造方法,也起到了给属性赋值的作用)
list.add(student);//此时集合里面就有数据了,里面存放的是student对象,
}
br.close();//流使用过后要关闭流,有个规则是:先用后关
//System.out.println("list"+list);//打印list集合里面的信息
}
//封装一个方法,返回值是ArrayList集合,目的是为了和控制台输入的信息比较
public ArrayList<Student> getStudentLogin(){
return list;
}
}
package com.abc.dao;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import com.abc.entity.Student;
import com.abc.entity.Teacher;
public class StudentDao {
// 这是一个空的容器,用来接收从student.txt里面读取的信息
private static ArrayList<Student> list = new ArrayList<Student>();
// 封装一个方法init2(),用来读取student.txt里面的数据
public static void init2() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(new File("E:\\JAVA\\workspace\\studentManager\\src\\student.txt")));
String s;
while ((s = br.readLine()) != null) {
String[] strs = s.split(" ");
Student student = new Student(strs[0], strs[1], strs[2], Integer.parseInt(strs[3]),Double.parseDouble(strs[4]));
list.add(student);
}
br.close();
}
// 封装一个方法来取值
public static ArrayList<Student> getStudents() {
return list;
}
// dao层通过servic传参传过来的值,写入student.txt文件中
public void addStudent(Student student) throws IOException {
list.add(student);
// 写入student.txt文件中
writeStudent();
}
// 在dao层通过service传过来的参数进行数据修改
public void updateStudent(String updateId, Student newStudent) throws IOException {
// 修改学生信息 在集合里面使用set方法set(index, element)
int index = getIndex(updateId);
list.set(index, newStudent);// set(index, element)
// 修改完以后要写到student.txt里面
// 写入student.txt文件中
writeStudent();
}
// 删除学生信息
public void deleteStudent(String deleteId) throws IOException {
int index = getIndex(deleteId);// 获取要删除学生信息的索引
list.remove(index);// 通过索引删除
// 删除过学生信息之后,要把list集合里面的信息重新写入student.txt
writeStudent();
}
// 通过传过来要修改的id学号找到当前数据的索引
public int getIndex(String id) {
int index = -1;
for (int i = 0; i < list.size(); i++) {// 对list集合进行遍历
if (id.equals(list.get(i).getId())) {
index = i;
break;
}
}
return index;
}
// 封装一个将list集合信息重新写入student.txt的方法
public void writeStudent() throws IOException {
// 写入student.txt文件中
BufferedWriter bw = new BufferedWriter(
new FileWriter(new File("E:\\JAVA\\workspace\\studentManager\\src\\student.txt")));
for (Student student2 : list) {// 现在list集合里面添加了新的数据,此时就把里面的数据再重新输入一遍
// 每次循环写入一条数据,写完一条数据就换行写下一条数据
bw.write(student2.getId() + " " + student2.getName() + " " + student2.getGender() + " " + student2.getAge()
+ " " + student2.getScore() + "\r\n");
}
bw.close();
}
}
package com.abc.dao;
/**
* 将teacher.txt的数据读取出来,添加到集合里面
* @author 紫色的贝壳
*
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import com.qfedu.entity.Teacher;
public class TeacherDao {
// 集合里面存放的是Teacher对象,所以泛型<Teacher>,现在把这个集合当作是一个空的容器,用来接收从teacher.txt里面读取出来的信息
private static ArrayList<Teacher> list = new ArrayList<Teacher>();
// 将teacher.txt数据读取出来,添加到集合中,封装一个方法来读取(封装一个方法可以提高代码的重用性)。由于teacher.txt是纯文本文件,所以可以使用字符流来读取数据,这样效率会更高
public void init1() throws Exception {
BufferedReader br = new BufferedReader(
new FileReader(new File("E:\\JAVA\\workspace\\studentManager\\src\\teacher.txt")));
String s;
// readLine()每次读取一行数据
while ((s = br.readLine()) != null) {
String[] strs = s.split(" ");// 用空格来切割字符串,目的是为了获取到用户名和密码[张三,123456],第一个是用户名,第2个是密码
Teacher teacher = new Teacher(strs[0], strs[1]);// 将切割好的数组通过下标取出来赋值给Teacher对象,循环一次放一次数据(此语句调用了双参构造方法,也起到了给属性赋值的作用)
list.add(teacher);//此时集合里面就有数据了,里面存放的是teacher对象,
}
br.close();//流使用过后要关闭流,有个规则是:先用后关
//System.out.println("list"+list);//打印list集合里面的信息
}
//封装一个方法,返回值是ArrayList集合,目的是为了和控制台输入的信息比较
public ArrayList<Teacher> getTeacher(){
return list;
}
}
Service层:通过dao下面类来进行一些业务逻辑的判断
package com.abc.service;
import java.util.ArrayList;
import com.abc.dao.StudentLoginDao;
import com.abc.entity.Student;
public class StudentLoginService {
//此时我们将StudentDao处理完的数据传给了StudentLoginService这个类
private StudentLoginDao studentLoginDao = new StudentLoginDao();
public ArrayList<Student> getStudentLogin(){
return studentLoginDao.getStudentLogin();
}
}
package com.abc.service;
import java.io.IOException;
import java.util.ArrayList;
import com.abc.dao.StudentDao;
import com.abc.entity.Student;
public class StudentService {
private StudentDao studentDao = new StudentDao();
//取出全部的学生信息,此时数据都在StudentDao里面,把它取到StudentService里面
public ArrayList<Student> getStudents(){
return studentDao.getStudents();
}
//添加学生信息
public void addStudent(Student student) throws IOException {
studentDao.addStudent(student);
}
//修改学生信息
public void updateStudent(String updateId,Student student) throws IOException {//这里的是形参,TeacherController里面传的是实参
studentDao.updateStudent(updateId,student);
}
//删除学生信息
public void deleteStudent(String deleteId) throws IOException {
studentDao.deleteStudent(deleteId);
}
//判断控制台输入的id(实参)学号是否在student.txt文件中存在
public boolean isIdExist(String id) {//这个(形参)id与控制台用户输入的id(实参)是一致的,也就是从前端获取的值传过来
//和dao里面的所有学生的学号进行匹配
//先获取所有学生信息
ArrayList<Student> list = studentDao.getStudents();//这里体现了代码的重用性
boolean existen = false;//声明一个标记,用来标记输入的id与数据库里面的id是否一样,没有相同id的话就为false
for (int i = 0; i < list.size(); i++) {//通过for循环遍历
if(id.equals(list.get(i).getId())) {//使控制台的id与student.txt文件的id比较
existen = true;
break;//如果一样的话,就终止当前for循环
}
}
return existen;
}
}
package com.abc.service;
import java.util.ArrayList;
import com.abc.dao.TeacherDao;
import com.abc.entity.Teacher;
public class TeacherService {
//此时我们将TeacherDao处理完的数据传给了TeacherService这个类
private TeacherDao teacherDao = new TeacherDao();
public ArrayList<Teacher> getTeacher(){
return teacherDao.getTeacher();
}
}
controller层: 控制器层, 将model层和view进行媒介的传输
package com.abc.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import com.abc.entity.Student;
import com.abc.service.StudentLoginService;
import com.abc.service.StudentService;
public class StudentController {
private StudentService studentService = new StudentService();
private StudentLoginService studentLoginService = new StudentLoginService();
private Scanner sc = new Scanner(System.in);
// 判断是否登录成功
public void studentLogin() throws IOException {
// 首先要判断控制台输入的数据与studentLogin.txt里面的输入比较,现在数据已经存到studentLoginService里面了
ArrayList<Student> list = studentLoginService.getStudentLogin();
System.out.println(list);
boolean flag = false;// 用来标记是否登录成功
while (true) {// 这是一个死循环,是为了输入错误还可以再次输入
System.out.print("请输入用户名:");
String name = sc.next();// 用户输入的用户名
System.out.print("请输入密码:");
String password = sc.next();// 用户输入的密码
for (Student student : list) {
if (name.equals(student.getName()) && password.equals(student.getPassword())) {
flag = true;// 可以登录
}
}
if (!flag) {
System.out.println("用户名或密码不正确,请重新输入!");
} else {
System.out.println("登录成功,欢迎:" + name);
start();// 登录成功后可以查询学生信息的操作
break;// 直到用户输入的用户名和密码正确的时候循环才结束
// 登录成功后,接下来就要进入学生的角色
}
}
}
//封装一个方法,对学生信息进行操作
public void start() throws IOException {
controllerLoop:
while(true) {//此处仍然写为死循环,目的是为了用户输入错误后还可以再次输入
System.out.println("请输入您的选择:(1.查看学生信息 2.修改个人信息 3.退出)");
String choice = sc.next();//接收用户的选择
switch (choice) {
case "1":
//查看学生信息
lookStudentInfor();
break;
case "2":
//修改个人信息
updateStudentInfor();
break;
case "3":
//退出while循环,不能退出程序
System.out.println("回到主菜单");
break controllerLoop;//回到标记的地方
default:
System.out.println("您输入的信息有误,请重新输入!");
}
}
}
//封装一个方法,用来查看学生信息.取信息的流程:student.txt---->dao----->service----->controller
public void lookStudentInfor() {
ArrayList<Student> list = studentService.getStudents();
//判断list集合里面的数据是否为空
if(list.size() == 0) {
System.out.println("没有学生信息...");
}else {//有学生信息
System.out.println("学号\t姓名\t性别\t年龄\tJava成绩");//字段名
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getId()+"\t"+list.get(i).getName()+"\t"+list.get(i).getGender()+"\t"+list.get(i).getAge()+"\t"+list.get(i).getScore());
}
}
}
//修改学生信息的方法
public void updateStudentInfor() throws IOException {
//修改学生信息之前要先判断是否有这条数据
//通过id学生学号判断,如果存在这条数据,才能进行修改,否则不能进行修改(修改的是student.txt的数据)
//控制台输入的id,我们要先判断
String updateId = inputStudentId();//要封装一个inputStudent方法判断控制台输入的id学号是否在student.txt文件里面存在
//接收控制台输入的学生信息,使用Student对象接一下
Student newStudent = inputStudentInfor2(updateId);
//将新的水果对象添加到list集合中,然后去修改student.txt
studentService.updateStudent(updateId,newStudent);//updateId是控制台输入的正确的id,newStudent是从控制台输入的学生信息赋值给的学生对象
}
public String inputStudentId() {
//判断集合(student.txt)里面是否有数据
ArrayList<Student> list = studentService.getStudents();//获取集合里面所有的数据
if(list.size() == 0) {
System.out.println("没有学生信息");
return null;//由于是String 类型的数据,并且没有数据,所以返回的是null
}
String id;//获取修改的id学号
while(true) {//如果输入不正确(也就是student.txt文件里面不存在这样的学号id,那么就会让用户一直输入)
System.out.print("请输入学生的学号:");
id= sc.next();//控制台输入的id学号
//在增添学生信息的时候已判断过id在student.txt文件里是否存在。如果存在就为true
//但是修改学生这里的判断与增加学生信息的判断侧重点是不一样的,这里要的是true,学生学号存在才能修改学生信息,而增加学生信息的是时候要的是false,只有学号不存在的时候才能添加
if(studentService.isIdExist(id)) {
break;//如果存在的话,就终止当前的while循环
}else {
System.out.println("您输入的学号不存在,请重新输入!");
}
}
return id;//获取前端控制台的id
}
//获取控制台的信息,并赋值给Student对象
public Student inputStudentInfor2(String id) {
System.out.print("请输入学生姓名:");
String name = sc.next();//从控制台获取的学生姓名
System.out.print("请输入性别:");
String gender = sc.next();//从控制台获取的学生性别
System.out.print("请输入年龄:");
int age = sc.nextInt();//从控制台获取的学生年龄
//从list集合中通过学号id获取对应学生的成绩
ArrayList<Student> list = studentService.getStudents();
double score =0;
for (int i = 0; i < list.size(); i++) {//通过for循环遍历
if(id.equals(list.get(i).getId())) {//使控制台的id与student.txt文件的id比较
score =list.get(i).getScore();
break;//如果一样的话,就终止当前for循环
}
}
Student newStudent = new Student(id,name,gender,age,score);//获取到控制台输入的学生信息,并赋值给Student对象
return newStudent;
}
}
package com.abc.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import com.abc.entity.Student;
import com.abc.entity.Teacher;
import com.abc.service.StudentLoginService;
import com.abc.service.StudentService;
import com.abc.service.TeacherService;
public class TeacherController {
//目前数据已经存到teacherService里面了,所以现在用teacherService进行数据的操作
private TeacherService teacherService = new TeacherService();
private StudentService studentService = new StudentService();
private Scanner sc = new Scanner(System.in);
//判断是否登录成功
public void login() throws IOException {
//首先要判断控制台输入的数据与teacher.txt里面的输入比较,现在数据已经存到teacherService里面了
ArrayList<Teacher> list = teacherService.getTeacher();
System.out.println(list);
boolean flag = false;//用来标记是否登录成功
while(true) {//这是一个死循环,是为了输入错误还可以再次输入
System.out.print("请输入用户名:");
String name = sc.next();//用户输入的用户名
System.out.print("请输入密码:");
String password = sc.next();//用户输入的密码
for (Teacher teacher : list) {
if(name.equals(teacher.getName()) && password.equals(teacher.getPassword())) {
flag = true;//可以登录
}
}
if (!flag) {
System.out.println("用户名或密码不正确,请重新输入!");
}else {
System.out.println("登录成功,欢迎:"+name);
startStudent();//登录成功后可以进行增删改查的操作
break;//直到用户输入的用户名和密码正确的时候循环才结束
//登录成功后,接下来就要进入管理员的角色
}
}
}
//封装一个方法,对学生信息进行操作
public void startStudent() throws IOException {
controllerLoop:
while(true) {//此处仍然写为死循环,目的是为了用户输入错误后还可以再次输入
System.out.println("请输入您的选择:(1.查看学生信息 2.添加学生信息 3.修改学生信息 4.删除学生信息 5.退出)");
String choice = sc.next();//接收用户的选择
switch (choice) {
case "1":
//查看学生信息
lookStudentInfor();
break;
case "2":
//添加学生信息
addStudentInfor();
break;
case "3":
//修改学生信息
updateStudentInfor();
break;
case "4":
deleteStudentInfor();
//删除学生信息
break;
case "5":
//退出while循环,不能退出程序
System.out.println("回到主菜单");
break controllerLoop;//回到标记的地方
default:
System.out.println("您输入的信息有误,请重新输入!");
}
}
}
//封装一个方法,用来查看学生信息.取信息的流程:student.txt---->dao----->service----->controller
public void lookStudentInfor() {
ArrayList<Student> list = studentService.getStudents();
//判断list集合里面的数据是否为空
if(list.size() == 0) {
System.out.println("没有学生信息...");
}else {//有学生信息
System.out.println("学号\t姓名\t性别\t年龄\tJava成绩");//字段名
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getId()+"\t"+list.get(i).getName()+"\t"+list.get(i).getGender()+"\t"+list.get(i).getAge()+"\t"+list.get(i).getScore());
}
}
}
//增加学生信息
public void addStudentInfor() throws IOException {
String id;
while(true) {//写true的原因:如果输入的id学号是一样的话,就会让用户一直输入,直到不一样为止,循环才退出
System.out.print("请输入学生学号:");
id = sc.next();//实参,是控制台输入的参数
//判断控制台输入的id与student.txt文件里学生的学号是否一样,如果一样就不能添加,如果不一样才能够添加(业务逻辑的判断放在service里面),再sevice层专门判断id是否一样
boolean flag = studentService.isIdExist(id);
if(flag) {//true,有一样的id学号
System.out.println("您输入的学号已存在,请重新输入!");
}else {
//id学号不一样
break;
}
}
//继续添加其他字段的信息,取到控制台的所有信息,把取到的值赋值给实体类Student来接收
Student student = inputStudentInfor(id);
//service--->dao---->txt
studentService.addStudent(student);
System.out.println("添加学生信息成功");
}
//获取控制台的信息,并赋值给Student对象
public Student inputStudentInfor(String id) {
System.out.print("请输入学生姓名:");
String name = sc.next();//从控制台获取的学生姓名
System.out.print("请输入性别:");
String gender = sc.next();//从控制台获取的学生性别
System.out.print("请输入年龄:");
int age = sc.nextInt();//从控制台获取的学生年龄
System.out.print("请输入Java成绩:");
double score = sc.nextDouble();//从控制台获取的学生Java成绩
Student newStudent = new Student(id,name,gender,age,score);//获取到控制台输入的学生信息,并赋值给Student对象
return newStudent;
}
//修改学生信息的方法
public void updateStudentInfor() throws IOException {
//修改学生信息之前要先判断是否有这条数据
//通过id学生学号判断,如果存在这条数据,才能进行修改,否则不能进行修改(修改的是student.txt的数据)
//控制台输入的id,我们要先判断
String updateId = inputStudentId();//要封装一个inputStudent方法判断控制台输入的id学号是否在student.txt文件里面存在
//接收控制台输入的学生信息,使用Student对象接一下
Student newStudent = inputStudentInfor(updateId);
//将新的水果对象添加到list集合中,然后去修改student.txt
studentService.updateStudent(updateId,newStudent);//updateId是控制台输入的正确的id,newStudent是从控制台输入的学生信息赋值给的学生对象
}
//删除学生信息的方法
public void deleteStudentInfor() throws IOException {
//获取到要删除学生信息的学号id,可以借助于修改学生信息时的id
String deleteId = inputStudentId();
studentService.deleteStudent(deleteId);
System.out.println("学生信息删除成功!");
}
public String inputStudentId() {
//判断集合(student.txt)里面是否有数据
ArrayList<Student> list = studentService.getStudents();//获取集合里面所有的数据
if(list.size() == 0) {
System.out.println("没有学生信息");
return null;//由于是String 类型的数据,并且没有数据,所以返回的是null
}
String id;//获取修改的id学号
while(true) {//如果输入不正确(也就是student.txt文件里面不存在这样的学号id,那么就会让用户一直输入)
System.out.print("请输入学生的学号:");
id= sc.next();//控制台输入的id学号
//在增添学生信息的时候已判断过id在student.txt文件里是否存在。如果存在就为true
//但是修改学生这里的判断与增加学生信息的判断侧重点是不一样的,这里要的是true,学生学号存在才能修改学生信息,而增加学生信息的是时候要的是false,只有学号不存在的时候才能添加
if(studentService.isIdExist(id)) {
break;//如果存在的话,就终止当前的while循环
}else {
System.out.println("您输入的学号不存在,请重新输入!");
}
}
return id;//获取前端控制台的id
}
}
domain:
package com.abc.domain;
import java.util.Scanner;
import com.abc.controller.StudentController;
import com.abc.controller.TeacherController;
import com.abc.dao.StudentDao;
import com.abc.dao.StudentLoginDao;
import com.abc.dao.TeacherDao;
public class Entry {
public static TeacherDao teacherDao = new TeacherDao();//teacherDao是静态的是因为他要在静态方法里面使用,静态方法里面是不能使用非静态变量的
public static StudentDao studentDao = new StudentDao();
public static StudentLoginDao studentLoginDao = new StudentLoginDao();
public static void main(String[] args) throws Exception {
teacherDao.init1();//从txt文件里面取数据(字符流)
studentDao.init2();//从student.txt文件中用字符流读取学生信息
studentLoginDao.init();
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("----------欢迎来到学生信息管理系统----------");
System.out.println("请输入您的选择:(1.学生角色 2.教师角色 3.退出)");
String choice = sc.next();
switch (choice) {
case "1":
//学生角色
StudentController studentController = new StudentController();
studentController.studentLogin();
break;
case "2":
//教师角色
TeacherController teacherController = new TeacherController();
teacherController.login();//用来判断是否登录成功
break;
case "3":
//退出
System.out.println("感谢您的使用");
System.exit(0);
break;
default:
System.out.println("您的输入有误,请重新输入!");
break;
}
}
}
}
功能实现效果图: