目录
这是一个简单的学生管理系统(利用集合充当了一下数据库)
实体层(entity)
Admint实体类
package entity;
public class Admin {
private String account;//账户
private String 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 Admin(String account, String password) {
super();
this.account = account;
this.password = password;
}
public Admin() {
super();
// TODO Auto-generated constructor stub
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Admin other = (Admin) obj;
if (account == null) {
if (other.account != null)
return false;
} else if (!account.equals(other.account))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
return true;
}
}
Student实体类
package entity;
public class Student {
private String name;
private int age;
private String studentID;//学号
private String classType;//班级
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (classType == null) {
if (other.classType != null)
return false;
} else if (!classType.equals(other.classType))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (studentID != other.studentID)
return false;
return true;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age, String studentID, String classType) {
super();
this.name = name;
this.age = age;
this.studentID = studentID;
this.classType = classType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String getClassType() {
return classType;
}
public void setClassType(String classType) {
this.classType = classType;
}
}
逻辑层(dao)
AdminDao类
package dao;
import java.util.ArrayList;
import data.datas;
import entity.Admin;
//主要对管理员数据进行一些查的操作
public class AdminDao {
private static ArrayList<Admin> ads=(ArrayList)datas.ads;
public boolean query(String account,String password) {
return query(new Admin(account,password));
}
public boolean query(Admin loginAdmin) {
if(ads.isEmpty()) {
return false;
}
for(Admin admin:ads) {
if(admin.equals(loginAdmin)) {
return true;
}
}
return false;
}
}
StudentDao类
package dao;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import data.datas;
import entity.Student;
//主要对学生数据进行一些查增删改操作
public class StudentDao {
private static ArrayList<Student> students = (ArrayList)datas.stus;
public static void queryAll() {
if(students.isEmpty()) {
System.out.println("学生表的信息是空的");
}
else {
//按照学号大小进行排序
students.sort(new Comparator<Student>() {
@Override
public int compare(Student o1,Student o2) {
return o1.getStudentID().compareTo(o2.getStudentID());
}
});
for(Student student:students) {
System.out.println(student);
}
}
}
//按照学号查找学生,true找到,false没找到
public static boolean queryBystudentID(String studentID) {
if(students.isEmpty()) {
return false;
}
for(Student student:students) {
if(student.getStudentID().equals(studentID)) {
return true;
}
}
return false;
}
//增加一个学生
public static boolean addStudent(Student student) {
return students.add(student);//返回true表示添加成功
}
//删除一个学生
public static boolean deleteByStudentID(String studentID) {
for(int i=0;i<students.size();i++) {
if(students.get(i).getStudentID().equals(studentID)) {
students.remove(i);
return true;//说明删掉了
}
}
return false;//说明没有找到
}
//根据学号修改一个学生的信息
public static boolean modifyByStudentID(String studentID,Student newStudent) {
int index=-1;
for(int i=0;i<students.size();i++) {
if(students.get(i).getStudentID().equals(studentID)) {
index=i;
}
}
if(index==-1) {
return false;
}
//删掉原来的
//将原来的数据进行修改
students.get(index).setStudentID(newStudent.getStudentID());
students.get(index).setName(newStudent.getName());
students.get(index).setAge(newStudent.getAge());
students.get(index).setClassType(newStudent.getClassType());
return true;
}
}
数据层(datas)
datas类
package data;
import java.util.ArrayList;
import java.util.Collection;
import entity.Admin;
import entity.Student;
//模拟数据库存储一些数据
public class datas {
//管理员数据容器
public static Collection<Admin> ads =new ArrayList<Admin>();
//学生数据容器
public static Collection<Student> stus =new ArrayList<Student>();
//初始化数据
static {
ads.add(new Admin("123","123"));
stus.add(new Student("张三",18,"201401","一班"));
stus.add(new Student("张四",19,"201402","二班"));
stus.add(new Student("张五",20,"201403","三班"));
stus.add(new Student("张六",21,"201404","四班"));
stus.add(new Student("张七",22,"201405","五班"));
}
}
测试层(main)
main类
package main;
import java.util.Scanner;
import dao.AdminDao;
import dao.StudentDao;
import entity.Student;
public class StudentMannager {
private static AdminDao ado=new AdminDao();
private static StudentDao sdo=new StudentDao();
private static Scanner scanner=new Scanner(System.in);
public static void main(String agrs[]) {
//1,输入管理员的密码和账号
System.out.println("输入管理员的账号");
String account=scanner.nextLine();
System.out.println("输入管理员的密码");
String password=scanner.nextLine();
if(ado.query(account,password)) {
System.out.println("管理员登录成功");
}
else {
System.out.println("管理员登录失败");
}
showStudentManagementMenu();
}
private static void showStudentManagementMenu() {
System.out.println("--------学生管理系统界面--------");
System.out.println("\t 1.查询学生信息");
System.out.println("\t 2.添加学生信息");
System.out.println("\t 3.修改学生信息");
System.out.println("\t 4.删除学生信息");
System.out.println("\t 5.退出");
System.out.println("--->请输入你的选择操作");
while(true) {
int choice = Integer.parseInt(scanner.nextLine());
if(choice==5) {
break;
}
if(choice==1) {
//查询学生的信息
sdo.queryAll();
}
//添加学生的信息
else if(choice==2) {
System.out.println("请输入学生的学号");
String StudentID=scanner.nextLine();
if(sdo.queryBystudentID(StudentID)) {
System.out.println("该学生已经存在了");
continue;
}
System.out.println("请输入学生的姓名");
String name = scanner.nextLine();
System.out.println("请输入学生的年龄");
int age = Integer.parseInt(scanner.nextLine());
System.out.println("请输入学生的学号");
String studentID = scanner.nextLine();
System.out.println("请输入学生的班级");
String classType = scanner.nextLine();
if(sdo.addStudent(new Student(name,age,studentID,classType))) {
System.out.println("---->添加学生的信息成功");
}
else {
System.out.println("---->添加学生的信息失败");
}
}
//删除学生
else if(choice==4) {
System.out.println("---->请输入要删除学生的学号");
String studentID = scanner.nextLine();
if(sdo.deleteByStudentID(studentID)) {
System.out.println("---->删除成功了");
}
else {
System.out.println("---->删除失败了");
}
}
//修改学生的信息
else if(choice==3) {
System.out.println("---->请输入学生的学号");
String studentID = scanner.nextLine();
if(!sdo.queryBystudentID(studentID)) {
System.out.println("学生学号不存在");
continue;
}
System.out.println("请输入修改后学生的姓名");
String name = scanner.nextLine();
System.out.println("请输入修改后学生的年龄");
int age = Integer.parseInt(scanner.nextLine());
System.out.println("请输入修改后学生的学号");
String sotudentID = scanner.nextLine();
System.out.println("请输入修改后学生的班级");
String classType = scanner.nextLine();
if(sdo.modifyByStudentID(studentID,new Student(name,age,sotudentID,classType))){
System.out.println("--->修改信息成功");
}
else {
System.out.println("--->修改信息失败");
}
}
}
System.out.println("---->系统退出");
}
}
显示的主界面是:
后期会进行完善