java實現客戶信息管理系統

本文實例為大傢分享瞭java實現客戶信息管理系統的具體代碼,供大傢參考,具體內容如下

一、CMUtility工具類

講不同的功能封裝為方法,就是可以直接通過調用方法使用它的功能,而無需考慮具體的功能實現細節。

代碼如下:

package com.p2.util;

import java.util.*;

/**
 * 工具類
 * 講不同的功能封裝為方法,就是可以直接通過調用方法使用它的功能,而無需考慮具體的功
 * 能實現細節。
 * 
 **/
public class CMUtility {
 private static Scanner scanner = new Scanner(System.in);
 /**
 * 用於界面菜單的選擇,該方法讀取鍵盤,如果用戶鍵入"1"-"5"中的任意字符,則方法返回。
 * 返回值為用戶選擇的數字。
 **/
 public static char readMenuSelection(){
 char c;
 for (; ; ){
 String str = readaKeyBoard(1, false);
 c = str.charAt(0);
 if (c != '1' && c!= '2' && c!= '3' && c!= '4'
 && c!= '5'){
 System.out.println("選擇錯誤,請重新輸入:");
 }else break;
 }
 return c;
 }
 /**
 * 從鍵盤讀取一個字符,並將其作為方法的返回值。
 **/
 public static char readChar(){
 String str = readaKeyBoard(1, false);
 return str.charAt(0);
 }
 /**
 * 從鍵盤讀取一個字符,並將其作為方法的返回值。
 * 如果用戶不輸入字符而直接回車,方法將以defaultValue 作為返回值。
 **/
 public static char readChar(char defaultValue){
 String str = readaKeyBoard(1, true);
 return (str.length() == 0) ? defaultValue :str.charAt(0);
 }
 /**
 * 從鍵盤讀取一個長度不超過2位的整數,並將其作為方法的返回值。
 **/
 public static int readInt(){
 int n;
 for (; ; ){
 String str = readaKeyBoard(2, false);
 try{
 n = Integer.parseInt(str);
 break;
 }catch(NumberFormatException e){
 System.out.println("數字輸入錯誤,請重新輸入:");
 }
 }
 return n;
 }
 /**
 * 從鍵盤讀取一個長度不超過2位的整數,並將其為方法的返回值。
 * 如果用戶不輸入字符而直接回車,方法將以defaultValue 作為返回值。
 **/
 public static int readInt(int defaultValue){
 int n;
 for(; ; ){
 String str = readaKeyBoard(2, true);
 if (str.equals("")){
 return defaultValue;
 }
 try{
 n = Integer.parseInt(str);
 break;
 }catch(NumberFormatException e){
 System.out.println("數字輸入錯誤,請重新輸入:");
 }
 }
 return n;
 }
 /**
 * 從鍵盤 讀取一個長讀不超過limit的字符串,並將其作為方法的返回值。
 **/
 public static String readString (int limit){
 return readaKeyBoard(limit,false);
 }
 
 public static String readString (int limit, String defaultValue){
 return readaKeyBoard(limit,false);
 }
 /**
 * 從鍵盤讀取一個瘡毒不超過limit的字符串,將其作為方法的返回值。
 * 如果用戶不輸入字符而直接回車,方法以defaultValue作為返回值。
 **/
 public static char readComfirmSelection(){
 char c;
 for (; ; ){
 String str = readaKeyBoard(1, false).toUpperCase();
 c = str.charAt(0);
 if (c == 'Y' || c == 'N'){
 break;
 }else{
 System.out.println("選擇錯誤,請重新輸入:");
 }
 }
 return c;
 }
 private static String readaKeyBoard(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.println("輸入長度(不大於" + limit + ")錯誤,請重新輸入:");
 continue;
 }
 break;
 }
 return line;
 }
}

二、Customer

代碼如下:

package com.p2.bean;
/**
 * 把對象封裝到方法裡面
 * 
 * 
 **/
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, String phone, String email) {
 this.name = name;
 this.gender = gender;
 this.age = age;
 this.phone = phone;
 this.email = email;
 }
 public Customer() {
 
 }
 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;
 }
 

}

三、CustomerList

代碼如下:

package com.p2.service;
/**
 * 最主要的
 * 
 **/
import com.p2.bean.Customer;

public class CustomerList {
 private Customer[] customers;
 private int total = 0;
 //構造器
 /**
 * 用來初始化customers數組的構造器
 * @param totalCustomer: 指定數組的長度
 **/
 public CustomerList (int totalCustomer){//給正在創建的對象進行初始化
 customers = new Customer[totalCustomer];
 }
 //方法 
 /**
 * 將指定的客戶添加到數組中
 * @param customer
 * @return成功true,失敗:false
 **/
 public boolean addCustomer(Customer customer){//增
 if(total < 0 || total >= customers.length){
 return false;
 }
 customers[total++] = customer;
 return true;
 }
 /**
 * 修改指定位置的客戶信息
 * @param index
 * @param cust
 * @return
 **/
 public boolean replaceCustomer(int index, Customer cust){//改
 if(index <0 || index >= total){
 return false;
 }
 customers[index] = cust;
 return true;
 }
 /**
 * 刪除指定位置上的客戶
 * @param index
 * @return
 **/
 public boolean deleteCustsomer(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 --;
 customers[-- total] = null;
 return true;
 
 }
 /**
 * 獲得所有的客戶信息
 * @return
 **/
 public Customer[] getAllCustomers() {//查詢所有
 Customer[] custs = new Customer[total];
 for(int i = 0; i < total; i++){
 custs[i] = customers[i];
 }
 return custs;
 }
 /**
 * 獲取指定位置上的客戶信息
 * @param index
 * @return
 **/
 public Customer getCustomer(int index) {//查詢一個
 if(index < 0 || index >= total){
 return null;
 }
 return customers[index];
 }
 //封裝
 /**
 * 獲取存儲客戶的的數量
 * @return
 **/
 public int getTotal() {//統計數據
 return total;
 }
}

