客戶信息管理系統

来源:https://www.cnblogs.com/jsccc520/archive/2019/04/28/10787311.html
-Advertisement-
Play Games

這幾天在家,複習了了一下 Java SE ,到面向對象那邊找了個簡單數組項目做了一下,還是有收穫的。 只為記錄,好記性不如爛筆頭 有誤請指正 ありがとうございます。 我的公眾號 作者:晨鐘暮鼓c個人微信公眾號:程式猿的月光寶盒 1.首先,項目是客戶信息管理系統,需求如下: 2.涉及知識點 Ø 類結構 ...


這幾天在家,複習了了一下 Java-SE ,到面向對象那邊找了個簡單數組項目做了一下,還是有收穫的。

只為記錄,好記性不如爛筆頭

有誤請指正

ありがとうございます。

我的公眾號

作者:晨鐘暮鼓c
個人微信公眾號:程式猿的月光寶盒

1.首先,項目是客戶信息管理系統,需求如下:

圖片

圖片

圖片


圖片


圖片


圖片

2.涉及知識點

Ø 類結構的使用:屬性、方法及構造器

Ø 對象的創建與使用

Ø 類的封裝性

Ø 聲明和使用數組

Ø 數組的插入、刪除和替換

Ø 關鍵字的使用:this

3.源碼

實體類:

Customer.java

package pers.jsc.customer.pojo;

/**
 * @author 金聖聰
 * @title: Customer
 * @projectName CustomerInfo
 * @description: 實體對象,用來封裝客戶信息
 * @date 2019/4/2718:28
 */
public class Customer {
    /**
     * 客戶姓名
     */
    private String name;
    /**
     * 性別
     */
    private char gender;
    /**
     * 年齡
     */
    private int age;
    /**
     * 電話號碼
     */
    private String phone;
    /**
     * 電子郵箱
     */
    private String 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;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "name='" + name + '\'' +
                ", gender=" + gender +
                ", age=" + age +
                ", phone='" + phone + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

控制類:

CustomerList.java

package pers.jsc.customer.control;

import pers.jsc.customer.pojo.Customer;

/**
 * @author 金聖聰
 * @title: CustomerList
 * @projectName CustomerInfo
 * @description: Customer對象的管理模塊,
 *              內部用數組管理一組Customer對象,
 *              並提供相應的添加、修改、刪除和遍歷方法,
 *              供CustomerView調用
 * @date 2019/4/2718:30
 */
public class CustomerList {
    /**
     * 保存客戶對象的數組
     */
    private Customer[] customers;
    /**
     * 記錄已保存客戶對象的數量
     */
    private int total = 0;

    /**
     * @description 構造器,用來初始化customers數組
     * @param totalCustomer:指定customers數組的最大空間
     */
    public CustomerList(int totalCustomer){
        customers = new Customer[totalCustomer];
    }

    /**
     * @description 將參數customer添加到數組中最後一個客戶對象記錄之後
     * @param customer 指定要添加的客戶對象
     * @return 添加成功返回true;false表示數組已滿,無法添加
     */
    public boolean addCustomer(Customer customer){
        //判斷數組滿沒滿
        if (customers[customers.length-1] == null) {
            int i;
            for (i = 0 ; i < customers.length ; i++){
                if (customers[i] == null) {
                    customers[i] = customer;
                    break;
                }
            }
            total += 1;
            return true;
        }else{
            return false;
        }
    }

    /**
     * @description 用參數customer替換數組中由index指定的對象
     * @param index 指定所替換對象在數組中的位置,從0開始
     * @param cust 指定替換的新客戶對象
     * @return 替換成功返回true;false表示索引無效,無法替換
     */
    public boolean replaceCustomer(int index, Customer cust){
        if (index < 0 || index >= total) {
            return false;
        }
        //遍曆數組,找到對應下標後替換
        for (int i = 0; i < total ; i++) {
            if ( i == index ){
                customers[i] =cust;
                //跳出所在迴圈(for)
                break;
            }
        }
        return true;
    }

