package com.unit6.test;
public class Employee {
//工號(hào)
private String id;
//姓名
private String name;
//性別
private String sex;
//出生日期
private String birthday;
//號(hào)碼
private String phone;
//定義無參數(shù)的構(gòu)造方法
public Employee() {
}
//定義帶參數(shù)的構(gòu)造方法
public Employee(String id, String name, String sex, String birthday, String phone) {
this.id = id;//this代表使用Employee實(shí)例方法的當(dāng)前對(duì)象,this.id就表示使用當(dāng)前對(duì)象的變量id
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.phone = phone;
}
//set和get方法操作private變量,增強(qiáng)安全性
public void setId(String id) { //給對(duì)象的屬性賦值
this.id = id;
}
public String getId() { //取得對(duì)象的屬性值
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getBirthday() {
return birthday;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
}
package com.unit6.test;
import java.util.ArrayList;
import java.util.Scanner;
public class EmployeeManager {
/*
* 1. 用輸出語句完成主界面的編寫
* 2. 用Scanner實(shí)現(xiàn)鍵盤錄入數(shù)據(jù)
* 3. 用switch語句完成操作的選擇
* 4. 用循環(huán)完成再次回到主界面
*/
public static void main(String[] args) {
// 創(chuàng)建集合對(duì)象,用于存儲(chǔ)員工數(shù)據(jù)
ArrayList<Employee> array = new ArrayList<Employee>();
// 用循環(huán)完成再次回到主界面
while (true) {
// 1. 用輸出語句完成主界面的編寫
System.out.println("*******歡迎進(jìn)入員工管理系統(tǒng)*******");
System.out.println("\t1.輸出所有員工信息");
System.out.println("\t2.添加員工信息");
System.out.println("\t3.查詢指定工號(hào)員工信息");
System.out.println("\t4.修改員工信息");
System.out.println("\t5.刪除員工信息");
System.out.println("\t6.退出");
System.out.println("*****************************");
System.out.println("請(qǐng)選擇菜單功能:");
// 2. 用Scanner實(shí)現(xiàn)鍵盤錄入數(shù)據(jù)
Scanner in = new Scanner(System.in);
String choice = in.nextLine();//nextLine()返回Enter鍵之前的所有字符,可得到帶空格的字符串;而next()得不到帶空格的字符串,視字符串為分隔符
// 3. 用switch語句完成操作的選擇
switch (choice) {
case "1":
//查看所有員工
findAllStudent(array);
break;
case "2":
// 添加員工
addEmployee(array);
break;
case "3":
// 根據(jù)工號(hào)查看員工信息
findEmployeeById(array);
break;
case "4":
// 修改員工
updataStudent(array);
break;
case "5":
// 刪除員工
deleteStudent(array);
break;
case "6":
System.out.println("謝謝使用本系統(tǒng),再見!");
System.exit(0);
default:
// 輸入命令有誤時(shí),while循環(huán)實(shí)現(xiàn)返回重新輸入
System.out.println("輸入錯(cuò)誤,請(qǐng)重新輸入!");
}
}
}
// 1.定義一個(gè)方法,用于查看所有員工信息
public static void findAllStudent(ArrayList<Employee> array) {
if (array.size() == 0) {
System.out.println("抱歉!沒有員工記錄,請(qǐng)先添加員工信息再查詢");
// 為了不讓程序繼續(xù)往下執(zhí)行
return;
}
System.out.println("工號(hào)\t\t姓名\t\t性別\t\t出生日期\t\t\t電話");
// 將集合中數(shù)據(jù)取出按照對(duì)應(yīng)格式顯示員工信息
for (int i = 0; i < array.size(); i++) {
Employee e = array.get(i);
System.out.println(e.getId() + "\t\t" + e.getName() + "\t\t" + e.getSex() +
"\t\t" + e.getBirthday()+ "\t\t" + e.getPhone());
}
}
// 2.定義一個(gè)方法,用于添加員工信息
public static void addEmployee(ArrayList<Employee> array) {
// 用鍵盤錄入選擇添加員工,顯示提示信息,提示要輸入何種信息
Scanner in = new Scanner(System.in);
// 為了讓id在while循環(huán)外面也能被訪問到,我們就把它定義在了循環(huán)外
String id;
// 為了讓程序能夠回到這里,我們使用循環(huán)使用
while (true) {
System.out.println("請(qǐng)輸入工號(hào):");
id = in.nextLine();
boolean flag = isUsed(array, id);
if (flag) {
System.out.println("你輸入的工號(hào)已經(jīng)被使用,請(qǐng)重新輸入");
} else {
break;
}
}
System.out.println("請(qǐng)輸入員工姓名:");
String name = in.nextLine();
System.out.println("請(qǐng)輸入員工性別:");
String sex = in.nextLine();
System.out.println("請(qǐng)輸入員工出生日期:");
String birthday = in.nextLine();
System.out.println("請(qǐng)輸入員工電話:");
String phone = in.nextLine();
// 創(chuàng)建員工對(duì)象,把鍵盤錄入的數(shù)據(jù)賦值給員工對(duì)象的成員變量
Employee e = new Employee();
e.setId(id);//set賦值
e.setName(name);
e.setSex(sex);
e.setBirthday(birthday);
e.setPhone(phone);
// 將員工對(duì)象添加到集合中保存
array.add(e);
// 給出添加成功提示
System.out.println("添加員工成功");
}
// 定義一個(gè)方法,判斷工號(hào)是否被占用
public static boolean isUsed(ArrayList<Employee> array, String id) {
// 如果與集合中的某一個(gè)員工工號(hào)相同,返回true 如果都不相同,返回false
boolean flag = false;
for (int i = 0; i < array.size(); i++) {
Employee e = array.get(i);
if (e.getId().equals(id)) {
flag = true;
break;
}
}
return flag;
}
// 3.定義一個(gè)方法,用于根據(jù)員工工號(hào)查看員工信息
public static void findEmployeeById(ArrayList<Employee> array) {
// 用鍵盤錄入選擇添加員工,顯示提示信息
Scanner in = new Scanner(System.in);
// 輸入員工工號(hào)
System.out.println("請(qǐng)輸入員工工號(hào):");
String id = in.nextLine();
// 在根據(jù)員工工號(hào)查詢前,對(duì)工號(hào)是否存在進(jìn)行判斷
// 如果不存在,顯示不存在提示信息
// 如果存在,輸出員工信息
int index = -1;
Employee e = null;
// 遍歷集合將對(duì)應(yīng)員工對(duì)象從集合中拿出
for (int i = 0; i < array.size(); i++) {
e = array.get(i);
if (e.getId().equals(id)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("該"+id+"的工號(hào)不存在!");
} else {
System.out.println("工號(hào)"+"\t\t"+"姓名"+"\t\t"+"性別"+"\t\t"+"出生日期"+"\t\t\t"+"電話");
// 將集合中數(shù)據(jù)取出按照對(duì)應(yīng)格式顯示員工信息
System.out.println(e.getId() + "\t\t" + e.getName() + "\t\t" + e.getSex() +
"\t\t" + e.getBirthday()+ "\t\t" + e.getPhone());
}
}
// 4.定義一個(gè)方法,用于修改員工信息
public static void updataStudent(ArrayList<Employee> array) {
// 鍵盤錄入要修改的員工工號(hào),顯示提示信息
Scanner in = new Scanner(System.in);
System.out.println("請(qǐng)輸入要修改的員工工號(hào):");
String id = in.nextLine();
// 在修改員工操作前,對(duì)工號(hào)是否存在進(jìn)行判斷
// 如果不存在,顯示提示信息
// 如果存在,執(zhí)行修改操作
int index = -1;
// 遍歷集合修改對(duì)應(yīng)的員工信息
for (int i = 0; i < array.size(); i++) {
Employee e = array.get(i);
if (e.getId().equals(id)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("該工號(hào)不存在,請(qǐng)重新輸入");
} else {
for (int j = 0; j < array.size(); j++) {
Employee student = array.get(j);
if (student.getId().equals(id)) {
// 鍵盤錄入要修改的員工信息
System.out.println("請(qǐng)輸入員工的新姓名:");
String name = in.nextLine();
System.out.println("請(qǐng)輸入員工的新性別:");
String sex = in.nextLine();
System.out.println("請(qǐng)輸入員工的新出生日期:");
String birthday = in.nextLine();
System.out.println("請(qǐng)輸入員工的新電話:");
String phone = in.nextLine();
// 創(chuàng)建員工對(duì)象,set進(jìn)行賦值
Employee e = new Employee();
e.setId(id);
e.setName(name);
e.setSex(sex);
e.setBirthday(birthday);
e.setPhone(phone);
array.set(j, e);
// 給出修改成功提示
System.out.println("修改員工成功");
break;
}
}
}
}
// 5.定義一個(gè)方法,用于刪除員工信息
public static void deleteStudent(ArrayList<Employee> array) {
// 鍵盤錄入要?jiǎng)h除的員工工號(hào),顯示提示信息
Scanner in = new Scanner(System.in);
System.out.println("請(qǐng)輸入要?jiǎng)h除的員工工號(hào):");
String id = in.nextLine();
// 在刪除員工操作前,對(duì)工號(hào)是否存在進(jìn)行判斷
// 如果不存在,顯示提示信息
// 如果存在,執(zhí)行刪除操作
int index = -1;
// 遍歷集合將對(duì)應(yīng)員工對(duì)象從集合中刪除
for (int i = 0; i < array.size(); i++) {
Employee e = array.get(i);
if (e.getId().equals(id)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("該"+id+"的工號(hào)不存在!");
} else {
array.remove(index);
System.out.println("刪除員工成功!");
}
}
}
文章來源:http://www.zghlxwxcb.cn/news/detail-521977.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-521977.html
到了這里,關(guān)于Java員工信息管理系統(tǒng)(注釋全)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!