1. 程式人生 > >project--客戶資訊管理系統

project--客戶資訊管理系統

軟體設計分析

  該軟體有三個模組組成:Customer  CustomerList  CustomerView

  Customer 為實體物件,用來封裝客戶資訊;

  CustomerList 為 Customer 物件的管理模組,內部用陣列管理一組Customer 物件,並提供相應的增加,刪除,修改和遍歷方法,功CustomerView 呼叫;

  CustomerView 為主模組,負責選單的顯示和處理使用者操作;

一、Customer 類的設計

  1、該類封裝客戶的資訊

    String name ; 客戶姓名

    String gender ; 性別

    ing age ; 年齡

    String phone ; 電話號碼

    String email ; 電子郵箱

  2、提供個屬性的get\set 方法

  3、提供所需要的構造器

  4、程式碼

package com.xuanxiao.customer;

/**
 * @see Customer為實體類,用來封裝客戶資訊
 * @author mn Email:[email protected]
 * @version
 * @data 2020年11月11日
 */

public class Customer {
    
    private String name; //客戶姓名
    private String gender; //性別
    private int age; //年齡
    private String phone; //電話號碼
    private String email; //電子郵箱
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String 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 Customer() {
    }
    
    public Customer(String name, String gender, int age, String phone, String email) {
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.phone = phone;
        this.email = email;
    }
        
}

二、CustomerList 類的設計

  1、本類封裝以下資訊

    Customer[] customers; 用來儲存客戶物件的陣列

    int total ; 記錄已儲存客戶物件的數量

  2、提供以下構造器和方法

    public CustomerList(int totalCustomer)  ;用來初始化customers陣列的構造器

    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()  ;獲取儲存的客戶的數量

  3、程式碼

package com.xuanxiao.server;

import com.xuanxiao.customer.Customer;

/**
 * @see CustomerList是Customer物件的管理模組,內部使用Customer陣列,並提供相應的新增,刪除,修改和遍歷操作。
 * @author mn Email:[email protected]
 * @version
 * @data 2020年11月11日
 *
 */


public class CustomerList {

    private Customer[] customers; //用來儲存客戶物件的陣列
    private int total; //記錄已儲存客戶物件的數量
    
    /**
     * @see 用來初始化customers陣列的構造器
     * @param totleCoustmer:制定陣列的長度的引數
     */
    public CustomerList(int totalCustomer) {
        customers = new Customer[totalCustomer];
    }
    /**
     * @see 將指定的客戶新增到陣列中
     * @author mn
     * @param customer
     * @return true:新增成功 false:新增失敗
      */
    public boolean addCustomer(Customer customer) {
        
        if(total >= customers.length) {
            return false;
        }
        customers[total] = customer;
        total ++;
        
        //或者 customers[total++] = customer;
        return true;
    }
    
    /**
     * @see 修改指定索引位置的客戶資訊
     * @author mn
     * @param index
     * @param cust
     * @return true:修改成功 false:修改失敗
     */
    public boolean replaceCustomer(int index,Customer cust) {
        
        if(index < 0 || index >= total) {
            return false;
        }else {
            customers[index] = cust;
            return true;
        }
    }
    
    /**
     * @see 刪除指定位置的客戶
     * @author mn
     * @param index
     * @return true:刪除成功 false:刪除失敗
     */
    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 --;
        //或者 customers[--total] = null;
        return true;
    }
    
    /**
     * @see 獲取所有的客戶資訊
     * @author mn
     * @return
     */
    public Customer[] getAllCustomers() {
        
        Customer[] custs = new Customer[total];
        for(int i = 0;i < total;i++) {
            custs[i] = customers[i];
        }
        return custs;
    }
    
    /**
     * @see 獲取指定索引位置上的客戶
     * @author mn
     * @param index
     * @return 找到元素返回;未找到返回null
     */
    public Customer getCustomer(int index) {
        
        if(index < 0 || index >= total) {
            return null;
        }
        return customers[index];
        
    }
    
    /**
     * @see 獲取儲存的客戶的數量
     * @author mn
     * @return
     */
    public int getTotal() {
        return total;
    }
 }