    /**
     * @description 從數組中刪除參數index指定索引位置的客戶對象記錄
     * @param index 指定所刪除對象在數組中的索引位置,從0開始
     * @return 刪除成功返回true;false表示索引無效,無法刪除
     */
    public boolean deleteCustomer(int index){
        if(index < 0 || index >= total){
            return false;
        }
        //total -1 是因為 不-1 ,i+1 就要 = total
        for (int i = index; i < total -1; i++) {
            customers[i] = customers[i+1];
        }

        customers[total-1] = null;
        total --;
        return true;
    }

    /**
     * @description 返回數組中記錄的所有客戶對象
     * @return 數組中包含了當前所有客戶對象,該數組長度與對象個數相同
     */
    public Customer[] getAllCustomers(){
        //新建一個數組,用來copy
        Customer[] newCustomers = new Customer[total];
        int i;
        //copy
        for (i = 0 ; i < total ; i++){
            newCustomers[i] = customers[i];
        }
        return newCustomers;
    }

    /**
     *
     * @param index 指定所要獲取的客戶在數組中的索引位置,從0開始
     * @return 封裝了客戶信息的Customer對象
     */
    public Customer getCustomer(int index){
        //判斷形參是否符合標準
        if (index < 0 || index >= total){
            //不符合
            return null;
        }else {
            int i;
            for (i = 0 ; i < total ; i++) {
                //下標相等,返回這個對象
                if (i == index) {
                    return customers[i];
                }
            }
        }
        return null;
    }

    /**
     *
     * @return 返保存客戶對象的數量
     */
    public int getTotal(){
        //this 直接返回這個類裡面的total欄位
        return this.total;
    }

}

視圖類:

CustomerView.java

package pers.jsc.customer.views;

import pers.jsc.customer.control.CustomerList;

import pers.jsc.customer.pojo.Customer;
import pers.jsc.customer.utils.CMUtility;

/**
 * @author 金聖聰
 * @title: CustomerView
 * @projectName CustomerInfo
 * @description: 為主模塊,負責菜單的顯示和處理用戶操作
 * @date 2019/4/2718:29
 */
public class CustomerView {
    /**
     * 創建最大包含10個客戶對象的CustomerList 對象,供以下各成員方法使用
     */
    private CustomerList customerList = new CustomerList(10);

    /**
     * 顯示主菜單,響應用戶輸入,
     * 根據用戶操作分別調用其他相應的成員方法(如addNewCustomer),
     * 以完成客戶信息處理
     */
    private void enterMainMenu() {
        boolean flag = true;

        do {
            System.out.println("-----------------客戶信息管理軟體-----------------");
            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 readMenuSelection = CMUtility.readMenuSelection();
            switch (readMenuSelection) {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomers();
                    break;
                case '5':
                    flag = isExit();
                    break;
                default:
                    System.out.println("Input Error!");
            }
        } while (flag);
    }

    /**
     * 添加客戶
     */
    private void addNewCustomer() {
        boolean flag;
        do {
            System.out.println("---------------------添加客戶---------------------");
            Customer customer = new Customer();
            System.out.print("姓名:");
            String userName = CMUtility.readString(5, null);
            customer.setName(userName);

            System.out.print("性別:");
            String userGender = CMUtility.readString(5, null);
            if (userGender != null){
                customer.setGender(userGender.charAt(0));
            }
            System.out.print("年齡:");
            int userAge = CMUtility.readInt(0);
            customer.setAge(userAge);

            System.out.print("電話:");
            String userPhone = CMUtility.readString(11, null);
            customer.setPhone(userPhone);

            System.out.print("郵箱:");
            String userEmail = CMUtility.readString(100, null);
            customer.setEmail(userEmail);

            //判斷輸入是否有空
            if (userName == null || userGender == null || userAge == 0 || userPhone == null || userEmail == null) {
                flag = true;
                System.out.println("關鍵信息不能為空");
                //跳過本次do-while迴圈,進入下一次
                continue;
            }

            //是否添加成功
            boolean isSuccess = customerList.addCustomer(customer);
            if (isSuccess) {
                System.out.println("---------------------添加完成---------------------\n");
                flag = false;
            } else {
                //添加失敗
                System.out.println("數組已滿,無法添加");
                flag = false;
            }
        } while (flag);


    }

