簡単な顧客管理javaプロジェクト(初心者に楽しいプロジェクトを得ることができ、簡単で面白い)

11914 ワード

package com.hy.object.customerMessageManagement;
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;
    }
}
package com.hy.object.customerMessageManagement;

public 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 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;
    }
    public String info(){
       return this.getName() + "\t" +this.getGender() + "\t" +this.getAge() + "\t" +this.getPhone() + "\t" +this.getEmail() + "\t";
    }
}

3つ目のクラスは、顧客の集合に対して処理されるクラスです.
package com.hy.object.customerMessageManagement;
/**
 *
 * @author hanye
 * @date 2020-06-14 10:39:18
 **/

public class CustomerList {

    private Customer[] customers;
    int total=0;

    /**
     * CustomerList     
     * @param totalCustomer
     */
    public CustomerList(int totalCustomer){
        customers = new Customer[totalCustomer];
    }

    public boolean addCustomer(Customer customer){
        if(total >= customers.length){
            return false;
        }
        customers[total]=customer;
        total++;
        return true;
    }

    /**
     *           
     * @param index  :      
     * @param customer :       
     * @return       
     */
    public boolean replaceCustomer(int index,Customer customer){
        if(index<0 || index>=total ){
            return  false;
        }
        customers[index]=customer;
        return true;
    }
    public boolean deleteCustomer(int index){
        if(index<0 || index>=total){
            return false;
        }
        for(int i=0;i=total)
                return null;
            return customers[index];
    }

    public int getTotal(){
        return total;
    }

}

4つ目のクラスはユーザーインタフェースクラスで、入出力とインタフェースの展示を担当しています.
package com.hy.object.customerMessageManagement;

/**
 * @author MSI-PC
 * @date 2020-06-14 10:53
 */

public class CustomerView {
    private CustomerList customerList = new CustomerList(10);
    public CustomerView(){
        Customer cust = new Customer("  ", ' ', 30, "010-52776920", "[email protected]");
        customerList.addCustomer(cust);
    }
    /**
     *      
     */
    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();
            System.out.println();
            switch(key){
                case '1':
                    addNewCustomer();
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomer();
                    break;
                case '5':
                    System.out.println("      (Y/N):");
                    char yn =CMUtility.readConfirmSelection();
                    if(yn == 'Y') flag = false;
                    break;
            }
        }while(flag);
    }

    /**
     *      
     */

    private void addNewCustomer(){
        System.out.println("-----------------    ---------------");
        System.out.print("  :");
        String name = CMUtility.readString(4);
        System.out.print("  :");
        char gender = CMUtility.readChar();
        System.out.print("  :");
        int age = CMUtility.readInt();
        System.out.print("  :");
        String phone = CMUtility.readString(15);
        System.out.print("  :");
        String email = CMUtility.readString(15);

        Customer customer = new Customer(name,gender,age,phone,email);
        boolean flag=customerList.addCustomer(customer);
        if(flag){
            System.out.println("---------------------    ---------------------");
        } else {
            System.out.println("----------------    ,    -----------------");
        }
    }

    /**
     *     
     */
    private void modifyCustomer(){
        System.out.println("---------------------    ---------------------");
        int number;
        Customer cust;
        for(;;) {
            System.out.println("          (-1)  :");
            number =CMUtility.readInt();
            if(number == -1){
                return;
            }
            cust=customerList.getCustomer(number-1); //        
            if(null == cust){
                System.out.println("        ");
            }else{
                break; //    
            }
        }
        System.out.print("  (" + cust.getName() + "):");
        String name = CMUtility.readString(4, 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(15, cust.getPhone());

        System.out.print("  (" + cust.getEmail() + "):");
        String email = CMUtility.readString(15, cust.getEmail());

        cust = new Customer(name, gender, age, phone, email);
        boolean isReplaced = customerList.replaceCustomer(number-1,cust);
        if(isReplaced){
            System.out.println("---------------    --------------");
        }else{
            System.out.println("---------------    --------------");
        }
    }

    /**
     *     
     */
    private void deleteCustomer(){
        System.out.println("------------------    ------------------");
        int number;
        for(;;){
            System.out.println("          (-1  ):");
            number =CMUtility.readInt();
            if(-1 == number){
                return;
            }
            Customer cust = customerList.getCustomer(number-1);
            if (null == cust){
                System.out.println("        !");
            }else{
                break;
            }
        }
        System.out.println("      (Y/N):");
        char confirmDelete = CMUtility.readConfirmSelection();
        if( 'Y' == confirmDelete){
            boolean flag = customerList.deleteCustomer(number-1);
            if(flag){
                System.out.println("----------------    -----------------");
            }else{
                System.out.println("----------------    -----------------");
            }
        }


    }
    /**
     *       
     */
    private void listAllCustomer(){
        System.out.println("----------------    -----------------");
        int total = customerList.getTotal();
        if (total == 0){
            System.out.println("----------------      -----------------");
        }else{
            System.out.println("  \t  \t  \t  \t  \t\t\t\t\t  \t");
            Customer[] custs=customerList.getAllCustomers();
            for(int i = 0;i