学生信息管理系统
Map集合(二刷)
结构
界面类(控制类):专门用来与界面进行交互
逻辑类(服务类):写逻辑
【最好抛出自定义异常】
界面
步骤
- 新建项目student
- 新建包
pojo:存放pojo类Student;
service:存放接口IStudentService和实现接口的类StudentServiceImpl;
main:存放界面类Application;
exception:存放自定义异常StudentException。 - 新建类与接口
- Student:
- 定义私有属性id、name、gender、score;
- Eclipse自动生成有参无参构造方法,getter和setter方法。
package com.briup.pojo;
public class Student {
private String id;//学号
private String name;//姓名
private String gender;//性别
private double 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 double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public Student() {
}
public Student(String id, String name, String gender, double score) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.score = score;
}
}
- IStudentService:接口前面加“I”表示接口,定义各种功能的接口;
- Login(String username, String password):void型,传入username和password;
- isLogin():boolean型;
- Logout():void型;
- addStudent(Student student):void型,传入学生;
- removeStudentById(String id):void型,传入id;
- removeAllStudent():void型;
- updateStudentById(Student student):void型,传入学生;
- queryStudentById(String id):Student型,传入id;
- queryAllStudent():Collection < Student >。
package com.briup.service;
import java.util.Collection;
import com.briup.exception.StudentException;
import com.briup.pojo.Student;
/**
* 接口
* @author Elvira
*
*/
public interface IStudentService {
/**
* 根据用户名密码去登录
* @param username 用户名
* @param password 密码
* @throws StudentException
*/
void login(String username, String password) throws StudentException;
/**
* 判断用户是否登录
* @return 登录成功 true 未登录 false
* @throws StudentException
*/
boolean isLogin() throws StudentException;
/**
* 注销账户
* @throws StudentException
*/
void logout() throws StudentException;
/**
* 添加一条学生信息
* @throws StudentException
*/
void addStudent(Student student) throws StudentException;
/**
* 删除一条学生信息
* @throws StudentException
*/
void removeStudentById(String id) throws StudentException;
/**
* 删除所有学生信息
* @throws StudentException
*/
void removeAllStudent() throws StudentException;
/**
* 修改一条学生信息
* @throws StudentException
*/
void updateStudentById(Student student) throws StudentException;
/**
* 查询一条学生信息
* @return 根据id查询到的学生信息
* @throws StudentException
*/
Student queryStudentById(String id) throws StudentException;
/**
* 查询所有学生信息
* @return 所有学生信息
* @throws StudentException
*/
Collection<Student> queryAllStudent() throws StudentException;
}
- StudentServiceImpl:接口的实现类,实现各种接口的功能,负责逻辑。
- login:首先用户名密码不为空,若为空抛出一个自定义异常;若用户名密码为admin,则往map集合中传入一个为“logined”的键,值为null;
- isLogin:判断是否登录,即判断map几何中是否存在“logined”的键;
- logout:注销账户。即将map集合中为“logined”的键删除;
- addStudent:添加学生。首先学生不为空,若输入的值为空,则抛出一个自定义异常;若不为空,则往map集合中添加一个键为id,值为学生信息的键值对;
- removeStudentById:根据id删除单个学生。首先判断id是否为空,若为空,抛出一个自定义异常,若id正确,则根据id删除键值对;
- removeAllStudent:清空信息。将map集合中的键值对直接删除。
- updateStudentById:根据已存在学生信息修改单个学生信息。首先学生不为空,然后判断map集合中是否包含所输入id的键,若包含此键,则将map集合中的此键值对更新,若不包含则抛出异常;
- queryStudentById:根据学生id查询单个学生信息。首先输入的id不为空,若为空,则抛出异常,不为空则获取输入的id;
- queryAllStudent:使用Collection存放信息,获取map中的值。
package com.briup.service.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.briup.exception.StudentException;
import com.briup.pojo.Student;
impor