Java小练习2-----客户信息管理软件

目标

Java小练习2-----客户信息管理软件_第1张图片

需求说明

Java小练习2-----客户信息管理软件_第2张图片
Java小练习2-----客户信息管理软件_第3张图片
Java小练习2-----客户信息管理软件_第4张图片
Java小练习2-----客户信息管理软件_第5张图片
Java小练习2-----客户信息管理软件_第6张图片
Java小练习2-----客户信息管理软件_第7张图片

实现

Java小练习2-----客户信息管理软件_第8张图片

C u s t o m e r . j a v a Customer.java Customer.java

package Two;

public class Customer {
     
    private String name;
    private String gender;
    private int age;
    private String phone;
    private String email;

    public Customer(String name, String gender, int age) {
     
        this(name, gender, age, "", "");
    }

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

    public String getDetails() {
     
        return "Name:" + name + '\t' + "Gander:" + gender + "\t" + "Age:" + age + "\t\t" + "Phone:" + phone + "\t" + "Email:" + 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;
    }
}

C u s t o m e r L i s t . j a v a CustomerList.java CustomerList.java

package Two;

public class CustomerList {
     
    private Customer[] customers;
    private int total = 0;

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

    public boolean addCustomer(Customer customer){
     
        if(total>=customers.length)
            return false;

        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;

        for( int i=index; i < total-1; i++)
            customers[i] = customers[i-1];
        customers[--total] = null;
        return true;
    }
    public Customer[] getAllCustomers(){
     
        Customer[] custs = new Customer[total];
        for(int i=0; i< total; i++){
     
            custs[i] = customers[i];
        }
        return custs;
    }

    public Customer getCustomers(int index) {
     
        if (index<0 || index>= customers.length)
            return null;

        return customers[index];
    }

    public int getTotal() {
     
        return total;
    }
}

C M U t i l i t y . j a v a CMUtility.java CMUtility.java

package Two;

import javax.sound.midi.MidiFileFormat;
import java.util.Locale;
import java.util.Scanner;

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

    public static char readMenuSelection() {
     
        /*用于界面菜单的选择*/
        char c;
        while (true) {
     
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (!('0' < c && c < '6'))
                System.out.print("Select Error, please input again:");
            else
                break;
        }
        return c;
    }

    public static char readChar() {
     
        /*从键盘读取一个字符,并将其作为方法的返回值*/
        String str = readKeyBoard(1, false);
        return str.charAt(0);
    }

    public static char readChar(char defaultValue) {
     
        /*如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。*/
        String str = readKeyBoard(1, false);
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }

    public static int readInt() {
     
        /*从键盘读取一个长度不超过2位的整数*/
        int n;
        while (true) {
     
            String str = readKeyBoard(2, false);
            try {
     
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
     
                System.out.print("Number is wrong. Please input again:");
            }
        }

        return n;
    }

    public static int readInt(int defaultValue) {
     
        int n;
        while (true) {
     
            String str = readKeyBoard(2, true);
            if (str.equals("")) {
     
                return defaultValue;
            }

            try {
     
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
     
                System.out.print("Number is wrong. Please input again:");
            }
        }

        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;
        while (true) {
     
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N')
                break;
            else
                System.out.print("Select Error. Please input again:");
        }
        return c;
    }


    private static String readKeyBoard(int limit, boolean blankReturn) {
     
        String line = "";

        while (in.hasNextLine()) {
     
            line = in.nextLine();
            if (line.length() == 0) {
     
                if (blankReturn)
                    return line;
                else
                    continue;
            }

            if (line.length() < 1 || line.length() > limit) {
     
                System.out.print("Input's limit is " + limit + ". Please input again:");
                continue;
            }
            break;
        }
        return line;
    }
}

C u s t o m e r V i e w . j a v a CustomerView.java CustomerView.java

package Two;

public class CustomerView {
     
    private CustomerList customers = new CustomerList(10);

   /* public CustomerView(){
        Customer cust = new Customer("Tom","man",30,"010-56283825","[email protected]");
        customers.addCustomer(cust);
    }*/