三、CustomerView 類的設計

  1、本類封裝以下的資訊

    CustomerList customerList = new CustomerList[10] ;

  2、提供以下的方法和構造器

    public void enterMainView()  ;顯示選單

    public void addNewCustomer()  ;增加客戶資訊

    public void modifyCustomer()  ;修改客戶資訊

    public void deleteCustomer()  ;刪除客戶資訊

    public void listAllCustomers()  ;遍歷客戶資訊

    public static void main(String[] args)  ;主函式

  3、程式碼

package com.xuanxiao.view;

import java.util.Scanner;

import com.xuanxiao.customer.Customer;
import com.xuanxiao.server.CustomerList;

/**
 * @see CustomerView為主模組,負責選單的顯示和處理使用者操作
 * @author mn Email:[email protected]
 * @version
 * @data 2020年11月11日
 */
public class CustomerView {

    private CustomerList customerList = new CustomerList(10);
    
    public CustomerView() {
        //Customer customer = new Customer("馬成功", '男', 21, "17345625812", "[email protected]");
        //customerList.addCustomer(customer);
    }

    /**
     * @see 顯示選單
     */
    public void enterMainView() {

        boolean isFlag = true;
        while (isFlag) {
            System.out.println("----------客戶資訊管理軟體----------");
            System.out.println("-           1. 新增客戶                     -");
            System.out.println("-           2. 修改客戶                     -");
            System.out.println("-           3. 刪除客戶                     -");
            System.out.println("-           4. 客戶列表                     -");
            System.out.println("-           5. 退出系統                     -");
            System.out.println("-----------------------------------");
            System.out.println();
            System.out.print(" 請選擇(1-5):");

            Scanner input = new Scanner(System.in);
            int choose;
            choose = input.nextInt();
            if (choose != 1 && choose != 2 && choose != 3 && choose != 4 && choose != 5) {
                System.out.println("輸入資訊有誤,請重新輸入!");
            }
            switch (choose) {
            case 1:
                addNewCustomer();
                break;
            case 2:
                modifyCustomer();
                break;
            case 3:
                deleteCustomer();
                break;
            case 4:
                listAllCustomers();
                break;
            case 5:
                System.out.print("確定是否退出(退出11/不退00):");
                Scanner input1 = new Scanner(System.in);
                int exit;
                exit = input1.nextInt();
                if (exit == 11) {
                    isFlag = false;
                }
            }

        }

    }

    /**
     * @see 新增客戶的操作
     */
    public void addNewCustomer() {
//        System.out.println("新增客戶的操作");
        System.out.println("-----------------客戶列表------------------");
        Scanner scanner = new Scanner(System.in);
        System.out.print("姓名:");
        String name;
        name = scanner.nextLine();
        System.out.print("性別:");
        String gender;
        gender = scanner.nextLine();
        System.out.print("年齡:");
        Scanner scanner1 = new Scanner(System.in);
        int age;
        age = scanner1.nextInt();
        System.out.print("電話號碼:");
        String phone;
        phone = scanner.nextLine();
        System.out.print("郵箱:");
        String email;
        email = scanner.nextLine();
        
        //將上面的資料封裝到物件中
        Customer customer = new Customer(name,gender,age,phone,email);
        boolean isSuccess = customerList.addCustomer(customer);
        if(isSuccess) {
            System.out.println("-----------------新增完成------------------");
        }else {
            System.out.println("-----------------目錄已滿,新增失敗--- -----");
        }
    }

    /**
     * @see 修改客戶的操作
     */
    public void modifyCustomer() {
//        System.out.println("修改客戶的操作");
        System.out.println("-----------------新增完成------------------");
        Customer cust;
        int number;
        for (;;) {
            System.out.println("請選擇待修改的客戶編號(-1退出):");
            Scanner input = new Scanner(System.in);
            
            number = input.nextInt();
            if (number == -1) {
                return;
            }
            cust = customerList.getCustomer(number-1);
            if(cust == null) {
                System.out.println("無法找到指定的客戶!");
            }else {
                break;
            }
        }
        //找到了指定的客戶,修改客戶資訊
        Scanner input = new Scanner(System.in);
        Scanner input1 = new Scanner(System.in);
        System.out.println("姓名(" + cust.getName() + "):");
        String name;
        name = input.nextLine();
        System.out.println("性別(" + cust.getGender() + "):");
        String gender;
        gender = input.nextLine();
        System.out.println("年齡(" + cust.getAge() + "):");
        int age;
        age = input1.nextInt();
        System.out.println("電話號碼(" + cust.getPhone() + "):");
        String phone;
        phone = input.nextLine();
        System.out.println("郵箱(" + cust.getEmail() + "):");
        String email;
        email = input.nextLine();
        
        Customer newcust = new Customer(name, gender, age, phone, email);
        boolean isReplace = customerList.replaceCustomer(number-1, newcust);
        if(isReplace) {
            System.out.println("-----------------修改完成------------------");
        }else {
            System.out.println("-----------------修改失敗------------------");
        }
        
    }

