Java实现一个疫情人数管理系统
程序员文章站
2022-04-16 09:18:39
主要是对省份人数进行增加减少重查重增加,管理员注册登录。借鉴了一些资料写的比较简陋,功能不多,加一个主函数就能运行。package yiqing;public class Province {private String ProvinceName;@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((ProvinceName ==...
主要是对省份人数进行增加减少重查重增加,管理员注册登录。
借鉴了一些资料写的比较简陋,功能不多,加一个主函数就能运行。
package yiqing;
public class Province {
private String ProvinceName;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ProvinceName == null) ? 0 : ProvinceName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Province other = (Province) obj;
if (ProvinceName == null) {
if (other.ProvinceName != null)
return false;
} else if (!ProvinceName.equals(other.ProvinceName))
return false;
return true;
}
public String getProvinceName() {
return ProvinceName;
}
public void setProvinceName(String provinceName) {
ProvinceName = provinceName;
}
public Province(String provinceName) {
ProvinceName = provinceName;
}
public Province() {}
}
package yiqing;
public class MenuUtils {
public static void Welcome() {
System.out.println("====请登录疫情控制终端====");
System.out.println("1.登录");
System.out.println("2.注册");
System.out.println("3.退出");
}
public static void control() {
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.println("6.返回上一级");
}
}
package yiqing;
import java.awt.print.Book;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class ProvinceManager {
private Map<Province,Integer> provinces = new HashMap<>();
{
provinces.put(new Province("北京"), 0);
provinces.put(new Province("天津"), 0);
provinces.put(new Province("河北"), 0);
provinces.put(new Province("山西"), 0);
provinces.put(new Province("内蒙古"), 0);
provinces.put(new Province("宁夏"), 0);
provinces.put(new Province("青海"), 0);
provinces.put(new Province("陕西"), 0);
provinces.put(new Province("甘肃"), 0);
provinces.put(new Province("*"), 0);
provinces.put(new Province("辽宁"), 0);
provinces.put(new Province("吉林"), 0);
provinces.put(new Province("黑龙江"), 0);
provinces.put(new Province("山东"), 0);
provinces.put(new Province("江苏"), 0);
provinces.put(new Province("上海"), 0);
provinces.put(new Province("浙江"), 0);
provinces.put(new Province("安徽"), 0);
provinces.put(new Province("福建"), 0);
provinces.put(new Province("江西"), 0);
provinces.put(new Province("河南"), 0);
provinces.put(new Province("湖南"), 0);
provinces.put(new Province("湖北"), 0);
provinces.put(new Province("四川"), 0);
provinces.put(new Province("贵州"), 0);
provinces.put(new Province("云南"), 0);
provinces.put(new Province("重庆"), 0);
provinces.put(new Province("*"), 0);
provinces.put(new Province("广东"), 0);
provinces.put(new Province("广西"), 0);
provinces.put(new Province("海南"), 0);
provinces.put(new Province("香港"), 0);
provinces.put(new Province("澳门"), 0);
provinces.put(new Province("*"), 0);
}
private ProvinceManager() {}
private static ProvinceManager instance = new ProvinceManager();
public static ProvinceManager getInstance() {
if(instance == null) {
instance = new ProvinceManager();
}
return instance;
}
Scanner sc = new Scanner(System.in);
public void start(User u) {
while(true) {
MenuUtils.control();
System.out.println("请选择");
int choose = sc.nextInt();
switch(choose) {
case 1:
{
showProvinces();
break;
}
case 2:
{
HashMap<Province,Integer> map = ReduceProvince();
if(map!=null) {
u.saveProvince(map);
}else {
System.out.println("减少人数失败");
}
break;
}
case 3:
{
Map<Province,Integer> uMap = u.returnProvince();
if(uMap!=null) {
returnProvinceManager(uMap);
}else {
System.out.println("复发人数添加失败");
}
break;
}
case 4:
{
u.showProvince();
break;
}
case 5:
{
HashMap<Province,Integer> map = addProvinces();
System.out.println("请输入要增加人数的省份名字和增加的数量");
if(map!=null) {
u.saveProvince(map);
}else {
System.out.println("增加人数失败");
}
break;
}
case 6:
{
return;
}
}
}
}
private void showProvinces() {
Set<Entry<Province,Integer>> entrySet = provinces.entrySet();
System.out.println("省份\t数量");
for(Entry<Province,Integer> entry:entrySet) {
System.out.println(entry.getKey().getProvinceName()+"\t"+entry.getValue());
}
}
private HashMap<Province,Integer> ReduceProvince(){
while(true) {
System.out.println("请输入省份名");
String pname = sc.next();
Province province = new Province(pname);
boolean containsKey = provinces.containsKey(province);
if(!containsKey) {
System.out.println("没这个省");
continue;
}
System.out.println("请输入你要减少的数量");
int num = sc.nextInt();
Integer count = provinces.get(province);
if(num>count) {
System.out.println("没那么多人");
return null;
}
provinces.put(province, count-num);
HashMap<Province,Integer> map = new HashMap<>();
map.put(province, num);
return map;
}
}
private void returnProvinceManager(Map<Province,Integer> uMap) {
Set<Entry<Province,Integer>> entrySet = uMap.entrySet();
for(Entry<Province,Integer> entry:entrySet) {
Integer count = entry.getValue();
Province province = entry.getKey();
Integer num = provinces.get(province);
provinces.put(province, num==null?count:count+num);
}
System.out.println("复发病人添加成功");
}
private HashMap<Province,Integer> addProvinces(){
System.out.println("请输入省份名字");
String pname = sc.next();
Province province = new Province(pname);
boolean ContainsKey = provinces.containsKey(province);
if(!ContainsKey) {
System.out.println("没这个省");
}
System.out.println("输入你要添加的人数");
int num = sc.nextInt();
Integer count = provinces.get(province);
provinces.put(province, num+count);
return (HashMap<Province, Integer>) provinces;
}
}
package yiqing;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class User {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((UserName == null) ? 0 : UserName.hashCode());
result = prime * result + ((UserPassword == null) ? 0 : UserPassword.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (UserName == null) {
if (other.UserName != null)
return false;
} else if (!UserName.equals(other.UserName))
return false;
if (UserPassword == null) {
if (other.UserPassword != null)
return false;
} else if (!UserPassword.equals(other.UserPassword))
return false;
return true;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getUserPassword() {
return UserPassword;
}
public void setUserPassword(String userPassword) {
UserPassword = userPassword;
}
private String UserName;
private String UserPassword;
public User(String userName, String userPassword) {
super();
UserName = userName;
UserPassword = userPassword;
}
public User() {}
/*
* 存储疫情人数的表
*/
private HashMap<Province,Integer> hashmap = new HashMap<>();
public void saveProvince(HashMap<Province,Integer> map) {
Set<Entry<Province,Integer>> entrySet = map.entrySet();
for(Entry<Province,Integer> entry:entrySet) {
Province province = entry.getKey();
Integer count = entry.getValue();
Integer num = hashmap.get(province);
hashmap.put(province, num==null ? count:num+count);
}
System.out.println("成功!");
}
public Map<Province,Integer> returnProvince(){
while(true) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要增加人数的省份的名字");
String pname = sc.next();
Province province = new Province(pname);
boolean containsKey = hashmap.containsKey(province);
if(!containsKey) {
System.out.println("没有这个省份 请重新输入");
continue;
}
System.out.println("请输入复发人的数量");
int num = sc.nextInt();
Integer count = hashmap.get(province);
if(num>count) {
System.out.println("没有那么多人");
return null;
}
hashmap.put(province, count-num);
Map<Province,Integer> map = new HashMap<>();
map.put(province, num);
return map;
}
}
public void showProvince() {
Set<Entry<Province,Integer>> entryset = hashmap.entrySet();
System.out.println("省份\t数量");
for(Entry<Province,Integer> entry:entryset) {
System.out.println(entry.getKey().getProvinceName()+"\t"+entry.getValue());
}
}
}
package yiqing;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class UserManager {
private Set<User> users = new HashSet<>();
public UserManager() {
users.add(new User("admin","root"));
}
/*
* 开始菜单
*/
public void start() {
while(true) {
MenuUtils.Welcome();
System.out.println("请输入您的选择");
Scanner sc = new Scanner(System.in);
int choose = sc.nextInt();
switch(choose) {
case 1:
{
User luser = login();
User lUser = new User();
if(lUser != null) {
ProvinceManager.getInstance().start(lUser);
}else {
System.out.println("登陆失败");
}
break;
}
case 2:
{
boolean reg = register();
System.out.println(reg?"注册成功":"注册失败");
break;
}
case 3:
{
System.out.println("退出程序");
System.exit(0);
break;
}
default:
{
break;
}
}
}
}
Scanner sc = new Scanner(System.in);
private User login() {
System.out.println("请输入您的用户名,exsit退出");
String username = sc.next();
if(username.equals("exsit")) {
start();
}
boolean checkusername = checkUsername(username);
if(!checkusername) {
System.out.println("用户名不存在,请重新输入");
return login();
}
System.out.println("请输入您的密码");
String password = sc.next();
for(User user :users) {
if(user.getUserName().equals(username)&&user.getUserPassword().equals(password)) {
return user;
}else {
System.out.println("密码输入错误,请重新输入");
return login();
}
}
return null;
}
private boolean checkUsername(String username) {
for(User user:users) {
if(user.getUserName().equals(username)) {
return true;
}
}
return false;
}
private boolean register() {
while(true) {
System.out.println("请输入您想要注册的用户名");
String username = sc.next();
if(username.equals("exsit")) {
return false;
}
boolean isCheck = checkUsername(username);
if(isCheck) {
System.out.println("用户名已经存在,请重新输入");
continue;
}
System.out.println("请输入您想要注册的密码");
String password = sc.next();
return users.add(new User(username,password));
}
}
}
本文地址:https://blog.csdn.net/weixin_44976835/article/details/109587897
上一篇: SQL查询结果自定义排序