更新時(shí)間:2022-10-08 10:53:55 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1290次
1.Person類
package p1;?
public class Person {
?? ?// Person屬性
?? ?private int num;
?? ?private String name;
?? ?private String sex;
?? ?private int salary;?
?? ?public Person(int num, String name, String sex, int salary) {
?? ??? ?super();
?? ??? ?this.num = num;
?? ??? ?this.name = name;
?? ??? ?this.sex = sex;
?? ??? ?this.salary = salary;
?? ?}?
?? ?// 對(duì)Perosn操作的方法
?? ?public int getNum() {
?? ??? ?return num;
?? ?}?
?? ?public void setNum(int num) {
?? ??? ?this.num = num;
?? ?}?
?? ?public String getName() {
?? ??? ?return name;
?? ?}?
?? ?public void setName(String name) {
?? ??? ?this.name = name;
?? ?}?
?? ?public String getSex() {
?? ??? ?return sex;
?? ?}
?? ?public void setSex(String sex) {
?? ??? ?this.sex = sex;
?? ?}?
?? ?public int getSalary() {
?? ??? ?return salary;
?? ?}?
?? ?public void setSalary(int salary) {
?? ??? ?this.salary = salary;
?? ?}?
}
2.SysMenu類
package p1;
public class SysMenu {
public static final String[] MENU = { "1.員工信息管理", "2.退出" };
public static final String[] OPERATION_MENU = { "1.新增", "2.查看", "3.修改", "4.刪除", "5.返回" };
public static void showMenu(String[] Menu) {
for (int i = 0; i < Menu.length; i++)
System.out.print(Menu[i] + "\t\t");
System.out.println();
System.out.println("---------------------------------------");
}
}
3.SysInfo類
package p1;
import java.util.ArrayList;
import java.util.List;
public class SysInfo {
private static List informationList = new ArrayList();
// 獲取 informationList
public static List getList() {
return informationList;
}
}
4.InformationService類
package p1;
import java.util.List;
public class InformationService {
private List informationList = SysInfo.getList();
// 獲取信息列表
public List getList() {
return informationList;
}
// 按編號(hào)查找信息
public Person getPersonByNum(final int num) {
if (num < 1) {
System.out.println("編號(hào)錯(cuò)誤");
return null;
}
for (int i = 0; i < informationList.size(); i++) {
Person p = (Person) informationList.get(i);
if (p.getNum() == num) {
System.out.println("查找成功");
return p;
}
}
System.out.println("查找失敗");
return null;
}
//查看單一Person信息
public void showAPerson(Person p)
{
System.out.println("編號(hào)\t\t姓名\t\t性別\t\t薪水");
System.out.println(p.getNum()+ "\t\t" + p.getName() + "\t\t" + p.getSex() + "\t\t" + p.getSalary());
}
//show all Person
public void showPerson() {
System.out.println("編號(hào)\t\t姓名\t\t性別\t\t薪水");
List ps = getList();
for (int i = 0; i < ps.size(); i++) {
Person p = (Person) ps.get(i);
System.out.println(p.getNum() + "\t\t" + p.getName() + "\t\t" + p.getSex() + "\t\t" + p.getSalary());
}
}
// 按名字查找信息
public Person getPersonByName(final String name) {
if (name == null)
return null;
for (int i = 0; i < informationList.size(); i++) {
Person p = (Person) informationList.get(i);
if (p.getName().equals(name)) {
return p;
}
}
return null;
}
//檢查對(duì)象是否存在
public boolean CheckExitByNum(int num)
{
for(int i=0;i<informationList.size();i++)
{
Person p = (Person)informationList.get(i);
if(p.getNum()==num)
return true;
}
return false;
}
//save Person
public void savePerson(Person p)
{
p.setNum(getPersonMaxInt()+1);
informationList.add(p);
}
// 查找最大編號(hào)
public int getPersonMaxInt()
{
int max = 0;
for(int i =0;i<informationList.size();i++)
{
Person p =(Person)informationList.get(i);
if(max < p.getNum())
max = p.getNum();
}
return max;
}
}
5.SysRun類
package p1;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class SysRun {
private List informationList = SysInfo.getList();
private Scanner s = new Scanner(System.in);
private InformationService is = new InformationService();
// 系統(tǒng)運(yùn)行類
public static void main(String[] args) {
SysRun sys = new SysRun();
sys.sysRun();
}
public void sysRun() {
System.out.println("啟動(dòng)系統(tǒng)管理系統(tǒng)");
boolean isExit = false;
do {
System.out.println("----------操作選項(xiàng)-------------");
SysMenu.showMenu(SysMenu.MENU);
// 獲取用戶輸入
int operNum = getCorrONum(SysMenu.MENU);
// 管理操作
isExit = doManageNum(operNum);
} while (!isExit);
System.out.println("系統(tǒng)退出.");
}
private boolean doManageNum(int operNum) {
boolean isExit = false;
switch (operNum) {
case 1:
is.showPerson();
System.out.println("----------操作選項(xiàng)-------------");
SysMenu.showMenu(SysMenu.OPERATION_MENU);
// addPerson();//test
System.out.println("輸入功能選擇:");
int num = getVaildInt();
doOperationNum(num);
break;
case 2:
isExit = true;
return isExit;
}
return isExit;
}
// doOperationNum
private void doOperationNum(int OperationNum) {
// 增,查,修,刪,返回
switch (OperationNum) {
case 1:
// add
addPerson();
is.showPerson();
break;
case 2:
// 查看
viewPerson();
break;
case 3:
updatePerson();
break;
case 4:
deletePerson();
is.showPerson();
break;
case 5:
break;
}
}
// 刪除Person
private void deletePerson() {
int num;
// Person p;
boolean isOk = false;
System.out.println("請(qǐng)輸入要?jiǎng)h除信息的編號(hào):");
do {
num = getVaildInt();
isOk = is.CheckExitByNum(num);
if (isOk == true) {
System.out.println("編號(hào)信息查找成功。");
informationList.remove(is.getPersonByNum(num));
} else
System.out.println("輸入編號(hào)有誤,請(qǐng)重新輸入:");
} while (!isOk);
}
// 修改Person
public void updatePerson() {
System.out.println("請(qǐng)輸入要修改的信息編號(hào):");
boolean isOk = false;
Person p;
do {
int num = getVaildInt();
isOk = is.CheckExitByNum(num);
if (isOk == true) {
isOk = true;
p = is.getPersonByNum(num);
is.showAPerson(p);
System.out.println("請(qǐng)輸入名字:");
String name = s.next();
System.out.println("請(qǐng)輸入性別:");
String sex = getVaildSex();
System.out.println("請(qǐng)輸入工資:");
int salary = getVaildInt();
p.setName(name);
p.setSex(sex);
p.setSalary(salary);
is.showPerson();
} else
System.out.println("輸入要修改的編號(hào)有誤,請(qǐng)重新輸入:");
} while (!isOk);
}
// 查看viewPerson()
private void viewPerson() {
System.out.println("請(qǐng)輸入要查看的人的信息編號(hào):");
Person p;
boolean isOk = false;
do {
int num = getVaildInt();
boolean NumIsOk = is.CheckExitByNum(num);
if (NumIsOk == true) {
p = is.getPersonByNum(num);
is.showAPerson(p);
isOk = true;
} else {
System.out.println("無(wú)此編號(hào)的人的信息,請(qǐng)重新輸入:");
}
} while (!isOk);
}
// addPerson()
private void addPerson() {
System.out.println("------------新增對(duì)象---------------");
boolean isOk = false;
String name = null;
do {
System.out.println("請(qǐng)輸入名稱(且不能與現(xiàn)有的對(duì)象重名)");
name = s.next();
// 處理同名沖突
if (is.getPersonByName(name) == null) {
isOk = true;
} else {
System.out.println("該人信息已存在,請(qǐng)重新輸入!");
s.next();
}
} while (!isOk);
// other information
System.out.println("請(qǐng)輸入其他信息...");
System.out.println("sex:");
String sex = getVaildSex();
System.out.println("salary:");
int salary = getVaildInt();
// save
is.savePerson(new Person(0, name, sex, salary));
}
/* 輸入有效int */
private int getVaildInt() {
int num = 0;
boolean isOk = false;
do {
try {
num = s.nextInt();
isOk = true;
} catch (InputMismatchException e) {
System.out.println("輸入錯(cuò)誤,請(qǐng)重新輸入");
s.next();
}
} while (!isOk);
return num;
}
/* 輸入有效sex信息 */
private String getVaildSex() {
String sex = null;
boolean isOk = false;
do {
sex = s.next();
if (sex.equals("f") || sex.equals("m"))
isOk = true;
else {
System.out.println("sex輸入讓 有誤,請(qǐng)重新輸入");
}
} while (!isOk);
return sex;
}
public int getCorrONum(String[] targetMenu) {
System.out.println("請(qǐng)輸入要選擇的操作:");
int inputNum = 0;
boolean inputIsOk = false;
do {
try {
inputNum = s.nextInt();
System.out.println("輸入的是" + inputNum);
if (inputNum >= 1 && inputNum <= targetMenu.length) {
inputIsOk = true;
} else {
System.out.println("輸入錯(cuò)誤,請(qǐng)重新輸入!");
}
} catch (InputMismatchException e) {
System.out.println("輸入有誤,請(qǐng)重新輸入");
// 若輸入出現(xiàn)異常,Scanner要丟棄上一次的輸入,否則 do-while會(huì)出現(xiàn)死循環(huán)
s.next();
}
} while (!inputIsOk);
return inputNum;
}
}
效果圖:
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743