------實現(xiàn)系統(tǒng)的增刪改查,代碼會在底部會發(fā)出來;
一、準備工作
1.1
? ? ? ? 右鍵在指定目錄新建一個軟件包;
1.2
? ? ? ? 在 Name 取好名字點擊 Finish 建立包;
?1.3
? ? ? ? 右鍵新建的包創(chuàng)建兩個類;
?1.4
? ? ? ? 取名后點擊 Finish ;
? ? ? ? 為了看起來更規(guī)范,我一個取名為 Staff 、一個取名為 StaffManagement ;
二、Staff 類
?2.1
? ? ? ? 于 Staff? 類中定義四個變量,分別存儲人員的 id 、姓名 、年齡 、生源 ;
?2.2
? ? ? ? 在類中再定義一個無參構造函數(shù),一個有參構造函數(shù);
????????( 不一定都會用到,但是多寫一個也沒什么不好 )
?2.3
? ? ? ? ?給每一個元素編寫 set 、get 代碼,用于讀取跟修改;
三、StaffManagement 類
3.1
? ? ? ? 1.因為要在后續(xù)多個方法內(nèi)使用輸入,故將 Scanner 聲明在全局;
? ? ? ? 2.聲明全局變量 n ,用于接收后續(xù)操作信息;
? ? ? ? 3.創(chuàng)建一個 Staff 類的集合,用于保存人員信息;
? ? ? ? 4.輸出一條進入系統(tǒng)的提示;( 樣式憑自己愛好調(diào)整即可 )
?3.2
? ? ? ? 1.調(diào)用 show 方法進入下一步操作;
? ? ? ? 2.show 方法首先輸出有關指令的提示信息;
? ? ? ? 3.使用 n 接收指令,因為只有 1 - 5 的指令,故超出范圍則需要重新輸入;
? ? ? ? 4.因為后續(xù)會使用讀取字符的語句,輸入 n 時會殘余一個回車,為避免讀取錯誤,使用 nextLine 將回車無效化;
?3.3
????????對 n 取值進行判斷,使各取值調(diào)用各自所對應的方法,完畢后再次進入 show 方法;
? ? ? ? 若 n 取值為 5 ,則直接結束整個循環(huán);
? ? ? ? 結束循環(huán)后沒有任何語句,故調(diào)用返回;
? ? ? ? 返回主方法后輸出退出系統(tǒng)的提示;
?3.4
? ? ? ? select 方法,該方法會將集合內(nèi)所有的人員信息打印出來;
? ? ? ? ( 可憑自己愛好調(diào)整 )
?3.5
? ? ? ? 在剛進入?select 方法時增加一條判斷,如果表內(nèi)無信息,則輸出提示;
? ? ? ? 根據(jù)提示信息可選擇進行其他操作也可退出系統(tǒng);?
3.6
? ? ? ? insert 方法先實例化一個 Staff 對象,并通過一個 str 字符串接收數(shù)據(jù),將數(shù)據(jù)傳入新對象,在添加每一項之前都輸出一句提示信息,接收完數(shù)據(jù)后將對象增加進集合;
? ? ? ? ( 提示信息樣式可憑個人喜歡更改 )
?3.7
? ? ? ? 為保證 id 的唯一性,在輸入員工 id 后使用 foreach 進行對集合的遍歷判斷,如果存在該 id 則輸出提示信息并返回,反之則繼續(xù)運行;
?3.8
? ? ? ? ?update 方法首先輸出一條提示信息提示輸入 id ;
? ? ? ? 使用 str 字符串接收輸入數(shù)據(jù);
????????定義一個整型變量 x 初值為 -1 ,用于存放員工在集合內(nèi)的索引值;
? ? ? ? 通過 foreach 遍歷整個集合尋找是否有所尋找的員工 id ,如果找到了將索引值傳入;
? ? ? ? 通過判斷 x 內(nèi)存放的值是否被改變確定是否有這個 id ,如果沒有此 id 輸出提示并返回,反之則繼續(xù);
3.9
? ? ? ? 后續(xù)跟 insert 方法類似,區(qū)別是利用 get( 索引 ).set( 所修改的值 );
?4.0
? ? ? ? 最后 remove 方法跟 update 一致,先確定是否有所查找的員工 id ;
? ? ? ? 如果有則使用集合的 remove 方法進行刪除;
? ? ? ? 刪除成功后輸出提示并返回;文章來源:http://www.zghlxwxcb.cn/news/detail-500551.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-500551.html
四、代碼分享
4.1 Staff 類
package com.caterpillar;
public class Staff {
private String id;
private String name;
private int age;
private String address;
public Staff() {
}
public Staff(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
4.2?StaffManagement 類
package com.caterpillar;
import java.util.ArrayList;
import java.util.Scanner;
public class StaffManagement {
static Scanner sc = new Scanner( System.in );
static int n;
static ArrayList<Staff> staffArrayList = new ArrayList<>();
public static void main(String[] args) {
System.out.println(" -------------歡迎進入人員管理系統(tǒng)-------------");
show();
System.out.println("\n\t謝謝使用!??!");
}
public static void show(){
System.out.println("\n-\t\t1.查看所有員工;\t\t-");
System.out.println("-\t\t2.添加員工信息;\t\t-");
System.out.println("-\t\t3.修改員工信息;\t\t-");
System.out.println("-\t\t4.刪除員工信息;\t\t-");
System.out.println("-\t\t5.退出管理系統(tǒng);\t\t-");
System.out.print("\n\t請輸入你要進行的操作:\t" );
n = sc.nextInt();
for ( ; n < 1 || n > 5 ; ){
System.out.print("\t請輸入合法的數(shù)字:\t");
n = sc.nextInt();
}
sc.nextLine();
while ( true ){
if ( n == 1 )
select();
if ( n == 2 )
insert();
if ( n == 3 )
update();
if ( n == 4 )
remove();
if ( n == 5 )
break;
show();
}
}
public static void insert(){
Staff staff = new Staff();
String str;
System.out.print("\t請輸入員工id:\n\t");
str = sc.nextLine();
for ( Staff list: staffArrayList
) {
if ( list.getId().equals(str)){
System.out.println("\t已存在當前 ID ,請勿重復輸入!!!");
return;
}
}
staff.setId( str );
System.out.print("\t請輸入員工姓名:\n\t");
str = sc.nextLine();
staff.setName( str );
System.out.print("\t請輸入員工年齡:\n\t");
str = sc.nextLine();
staff.setAge( Integer.parseInt( str ) );
System.out.print("\t請輸入員工生源:\n\t");
str = sc.nextLine();
staff.setAddress( str );
staffArrayList.add( staff );
System.out.println("\t添加成功!!!");
}
public static void remove(){
System.out.print("\t請輸入員工id:\n\t");
String str = sc.nextLine();
int x = -1 ;
for ( Staff list: staffArrayList
) {
if ( list.getId().equals(str)){
x = staffArrayList.indexOf( list );
break;
}
}
if ( x == -1 ){
System.out.println("\t查無此人!!!");
return;
}
for ( Staff list: staffArrayList
) {
if ( list.getId().equals(str)){
staffArrayList.remove( x );
System.out.println("\t刪除成功?。?!");
break;
}
}
}
public static void update(){
System.out.print("\t請輸入員工id:\n\t");
String str = sc.nextLine();
int x = -1 ;
for ( Staff list: staffArrayList
) {
if ( list.getId().equals(str)){
x = staffArrayList.indexOf( list );
break;
}
}
if ( x == -1 ){
System.out.println("\t查無此人!!!");
return;
}
System.out.print("\t請修改員工姓名:\n\t");
str = sc.nextLine();
staffArrayList.get( x ).setName( str );
System.out.print("\t請修改員工年齡:\n\t");
str = sc.nextLine();
staffArrayList.get( x ).setAge( Integer.parseInt( str ) );
System.out.print("\t請修改員工生源:\n\t");
str = sc.nextLine();
staffArrayList.get( x ).setAddress( str );
System.out.println("\t修改成功!!!");
}
public static void select(){
if ( staffArrayList.size() == 0 ){
System.out.print("\t表內(nèi)無信息,請?zhí)砑有畔⒑笤俨樵儯。。?輸入 1 ,繼續(xù)使用,其他則自動關閉系統(tǒng) )\n\t");
int temp = sc.nextInt();
if ( temp != 1 ){
System.out.println("\n\t謝謝使用!?。?);
System.exit(0);
}
return;
}
System.out.printf("\n%5s\t\t%5s\t\t%5s\t\t%5s","id","姓名","年齡","生源");
for ( Staff list:
staffArrayList
) {
System.out.printf("\n %5s\t\t %5s\t\t%5d歲\t\t %5s\n",list.getId(),list.getName(),list.getAge(),list.getAddress());
}
}
}
到了這里,關于JAVA:實現(xiàn)簡單的人員管理系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!