Java實(shí)現(xiàn)簡(jiǎn)單客戶信息管理系統(tǒng)
本文實(shí)例為大家分享了Java實(shí)現(xiàn)客戶信息管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
一、目標(biāo)
模擬實(shí)現(xiàn)一個(gè)基于文本界面的《客戶信息管理軟件》
進(jìn)一步掌握編程技巧和調(diào)試技巧,熟悉面向?qū)ο缶幊?/p>
主要涉及以下知識(shí)點(diǎn):
- 類結(jié)構(gòu)的使用:屬性、方法及構(gòu)造器
- 對(duì)象的創(chuàng)建與使用
- 類的封裝性
- 聲明和使用數(shù)組
- 數(shù)組的插入、刪除和替換
- 關(guān)鍵字的使用:this
二、系統(tǒng)結(jié)構(gòu)設(shè)計(jì)
- CustomerView為主模塊,負(fù)責(zé)菜單的顯示和處理用戶操作
- CustomerList為Customer對(duì)象的管理模塊,內(nèi)部用數(shù)組管理一組Customer對(duì)象,并提供相應(yīng)的添加、修改、刪除和遍歷方法,供CustomerView調(diào)用
- Customer為實(shí)體對(duì)象,用來(lái)封裝客戶信息
三、鍵盤訪問(wèn)的實(shí)現(xiàn)
項(xiàng)目中提供了CMUtility.java類,可用來(lái)方便地實(shí)現(xiàn)鍵盤訪問(wèn)。
四、Customer類
Customer為實(shí)體類,用來(lái)封裝客戶信息
該類封裝客戶的以下信息:
- String name :客戶姓名
- char gender :性別
- int age :年齡
- String phone:電話號(hào)碼
- String email :電子郵箱
提供各屬性的get/set方法
提供所需的構(gòu)造器(可自行確定)
五、CustomerList類
CustomerList為Customer對(duì)象的管理模塊,內(nèi)部使用數(shù)組管理一組Customer對(duì)象
本類封裝以下信息:
Customer[] customers
:用來(lái)保存客戶對(duì)象的數(shù)組int total = 0
:記錄已保存客戶對(duì)象的數(shù)量
該類至少提供以下構(gòu)造器和方法:
public CustomerList(int totalCustomer)
public boolean addCustomer(Customer customer)
public boolean replaceCustomer(int index, Customer cust)
public boolean deleteCustomer(int index)
public Customer[] getAllCustomers()
public Customer getCustomer(int index)
public int getTotal()
六、CustomerView類
CustomerView為主模塊,負(fù)責(zé)菜單的顯示和處理用戶操作
本類封裝以下信息:
CustomerList customerList = new CustomerList(10);
創(chuàng)建最大包含10個(gè)客戶對(duì)象的CustomerList 對(duì)象,供以下各成員方法使用。
該類至少提供以下方法:
public void enterMainMenu()
private void addNewCustomer()
private void modifyCustomer()
private void deleteCustomer()
private void listAllCustomers()
public static void main(String[] args)
七、代碼
CMUtility類
package com.hsy.pack.project; import java.util.Scanner; /** ?* CMUtility工具類: ?* 將不同的功能封裝為方法,就是可以直接通過(guò)調(diào)用方法使用它的功能,而無(wú)需考慮具體的功能實(shí)現(xiàn)細(xì)節(jié)。 ?*/ public class CMUtility { ? ? private static Scanner scanner = new Scanner(System.in); ? ? /** ? ? ?* 用于界面菜單的選擇。該方法讀取鍵盤,如果用戶鍵入'1'-'5'中的任意字符,則方法返回。返回值為用戶鍵入字符。 ? ? ?*/ ? ? public static char readMenuSelection() { ? ? ? ? char c; ? ? ? ? for (; ; ) { ? ? ? ? ? ? String str = readKeyBoard(1, false); ? ? ? ? ? ? c = str.charAt(0); ? ? ? ? ? ? if (c != '1' && c != '2' && ? ? ? ? ? ? ? ? ? ? c != '3' && c != '4' && c != '5') { ? ? ? ? ? ? ? ? System.out.print("選擇錯(cuò)誤,請(qǐng)重新輸入:"); ? ? ? ? ? ? } else break; ? ? ? ? } ? ? ? ? return c; ? ? } ? ? /** ? ? ?* 從鍵盤讀取一個(gè)字符,并將其作為方法的返回值。 ? ? ?*/ ? ? public static char readChar() { ? ? ? ? String str = readKeyBoard(1, false); ? ? ? ? return str.charAt(0); ? ? } ? ? /** ? ? ?* 從鍵盤讀取一個(gè)字符,并將其作為方法的返回值。 ? ? ?* 如果用戶不輸入字符而直接回車,方法將以defaultValue 作為返回值。 ? ? ?*/ ? ? public static char readChar(char defaultValue) { ? ? ? ? String str = readKeyBoard(1, true); ? ? ? ? return (str.length() == 0) ? defaultValue : str.charAt(0); ? ? } ? ? /** ? ? ?* 從鍵盤讀取一個(gè)長(zhǎng)度不超過(guò)2位的整數(shù),并將其作為方法的返回值。 ? ? ?*/ ? ? public static int readInt() { ? ? ? ? int n; ? ? ? ? for (; ; ) { ? ? ? ? ? ? String str = readKeyBoard(2, false); ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? n = Integer.parseInt(str); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } catch (NumberFormatException e) { ? ? ? ? ? ? ? ? System.out.print("數(shù)字輸入錯(cuò)誤,請(qǐng)重新輸入:"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return n; ? ? } ? ? /** ? ? ?* 從鍵盤讀取一個(gè)長(zhǎng)度不超過(guò)2位的整數(shù),并將其作為方法的返回值。 ? ? ?* 如果用戶不輸入字符而直接回車,方法將以defaultValue 作為返回值。 ? ? ?*/ ? ? public static int readInt(int defaultValue) { ? ? ? ? int n; ? ? ? ? for (; ; ) { ? ? ? ? ? ? String str = readKeyBoard(2, true); ? ? ? ? ? ? if (str.equals("")) { ? ? ? ? ? ? ? ? return defaultValue; ? ? ? ? ? ? } ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? n = Integer.parseInt(str); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } catch (NumberFormatException e) { ? ? ? ? ? ? ? ? System.out.print("數(shù)字輸入錯(cuò)誤,請(qǐng)重新輸入:"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return n; ? ? } ? ? /** ? ? ?* 從鍵盤讀取一個(gè)長(zhǎng)度不超過(guò)limit的字符串,并將其作為方法的返回值。 ? ? ?*/ ? ? public static String readString(int limit) { ? ? ? ? return readKeyBoard(limit, false); ? ? } ? ? /** ? ? ?* 從鍵盤讀取一個(gè)長(zhǎng)度不超過(guò)limit的字符串,并將其作為方法的返回值。 ? ? ?* 如果用戶不輸入字符而直接回車,方法將以defaultValue 作為返回值。 ? ? ?*/ ? ? public static String readString(int limit, String defaultValue) { ? ? ? ? String str = readKeyBoard(limit, true); ? ? ? ? return str.equals("") ? defaultValue : str; ? ? } ? ? /** ? ? ?* 用于確認(rèn)選擇的輸入。該方法從鍵盤讀取‘Y'或'N',并將其作為方法的返回值。 ? ? ?*/ ? ? public static char readConfirmSelection() { ? ? ? ? char c; ? ? ? ? for (; ; ) { ? ? ? ? ? ? String str = readKeyBoard(1, false).toUpperCase(); ? ? ? ? ? ? c = str.charAt(0); ? ? ? ? ? ? if (c == 'Y' || c == 'N') { ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? System.out.print("選擇錯(cuò)誤,請(qǐng)重新輸入:"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return c; ? ? } ? ? private static String readKeyBoard(int limit, boolean blankReturn) { ? ? ? ? String line = ""; ? ? ? ? while (scanner.hasNextLine()) { ? ? ? ? ? ? line = scanner.nextLine(); ? ? ? ? ? ? if (line.length() == 0) { ? ? ? ? ? ? ? ? if (blankReturn) return line; ? ? ? ? ? ? ? ? else continue; ? ? ? ? ? ? } ? ? ? ? ? ? if (line.length() < 1 || line.length() > limit) { ? ? ? ? ? ? ? ? System.out.print("輸入長(zhǎng)度(不大于" + limit + ")錯(cuò)誤,請(qǐng)重新輸入:"); ? ? ? ? ? ? ? ? continue; ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return line; ? ? } }
Customer類
package com.hsy.pack.project; public class Customer { ? ? private String name; ? ? private char gender; ? ? private int age; ? ? private String phone; ? ? private String email; ? ? public Customer(String name, char gender, int age) { ? ? ? ? this(name, gender, age, "", ""); ? ? } ? ? 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 String getName() { ? ? ? ? return name; ? ? } ? ? public void setName(String name) { ? ? ? ? this.name = name; ? ? } ? ? public char getGender() { ? ? ? ? return gender; ? ? } ? ? public void setGender(char gender) { ? ? ? ? this.gender = gender; ? ? } ? ? public int getAge() { ? ? ? ? return age; ? ? } ? ? public void setAge(int age) { ? ? ? ? this.age = age; ? ? } ? ? public String getPhone() { ? ? ? ? return phone; ? ? } ? ? public void setPhone(String phone) { ? ? ? ? this.phone = phone; ? ? } ? ? public String getEmail() { ? ? ? ? return email; ? ? } ? ? public void setEmail(String email) { ? ? ? ? this.email = email; ? ? } ? ? public String getDetails() { ? ? ? ? return name + "\t" + gender + "\t" + age + "\t\t" + phone + "\t" + email; ? ? } }
CustomerList類
package com.hsy.pack.project; public class CustomerList { ? ? private Customer[] customers; ? ? private int total = 0; ? ? public CustomerList(int totalCustomer) { ? ? ? ? customers = new Customer[totalCustomer]; ? ? } ? ? public boolean addCustomer(Customer customer) { ? ? ? ? if (total >= customers.length) return false; ? ? ? ? customers[total++] = customer; ? ? ? ? return true; ? ? } ? ? public boolean replaceCustomer(int index, Customer cust) { ? ? ? ? if (index < 0 || index >= total) return false; ? ? ? ? customers[index] = cust; ? ? ? ? return true; ? ? } ? ? public boolean deleteCustomer(int index) { ? ? ? ? if (index < 0 || index >= total) return false; ? ? ? ? for (int i = index; i < total - 1; i++) { ? ? ? ? ? ? customers[i] = customers[i + 1]; ? ? ? ? } ? ? ? ? customers[--total] = null; ? ? ? ? return true; ? ? } ? ? public Customer[] getAllCustomers() { ? ? ? ? Customer[] custs = new Customer[total]; ? ? ? ? for (int i = 0; i < total; i++) { ? ? ? ? ? ? custs[i] = customers[i]; ? ? ? ? } ? ? ? ? return custs; ? ? } ? ? public int getTotal() { ? ? ? ? return total; ? ? } ? ? public Customer getCustomer(int index) { ? ? ? ? if (index < 0 || index >= total) return null; ? ? ? ? return customers[index]; ? ? } }
CustomerView類
package com.hsy.pack.project; public class CustomerView { ? ? private CustomerList customers = new CustomerList(10); ? ? public CustomerView() { ? ? ? ? Customer cust = new Customer("張三", '男', 30, "010-56253825", ? ? ? ? ? ? ? ? "abc@email.com"); ? ? ? ? customers.addCustomer(cust); ? ? } ? ? public void enterMainMenu() { ? ? ? ? boolean loopFlag = true; ? ? ? ? do { ? ? ? ? ? ? System.out ? ? ? ? ? ? ? ? ? ? .println("\n-----------------客戶信息管理軟件-----------------\n"); ? ? ? ? ? ? System.out.println(" ? ? ? ? ? ? ? ? ? 1 添 加 客 戶"); ? ? ? ? ? ? System.out.println(" ? ? ? ? ? ? ? ? ? 2 修 改 客 戶"); ? ? ? ? ? ? System.out.println(" ? ? ? ? ? ? ? ? ? 3 刪 除 客 戶"); ? ? ? ? ? ? System.out.println(" ? ? ? ? ? ? ? ? ? 4 客 戶 列 表"); ? ? ? ? ? ? System.out.println(" ? ? ? ? ? ? ? ? ? 5 退 ? ? ? 出\n"); ? ? ? ? ? ? System.out.print(" ? ? ? ? ? ? ? ? ? 請(qǐng)選擇(1-5):"); ? ? ? ? ? ? char key = CMUtility.readMenuSelection(); ? ? ? ? ? ? System.out.println(); ? ? ? ? ? ? switch (key) { ? ? ? ? ? ? ? ? case '1': ? ? ? ? ? ? ? ? ? ? addNewCustomer(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '2': ? ? ? ? ? ? ? ? ? ? modifyCustomer(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '3': ? ? ? ? ? ? ? ? ? ? deleteCustomer(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '4': ? ? ? ? ? ? ? ? ? ? listAllCustomers(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '5': ? ? ? ? ? ? ? ? ? ? System.out.print("確認(rèn)是否退出(Y/N):"); ? ? ? ? ? ? ? ? ? ? char yn = CMUtility.readConfirmSelection(); ? ? ? ? ? ? ? ? ? ? if (yn == 'Y') ? ? ? ? ? ? ? ? ? ? ? ? loopFlag = false; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? } while (loopFlag); ? ? } ? ? private void addNewCustomer() { ? ? ? ? System.out.println("---------------------添加客戶---------------------"); ? ? ? ? System.out.print("姓名:"); ? ? ? ? String name = CMUtility.readString(4); ? ? ? ? System.out.print("性別:"); ? ? ? ? char gender = CMUtility.readChar(); ? ? ? ? System.out.print("年齡:"); ? ? ? ? int age = CMUtility.readInt(); ? ? ? ? System.out.print("電話:"); ? ? ? ? String phone = CMUtility.readString(15); ? ? ? ? System.out.print("郵箱:"); ? ? ? ? String email = CMUtility.readString(15); ? ? ? ? Customer cust = new Customer(name, gender, age, phone, email); ? ? ? ? boolean flag = customers.addCustomer(cust); ? ? ? ? if (flag) { ? ? ? ? ? ? System.out ? ? ? ? ? ? ? ? ? ? .println("---------------------添加完成---------------------"); ? ? ? ? } else { ? ? ? ? ? ? System.out.println("----------------記錄已滿,無(wú)法添加-----------------"); ? ? ? ? } ? ? } ? ? private void modifyCustomer() { ? ? ? ? System.out.println("---------------------修改客戶---------------------"); ? ? ? ? int index = 0; ? ? ? ? Customer cust = null; ? ? ? ? for (; ; ) { ? ? ? ? ? ? System.out.print("請(qǐng)選擇待修改客戶編號(hào)(-1退出):"); ? ? ? ? ? ? index = CMUtility.readInt(); ? ? ? ? ? ? if (index == -1) { ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? cust = customers.getCustomer(index - 1); ? ? ? ? ? ? if (cust == null) { ? ? ? ? ? ? ? ? System.out.println("無(wú)法找到指定客戶!"); ? ? ? ? ? ? } else ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? System.out.print("姓名(" + cust.getName() + "):"); ? ? ? ? String name = CMUtility.readString(4, cust.getName()); ? ? ? ? System.out.print("性別(" + cust.getGender() + "):"); ? ? ? ? char gender = CMUtility.readChar(cust.getGender()); ? ? ? ? System.out.print("年齡(" + cust.getAge() + "):"); ? ? ? ? int age = CMUtility.readInt(cust.getAge()); ? ? ? ? System.out.print("電話(" + cust.getPhone() + "):"); ? ? ? ? String phone = CMUtility.readString(15, cust.getPhone()); ? ? ? ? System.out.print("郵箱(" + cust.getEmail() + "):"); ? ? ? ? String email = CMUtility.readString(15, cust.getEmail()); ? ? ? ? cust = new Customer(name, gender, age, phone, email); ? ? ? ? boolean flag = customers.replaceCustomer(index - 1, cust); ? ? ? ? if (flag) { ? ? ? ? ? ? System.out ? ? ? ? ? ? ? ? ? ? .println("---------------------修改完成---------------------"); ? ? ? ? } else { ? ? ? ? ? ? System.out.println("----------無(wú)法找到指定客戶,修改失敗--------------"); ? ? ? ? } ? ? } ? ? private void deleteCustomer() { ? ? ? ? System.out.println("---------------------刪除客戶---------------------"); ? ? ? ? int index = 0; ? ? ? ? Customer cust = null; ? ? ? ? for (; ; ) { ? ? ? ? ? ? System.out.print("請(qǐng)選擇待刪除客戶編號(hào)(-1退出):"); ? ? ? ? ? ? index = CMUtility.readInt(); ? ? ? ? ? ? if (index == -1) { ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? cust = customers.getCustomer(index - 1); ? ? ? ? ? ? if (cust == null) { ? ? ? ? ? ? ? ? System.out.println("無(wú)法找到指定客戶!"); ? ? ? ? ? ? } else ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? System.out.print("確認(rèn)是否刪除(Y/N):"); ? ? ? ? char yn = CMUtility.readConfirmSelection(); ? ? ? ? if (yn == 'N') ? ? ? ? ? ? return; ? ? ? ? boolean flag = customers.deleteCustomer(index - 1); ? ? ? ? if (flag) { ? ? ? ? ? ? System.out.println("---------------------刪除完成---------------------"); ? ? ? ? } else { ? ? ? ? ? ? System.out.println("----------無(wú)法找到指定客戶,刪除失敗--------------"); ? ? ? ? } ? ? } ? ? private void listAllCustomers() { ? ? ? ? System.out.println("---------------------------客戶列表---------------------------"); ? ? ? ? Customer[] custs = customers.getAllCustomers(); ? ? ? ? if (custs.length == 0) { ? ? ? ? ? ? System.out.println("沒(méi)有客戶記錄!"); ? ? ? ? } else { ? ? ? ? ? ? System.out.println("編號(hào)\t姓名\t性別\t年齡\t電話\t\t\t郵箱"); ? ? ? ? ? ? for (int i = 0; i < custs.length; i++) { ? ? ? ? ? ? ? ? System.out.println(i + 1 + "\t\t" + custs[i].getName() + "\t" + custs[i].getGender() + "\t\t" + custs[i].getAge() + "\t\t" + custs[i].getPhone() + "\t" + custs[i].getEmail()); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? System.out.println("-------------------------客戶列表完成-------------------------"); ? ? } ? ? public static void main(String[] args) { ? ? ? ? CustomerView cView = new CustomerView(); ? ? ? ? cView.enterMainMenu(); ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java基礎(chǔ)詳解之集合框架工具Collections
這篇文章主要介紹了Java基礎(chǔ)詳解之集合框架工具Collections,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-04-04springboot?filter配置多個(gè)時(shí),執(zhí)行順序問(wèn)題
這篇文章主要介紹了springboot?filter配置多個(gè)時(shí),執(zhí)行順序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Mybatis中使用updateBatch進(jìn)行批量更新
這篇文章主要介紹了Mybatis中使用updateBatch進(jìn)行批量更新的相關(guān)資料,有逐條更新,sql批量更新等,具體實(shí)例代碼大家參考下本文2018-04-04Spring Cloud Feign組成配置過(guò)程解析
這篇文章主要介紹了Spring Cloud Feign組成配置過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03java求數(shù)組元素重復(fù)次數(shù)和java字符串比較大小示例
這篇文章主要介紹了java求數(shù)組元素重復(fù)次數(shù)和java字符串比較大小示例,需要的朋友可以參考下2014-04-04MyEclipse設(shè)置Console輸出到文件的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇MyEclipse設(shè)置Console輸出到文件的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07java 直接調(diào)用python腳本,并傳遞參數(shù)代碼實(shí)例
這篇文章主要介紹了java調(diào)用python腳本傳遞參數(shù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04