学生信息管理系统(二、Map集合精进版)

本文介绍了一个使用Map集合实现的学生信息管理系统,详细阐述了系统的结构、界面设计和实现步骤。通过三层架构(界面类、逻辑类、自定义异常)实现了登录、注销、添加、删除、查询学生信息等功能,并讨论了遇到的问题及解决方案。

学生信息管理系统

Map集合(二刷)

结构

界面类(控制类):专门用来与界面进行交互
逻辑类(服务类):写逻辑
【最好抛出自定义异常】

界面

在这里插入图片描述

步骤

  1. 新建项目student
  2. 新建包
    pojo:存放pojo类Student;
    service:存放接口IStudentService和实现接口的类StudentServiceImpl;
    main:存放界面类Application;
    exception:存放自定义异常StudentException。
  3. 新建类与接口
  • 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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值