java實(shí)現(xiàn)簡單的客戶信息管理系統(tǒng)
本文實(shí)例為大家分享了java實(shí)現(xiàn)簡單客戶信息管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
全篇文章開源,源碼供讀者使用。這是一篇關(guān)于java的客戶信息管理系統(tǒng)的文章,里面簡單實(shí)現(xiàn)了數(shù)據(jù)庫管理系統(tǒng)的基本功能,可以算是算筆者的學(xué)習(xí)筆記,也為大家學(xué)習(xí)提供便利。所以代碼都是在一個(gè)包下完成的,所以沒有使用導(dǎo)包的操作,省去了外賣project的申明,剩下的就寫的文章里了。話不多說,看文章吧。
首先給大家看一下總的操作界面(別看簡單,里面的還是有東西的),后面就附上實(shí)現(xiàn)源碼、要求和注釋

Customer類
下面是關(guān)于Customer類的編寫要求:
* 用來封裝客戶的以下信息
* Sting name
* int age
* char gender
* Stirng phone
* String email
* 提供getset方法
* 構(gòu)造器自行設(shè)定
public class Customer {
?? ?private String name;
?? ?private int age;
?? ?private String gender;
?? ?private String phone;
?? ?private String email;
?? ?
?? ?public Customer() {
?? ??? ?
?? ?}
?? ?
?? ?public Customer(String name,int age,String gender,String phone,String email) {
?? ??? ?this.name = name;
?? ??? ?this.age = age;
?? ??? ?this.gender = gender;
?? ??? ?this.phone = phone;
?? ??? ?this.email = email;
?? ?}
?? ?
?? ?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 getGender() {
?? ??? ?return gender;
?? ?}
?? ?public void setGender(String gender) {
?? ??? ?this.gender = gender;
?? ?}
?? ?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;
?? ?}
?? ?
}CustomerList類
下面是關(guān)于CustomerList類的編寫要求:
* CustomerList類的設(shè)計(jì)
* Customer[] 用來保存客戶信息
* int total 用來保存當(dāng)前存入客戶數(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[] getallCustomer();
* public Customer getCustomer(int index);
* public int getToal();
public class CustomerList {
?? ?private static Customer customers[];
?? ?private static int total;
?? ?
?? ?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 - 1] = null;
?? ??? ?total--;
?? ??? ?return true;
?? ?}
?? ?
?? ?// 得到所有客戶的信息
?? ?public static Customer[] getallCustomer() {
?? ??? ?Customer[] custs = new Customer[total];
?? ??? ?for(int i = 0;i < total;i++) {
?? ??? ??? ?custs[i] = customers[i];
?? ??? ?}
?? ??? ?return custs;
?? ?}
?? ?
?? ?// 得到指定客戶的信息
?? ?public Customer getCustomer(int index) {
?? ??? ?if(index < 0 || index >= total) {
?? ??? ??? ?return null;
?? ??? ?}
?? ??? ?return customers[index];
?? ?}
?? ?
?? ?// 得到Customers中所有客戶的個(gè)數(shù)
?? ?public static int getTotal() {
?? ??? ?return total;
?? ?}
?? ?
}CustomerView類
CustomerView類的編寫
* 主模塊:用于用戶界面的展示、與用戶交互
* CustomerList customerList = new CustomerList(10)
* 應(yīng)含有的構(gòu)造器和方法
* private void enterMainMenue()
* private void addNewCustomer()
* private void modifyCustomer()
* private void deleteCustomer()
* private void listAllCustomers()
* private static void main(String[] args)
public class CustomerView {
?? ?CustomerList customerList = new CustomerList(10);
?? ?public CustomerView() {
?? ??? ?Customer customer = new Customer("王龍", 20, "Male", "18965391649", "465989777@qq.com");
?? ??? ?customerList.addCustomer(customer);
?? ?}
?? ?// 用戶主菜單
?? ?private void enterMainMenue() {
?? ??? ?System.out.println("-----------------客戶信息管理系統(tǒng) --------------------");
?? ??? ?System.out.println("?? ??? ??? ??? ??? ?1-添加客戶");
?? ??? ?System.out.println("?? ??? ??? ??? ??? ?2-修改客戶");
?? ??? ?System.out.println("?? ??? ??? ??? ??? ?3-刪除客戶");
?? ??? ?System.out.println("?? ??? ??? ??? ??? ?4-客戶列表");
?? ??? ?System.out.println("?? ??? ??? ??? ??? ?5-退出");
?? ??? ?System.out.println("請(qǐng)選擇1-5:");
?? ?}
?? ?// 添加客戶
?? ?private void addNewCustomer() {
?? ??? ?// System.out.println("添加客戶的操作");
?? ??? ?System.out.println("-----------------添加客戶-------------------------");
?? ??? ?Scanner in = new Scanner(System.in);
?? ??? ?System.out.print("姓名:");
?? ??? ?String name = in.nextLine();
?? ??? ?System.out.print("年齡:");
?? ??? ?int age = in.nextInt();
?? ??? ?System.out.print("性別:");
?? ??? ?String gender = in.nextLine();
?? ??? ?System.out.print("電話:");
?? ??? ?String phone = in.nextLine();
?? ??? ?System.out.print("郵箱:");
?? ??? ?String email = in.nextLine();
?? ??? ?// 將上述數(shù)據(jù)封裝到Customer中
?? ??? ?Customer customer = new Customer(name, age, gender, phone, email);
?? ??? ?boolean isSuccess = customerList.addCustomer(customer);
?? ??? ?if (isSuccess == true) {
?? ??? ??? ?System.out.println("---------------添加完成---------------------");
?? ??? ?} else {
?? ??? ??? ?System.out.println("---------------客戶目錄已滿,添加失??!---------------------");
?? ??? ?}
?? ?}
?? ?// 修改客戶
?? ?private void modifyCustomer() {
?? ??? ?// System.out.println("修改客戶的操作");
?? ??? ?Scanner in = new Scanner(System.in);
?? ??? ?Customer cust;
?? ??? ?int num;
?? ??? ?System.out.print("請(qǐng)選擇待修改客戶的編號(hào)(-1退出):");
?? ??? ?while (true) {
?? ??? ??? ?num = in.nextInt();
?? ??? ??? ?if (num == -1) {
?? ??? ??? ??? ?return;
?? ??? ??? ?}
?? ??? ??? ?cust = customerList.getCustomer(num - 1);
?? ??? ??? ?if (cust == null) {
?? ??? ??? ??? ?System.out.print("無法找到指定客戶,請(qǐng)選擇待修改客戶的編號(hào)(-1退出):");
?? ??? ??? ?} else {
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?// 開始修改客戶信息
?? ??? ?System.out.print("姓名(" + cust.getName() + "):");
?? ??? ?String name = in.nextLine();
?? ??? ?System.out.print("性別(" + cust.getGender() + "):");
?? ??? ?String gender = in.nextLine();
?? ??? ?System.out.print("年齡(" + cust.getAge() + "):");
?? ??? ?int age = in.nextInt();
?? ??? ?System.out.print("電話(" + cust.getPhone() + "):");
?? ??? ?String phone = in.nextLine();
?? ??? ?System.out.print("郵箱(" + cust.getEmail() + "):");
?? ??? ?String email = in.nextLine();
?? ??? ?Customer cust2 = new Customer(name, age, gender, phone, email);
?? ??? ?boolean isreplace = customerList.replaceCustomer(num - 1, cust2);
?? ??? ?if (isreplace == true) {
?? ??? ??? ?System.out.println("---------------修改完成---------------------");
?? ??? ?} else {
?? ??? ??? ?System.out.println("---------------修改失敗---------------------");
?? ??? ?}
?? ?}
?? ?// 刪除用戶
?? ?private void deleteCustomer() {
?? ??? ?// System.out.println("刪除客戶的操作");
?? ??? ?Scanner in = new Scanner(System.in);
?? ??? ?int num;
?? ??? ?System.out.println("------------------刪除客戶-----------------------");
?? ??? ?while (true) {
?? ??? ??? ?System.out.println("輸入要?jiǎng)h除的客戶的序號(hào)(-1退出):");
?? ??? ??? ?num = in.nextInt();
?? ??? ??? ?if (num == -1) {
?? ??? ??? ??? ?return;
?? ??? ??? ?}
?? ??? ??? ?Customer customer = customerList.getCustomer(num - 1);
?? ??? ??? ?if (customer == null) {
?? ??? ??? ??? ?System.out.println("------------------刪除失?。?----------------------");
?? ??? ??? ??? ?return;
?? ??? ??? ?}
?? ??? ??? ?// 執(zhí)行刪除操作
?? ??? ??? ?System.out.print("是否確認(rèn)刪除(y/n):");
?? ??? ??? ?char isdelete = in.nextLine().charAt(0);
?? ??? ??? ?if (isdelete == 'y') {
?? ??? ??? ??? ?boolean is = customerList.deleteCustomer(num - 1);
?? ??? ??? ??? ?if(is) {
?? ??? ??? ??? ??? ?System.out.println("------------------刪除成功-----------------------");?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?System.out.println("------------------刪除失敗-----------------------");?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}
?? ??? ??? ?}else {
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?// 列出所有客戶信息
?? ?private void listAllCustomers() {
?? ??? ?System.out.println("---------------客戶列表--------------------");
?? ??? ?int total = CustomerList.getTotal();
?? ??? ?if (total == 0) {
?? ??? ??? ?System.out.println("沒有客戶記錄!");
?? ??? ?} else {
?? ??? ??? ?System.out.println("編號(hào)\t姓名\t性別\t電話\t\t郵箱\t");
?? ??? ??? ?Customer custs[] = CustomerList.getallCustomer();
?? ??? ??? ?for (int i = 0; i < custs.length; i++) {
?? ??? ??? ??? ?Customer cust = custs[i];
?? ??? ??? ??? ?System.out.println((i + 1) + "\t" + cust.getName() + "\t" + cust.getGender() + "\t" + cust.getPhone()
?? ??? ??? ??? ??? ??? ?+ "\t" + cust.getEmail());
?? ??? ??? ?}
?? ??? ?}
?? ??? ?System.out.println("---------------客戶列表加載完成--------------------");
?? ?}
?? ?// 主函數(shù)
?? ?public static void main(String[] args) {
?? ??? ?CustomerView view = new CustomerView();
?? ??? ?Scanner in = new Scanner(System.in);
?? ??? ?boolean isFlag = true;
?? ??? ?while (isFlag) {
?? ??? ??? ?view.enterMainMenue();
?? ??? ??? ?char menu = in.nextLine().charAt(0);
?? ??? ??? ?switch (menu) {
?? ??? ??? ?case '1':
?? ??? ??? ??? ?view.addNewCustomer();
?? ??? ??? ??? ?break;
?? ??? ??? ?case '2':
?? ??? ??? ??? ?view.modifyCustomer();
?? ??? ??? ??? ?break;
?? ??? ??? ?case '3':
?? ??? ??? ??? ?view.deleteCustomer();
?? ??? ??? ??? ?break;
?? ??? ??? ?case '4':
?? ??? ??? ??? ?view.listAllCustomers();
?? ??? ??? ??? ?break;
?? ??? ??? ?case '5':
?? ??? ??? ??? ?System.out.print("確認(rèn)是否退出(y/n):");
?? ??? ??? ??? ?char flag = in.nextLine().charAt(0);
?? ??? ??? ??? ?if (flag == 'y') {
?? ??? ??? ??? ??? ?isFlag = false;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?// isFlag = false;
?? ??? ?}
?? ??? ?in.close();
?? ?}
}以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
eclipse構(gòu)建和發(fā)布maven項(xiàng)目的教程
這篇文章主要為大家詳細(xì)介紹了eclipse構(gòu)建和發(fā)布maven項(xiàng)目的教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Java通過 Socket 實(shí)現(xiàn) TCP服務(wù)端
這篇文章主要介紹了Java通過 Socket 實(shí)現(xiàn) TCP服務(wù)端的相關(guān)資料,需要的朋友可以參考下2017-05-05
Spring 源碼解析CommonAnnotationBeanPostProcessor
這篇文章主要為大家介紹了Spring 源碼解析CommonAnnotationBeanPostProcessor示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
分析Java并發(fā)編程之信號(hào)量Semaphore
Semaphore一般譯作信號(hào)量,它也是一種線程同步工具,主要用于多個(gè)線程對(duì)共享資源進(jìn)行并行操作的一種工具類。它代表了一種許可的概念,是否允許多線程對(duì)同一資源進(jìn)行操作的許可,使用Semaphore可以控制并發(fā)訪問資源的線程個(gè)數(shù)2021-06-06
springboot使用webservice發(fā)布和調(diào)用接口的實(shí)例詳解
本文介紹了如何在Springboot中使用webservice發(fā)布和調(diào)用接口,涵蓋了必要的依賴添加和代碼示例,文中提供了服務(wù)端和客戶端的實(shí)現(xiàn)方法,以及如何設(shè)置端口和服務(wù)地址,幫助讀者更好地理解和應(yīng)用Springboot結(jié)合webservice的技術(shù)2024-10-10
java.net.UnknownHostException異常的一般原因及解決步驟
關(guān)于java.net.UnknownHostException大家也許都比較熟悉,這篇文章主要給大家介紹了關(guān)于java.net.UnknownHostException異常的一般原因及解決步驟,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02

