JAVA 語言客戶信息管理系統解題報告

                               客戶信息管理軟件解題分析

一,實驗目的:
1,模擬實現一個基於文本界面的《客戶信息管理軟件》
2,進一步掌握編程技巧和調試技巧,熟悉面向對象編程
3,主要涉及以下知識點:
類和對象(屬性、方法及構造器)
類的封裝
引用數組
數組的插入、刪除和替換
對象的聚集處理
多對象協同工作
二,需求分析:
模擬實現基於文本界面的《客戶信息管理軟件》。
該軟件能夠實現對客戶對象的插入、修改和刪除(用數組實現),並能夠打印客戶明細表。

三,軟件設計結構:
該軟件由以下三個模塊組成:
CustomerView爲主模塊,負責菜單的顯示和處理用戶操作
CustomerList爲Customer對象的管理模塊,內部用數組管理一組Customer對象,並提供相應的添加、修改、刪除和獲取方法,供CustomerView調用
Customer爲實體對象,用來封裝客戶信息

四,實驗具體內容:
見p2.ppt.

五,實驗代碼:
見打包程序.

六,實驗後收穫:

本次實驗室建立在java語言對象的實驗基礎上進行的實驗,剛拿到需求分析的時候,不知道如何下手,主要是類類型的定義和類類型的引用,類數組—存的是類的對象.比如本次實驗中 Customer類是對象的屬性的封裝,CustomerList類是類Customer類的封裝
在CustomerList類中 定義了一個數組customers[];用來存儲對象,然後通過CustomerList 來操縱數組的改變,比如增刪改.

此次實驗三個類之間的關係是 CustomerView 是一個主菜單 來顯示操縱後的結果,其中定義了CustomerList類的對象,然後通過它的對象來操縱數組customers[];然後再通過類Customer來改變對象的屬性,最終達到該軟件的功能.

此次實驗個人不足的是沒有具體理解對象,類的作用,類類型,以及邏輯思維的嚴謹和分析能力不足.
要引用類中的方法就要創建對象才能調用.

這裏寫代碼片
//導入輸入和輸出語句類
import java.util.*;

public class CMUtility {
    private static Scanner scanner = new Scanner(System.in);

    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);
    }

    public static char readChar(char defaultValue) {
        String str = readKeyBoard(1, true);
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }

    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;
    }

    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;
    }

    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }

    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }

    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;
    }
}
//主菜單視圖
class CustomerView 
{

   private CustomerList cl = new CustomerList(10);


   public 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 退           出");
            System.out.println("                  請選擇(1-5):_");    


          char key = CMUtility.readMenuSelection();

