package cn.itcast;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
/*1)实现登录操作,要求输入用户名和密码验证。
2)用户登录成功可以
显示所有用户
添加用户
修改用户
删除用户*/
public class Landing {
// Persion p = new Persion();
private ArrayList list;
Scanner sc = new Scanner(System.in);
public Landing(ArrayList list) {
super();
this.list = list;
}
public ArrayList getList() {
return list;
}
public void setList(ArrayList list) {
this.list = list;
}
// 登陆方法
public boolean landing(String name, String password) {
boolean flag = true;
if (Admin.NAME.equals(name) && Admin.PASSWORD.equals(password)) {
flag = true;
System.out.println("欢迎管理员" + name + "登陆。");
} else {
Persion p = null;
Iterator it = list.iterator();
while (it.hasNext()) {
Persion pp = (Persion) it.next();
if (pp.getName().equals(name)
&& pp.getPassword().equals(password)) {
flag = true;
System.out.println("欢迎普通员工" + name + "登陆。");
} else {
System.out.println("你输入的账号或密码错误");
flag = false;
}
}
// System.out.println("你输入的账号或密码错误");
// flag = true;
}
return flag;
}
/*
* 删除数据: 返回值类型:void 未知数据:
*/
public void clearPersion() {
boolean flag = true;
while (flag) {
System.out.print("输入要删除的用户名:");
String name = sc.nextLine();
if (sameName(name)) {
Persion p = null;
Iterator it = list.iterator();
while (it.hasNext()) {
Persion pp = (Persion) it.next();
if (pp.getName().equals(name)) {
p = pp;
break;
}
}
list.remove(p);
System.out.println(p.getName()+"员工信息删除成功");
} else {
System.out.println("用户不存在无法删除。");
}
System.out.println("是否继续删除(y/n)");
String str = sc.nextLine();
if (!"y".equals(str)) {
flag = false;
}
}
}
/*
* 修改数据 返回值类型:void 未知量:
*/
public void replacePersion() {
String flag;
do {
System.out.println("输入要修改的用户名:");
String name = sc.nextLine();
if (sameName(name)) {
Iterator it = list.iterator();
while (it.hasNext()) {
Persion pp = (Persion) it.next();
if (pp.getName().equals(name)) {
System.out.println("输入新密码:");
String password = sc.nextLine();
pp.setPassword(password);
System.out.println(name+"员工登录密码修改成功。");
}
}
} else {
System.out.println(name+"用户不存在无法修改信息。");
}
System.out.println("是否继续修改(y/n)");
flag = sc.nextLine();
} while (flag.equals("y"));
}
/*
* 添加用户 明确: 返回值类型:void 未知量:
*/
public void addPersion() {
String flag;
do {
Persion p = new Persion();
/System.out.print("请输入新用户:");
String name = sc.nextLine();
p.setName(name);
System.out.print("请输入新密码:");
String password = sc.nextLine();
/p.setPassword(password);
list.add(p);
System.out.println("是否继续添加(y/n)");
flag = sc.nextLine();
} while (flag.equals("y"));
}
//添加新员工信息
public void addPersion() {
String flag;
do {
Persion p = new Persion();
System.out.print("请输入新用户:");
String name = sc.nextLine();
if (!sameName(name)) {
p.setName(name);
System.out.print("请输入密码:");
String password = sc.nextLine();
p.setPassword(password);
list.add(p);
System.out.println(name+"员工信息添加成功");
} else {
System.out.println("用户名已存在不发创建此用户。");
}
System.out.println("是否继续添加(y/n)");
flag = sc.nextLine();
} while (flag.equals("y"));
}
//查找输入的员工name集合中是否存在
private boolean sameName(String name) {
boolean flag = false;
Iterator it = list.iterator();
while (it.hasNext()) {
Persion pp = (Persion) it.next();
if (name.equals(pp.getName())) {
flag = true;
}
}
return flag;
}
//显示所有员工信息
public void openAll() {
System.out.println("用户名" + "\t" + "密码");
Iterator it = list.iterator();
while (it.hasNext()) {
Persion pp = (Persion) it.next();
System.out.println(pp.getName() + "\t" + pp.getPassword());
}
}
// 管理员显示界面
/*
* public void adminDisplay(){ www: while (true) {
*
* 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 num =
* sc.nextInt(); if (num == 1 || num == 2 || num == 3 || num == 4 || num ==
* 5) {
*
* switch (num) { case 1: openAll(); break; case 2: addPersion(); break;
* case 3: replacePersion(); break; case 4: clearPersion(); break; case 5:
* break www;
*
* } } else { System.out.println("输入的数据不正确");
*
* } }
*
* }
* 员工显示界面
* public void persionDisplay(){ www: while (true) {
*
* 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 num =
* sc.nextInt(); if (num == 1 || num == 2 || num == 3 || num == 4 || num ==
* 5) {
*
* switch (num) { case 1: openAll(); break; case 2: addPersion(); break;
* case 3: replacePersion(); break; case 4: clearPersion(); break; case 5:
* break www;
*
* } } else { System.out.println("输入的数据不正确");
*
* } } }
|
|