    /**
     * 修改客戶
     */
    private void modifyCustomer() {
        System.out.println("---------------------修改客戶---------------------");
        boolean flag;
        do {
            System.out.print("請選擇待修改客戶編號(-1退出):");
            String nums = CMUtility.readString(1000, null);
            if (nums != null) {
                //String to int
                if (Integer.parseInt(nums) == -1) {
                    System.out.println();
                    return;
                }
                int num = Integer.parseInt(nums) - 1;
                Customer customer = customerList.getCustomer(num);
                if (customer != null) {

                    System.out.print("姓名(" + customer.getName() + "):");
                    String userName = CMUtility.readString(5, customer.getName());
                    customer.setName(userName);


                    System.out.print("性別(" + customer.getGender() + "):");
                    String userGender = CMUtility.readString(5, customer.getGender()+"");
                    customer.setGender(userGender.charAt(0));

                    System.out.print("年齡(" + customer.getAge() + "):");
                    int userAge = CMUtility.readInt(customer.getAge());
                    customer.setAge(userAge);

                    System.out.print("電話(" + customer.getPhone() + "):");
                    String userPhone = CMUtility.readString(11, customer.getPhone());
                    customer.setPhone(userPhone);

                    System.out.print("郵箱(" + customer.getEmail() + "):");
                    String userEmail = CMUtility.readString(100, customer.getEmail());
                    customer.setEmail(userEmail);

                    //是否修改成功
                    boolean isRep = customerList.replaceCustomer(num, customer);
                    if (isRep) {
                        System.out.println("---------------------修改完成---------------------\n");
                        flag = false;
                    } else {
                        //失敗
                        System.out.println("索引無效,無法替換");
                        flag = true;
                    }
                } else {
                    System.out.println("查無此人。");
                    flag = false;
                }
            } else {
                System.out.print("您沒有輸入任何編號,請重新輸入!");
                flag = true;
            }
        } while (flag);
    }

    /**
     * 刪除客戶
     */
    private void deleteCustomer() {
        System.out.println("---------------------刪除客戶---------------------");
        boolean flag;
        do {
            System.out.print("請選擇待刪除客戶編號(-1退出):");
            String nums = CMUtility.readString(1000, null);
            //有輸入
            if (nums != null) {
                //String to int
                if (Integer.parseInt(nums) == -1) {
                    System.out.println();
                    return;
                }
                int num = Integer.parseInt(nums) - 1;
                Customer customer = customerList.getCustomer(num);
                //不為空,有值
                if (customer != null) {
                    System.out.print("確認是否刪除(Y/N):");
                    char isDeletes = CMUtility.readConfirmSelection();
                    switch (isDeletes) {
                        case 'Y':
                            boolean isDelete = customerList.deleteCustomer(num);
                            flag = !isDelete;
                            if (isDelete) {
                                System.out.println("---------------------刪除完成---------------------\n");
                            }
                            break;
                        default:
                            System.out.println("您沒有刪除任何客戶數據,請重新輸入!");
                            flag = true;
                    }
                } else {
                    //為空,沒值
                    System.out.println("查無此人。");
                    flag = true;
                }
            } else {
                //無輸入
                System.out.print("您沒有輸入任何編號,請重新輸入!");
                flag = true;
            }
        } while (flag);


    }

    /**
     * 客戶列表
     */
    private void listAllCustomers() {
        System.out.println("---------------------------客戶列表---------------------------");
        System.out.println("編號  姓名     性別    年齡   電話           郵箱");
        int i;
        Customer[] customers = customerList.getAllCustomers();
        for (i = 0; i < customerList.getTotal(); i++) {
            System.out.println((i + 1) + "    " +
                    customers[i].getName() +
                    "    " + customers[i].getGender() +
                    "      " + customers[i].getAge() +
                    "    " + customers[i].getPhone() +
                    "    " + customers[i].getEmail()
            );
        }
        System.out.println("-------------------------客戶列表完成--------------------------");
    }

    /**
     * @return true Exit,false Continue
     */
    private boolean isExit() {
        System.out.print("確認是否退出(Y/N):");
        char readConfirmSelection = CMUtility.readConfirmSelection();
        switch (readConfirmSelection) {
            case 'Y':
                System.out.println("ありがとうございます\nではまた");
                return false;
            default:
                return true;
        }
    }


