Java项目1模拟实现一个机遇文本界面的《客户信息管理软件》

1-项目知识点介绍

/*
* 1-模拟实现一个机遇文本界面的《客户信息管理软件》
* 2-进一步掌握编程技巧和调试技巧,熟悉面向对象编程
* 3主要涉及以下知识点:
* 1.类结构的使用:属性、方法及构造器
* 2.类的创建与使用
* 3.类的封装性
* 4.声明和使用数组
* 5.数组的插入、删除和替换
* 5.关键字的使用:this
*/

2-项目需求

 

3-软件设计结构

 

4-实现步骤

 

 

 

 

 

5-项目内存解析

 

6-代码实现

注:在src文件下创建不同的包

 

package com.lzh.p2.bean;
/**
 * 
 * @Description Customer为实体对象,用来封装客户信息
 * @author laizhenghua
 * @version v 1.8
 * @Date 2020年5月26日14:22:53
 */
public class Customer {
    private String name; // 客户关系
    private char gender; // 性别
    private int age; // 年龄
    private String phone; // 电话号码
    private String email; // 电子邮箱
    
    // 设置get和set方法
    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 Customer() {
        
    }
    public Customer(String name, char gender, int age, String phone, String email) {
        super();
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.phone = phone;
        this.email = email;
    }
}
Customer.java
package com.lzh.p2.service;
import com.lzh.p2.bean.Customer;

/**
 * 
 * @Description CustomerList为Customer对象的管理模块,内部用数组管理一组Customer对象,并提供相应的
 * 添加、删除和遍历方法,供CustomerView调用
 * @author laizhenghua
 * @version v 1.8
 * @Date 2020年5月26日14:22:53
 */
public class CustomerList {
    private Customer[] customers; // 用来保存客户对象的数组
    private int total = 0; // 记录已保存客户对象的数量
    
    public CustomerList(int totalCustomer) {
        // 构造器,用来初始化customers数组,totalCustomer指定customer数组的最大空间
        this.customers = new Customer[totalCustomer];
    }
    /**
     * @Description 将指定的客户添加到数组中
     * @param customer
     * @return true:添加失败 false:添加失败
     */
    public boolean addCustomer(Customer customer) {
        if(total >= customers.length) {
            return false;
        }
        customers[total] = customer;
        total++;
        // customer[total++] = customer;
        return true;
    }
    /**
     * @Description 修改指定索引位置的客户信息
     * @param index
     * @param cust
     * @return true:修改成功 false:修改失败
     */
    public boolean replaceCustomer(int index,Customer cust) {
        if(index < 0 || index >= total) {
            return false;
        }
        customers[index] = cust;
        return true;
    }
    /**
     * @Description 删除指定索引位置的客户信息
     * @param index
     * @param cust
     * @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;
    }
    /**
     * 获取所有客户信息
     * @return 返回所有客户对象
     */
    public Customer[] getAllCustomers() {
        Customer[] cuts = new Customer[total]; // 新建对象数组
        for(int i = 0;i < total;i++) {
            cuts[i] = customers[i];
        }
        return cuts;
    }
    /**
     * @Description 获取指定位置的上的客户
     * @param index
     * @return 如果找到元素,则返回,没有找到则返回null
     */
    public Customer getCustomer(int index) {
        if(index < 0 || index > total) {
            return null;
        }
        return customers[index];
    }
    /**
     * 获取客户存储数量
     * @return
     */
    public int getTotal() {
        return total;
    }
}
CustomerList.java
package com.lzh.p2.ui;
import java.util.Scanner;

import com.lzh.p2.bean.Customer;
import com.lzh.p2.service.CustomerList;

/**
 * 
 * @Description CustomerView为主模块,负责菜单的显示和处理用户操作
 * @author laizhenghua
 * @version v 1.8
 * @Date 2020年5月26日14:22:53
 */
