生活随笔
收集整理的這篇文章主要介紹了
基于Java基础的客户信息管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄 前言 一、前期準備 二、整體結構介紹 三、工具類介紹 四、功能淺談 1、Customer 類 2、CustomerView 類 3、CustomerList 類 五、收獲 六、總結
前言
這是狗子我學習過一遍 Java基礎 之后復習找到的 第二個練手案例 ,且兩個項目都來自于 小破站 中尚硅谷的練手案例,我在寫這兩個項目的時候除了 尚硅谷 中提及到的部分也添加了一些自己的想法在里面,因此可能會有一些許不同,還請見諒。如果閱讀過程中出現什么問題,歡迎評論留言。
第一個練手案例為 基于Java基礎的家庭收支記賬軟件,大家伙有興趣的話可以去瞄兩眼 ?(?????)?)
同時貼上尚硅谷Java基礎教學視頻的鏈接 – > 傳送門
一、前期準備
在開始搞事情之前得先準備好下面這些東西,其中 JDK 和 IDE 不一定需要統一,這只是狗子我使用的版本而已。準備好之后帶上我們的腦子就可以開搞啦
編程語言 : Java運行環境 : JDK1.8IDE : IDEA 2020儲備知識 : Java基礎
二、整體結構介紹
在這次案例中第一次使用到了 MVC框架,將業務層和視圖層進行分離,提高了程序的可維護性,降低了耦合性,這一框架模式也是從尚硅谷的教學中學習得來,‘真’ YYDS
三、工具類介紹
在尚硅谷的文件中提供了已經封裝好的 CMUtility 工具類,方便我們實現鍵盤的訪問。而由于狗子我是在復習,工具類中涉及到的基本上都已經接觸過,因此這個工具類便自己進行了編寫,如果是從視頻那邊過來的話也可以直接跳過工具類的編寫,不過還是推薦自己嘗試一下,我寫的工具類名為 CustomerUtility
函數名功能介紹返回類型 readMenuSelection 用于界面菜單的選擇 int readChar 從鍵盤中讀取一個字符且存在方法重載 char readInt 從鍵盤中讀取一個不超過二位數的整數且存在方法重載 int readString 從鍵盤中讀取一個長度不超過 limit 的字符串且存在方法重載 String readConfirmSelection 用于確認選擇的輸入,讀取‘Y’或‘N’ char readKeyBoard 用于記錄用戶從鍵盤中輸入的數據 String
在這里附上一小撮演示小截圖
四、功能淺談
1、Customer 類
顧名思義(第一次看的時候還去翻譯了),這是一個用來封裝客戶信息的類,這里面封裝了客戶的基本信息,并提供了各屬性的 get / set 方法以及自編的構造器對屬性進行初始化,下方代碼省略了 get / set 方法
public class Customer { private String name
; private char gender
; private int age
; private String phone
; private String email
; public Customer ( ) { } public Customer ( String name
, char gender
, int age
, String phone
, String email
) { this . name
= name
; this . gender
= gender
; this . age
= age
; this . phone
= phone
; this . email
= email
; } public Customer ( String name
, char gender
, int age
) { this ( name
, gender
, age
, "null" , "null" ) ; }
}
2、CustomerView 類
主函數所在的類,其為主模塊,負責菜單的顯示和處理用戶操作,其中該類下的成員方法僅供 enterMainMenu() 方法調用,該類中實現的主要有以下函數:
2.1、 public void enterMainMenu() 用于顯示主菜單 ,相應用戶輸入,根據用戶操作分別調用其他相應的成員方法,從而完成客戶信息處理
public void enterMainMenu ( ) { boolean loopFlag
= true ; do { System . out
. println ( "\n-----------------客戶信息管理系統-----------------\n" ) ; System . out
. println ( "\t\t1. 添 加 客 戶\t\t\t2. 修 改 客 戶" ) ; System . out
. println ( "\t\t3. 刪 除 客 戶\t\t\t4. 客 戶 列 表" ) ; System . out
. println ( "\t\t5. 退 出" ) ; System . out
. print ( "\t\t請選擇(1-5):" ) ; int selection
= CustomerUtility . readMenuSelection ( 5 ) ; switch ( selection
) { case 1 : { addNewCustomer ( ) ; break ; } case 2 : { modifyCustomer ( ) ; break ; } case 3 : { deleteCustomer ( ) ; break ; } case 4 : { listAllCustomers ( ) ; break ; } case 5 : { System . out
. print ( "確認是否退出(Y / N): " ) ; if ( CustomerUtility . readConfirmSelection ( ) == 'Y' ) { loopFlag
= false ; } break ; } } } while ( loopFlag
) ;
}
2.2、private void addNewCustomer() 用于添加 新用戶,且只允許 enterMainMenu 方法調用
private void addNewCustomer ( ) { Customer customer
= new Customer ( ) ; System . out
. println ( "---------------------添加客戶---------------------" ) ; System . out
. print ( "姓名:" ) ; customer
. setName ( CustomerUtility . readString ( 4 ) ) ; System . out
. print ( "性別:" ) ; customer
. setGender ( CustomerUtility . readChar ( ) ) ; System . out
. print ( "年齡:" ) ; customer
. setAge ( CustomerUtility . readInt ( ) ) ; System . out
. print ( "電話:" ) ; customer
. setPhone ( CustomerUtility . readString ( 15 ) ) ; System . out
. print ( "郵箱:" ) ; customer
. setEmail ( CustomerUtility . readString ( 20 ) ) ; if ( customerList
. addCustomer ( customer
) ) { System . out
. println ( "---------------------添加完成---------------------" ) ; } else { System . out
. println ( "----------------記錄已滿,無法添加-----------------" ) ; }
}
2.3、 private void modifyCustomer() 實例化一個新的客戶對象傳遞給 replaceCustomer 方法從而達到修改 客戶信息的作用
private void modifyCustomer ( ) { System . out
. println ( "---------------------修改客戶---------------------" ) ; int index
; Customer customer
= null ; while ( true ) { System . out
. print ( "請選擇待修改客戶姓名(0退出):" ) ; String name
= CustomerUtility . readString ( 4 ) ; if ( name
. equals ( "0" ) ) { return ; } index
= customerList
. searchCustomer ( name
) ; if ( index
== - 1 ) { System . out
. println ( "該客戶不存在!" ) ; continue ; } customer
= customerList
. getCustomer ( index
+ 1 ) ; if ( customer
== null ) { System . out
. println ( "該客戶不存在!" ) ; } else { break ; } } System . out
. print ( "姓名(" + customer
. getName ( ) + "): " ) ; String name
= CustomerUtility . readString ( 4 , customer
. getName ( ) ) ; System . out
. print ( "性別(" + customer
. getGender ( ) + "): " ) ; char gender
= CustomerUtility . readChar ( customer
. getGender ( ) ) ; System . out
. print ( "年齡(" + customer
. getAge ( ) + "): " ) ; int age
= CustomerUtility . readInt ( customer
. getAge ( ) ) ; System . out
. print ( "電話(" + customer
. getPhone ( ) + "): " ) ; String phone
= CustomerUtility . readString ( 15 , customer
. getPhone ( ) ) ; System . out
. print ( "郵箱(" + customer
. getEmail ( ) + "): " ) ; String email
= CustomerUtility . readString ( 20 , customer
. getEmail ( ) ) ; if ( customerList
. replaceCustomer ( index
+ 1 , new Customer ( name
, gender
, age
, phone
, email
) ) ) { System . out
. println ( "---------------------修改完成---------------------" ) ; } else { System . out
. println ( "----------無法找到指定客戶,修改失敗--------------" ) ; } }
2.4、 private void deleteCustomer() 通過用戶輸入的下標 index 進行索引客戶對象信息,從而對其進行刪除
private void deleteCustomer ( ) { System . out
. println ( "---------------------刪除客戶---------------------" ) ; int index
= 0 ; while ( true ) { System . out
. print ( "請選擇待刪除客戶編號(0退出):" ) ; index
= CustomerUtility . readInt ( ) ; if ( index
== 0 ) { return ; } if ( customerList
. getCustomer ( index
) == null ) { System . out
. println ( "無法找到該用戶!" ) ; } else { break ; } } System . out
. print ( "確認是否刪除(Y / N):" ) ; if ( CustomerUtility . readConfirmSelection ( ) == 'N' ) { return ; } if ( customerList
. deleteCustomer ( index
) ) { System . out
. println ( "---------------------刪除完成---------------------" ) ; } else { System . out
. println ( "----------無法找到指定客戶,刪除失敗--------------" ) ; }
}
2.5、 private void listAllCustomers() 通過getCustomers() 方法獲得客戶對象列表后遍歷 已添加的客戶信息
private void listAllCustomers ( ) { System . out
. println ( "---------------------------客戶列表---------------------------" ) ; Customer [ ] customers
= customerList
. getCustomers ( ) ; if ( customers
. length
== 0 ) { System . out
. println ( "沒有客戶記錄!" ) ; } else { System . out
. println ( "序號\t\t姓名\t\t性別\t\t年齡\t\t電話\t\t\t\t\t郵箱" ) ; for ( int i
= 0 ; i
< customers
. length
; i
++ ) { System . out
. println ( "No." + ( i
+ 1 ) + "\t" + customers
[ i
] . getName ( ) + "\t\t" + customers
[ i
] . getGender ( ) + "\t\t" + customers
[ i
] . getAge ( ) + "\t\t" + customers
[ i
] . getPhone ( ) + "\t\t" + customers
[ i
] . getEmail ( ) ) ; } } System . out
. println ( "----------------------------------------------------------" ) ;
}
3、CustomerList 類
該類為 Customer對象 的管理模塊,內部使用 數組 管理一組Customer對象。在本類中封裝了實際用戶個數屬性 total 和存放客戶對象的數組 customers ,默認可存放十個客戶對象信息,可通過構造器進行自定義修改,以及具備對列表進行管理操作功能的方法:
3.1、 public boolean addCustomer(Customer customer) 將新的客戶對象 customer 通過尾插法添加 到客戶對象數組中
public boolean addCustomer ( Customer customer
) { if ( customers
. length
<= this . total
) { return false ; } customers
[ this . total
++ ] = customer
; return true ;
}
3.2、 public boolean replaceCustomer(int index, Customer customer) 用修改 過的客戶對象 customer 替換數組中由 index 指定的對象,其中 index 為用戶指定傷處對像在數組中的索引位置,用戶輸入從 1 開始
public boolean replaceCustomer ( int index
, Customer customer
) { if ( -- index
< 0 || index
> this . total
) { return false ; } customers
[ index
] = customer
; return true ;
}
3.3、 public int searchCustomer(String name) 通過名字檢索 指定客戶對象,這一方法為狗子我自己添加的,尚硅谷視頻中使用的是 下標檢索
public int searchCustomer ( String name
) { if ( this . total
== 0 ) { return - 1 ; } for ( int i
= 0 ; i
< this . total
; i
++ ) { if ( customers
[ i
] . getName ( ) . equals ( name
) ) { return i
; } } return - 1 ;
}
**3.4、public boolean deleteCustomer(int index) ** 從數組中刪除 指定索引位置 index 的客戶對象信息,其中為用戶指定傷處對像在數組中的索引位置,用戶輸入從 1 開始
public boolean deleteCustomer ( int index
) { if ( -- index
< 0 || index
> this . total
) { return false ; } for ( int i
= index
; i
< total
- 1 ; i
++ ) { customers
[ i
] = customers
[ i
+ 1 ] ; } customers
[ -- this . total
] = null ; return true ;
}
3.5、 public Customer[] getCustomers() 獲取數組中記錄的所有客戶對象
public Customer [ ] getCustomers ( ) { Customer [ ] customersList
= new Customer [ this . total
] ; for ( int i
= 0 ; i
< this . total
; i
++ ) { customersList
[ i
] = customers
[ i
] ; } return customersList
;
}
3.6、 public Customer getCustomer(int index) 返回指定索引位置 index 的客戶對象信息,其中 index 為用戶指定傷處對像在數組中的索引位置,用戶輸入從 1 開始
public Customer getCustomer ( int index
) { if ( -- index
< 0 || index
> this . total
) { return null ; } return customers
[ index
] ;
}
五、收獲
每次復習一部分知識之后都會有一定的新的收獲,補充了很多細節,最重要的莫過于是在 Java 中最重要的面向對象的思想(此對象非你對象(???????)???? ?? ????)
第一次接觸到了 MVC框架 ,讓自己的程序更加結構化,找到一個自己想要的函數真的很好找,改 bug 的時間縮短了 對封裝還是有了進一步的認識,函數的重用適當的變多,增加了程序的可維護性
六、總結
還是那句話:掉頭發這件事情你得實踐了才知道你掉的質量怎么樣,在學習Java甚至是任何一門語言中,只學不敲,頭發也就白掉了。這次相對于第一次的練手案例在結構上更加合理,動手真的是學習道路上必不可缺的一部分。
這里附上該案例中的全部代碼 – >傳送門
總結
以上是生活随笔 為你收集整理的基于Java基础的客户信息管理系统 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。