    public static void main(String[] args) {
        CustomerView customerView = new CustomerView();
        customerView.enterMainMenu();
    }
}


工具類:

CMUtility.java

package pers.jsc.customer.utils;


import java.util.*;
/**
CMUtility工具類:將不同的功能封裝為方法,就是可以直接通過調用方法使用它的功能,而無需考慮具體的功能實現細節。
*/
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("選擇錯誤,請重新輸入:");
            } else {
                break;
            }
        }
        return c;
    }
    /**
     * 從鍵盤讀取一個字元,並將其作為方法的返回值。
     */
    public static char readChar() {
        String str = readKeyBoard(1, false);
        return str.charAt(0);
    }
    /**
    * 從鍵盤讀取一個字元,並將其作為方法的返回值。
    * 如果用戶不輸入字元而直接回車,方法將以defaultValue 作為返回值。
    */
    public static char readChar(char defaultValue) {
        String str = readKeyBoard(1, true);
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }
    /**
     *從鍵盤讀取一個長度不超過2位的整數,並將其作為方法的返回值。
     */
    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("數字輸入錯誤,請重新輸入:");
            }
        }
        return n;
    }
    /**
     * 從鍵盤讀取一個長度不超過2位的整數,並將其作為方法的返回值。
     * 如果用戶不輸入字元而直接回車,方法將以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("數字輸入錯誤,請重新輸入:");
            }
        }
        return n;
    }
    /**
     * 從鍵盤讀取一個長度不超過limit的字元串,並將其作為方法的返回值。
     */
    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }
    /**
     * 從鍵盤讀取一個長度不超過limit的字元串,並將其作為方法的返回值。
     * 如果用戶不輸入字元而直接回車,方法將以defaultValue 作為返回值。
     */
    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }
    /**
     *用於確認選擇的輸入。該方法從鍵盤讀取‘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("選擇錯誤,請重新輸入:");
            }
        }
        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("輸入長度(不大於" + limit + ")錯誤,請重新輸入:");
                continue;
            }
            break;
        }

        return line;
    }
}

4.項目結構

圖片

圖片

以上。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 頁面內容 ...
  • 一、簡介 實際引用中,有些公司在不同地區會設置不同伺服器,因此就需要用到nginx以實現負載均衡,這時,將session數據保存至資料庫就成為了需要面對的問題,我們以MySQL資料庫為例,看看他是如何將數據上傳至mysql中的。在npm上有一個叫做:express-mysql-session的模塊, ...
  • 一、關於session session是另一種記錄客戶狀態的機制,與cookie保存在客戶端瀏覽器不同,session保存在伺服器當中;當客戶端訪問伺服器時,伺服器會生成一個session對象,對象中保存的是key:value值,同時伺服器會將key傳回給客戶端的cookie當中;當用戶第二次訪問服 ...
  • 前言 該筆記只是為了記錄以前開發使用的方式。 處理命名空間namespace extend及base命名下的常用方法 base下的DOM操作 base下的event事件 base下的array數組 base下的string字元串 base下的cookie操作 base下的date日期操作 base下 ...
  • 在前端學習過程中,涉及到路徑的問題非常多,相對路徑,絕對路徑等。有時候明明覺得沒問題,但是還是會出錯。或者說線下沒問題,但是到了線上就出現問題,因此弄懂路徑問題,非常關鍵。我們需要知道為什麼這個地方既可以使用相對路徑,又可以使用絕對路徑,為什麼有些地方只能使用絕對路徑。 一、Node.js中載入模塊 ...
  • 實驗環境:docker + openresty 我限制的5秒鐘內允許訪問兩次效果圖: default.conf 代碼如下: ...
  • 一、封裝 封裝:是面向對象方法的重要原則,就是把對象的屬性和行為(數據)結合為一個獨立的整體,並儘可能隱藏對象的內部實現細節,就是把不想告訴或者不該告訴別人的東西隱藏起來,把可以告訴別人的公開,別人只能用我提供的功能實現需求,而不知道是如何實現的。增加安全性 以上 Person 類封裝 name、g ...
  • 文章首發: "結構型模式:裝飾模式" 七大結構型模式之四:裝飾模式。 簡介 姓名 :裝飾模式 英文名 :Decorator Pattern 價值觀 :人靠衣裝,類靠裝飾 個人介紹 : Attach additional responsibilities to an object dynamicall ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...