public class CustomerView {
    private CustomerList customerList = new CustomerList(10);
    /**
     * @Description显示《客户信息管理软件》界面的方法
     */
    public CustomerView() {
        Customer customer = new Customer("alex", '男', 22, "17708821494", "101323@qq.com");
        customerList.addCustomer(customer);
    }
    public void enterMainMenu() {
        boolean isFlag = true;
        while(isFlag) {
            System.out.println("\n----------客户信息管理软件----------");
            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):");
            Scanner scanners = new Scanner(System.in);
            char choice = scanners.next().charAt(0);
            switch(choice) {
            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 = scanners.next().charAt(0);
                if(c == 'y') {
                    isFlag = false;
                }        
            }
        }
    }
    /**
     * @Description添加客户的操作
     */
    private void addNewCustomer() {
        System.out.println("-----------添加客户----------");
        System.out.print("姓名:");
        Scanner scan = new Scanner(System.in);
        String name = scan.next();
        System.out.print("性别:");
        char gender = scan.next().charAt(0);
        System.out.print("年龄:");
        int age = scan.nextInt();
        System.out.print("电话:");
        String phone = scan.next();
        System.out.print("邮箱:");
        String email = scan.next();
        
        // 将上面数据封装到对象中
        Customer customer = new Customer(name, gender, age, phone, email);
        boolean isFlag = customerList.addCustomer(customer);
        if(isFlag) {
            System.out.println("-----------添加完成----------");
        }else {
            System.out.println("-----------添加失败----------");
        }
    }
    /**
     * @Description修改客户的操作
     */
    private void modifyCustomer() {
        System.out.println("-----------修改客户----------");
        Scanner scan = new Scanner(System.in);
        Customer customer;
        int index;
        for(;;) {
            System.out.print("请选择待修改客户编号(0退出):");
            char choice = scan.next().charAt(0);
            if(choice == '0') {
                scan.close();
                return;
            }
            index = Character.getNumericValue(choice);
            // System.out.println(i);
            customer = customerList.getCustomer(index - 1);
            if(customer == null) {
                System.out.println("无法找到指定用户!");
            }else { // 找到了相应的客户
                break;
            }
        }
        // 修改客户信息
        System.out.println("姓名("+customer.getName()+"):");
        // scan.nextLine();
        String name = scan.next();
//        if(name == null) {
//            name = customer.getName();
//            System.out.println(111);
//            return;
//        }
        System.out.println("性别("+customer.getGender()+"):");
        char gender = scan.next().charAt(0);
        System.out.println("年龄("+customer.getAge()+"):");
        int age = scan.nextInt();
        System.out.println("电话("+customer.getPhone()+"):");
        String phone = scan.next();
        System.out.println("邮箱("+customer.getEmail()+"):");
        String email = scan.next();
        
        Customer c = new Customer(name, gender, age, phone, email);
        boolean isReplace = customerList.replaceCustomer(index - 1, c);
        if(isReplace) {
            System.out.println("-----------修改完成----------");
        }else {
            System.out.println("-----------修改失败----------");
        }
    }
    /**
     * @Description删除客户的操作
     */
    private void deleteCustomer() {
        System.out.println("-----------删除客户----------");
        Scanner scan = new Scanner(System.in);
        Customer customer;
        int index;
        for(;;) {
            System.out.print("请选择待删除客户编号(0退出):");
            char choice = scan.next().charAt(0);
            if(choice == '0') {
                scan.close();
                return;
            }
            index = Character.getNumericValue(choice);
            // System.out.println(i);
            customer = customerList.getCustomer(index - 1);
            if(customer == null) {
                System.out.println("无法找到指定用户!");
            }else { // 找到了相应的客户
                break;
            }
        }
        System.out.print("确认是否删除(y/n):");
        char isDelete = scan.next().charAt(0);
        if(isDelete == 'y') {
            boolean deleteSuccess = customerList.deleteCustomer(index-1);
            if(deleteSuccess) {
                System.out.println("-----------删除成功----------");
            }else {
                System.out.println("-----------删除失败----------");
            }
        }
    }
    /**
     * @Description显示客户列表的操作
     */
    public void listAllCustomers() {
        System.out.println("-----------客户列表----------");
        int total = customerList.getTotal();
        if(total == 0) {
            System.out.println("没有客户记录");
        }else {
            System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
            Customer[] cuts = customerList.getAllCustomers();
            for(int i = 0;i < cuts.length;i++) {
                System.out.println(i+1+"\t"+cuts[i].getName()+"\t"+cuts[i].getGender()+"\t"+cuts[i].getAge()+"\t"+cuts[i].getPhone()+"\t"+cuts[i].getEmail());
            }
        }
        System.out.println("-----------客户列表完成----------");
    }
    public static void main(String[] args) {
        CustomerView customerView = new CustomerView();
        customerView.enterMainMenu();
    }
}
CustomerView.java

posted @ 2020-06-01 18:27  赖正华  阅读(373)  评论(0编辑  收藏  举报