    public void enterMainMenu() {
     
        boolean loopFlag = true;
        do {
     
            System.out.println("\n-----------------------------------Customers massager System--------------------------");
            System.out.println("                                     1. add customer.");
            System.out.println("                                     2. modify Customer.");
            System.out.println("                                     3. delete customer.");
            System.out.println("                                     4. view all customers.");
            System.out.println("                                     5. exit.");
            System.out.print("Please choose 1-5:");

            char key = CMUtility.readMenuSelection();
            System.out.println();
            switch (key) {
     
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomer();
                    break;
                case '5':
                    System.out.print("Confirm to exit(Y/N)?");
                    char yn = CMUtility.readConfirmSelection();
                    if (yn == 'Y')
                        loopFlag = false;
                    break;
            }
        } while (loopFlag);
    }

    private void addNewCustomer() {
     
        System.out.println("-----------------------Add new customer-------------------------");
        System.out.print("Name:");
        String name = CMUtility.readString(4);
        System.out.print("Gender(1:man 0:woman):");
        String gender = CMUtility.readString(1);
        System.out.print("Age:");
        int age = CMUtility.readInt();
        System.out.print("Tel:");
        String phone = CMUtility.readString(15);
        System.out.print("email:");
        String email = CMUtility.readString(15);

        Customer cust = new Customer(name, gender, age, phone, email);
        boolean flag = customers.addCustomer(cust);

        if (flag)
            System.out.println("-----------------------------Add Successful-------------------------");
        else
            System.out.println("----------------------Full of List. Cannot add anymore--------------");
    }

    private void modifyCustomer() {
     
        System.out.println("------------------------Modify Customer-----------------------");
        int index = 0;
        Customer cust = null;
        while (true) {
     
            System.out.print("Please choose will be modified customer(input -1 exit):");
            index = CMUtility.readInt();
            if (index == -1)
                return;

            cust = customers.getCustomers(index - 1);
            if (cust == null)
                System.out.println("Cannot find the customer index is " + index + ".");
            else
                break;
        }
        System.out.print("Confirm modify it(Y/N):");
        char yn = CMUtility.readConfirmSelection();
        if (yn == 'N')
            return;

        System.out.print("Name(Now:" + cust.getName() + "):");
        String name = CMUtility.readString(4, cust.getName());
        System.out.print("Gender(1:man 0:woman)(Now:" + cust.getGender() + "):");
        String gender = CMUtility.readString(1, cust.getGender());
        System.out.print("Age(Now:" + cust.getAge() + "):");
        int age = CMUtility.readInt(cust.getAge());
        System.out.print("Tel(Now:)" + cust.getPhone() + ":");
        String phone = CMUtility.readString(15, cust.getPhone());
        System.out.print("email(Now:" + cust.getEmail() + "):");
        String email = CMUtility.readString(15, cust.getEmail());

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

        boolean flag = customers.replaceCustomer(index - 1, cust);
        if (flag)
            System.out.println("------------------------Modify Successful-----------------------");
        else
            System.out.println("--------------------Cannot find the customer--------------------");
    }

    private void deleteCustomer() {
     
        System.out.println("-------------------------Delete Customer------------------------");
        int index = 0;
        Customer cust = null;
        while (true) {
     
            System.out.print("Please choose will be deleted customer(input -1 exit):");
            index = CMUtility.readInt();
            if (index == -1)
                return;

            cust = customers.getCustomers(index - 1);
            if (cust == null)
                System.out.println("Cannot find the customer index is " + index + ".");
            else
                break;
        }

        System.out.print("Confirm delete it(Y/N):");
        char yn = CMUtility.readConfirmSelection();
        if (yn == 'N')
            return;

        boolean flag = customers.deleteCustomer(index - 1);
        if (flag)
            System.out.println("------------------------Delete Successful-----------------------");
        else
            System.out.println("--------------------Cannot find the customer--------------------");
    }

    private void listAllCustomer() {
     
        System.out.println("------------------------------customer list-------------------------");
        Customer[] custs = customers.getAllCustomers();
        if (custs.length == 0)
            System.out.println("No one customer");
        else {
     
            for (int i = 0; i < custs.length; i++)
                System.out.println("Id:" + (i + 1) + "\t" + custs[i].getDetails());
        }
        System.out.println("----------------------------------End--------------------------------");
    }

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

Ps:项目来源于尚硅谷的项目二,本人英文垃圾,代码中的中式英语请忽略(懂就好啦!)[]

你可能感兴趣的:(Java练笔,java)