package com.thirdhill;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class Operate {
private List<Person> list;
public Operate() {
this.list = new ArrayList<>();
}
// 1-添加记录
public void addPerson(Menu menu, DataRegex dataRegex) {
while (true) {
menu.addMenu();
int item = dataRegex.menuItemValidate(1, 3);
switch (item) {
case 1:
addOperation(dataRegex);
break;
case 2:
searchAll();
break;
case 3:
return;
}
}
}
// 2-查找记录
public void searchPerson(Menu menu, DataRegex dataRegex) {
while (true) {
menu.searchMenu();
int item = dataRegex.menuItemValidate(1, 7);
switch (item) {
case 1:
searchByName(dataRegex);
break;
case 2:
searchByAge(dataRegex);
break;
case 3:
searchBySex(dataRegex);
break;
case 4:
searchByTelNum(dataRegex);
break;
case 5:
searchByAddress(dataRegex);
break;
case 6:
searchAll();
break;
case 7:
return;
}
}
}
// 3-修改记录
public void modifyPerson(Menu menu, DataRegex dataRegex) {
while (true) {
menu.modifyMenu();
int item = dataRegex.menuItemValidate(1, 3);
switch (item) {
case 1:
searchAll();
break;
case 2:
modifyOperation(menu, dataRegex);
break;
case 3:
return;
}
}
}
// 4-删除记录
public void deletePerson(Menu menu, DataRegex dataRegex) {
while (true) {
menu.deleteMenu();
int item = dataRegex.menuItemValidate(1, 4);
switch (item) {
case 1:
searchAll();
break;
case 2:
deleteOperation(dataRegex);
break;
case 3:
deleteAllOperation();
break;
case 4:
return;
}
}
}
// 添加新记录
public void addOperation(DataRegex dataRegex) {
String name = dataRegex.nameValidate();
String age = dataRegex.ageValidate();
String sex = dataRegex.sexValidate();
String telNum = dataRegex.telNumValidate();
String address = dataRegex.addressValidate();
Person person = new Person(name, age, sex, telNum, address);
list.add(person);
person.setId(list.size());
}
// 查看全纪录
public void searchAll() {
if (listIsEmpty()) {
for (Person person : list) {
person.show();
}
}
}
public void searchBy(String input, String targetMethod) {
String target = null;
boolean flag = true;
for (Person person : list) {
try {
Method method = person.getClass().getDeclaredMethod(targetMethod);
target = (String) method.invoke(person);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
if (input.equals(target)) {
person.show();
flag = false;
}
}
if (flag) {
System.out.println("没有该记录!");
}
}
// 1-按姓名查找
public void searchByName(DataRegex dataRegex) {
if (listIsEmpty()) {
String input = dataRegex.nameValidate();
searchBy(input, "getName");
}
}
// 2-按年龄查找
public void searchByAge(DataRegex dataRegex) {
if (listIsEmpty()) {
String input = dataRegex.ageValidate();
searchBy(input, "getAge");
}
}
// 3-按性别查找
public void searchBySex(DataRegex dataRegex) {
if (listIsEmpty()) {
String input = dataRegex.sexValidate();
searchBy(input, "getSex");
}
}
// 4-按号码查找
public void searchByTelNum(DataRegex dataRegex) {
if (listIsEmpty()) {
String input = dataRegex.telNumValidate();
searchBy(input, "getTelNum");
}
}
// 5-按地址查找
public void searchByAddress(DataRegex dataRegex) {
if (listIsEmpty()) {
String input = dataRegex.addressValidate();
searchBy(input, "getAddress");
}
}
// list是否为空
public boolean listIsEmpty() {
if (list.size() != 0) {
return true;
}
System.out.println("没有记录!\n");
return false;
}
// 修改指定记录
public void modifyOperation(Menu menu, DataRegex dataRegex) {
if (listIsEmpty()) {
searchAll();
System.out.print("请输入要修改的联系人:");
int item = dataRegex.menuItemValidate(1, list.size());
menu.subModifyMenu();
int subItem = dataRegex.menuItemValidate(1, 6);
switch (subItem) {
case 1:
String name = dataRegex.nameValidate();
list.get(item - 1).setName(name);
break;
case 2:
String age = dataRegex.ageValidate();
list.get(item - 1).setAge(age);
break;
case 3:
String sex = dataRegex.sexValidate();
list.get(item - 1).setSex(sex);
break;
case 4:
String telNum = dataRegex.telNumValidate();
list.get(item - 1).setTelNum(telNum);
break;
case 5:
String address = dataRegex.addressValidate();
list.get(item - 1).setAddress(address);
break;
case 6:
break;
}
}
}
// 删除指定记录
public void deleteOperation(DataRegex dataRegex) {
if (listIsEmpty()) {
searchAll();
System.out.print("请输入要删除的联系人:");
int item = dataRegex.menuItemValidate(1, list.size());
list.remove(item - 1);
for (int i = 0; i < list.size(); i++) {
list.get(i).setId(i + 1);
}
System.out.println("删除成功!");
}
}
// 删除所有记录
public void deleteAllOperation() {
if (listIsEmpty()) {
list.clear();
System.out.println("所有记录已删除!");
}
}
}