四、CustomerView

代碼如下:

package com.p2.ui;

import com.p2.bean.Customer;
import com.p2.service.CustomerList;
import com.p2.util.CMUtility;

/**
 * 
 * 用戶交互
 * 
 * 
 * 
 **/
public class CustomerView {
 private CustomerList customerList = new CustomerList(100);
 
 public CustomerView(){
 Customer customer = new Customer("王濤",'男', 23, "13213123","[email protected]");
 customerList.addCustomer(customer);
 }
 /**
 * 顯示《客戶信息管理軟件》
 **/
 public void enterMainMenu(){
 
 boolean isFlag = true;
 while(isFlag){
 System.out.println("\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(" 請選擇(1-5):");
 
 char menu = CMUtility.readMenuSelection();
 switch(menu){
 case '1':
 addNewCustomer();
 break;
 case '2':
 modifyCustomer();
 break;
 case '3':
 deleteCustomer();
 break;
 case '4':
 listAllCustomers();
 
 break;
 case '5':
// System.out.println("退出");
 
 System.out.println("確認是否退出(Y/N):");
 char isExit = CMUtility.readComfirmSelection();
 if(isExit =='Y'){
 isFlag = false;
 }
// break;
 }
 
// isFlag = false;
 }
 
 }
 /**
 * 添加客戶
 * 
 **/
 private void addNewCustomer(){
 System.out.println("------添加客戶------");
 System.out.print("姓名:");
 String name = CMUtility.readString(10);
 System.out.print("性別:");
 char gender = CMUtility.readChar();
 System.out.print("年齡:");
 int age = CMUtility.readInt();
 System.out.print("電話:");
 String phone = CMUtility.readString(13);
 System.out.print("郵箱:");
 String email = CMUtility.readString(30);
 
 //將上述數據封裝到對象中
 Customer customer = new Customer(name, gender, age, phone, email);
 
 boolean isSucccess = customerList.addCustomer(customer);
 if(isSucccess){
 System.out.println("------添加完成------");
 }else{
 System.out.println("------客戶已滿------");
 } 
 
 }
 /**
 * 修改客戶
 **/
 private void modifyCustomer(){
 System.out.println("---------修改客戶-------");
 Customer cust;
 int number;
 for(;;){
 System.out.print("選擇要修改的客戶編號(-1退出):");
 number = CMUtility.readInt();
 
 if (number == -1){
 return;
 }else{
 cust = customerList.getCustomer(number - 1);
 if(cust == null){
 System.out.println("無法找到指定客戶!!");
 }else{//找到相應的客戶
 break;
 }
 }
 }
 //修改客戶信息
 System.out.print("姓名:(" + cust.getName() + "):");
 String name = CMUtility.readString(10, 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(13, cust.getPhone());
 System.out.print("郵箱:(" + cust.getEmail() + "):");
 String email = CMUtility.readString( 30,cust.getEmail());
 
 Customer newcust = new Customer(name, gender, age, phone, email);
 
 boolean isRepalaced = customerList.replaceCustomer(number - 1, newcust);
 if(isRepalaced){
 System.out.println("修改成功!!");
 }else{
 System.out.println("修改失敗!!");
 }
 
 }
 /**
 * 刪除客戶
 **/
 private void deleteCustomer(){
 System.out.println("-------刪除客戶-------");
 int number;
 for(;;){
 System.out.print("請選擇呆刪除客戶的編號(-1退出):");
 number = CMUtility.readInt();
 
 if (number ==-1){
 return;
 }
 Customer customer =customerList.getCustomer(number -1);
 if (customer == null){
 System.out.println("無法找到指定客戶!!");
 }else{
 break;
 }
 }
 //找到客戶瞭
 
 System.out.println("是否確認刪除(Y/N):");
 char isDelete = CMUtility.readComfirmSelection(); 
 if(isDelete == 'Y'){
 boolean deleteSuccess = customerList.deleteCustsomer(number - 1);
 if(deleteSuccess ){
 System.out.println("-----------刪除成功---------");
 }else{
 System.out.println("-----------刪除失敗---------");
 }
 }else{
 return;
 }
 }
 /**
 * 顯示客戶列表
 **/
 private void listAllCustomers(){
// System.out.println("顯示客戶");
 System.out.println("---------客戶列表-------");
 int total = customerList.getTotal();
 if(total == 0){
 System.out.println("沒有客戶記錄!");
 }else{
 System.out.println("編號\t姓名\t性別\t年齡\t電話\t\t郵箱");
 Customer[] custs = customerList.getAllCustomers();
 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.getAge() +
 "\t" + cust.getPhone() + "\t" + cust.getEmail());
 }
 }
 
 
 System.out.println("---------客戶列表結束-------");
 }
 
 public static void main(String[] args) {
 CustomerView view = new CustomerView();
 view.enterMainMenu();
 }

}

五、運行結果

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: