任务需求:
用HashMap,参考下图模式,完成用户的登陆,个人信息修改,遍历登陆用户的操作。(用户的数据信息用UserData类提前创建好)
所需知识点:
1.Map接口的实现类HashMap;
2.HashMap的常用API;
3.遍历HashMap的所有信息
运行截图:
代码如下:
ID_Password.java:
package cn.demo.person;
public class ID_Password {
private String id ;
private String password ;
public ID_Password() {
super();
// TODO Auto-generated constructor stub
}
public ID_Password(String id, String password) {
super();
this.id = id;
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "ID_Password [id=" + id + ", password=" + password + "]";
}
}
Person.java:
package cn.demo.person;
public class Person {
private String tele ;
private String home ;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String tele, String home) {
super();
this.tele = tele;
this.home = home;
}
public String getTele() {
return tele;
}
public void setTele(String tele) {
this.tele = tele;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
@Override
public String toString() {
return "Person [tele=" + tele + ", home=" + home + "]";
}
}
Main.java:
package cn.demo.main;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import javax.swing.text.StyledEditorKit.ForegroundAction;
import cn.demo.person.ID_Password;
import cn.demo.person.Person;
public class Main {
public static void main(String[] args) {
Main_Demo();
}
private static void Main_Demo() {
Scanner scr = new Scanner(System.in);
System.out.println("-----录入学生信息-----");
System.out.println("录入学生信息数量:");
int n = scr.nextInt() ;
ID_Password[] arr1 = new ID_Password[n] ;
Person[] arr2 = new Person[n] ;
for(int i=0;i<n;i++){
System.out.println("账号:");
String newID = scr.next() ;
System.out.println("密码:");
String newPassword =scr.next() ;
arr1[i] = new ID_Password(newID,newPassword);
System.out.println("电话:");
String newTele = scr.next() ;
System.out.println("地址:");
String newDizhi = scr.next() ;
arr2[i] = new Person(newTele,newDizhi);
}
HashMap<ID_Password,Person> hm = new HashMap<>() ;
for(int i=0;i<n;i++){
hm.put(arr1[i],arr2[i]);
}
int flagshow1 = 1 ;
while(flagshow1==1){
Show1();
int number = scr.nextInt() ;
if(number==1){
System.out.println("-----登录界面-----");
System.out.println("输入账号:");
String newID = scr.next() ;
System.out.println("输入密码:");
String newPASSWORD = scr.next() ;
int flag = 0 ;
ID_Password index = null ;
for (ID_Password p : hm.keySet()) {
if(p.getId().equals(newID)&&p.getPassword().equals(newPASSWORD)){
flag = 1 ;
System.out.println("登录成功!");
index = p ;
break;
}
}
if(flag==1){
Demo(scr, hm, index);
}else{
System.out.println("登录失败!");
}
}else if(number==2){
Demo4(hm);
}else{
flagshow1 = Demo5();
}
}
}
private static int Demo5() {
int flagshow1;
flagshow1 = 0 ;
System.out.println("退出系统!");
return flagshow1;
}
private static void Demo4(HashMap<ID_Password, Person> hm) {
System.out.println("-----账户信息如下-----");
System.out.println("账号\t密码\t电话\t地址");
for (ID_Password p1 : hm.keySet()) {
System.out.println(p1.getId()+"\t"+p1.getPassword()+"\t"+hm.get(p1).getTele()+"\t"+hm.get(p1).getHome());
}
}
private static void Demo(Scanner scr, HashMap<ID_Password, Person> hm,
ID_Password index) {
int flagshow22=1;
while(flagshow22==1){
Show2();
int flagshow2 = 1 ;
while(flagshow2 == 1){
System.out.println("请选择业务:");
int number2 = scr.nextInt() ;
if(number2==1){
Demo1(scr, index);
}else if(number2==2){
Demo2(scr, hm, index);
}else if(number2==3){
Demo3(scr, hm, index);
}else if(number2==4){
System.out.println("返回主页!");
flagshow2 = 0 ;
}else{
System.out.println("注销账号!");
flagshow2 = 0 ;
flagshow22 = 0 ;
}
}
}
}
private static void Demo3(Scanner scr, HashMap<ID_Password, Person> hm,
ID_Password index) {
System.out.println("-----修改地址-----");
Person newDizhi = hm.get(index);
System.out.println("输入新的地址:");
String DizhiNew = scr.next() ;
newDizhi.setHome(DizhiNew);
System.out.println("修改地址成功!");
}
private static void Demo2(Scanner scr, HashMap<ID_Password, Person> hm,
ID_Password index) {
System.out.println("-----修改电话-----");
Person newPerson = hm.get(index);
System.out.println("输入新的电话号码:");
String TeleNumber = scr.next() ;
newPerson.setTele(TeleNumber);
System.out.println("修改电话成功!");
}
private static void Demo1(Scanner scr, ID_Password index) {
System.out.println("-----修改密码-----");
System.out.println("输入新密码:");
String newPassword_1 = scr.next() ;
System.out.println("确认新密码:");
String newPassword_2 = scr.next() ;
if(!newPassword_1.equals(newPassword_2)){
System.out.println("两次输入的密码不相同!");
}else{
index.setPassword(newPassword_1);
System.out.println("修改密码成功!");
}
}
private static void Show1() {
System.out.println("-----1.登陆 2.遍历登陆用户 3.退出系统-----");
System.out.println("请选择业务:");
}
private static void Show2() {
System.out.println("-----个人中心-----");
System.out.println("1.修改密码 2.修改电话 3.修改地址 4.返回主页 5.注销用户");
}
}