用户 user1 user2
密码 user1 user2
管理员 admin1 admin2
密码 admin1 admin2
bean包下的Student类
package com.SMS.bean;
import java.util.Objects;
/**
*Student为实体对象,用来封装客户信息
*
*/
public class Student {
private String name;//5位
private int number;//0-
double temperature;//35-42
private String id;//18位
private String phone;//11位
private String address;//20
public Student() {
}
public Student(String name, int number, double temperature, String id, String phone, String address) {
this.name = name;
this.number = number;
this.temperature = temperature;
this.id = id;
this.phone = phone;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public double getTemperature() {
return temperature;
}
public void setTemperature(double temperature) {
this.temperature = temperature;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return
"姓名:" + name +
", 学号:" + number +
", 体温:" + temperature +
", 身份证号:" + id +
", 电话号:" + phone +
", 住址:" + address ;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return number == student.number &&
Double.compare(student.temperature, temperature) == 0 &&
Objects.equals(name, student.name) &&
Objects.equals(id, student.id) &&
Objects.equals(phone, student.phone) &&
Objects.equals(address, student.address);
}
@Override
public int hashCode() {
return Objects.hash(name, number, temperature, id, phone, address);
}
}
service包下的StudentList类
package com.SMS.service;
import com.SMS.bean.Student;
import java.util.ArrayList;
import java.util.Iterator;
public class StudentList {
private ArrayList<Student> list=new ArrayList<Student>();
/**
* 用来初始化集合list的构造器
* totalStudent 指定集合的长度
*/
public StudentList() {
list=new ArrayList<Student>();
}
public StudentList(int totalStudent) {
list=new ArrayList<Student>(totalStudent);
}
/**
* 将指定的学生添加到数组中
*/
public void addStudent(Student student) {
list.add(student);
}
/**
* 修改指定索引位置上的客户信息
*/
public boolean replaceStudent(int index,Student stud) {
if(index<0||index>=list.size()) {
return false;
}
list.set(index,stud);
return true;
}
/**
*插入指定索引位置上的客户
*/
public boolean Insert(int index,Student s) {
if(index<0||index>=list.size()) {
return false;
}
System.out.println("无此操作");
return true;
}
/**
*删除指定索引位置上的客户
*/
public boolean deleteStudent(int index) {
if(index<0||index>=list.size()) {
return false;
}
list.remove(index);
return true;
}
public void searchName(String name){
boolean isFound=false;
Iterator<Student> iterator=list.iterator();
while (iterator.hasNext()) {
Student s = iterator.next();
if(name.equals(s.getName())){
isFound=true;
System.out.println(s);
}
}
if (isFound==false){
System.out.println("没有找到姓名是"+name+"的人");
}
}
public void searchNum(int num){
boolean isFound=false;
Iterator<Student> iterator=list.iterator();
while (iterator.hasNext()) {
Student s = iterator.next();
if(num==s.getNumber()){
isFound=true;
System.out.println(s);
}
}
if (isFound==false){
System.out.println("没有找到学号是"+num+"的人");
}
}
public void searchNameNum(String name, int num){
boolean isFound=false;
Iterator<Student> iterator=list.iterator();
while (iterator.hasNext()) {
Student s = iterator.next();
if(num==s.getNumber()&&name.equals(s.getName())){
isFound=true;
System.out.println(s);
}
}
if (isFound==false){
System.out.println("没有找到姓名是"+name+"并且学号是"+num+"的人");
}
}
public void easyCount(double t) {
boolean isFound=false;
Iterator<Student> iterator=list.iterator();
while (iterator.hasNext()) {
Student s = iterator.next();
if(t==s.getTemperature()){
isFound=true;
System.out.println(s);
}
}
if (isFound==false){
System.out.println("没有找到温度是"+t+"的人");
}
}
public void Count(double t1, double t2) {
boolean isFound=false;
Iterator<Student> iterator=list.iterator();
while (iterator.hasNext()) {
Student s = iterator.next();
if(t1<=s.getTemperature()&&s.getTemperature()<=t2){
isFound=true;
System.out.println(s);
}
}
if (isFound==false){
System.out.println("没有找到温度从"+t1+"到"+t2+"的人");
}
}
/**
*获取所有的客户信息
*/
public ArrayList getAllStudents() {
return list;
}
/**
* 获取指定索引位置的客户信息
*/
public Student getStudent(int index) {
if(index<0||index>=list.size()) {
return null;
}
return (Student) list.get(index);
}
/**
*获取存储的客户数量
*/
public int getTotal() {
return list.size();
}
}
UI包下的StudentView类
package com.SMS.ui;
import com.SMS.bean.Student;
import com.SMS.service.StudentList;
import com.SMS.util.SMUtility;
import java.util.*;
public class StudentView {
/**
* StudentView为主模块,负责菜单的显示和处理用户的操作
*
*/
private StudentList studentList=new StudentList();
public StudentView() {
Student student =new Student("张三",1,36.5,"123456789987654321","12345678998","京西");
studentList.addStudent(student);
}
/**
*显示《学生信息管理软件》界面的方法
*/
public void userMainMenu() {
boolean isFlag=true;
while(isFlag) {
System.out.println("\n-----------学生信息管理软件-------------\n");
System.out.println(" 1 添 加 ");
System.out.println(" 2 修 改 ");
System.out.println(" 3 插 入 ");
System.out.println(" 4 删 除 ");
System.out.println(" 5 查 询 ");
System.out.println(" 6 功 能 ");
System.out.println(" 7 列 表");
System.out.println(" 8 文 件");
System.out.println(" 9 退 出\n");
System.out.print(" 请选择(1-9):");
char menu = SMUtility.readMenuSelection(1,9);
switch (menu) {
case '1':
addNewStudent();
break;
case '2':
modifyStudent();
break;
case '3':
insertStudent();
break;
case '4':
case '5':
case '6':
System.out.println("用户无此操作权限");
break;
case '7':
listAllStudents();
break;
case '8':
System.out.println("用户您无此操作权限");
break;
case '9':
// System.out.println("退出");
System.out.print("确认是否退出(Y/N):");
char isExit= SMUtility.readConfirmSelection();
if(isExit=='Y') {
isFlag=false;
}
}
}
}
public void adminMainMenu() {
boolean isFlag=true;
while(isFlag) {
System.out.println("\n-----------学生信息管理软件-------------\n");
System.out.println(" 1 添 加 ");
System.out.println(" 2 修 改 ");
System.out.println(" 3 插 入 ");
System.out.println(" 4 删 除 ");
System.out.println(" 5 查 询 ");
System.out.println(" 6 功 能 ");
System.out.println(" 7 列 表");
System.out.println(" 8 文 件");
System.out.println(" 9 退 出\n");
System.out.print(" 请选择(1-9):");
char menu = SMUtility.readMenuSelection(1,9);
switch (menu) {
case '1':
addNewStudent();
break;
case '2':
modifyStudent();
break;
case '3':
insertStudent();
break;
case '4':
deleteStudent();
break;
case '5':
searchStudents();
break;
case '6':
fuctionStudents();
break;
case '7':
listAllStudents();
break;
case '8':
fileStudents();
break;
case '9':
// System.out.println("退出");
System.out.print("确认是否退出(Y/N):");
char isExit= SMUtility.readConfirmSelection();
if(isExit=='Y') {
isFlag=false;
}
}
}
}
/**
*添加学生的操作
*/
private void addNewStudent() {
// System.out.println("添加学生的操作");
System.out.print("姓名:");
String name = SMUtility.readOverString(5);
System.out.print("学号:");
int number = SMUtility.readInt();
System.out.print("温度(35-42):");
double temperature = SMUtility.readDouble(35,42);
System.out.print("身份证号(18位):");
String id = SMUtility.readString(18);
System.out.print("电话号(11位):");
String phone = SMUtility.readString(11);
System.out.print("地址:");
String address = SMUtility.readOverString(20);
//将上述数据封装到对象中
Student student =new Student(name,number,temperature,id,phone,address);
studentList.addStudent(student);
System.out.println("---------------添加完成---------------");
}
/**
*修改学生的操作
*/
private void modifyStudent() {
// System.out.println("修改学生的操作");
System.out.println("-----------修改学生-------------");
int n;
Student stud;
for(;;) {
System.out.print("请选择修改学生编号(0退出):");
n=SMUtility.readInt();
if(n==0) {
return ;
}
stud = studentList.getStudent(n-1);
if(stud==null){
System.out.println("无法找到指定学生");
}else {
//找到了指定学生
break;
}
}
//修改学生信息
System.out.print("姓名("+stud.getName()+"):");
String name=SMUtility.readOverString(5, stud.getName());
System.out.print("学号("+stud.getNumber()+"):");
int number=SMUtility.readInt(stud.getNumber());
System.out.print("温度("+stud.getTemperature()+"):");
double temperature=SMUtility.readDouble(35,42,stud.getTemperature());
System.out.print("身份证号("+stud.getId()+"):");
String id=SMUtility.readString(18,stud.getId());
System.out.print("电话号("+stud.getPhone()+"):");
String phone=SMUtility.readString(11,stud.getPhone());
System.out.print("地址("+stud.getAddress()+"):");
String address=SMUtility.readOverString(20,stud.getAddress());
Student newstud =new Student(name,number,temperature,id,phone,address);
boolean isRepalaced=studentList.replaceStudent(n-1,newstud);
if(isRepalaced) {
System.out.println("---------------修改完成---------------");
}else {
System.out.println("---------------修改失败---------------");
}
}
/**
*插入学生的操作
*/
private void insertStudent(){
System.out.println("无此操作");
}
/**
*删除学生的操作
*/
private void deleteStudent() {
// System.out.println("删除学生的操作");
System.out.println("-----------删除学生-------------");
int number;
for(;;) {
System.out.print("请选择待删除学生编号(0退出):");
number=SMUtility.readInt();
if(number==0) {
return;
}
Student student=studentList.getStudent(number-1);
if(student==null) {
System.out.println("无法找到指定学生");
}else {
break;
}
}
//找到了指定的学生
System.out.println("确定是否删除(Y/N):");
char isDelete=SMUtility.readConfirmSelection();
if(isDelete=='Y') {
boolean deleteSuccess=studentList.deleteStudent(number-1);
if(deleteSuccess) {
System.out.println("-----------删除完成-------------");
}else {
//没机会
System.out.println("-----------删除失败-------------");
}
}else{
//可省
return;
}
}
private void viewSearchName(){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入姓名:");
String name=scanner.nextLine();
studentList.searchName(name);
}
private void viewSearchNum(){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入学号:");
int num=scanner.nextInt();
studentList.searchNum(num);
}
private void viewSearchNameNum(){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入姓名:");
String name=scanner.nextLine();
System.out.println("请输入学号:");
int num=scanner.nextInt();
studentList.searchNameNum(name,num);
}
/**
*查询学生的操作
*/
private void searchStudents(){
// System.out.println("暂无此功能");
while (true) {
System.out.println("--------查询-------");
System.out.println(" 1查询 姓名");
System.out.println(" 2查询 学号");
System.out.println(" 3查询 姓名&学号");
System.out.println(" 4退出");
System.out.print("请选择(1-4):");
int selection;
selection = SMUtility.readMenuSelection(1, 4);
switch (selection) {
case '1':
viewSearchName();
break;
case '2':
viewSearchNum();
break;
case '3':
viewSearchNameNum();
break;
case '4':
System.out.print("请确认是否退出(Y/N):");
char choice;
choice = SMUtility.readConfirmSelection();
if (choice == 'Y') {
return;
}
}
}
}
private void viewEasyCount(){
System.out.println("---简单排序---");
System.out.println("输入温度(35-42):");
Scanner scanner=new Scanner(System.in);
double t=SMUtility.readDouble(35,42);
studentList.easyCount(t);
}
private void viewCount(){
System.out.println("---综合排序---");
System.out.println("输入小温度(35-42):");
double t1=SMUtility.readDouble(35,42);
System.out.println("输入大温度("+t1+"-42):");
double t2=SMUtility.readDouble(t1,42);
studentList.Count(t1,t2);
}
private void ascendingSort(ArrayList<Student>list){
Comparator<Student> com=new Comparator<>() {
@Override
public int compare(Student s1, Student s2) {
return Integer.compare(s1.getNumber(),s2.getNumber());
}
};
list.sort(com);
for (Student s:list
) {
System.out.println(s);
}
}
private void descendingSort(ArrayList<Student>list){
Comparator<Student> com=new Comparator<>() {
@Override
public int compare(Student s1, Student s2) {
return -Double.compare(s1.getTemperature(),s2.getTemperature());
}
};
list.sort(com);
for (Student s:list
) {
System.out.println(s);
}
}
/**
* 功能
*/
private void fuctionStudents(){
while (true) {
System.out.println("--------功能-------");
System.out.println(" 1简单统计");
System.out.println(" 2综合统计");
System.out.println(" 3学号升序");
System.out.println(" 4温度降序");
System.out.println(" 5退 出");
System.out.print("请选择(1-5):");
int selection;
selection = SMUtility.readMenuSelection(1, 5);
switch (selection) {
case '1':
viewEasyCount();
break;
case '2':
viewCount();
break;
case '3':
ascendingSort(studentList.getAllStudents());
break;
case '4':
descendingSort(studentList.getAllStudents());
break;
case '5':
System.out.print("请确认是否退出(Y/N):");
char choice;
choice = SMUtility.readConfirmSelection();
if (choice == 'Y') {
return;
}
}
}
}
/**
*显示学生列表的操作
*/
private void listAllStudents() {
// System.out.println("显示学生列表的操作");
System.out.println("-----------学生列表-------------");
int total=studentList.getTotal();
if(total==0) {
System.out.println("没有学生记录");
}else {
System.out.println("编号\t姓名\t学号\t体温\t\t身份证号\t\t\t\t电话号\t\t地址");
ArrayList studs=studentList.getAllStudents();
for(int i=0;i<studs.size();i++) {
Student stud= (Student) studs.get(i);
System.out.println((i+1)+"\t"+stud.getName()
+"\t"+ stud.getNumber()+"\t"+ stud.getTemperature()+"\t"+stud.getId()+"\t"+stud.getPhone()+"\t"+stud.getAddress());
}
}
System.out.println("---------学生列表完成-------------");
}
/**
* 文件
*/
private void fileStudents(){
System.out.println("暂无此功能");
}
public void user(){
Scanner scanner=new Scanner(System.in);
HashMap<String,String> user=new HashMap<>();
user.put("user1","user1");
user.put("user2","user2");
String name;
String password=null;
while (true){
System.out.println("---------------用户系统---------------");
System.out.println(" 1 登录");
System.out.println(" 2 注册");
System.out.println(" 3 退出");
System.out.print("请选择(1-3):");
int choice;
choice=SMUtility.readMenuSelection(1,3);
switch (choice){
case '1':
System.out.println("登录");
while(true) {
System.out.println("请输入账户名");
name = scanner.nextLine();
int isThree=1 ;
boolean sgining=false;
if (user.containsKey(name)) {
System.out.println("请输入密码"+'('+isThree+')');
password = scanner.nextLine();
for (; isThree < 4; isThree++) {
if (user.get(name).equals(password)) {
System.out.println("欢迎用户" + name);
sgining=true;
break;
} else {
if (isThree == 3) {
System.out.println("密码错误超过3次\n");
break;
}
System.out.println("请重新输入密码!"+'('+(isThree+1)+')');
System.out.print("密码:");
password=scanner.nextLine();
if (user.get(name).equals(password)) {
System.out.println("欢迎用户" + name);
sgining=true;
break;
}
}
}
} else {
System.out.println("用户名错误");
break;
}
if(sgining==true){
StudentView view=new StudentView();
view.userMainMenu();
break;
}
if(isThree==3){
break;
}
}
break;
case '2':
System.out.println("注册");
System.out.println("请输入账户名");
name = scanner.nextLine();
System.out.println("请输入密码");
password = scanner.nextLine();
String word;
System.out.println("请再次输入密码");
word = scanner.nextLine();
if(password.equals(word)){
user.put(name,password);
System.out.println("注册成功!");
}else{
System.out.println("确认密码失败,请重新注册");
}
break;
case '3':
System.out.println("请确认是否退出(Y或N):");
char isExit;
isExit=SMUtility.readConfirmSelection();
if(isExit=='Y'){
return;
}
}
}
}
public void admin(){
Scanner scanner=new Scanner(System.in);
HashMap<String,String> admin=new HashMap<>();
admin.put("admin1","admin1");
admin.put("admin2","admin2");
String name;
String password;
while (true){
System.out.println("---------------管理员系统---------------");
System.out.println(" 1 登录");
System.out.println(" 2 注册");
System.out.println(" 3 退出");
System.out.print("请选择(1-3):");
int choice;
choice=SMUtility.readMenuSelection(1,3);
switch (choice){
case '1':
System.out.println("登录");
while(true) {
System.out.println("请输入账户名");
name = scanner.nextLine();
int isThree=1 ;
boolean sgining=false;
if (admin.containsKey(name)) {
System.out.println("请输入密码"+'('+(isThree)+')');
password = scanner.nextLine();
for (; isThree < 4; isThree++) {
if (admin.get(name).equals(password)) {
System.out.println("欢迎用户" + name);
sgining=true;
break;
} else {
if (isThree == 3) {
System.out.println("密码错误超过3次\n");
break;
}
System.out.println("请重新输入密码!"+'('+(isThree+1)+')');
System.out.print("密码:");
password=scanner.nextLine();
if (admin.get(name).equals(password)) {
System.out.println("欢迎管理员" + name);
sgining=true;
break;
}
}
}
} else {
System.out.println("用户名错误");
break;
}
if(sgining==true){
StudentView view=new StudentView();
view.adminMainMenu();
break;
}
if(isThree==3){
break;
}
}
break;
case '2':
System.out.println("注册");
System.out.println("请输入账户名");
name = scanner.nextLine();
System.out.println("请输入密码");
password = scanner.nextLine();
String word;
System.out.println("请再次输入密码");
word = scanner.nextLine();
if(password.equals(word)){
admin.put(name,password);
System.out.println("注册成功!");
}else{
System.out.println("确认密码失败,请重新注册");
}
break;
case '3':
System.out.println("请确认是否退出(Y或N):");
char isExit;
isExit=SMUtility.readConfirmSelection();
if(isExit=='Y'){
return;
}
}
}
}
public void help(){
System.out.println("帮助");
System.out.println("用户手册:");
System.out.println("用户只能添加、修改、插入、打印报表");
System.out.println("管理员手册:");
System.out.println("管理员可以添加、修改、插入、删除、查询、功能(统计、排序)、打印报表、文件操作");
}
public void systemMain() {
while (true){
System.out.println("---------------新冠疫情管理系统---------------");
System.out.println(" 1 用户登录");
System.out.println(" 2 管理员登录");
System.out.println(" 3 帮 助");
System.out.println(" 4 退 出");
System.out.print("请选择(1-4):");
int selection;
selection = SMUtility.readMenuSelection(1, 4);
switch (selection) {
case '1':
user();
break;
case '2':
admin();
break;
case '3':
help();
break;
case '4':
System.out.println("请确认是否退出(Y/N):");
char isExit;
isExit = SMUtility.readConfirmSelection();
if (isExit == 'Y') {
return;
}
}
}
}
public static void main(String[] args) {
StudentView view =new StudentView();
view.systemMain();
}
}
util包下的SMUtility工具类
package com.SMS.util;
import java.util.Scanner;
/**
* SMUtility 工具类
* 将不同的功能封装为方法,就是可以直接调用方法使用它的功能,而无需考虑具体功能实现细节
*/
public class SMUtility {
private static Scanner scanner=new Scanner(System.in) ;
/*
* 用于界面菜单的选择.该方法读取键盘,如果用户键入‘n1’-‘n2’中的任意字符,则方法返回。返回值为用户的输入值
*/
public static char readMenuSelection(int n1,int n2) {
char c;
for(; ;) {
String str =readKeyBoard(1,false);
c=str.charAt(0);
if(c<n1&&c>n2) {
System.out.println("选择错误,请重新输入:");
}else break;
}
return c;
}
/*
* 用于确认的输入。该方法从键盘读取‘Y'或'N',并将其作为方法的返回值
*/
public static char readConfirmSelection() {
char c;
for(; ;) {
String str =readKeyBoard(1,false).toUpperCase();
c=str.charAt(0);
if(c=='Y'||c=='N') {
break;
}else {
System.out.println("选择错误,请重新输入:");
}
}
return c;
}
/**
* 读正整数
*
*/
public static int readInt(){
int n;
while(true){
n=scanner.nextInt();
if (n>=0){
break;
}else {
System.out.println("输入错误,请重新输入");
}
}
return n;
}
public static int readInt(int defaultValue){
int n;
while(true){
String str=scanner.nextLine();
if(str.length()==0){
n=defaultValue;
break;
}else{
n=Integer.parseInt(str);
}
if (n>=0){
break;
}
}
return n;
}
/*
* 从键盘读取一个n1-n2的实数,并将其作为方法的返回值
* 用于输入
*/
public static double readDouble(double n1,double n2) {
double n;
for(;;) {
double v = scanner.nextDouble();
if(v>=n1&&v<=n2){
n=v;
break;
}else {
System.out.println("输入错误,请重新输入");
}
}
return n;
}
/*
* 从键盘读取一个n1-n2的实数,并将其作为方法的返回值
* 如果用户不输入字符而直接回车,方法将以defaultValue作为返回值。
* 用于修改
*/
public static double readDouble(double n1,double n2,double defaultValue) {
double n;
for(;;) {
String str = scanner.nextLine();
if(str.length()==0){
n=defaultValue;
break;
}
double v=Double.parseDouble(str);
if(v>=n1&&v<=n2){
n=v;
break;
}else {
System.out.println("输入错误,请重新输入");
}
}
return n;
}
/*
* 从键盘读取一个长度是limit的字符串,并将其作为方法的返回值
* 用于输入
*/
public static String readString(int limit) {
return readKeyBoard(limit,false);
}
/*
* 从键盘读取一个长度是limit的字符串,并将其作为方法的返回值
* 如果用户不输入字符而直接回车,方法将以defaultValue作为返回值。
* 用于修改
*/
public static String readString(int limit,String defaultValue) {
String str=readKeyBoard(limit,true);
return str.equals("")?defaultValue:str;
}
/*
* 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值
* 用于输入
*/
public static String readOverString(int limit) {
return readOverKeyBoard(limit,false);
}
/*
* 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值
* 如果用户不输入字符而直接回车,方法将以defaultValue作为返回值。
* 用于修改
*/
public static String readOverString(int limit,String defaultValue) {
String str=readOverKeyBoard(limit,true);
return str.equals("")?defaultValue:str;
}
private static String readKeyBoard(int limit,boolean blankReturn) {
String line="";
while(scanner.hasNextLine()) {
line=scanner.nextLine();
if(line.length()==0) {
if(blankReturn)return line;
else continue;
}
if(line.length()!=limit) {
System.out.println("输入长度(不等于"+limit+")错误,请重新输入:");
continue;
}
break;
}
return line;
}
private static String readOverKeyBoard(int limit,boolean blankReturn) {
String line="";
while(scanner.hasNextLine()) {
line=scanner.nextLine();
if(line.length()==0) {
if(blankReturn)return line;
else continue;
}
if(line.length()>=limit) {
System.out.println("输入长度(不大于"+limit+")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}