    /**
     * @see 刪除客戶的操作
     */
    public void deleteCustomer() {
//        System.out.println("刪除客戶的操作");
        System.out.println("-----------------刪除客戶------------------");
        Scanner input = new Scanner(System.in);
        int number;
        for (;;) {
            System.out.println("請選擇待修改的客戶編號(-1退出):");
            number = input.nextInt();
            if (number == -1) {
                return;
            }
            Customer cust = customerList.getCustomer(number-1);
            if(cust == null) {
                System.out.println("無法找到指定的客戶!");
            }else {
                break;
            }
        }
        //找到指定的客戶,刪除操作
        System.out.println("是否確認刪除(刪除11/不刪00):");
        int number1;
        number1 = input.nextInt();
        if(number1 == 00) {
            return;
        }else {
            boolean isDelete = customerList.deleteCustomer(number - 1);
            if(isDelete) {
                System.out.println("-----------------刪除成功------------------");
            }else {
                System.out.println("-----------------刪除失敗------------------");
            }
        }
        
    }

    /**
     * @see 顯示客戶的列表
     */
    public 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 < total;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.enterMainView();

    }
}

四、總結

  1、程式碼有冗餘

  2、可新增一個工具類對輸入進行約束

  3、增加使用者體驗

  4、連結資料庫,對資料進行持久化儲存

五、快捷鍵

  註釋:本人使用的是eclipse,   可使用裡面的快捷鍵加快開發效率

  1、補全程式碼的宣告:alt + /

  2、快速修復:ctrl + 1

  3、批量導包:crat + shift + o

  4、使用單行註釋:ctrl + /

  5、使用多行註釋:crtl + shift + /

  6、取消多行註釋:ctrl + shift + \

  7、複製指定行的程式碼:ctrl + alt + down 或者 ctrl + alt + up

  8、刪除指定行的程式碼:ctrl + d

  9、上下移動程式碼:alt + up 或者  alt + down

  10、切換到下一行程式碼空位:shift + enter

  11、切換到上一行程式碼空位:ctrl + shift + enter

  12、檢視原始碼:ctrl + 選中指定的結構  或者  ctrl + shift + t

  13、退回到前一個編輯頁面:alt + left

  14、進入到下一個編輯頁面:alt + right

  15、選中指定的類,檢視繼承樹結構:ctrl + t

  16、複製程式碼:ctrl + c

  17、撤銷:ctrl + z

  18、反撤銷:ctrl + y

  19、剪下:ctrl + x

  20、貼上:ctrl + v

  21、儲存:ctrl + s

  22、全選:ctrl + a

  23、格式化程式碼:ctrl + shift + f

  24、選中數行,整體向後移動:tab

  25、選中數行,整體向前移動:shift + tab

  26、在當前類中,顯示類結構,並支援搜尋指定的方法、屬性等:ctrl + o

  27、批量修改指定的方法名、變數名、類名等:alt + shift + r

  28、選中的結構大小寫切換,變成大寫:ctrl + shift + x

  29、選中的結構大小寫切換,變成小寫:ctrl + shift + y

  30、調出getter/setter構造器等結構:alt + shift + s

  31、顯示當前選擇資源(工程 or 檔案)的屬性:alt + enter

  32、快速查詢,參照選中的word快速定位到下一個:ctrl + k

  33、關閉當前視窗:ctrl + w

  34、關閉所有視窗:ctrl + shift + w

  35、檢視指定的結構使用過的地方:ctrl + alt + g

  36、查詢與替換:ctrl + f

  37、最大化當前的View:ctrl + m

  38、直接定位到當前行的首行:home

  39、直接定位到當前行的末行