          switch (key)
          {
          case '1':
              //添加客戶
          addNewCustomer(); 

               break;
          case '2':
              //修改客戶
          modifyCustomer();

               break;
          case '3':
              //刪除客戶
          deleteCustomer();
               break;
          case '4':
              //客戶列表
          listAllCustomers();
               break;
          case '5':
              //退出
            System.out.print("確認是否退出(Y/N):");

            char c = CMUtility.readConfirmSelection();
            if(c == 'Y')
            {
            flag = false;
            }

               break;
          }

        }while(flag);  



    }

    private void addNewCustomer() 
    {
     System.out.println("---------------------添加客戶---------------------");
     System.out.print("姓名:");
     String name = CMUtility.readString(5);
     System.out.print("性別:");
     char gender = CMUtility.readChar();
     System.out.print("年齡:");
        int age = CMUtility.readInt(); 
     System.out.print("電話:");
     String phone = CMUtility.readString(10);
     System.out.print("郵箱:");
     String email = CMUtility.readString(40);

     Customer cu = new Customer(name , gender, age, phone, email);

      boolean flag = cl.addCustomer(cu);

      if (flag)
      { 
          System.out.println("---------------------添加完成---------------------");
      }
      else
          System.out.println("---------------------添加失敗---------------------");



    }

    private void modifyCustomer()
    {
        System.out.println("---------------------修改客戶---------------------");

        Customer cust = null;
        int num = 0;
        for (; ; )
         {

            System.out.print("請選擇待修改客戶編號(-1退出):");
            num = CMUtility.readInt();

            if(num == -1){
                return;
            }

            cust = cl.getCustomer(num-1);

            if(cust == null)
                {
                System.out.println("對不起,找不到該用戶!");
                }else
                    {
                      break;
                    }

         }

        System.out.println("姓名:("+cust.getName()+")");
        String name = CMUtility.readString(10,cust.getName());


        System.out.println("性別:("+cust.getGender()+")");
        char gender = CMUtility.readChar(cust.getGender());

        System.out.println("年齡:("+cust.getAge()+")");
        int age = CMUtility.readInt(cust.getAge());

        System.out.println("電話:("+cust.getPhone()+")");
        String phone = CMUtility.readString(15, cust.getPhone());

        System.out.println("郵箱:("+cust.getEmail()+")");
        String email = CMUtility.readString(40,cust.getEmail());

        cust = new Customer(name, gender, age, phone, email);

        boolean flag = cl.replaceCustomer(num-1, cust);
        if(flag){
            System.out.println("---------------------修改完成---------------------");
        }else{
            System.out.println("---------------------找不到該用戶---------------------");
        }

    }

    private void deleteCustomer()
    {
        System.out.println("---------------------刪除客戶---------------------");
        Customer cust = null;
        int num = 0;
        for(;;){
            System.out.print("請選擇待修改客戶編號(-1退出):");
            num = CMUtility.readInt();

            if(num == -1){
                return;
            }
            cust = cl.getCustomer(num-1);

            if(cust == null){
                System.out.println("對不起,找不到該用戶!");
            }else{
                break;
            }
        }

        System.out.print("確認是否刪除(Y/N):");
        char c = CMUtility.readConfirmSelection();
        if(c == 'N'){
            return;
        }

        boolean flag = cl.deleteCustomer(num-1);
        if(flag){
            System.out.println("---------------------刪除完成---------------------");
        }else{
            System.out.println("---------------------找不到該用戶---------------------");
        }

    }

    private void listAllCustomers()
    {
        System.out.println("---------------------客戶列表---------------------");
        Customer[] customers = cl.getAllCustomers();
        if(customers.length == 0)
            {
            System.out.println("沒有客戶!");
        }else
            {
            System.out.println("編號\t姓名\t性別\t年齡\t電話\t郵箱");
        }

        for(int i = 0; i < customers.length; i++){
            System.out.println((i+1) + "\t" + customers[i].getDetails());
        }
        System.out.println("---------------------客戶列表完成---------------------");
    }



    public static void main(String[] args) 
    {
        CustomerView cv = new CustomerView();

        cv.enterMainMenu();
    }
}
//客戶數組類 定義了一組對象數組
class CustomerList
{
    private Customer customers[];
    private int total = 0;

    public CustomerList(int totalCustomer)
    {
        customers = new Customer[totalCustomer];

    }

    public boolean addCustomer(Customer customer)
    {
      if (total < 0 || total > customers.length)
      {
          return false;
      }else
          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;
       }else

           for (int i = index+1;i < total ;i++ )
           {
               customers[i-1] = customers[i];
           }
                customers[--total] = null;
       return true;
    }



    public Customer[] getAllCustomers() {
        Customer[] cust = new Customer[total];

        for(int i = 0; i < total; i++){
            cust[i] = customers[i];
        }

        return cust;
    }


    public Customer getCustomer(int index) {
        if(index < 0 || index >= total){
            return null;
        }

        return customers[index];

    } 

}

//客戶類  
class Customer 
{

        private String name;
        private char gender;
        private int age;
        private String phone;
        private String email;



    public Customer() {}


    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 void setName(String name)
    {
      this.name = name;
    }

    public String getName()
    {
      return name;

    }

    public void setGender(char gender)
    {

       this.gender = gender;
    }

    public char getGender()
    {
        return gender;

    }

    public void setAge(int age)
    {
       this.age = age;

    }

    public int getAge()
    {
       return age;

    }

    public void setPhone(String phone)
    {

       this.phone = phone;
    }

    public String getPhone()
    {
        return phone;
    }
     public void setEmail(String email)
    {

       this.email = email;
    }

    public String getEmail()
    {
        return email; 
    }

    public String getDetails()
    {
        return name + "\t" + gender + "\t" + age + "\t" + phone + "\t" + email;
    }



}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章