import java.util.ArrayList;
import java.util.Scanner;
//Iterator 迭代器
//hasNext
//用集合完成以下功能
//1.查询所有员工信息
//2.增加新员工
//3.输入员工工号修改员工数据
//3.1.修改姓名
//3.2.修改年龄
//3.3.修改肤色
//3.4.修改工号
//4.删除员工
//5.查询员工
//5.1.以姓名关键字查询
//5.2.以年龄范围查询
//5.3.以颜色查询
//5.4.以工号查询
public class Demo1 {
static ArrayList<Person> data = new ArrayList<>();
static Scanner input = new Scanner(System.in);
static {
data.add(new Person("张三", 23, "黄色", 10016));
data.add(new Person("李四", 32, "黄色", 10086));
}
public static void main(String[] args) {
System.out.println("是否开启程序:\n0.退出程序\n1.开启程序\n");
int num = input.nextInt();
if (num == 1) {
while (true) {
System.out.println("请输入需要执行的指令:");
System.out.println("0.退出程序\n1.查询所有员工信息\n2.增加新员工\n3.输入员工工号修改员工数据\n4.删除员工\n5.查询员工");
int dir = input.nextInt();
if (dir == 0) {
break;
} else if (dir == 1) {
staffInquire();
} else if (dir == 2) {
staffAdd();
} else if (dir == 3) {
staffRevise();
} else if (dir == 4) {
staffDel();
} else if (dir == 5) {
staffKey();
} else {
throw new DirectivesException("指令" + dir + "不存在");
}
}
} else {
return;
}
}
public static void staffInquire() {
for (Person per : data) {
System.out.println(per);
}
}
public static void staffAdd() {
System.out.println("请输入需要新增的人员信息,中间以‘-’隔开,例如:姓名-年龄-肤色-工号");
String staff = input.next();
String[] sta = staff.split("-");
data.add(new Person(sta[0], Integer.parseInt(sta[1]), sta[2], Integer.parseInt(sta[3])));
}
public static void staffRevise() {
System.out.println("请输入需要更改人员的工号:");
int number = input.nextInt();
for (int i = 0; i < data.size(); i++) {
if (data.get(i).getJobNo() == number) {
while (true) {
System.out.println("请输入指令:");
System.out.println("0.返回\n1.修改姓名\n2.修改年龄\n3.修改肤色\n4.修改工号");
int choose = input.nextInt();
if (choose == 0) {
break;
} else if (choose == 1) {
System.out.println("请输入需要修改成的姓名:");
data.get(i).setName(input.next());
} else if (choose == 2) {
System.out.println("请输入需要修改成的年龄:");
data.get(i).setAge(input.nextInt());
} else if (choose == 3) {
System.out.println("请输入需要修改成的肤色:");
data.get(i).setColor(input.next());
} else if (choose == 4) {
System.out.println("请输入需要修改成的工号:");
data.get(i).setJobNo(input.nextInt());
} else {
throw new DirectivesException("不存在此指令");
}
}
}
}
}
public static void staffDel() {
/*//删除所有员工——迭代器
Iterator<Person> its = data.iterator();
while (its.hasNext()){
Person str = its.next();
System.out.println(str);
its.remove();
}
System.out.println(data.size());*/
System.out.println("请输入需要删除的员工工号");
int number = input.nextInt();
for (int i = 0; i < data.size(); i++) {
if (data.get(i).getJobNo() == number) {
data.remove(i);
}
}
}
public static void staffKey() {
while (true) {
System.out.println("请输入需要查询的指令:");
System.out.println("0.返回主界面\n1.以姓名关键字查询\n2.以年龄范围查询\n3.以肤色查询\n4.以工号查询");
int choose = input.nextInt();
if (choose == 0) {
break;
} else if (choose == 1) {
System.out.println("请输入姓名关键字进行查询");
String name = input.next();
for (Person per : data) {
if (per.getName().contains(name)) {
System.out.println(per);
}
}
} else if (choose == 2) {
System.out.println("请输入低位年龄:");
int age1 = input.nextInt();
System.out.println("请输入高位年龄:");
int age2 = input.nextInt();
for (Person per : data) {
if (per.getAge() >= age1 && per.getAge() <= age2) {
System.out.println(per);
}
}
} else if (choose == 3) {
System.out.println("请输入肤色进行查询:");
String color = input.next();
for (Person per : data) {
if (per.getColor().equals(color)) {
System.out.println(per);
}
}
} else if (choose == 4) {
System.out.println("请输入需要查询的工号:");
int jobNo = input.nextInt();
for (Person per : data) {
if (per.getJobNo() == jobNo) {
System.out.println(per);
}
}
} else {
throw new DirectivesException("不存在此指令");
}
}
}
}
class DirectivesException extends RuntimeException{
public DirectivesException() {
}
public DirectivesException(String message) {
super(message);
}
}
class Person {
private String name;
private int age;
private String color;
private int jobNo;
public Person() {
}
public Person(String name, int age, String color, int jobNo) {
this.name = name;
this.age = age;
if (age <= 0 || age >= 150){
throw new DirectivesException("年龄不合理,范围应在0-150之间");
}
this.color = color;
this.jobNo = jobNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
if (age <= 0 || age >= 150){
throw new DirectivesException("年龄不合理,范围应在0-150之间");
}
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getJobNo() {
return jobNo;
}
public void setJobNo(int jobNo) {
this.jobNo = jobNo;
}
@Override
public String toString() {
return "{" +
"姓名:'" + name + '\'' +
", 年龄:" + age +
", 肤色:'" + color + '\'' +
", 工号:" + jobNo +
'}';
}
}
项目--集合的运用
最新推荐文章于 2024-09-25 10:59:43 发布