栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 简单控制台项目之客户信息管理软件 --- 凌宸1642

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 简单控制台项目之客户信息管理软件 --- 凌宸1642

项目二:客户信息管理软件
  • 模拟实现一个基于文本界面的《客户信息管理软件》

  • 进一步掌握编程技巧和调试技巧,熟悉面向对象编程

  • 主要涉及以下知识点:

    • 类结构的使用:属性、方法及构造器
    • 对象的创建与使用
    • 类的封装性
    • 声明和使用数组
    • 数组的插入、删除和替换
    • 关键字的使用:this
  • 该软件能够实现对客户对象的插入、修改和删除(用数组实现),并能够打印客户明细表。

  • 项目采用分级菜单方式。主菜单如下:

     
  • 需求说明

    • 每个客户的信息被保存在Customer对象中。

    • 以一个Customer类型的数组来记录当前所有的客户。

    • 每次“添加客户”(菜单1)后,客户Customer对象被添加到数组中。

    • 每次“修改客户”(菜单2)后,修改后的客户Customer对象替换数组中原对象。

    • 每次“删除客户”(菜单3)后,客户Customer对象被从数组中清除。

    • 执行“客户列表 ”(菜单4)时,将列出数组中所有客户的信息。

    • "添加客户"的界面及操作过程如下所示:

       
    • "修改客户"的界面及操作过程如下所示:

       
    • "删除客户"的界面及操作过程如下所示:

       
    • "客户列表"的界面及操作过程如下所示:

       
 
package com.lingchen.pojo.customermanager.bean;




public class Customer {
    String name; // 客户姓名
    char gender; // 性别
    int age; // 年龄
    String phone; // 电话号码
    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;
    }
}

package com.lingchen.pojo.customermanager.service;



import com.lingchen.pojo.customermanager.bean.Customer;


public class CustomerList {
    Customer[] customers; // 用来保存客户对象的数组
    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 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 = 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 getCustomer(int index){
        if(index < 0 || index >= total){
            return null;
        }
        return customers[index];
    }

    
    public int getTotal(){
        return total;
    }

}

package com.lingchen.pojo.customermanager.util;


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 read/confirm/iSelection() {
        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.lingchen.pojo.customermanager.view;



import com.lingchen.pojo.customermanager.bean.Customer;
import com.lingchen.pojo.customermanager.service.CustomerList;
import com.lingchen.pojo.customermanager.util.CMUtility;


public class CustomerView {
    CustomerList customerList = new CustomerList(10);

    public CustomerView() {
        Customer customer = new Customer("凌宸",'男',21,"18897993582","lc@qq.com");
        customerList.addCustomer(customer);
    }
    
    public void enterMainMenu(){
        boolean isFlag = true;
        do{
            System.out.println("n-----------------客户信息管理软件-----------------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):");

            char menuSelection = CMUtility.readMenuSelection();
            switch (menuSelection){
                case '1':
                    addNewCustomer(); break;
                case '2':
                    modifyCustomer(); break;
                case '3':
                    deleteCustomer(); break;
                case '4':
                    listAllCustomers(); break;
                case '5':
                    System.out.print("是否确认退出(Y/N):");
                    char isExit = CMUtility.read/confirm/iSelection();
                    if(isExit == 'Y'){
                        System.out.println("退出成功!");
                        isFlag = false;
                    }
            }
        }while(isFlag);
    }
    
    private void addNewCustomer(){
        System.out.println("n---------------------添加客户---------------------");
        System.out.print("姓名:");
        String name = CMUtility.readString(10);
        System.out.print("性别:");
        char gender = CMUtility.readChar();
        System.out.print("年龄:");
        int age = CMUtility.readInt();
        System.out.print("电话:");
        String phone = CMUtility.readString(13);
        System.out.print("邮箱:");
        String email = CMUtility.readString(32);

        Customer customer = new Customer(name, gender, age, phone, email);
        boolean isSuccess = customerList.addCustomer(customer);
        if(isSuccess){
            System.out.println("---------------------添加完成---------------------");
        }else{
            System.out.println("---------------------添加失败---------------------");
        }
    }
    
    private void modifyCustomer(){
        System.out.println("n---------------------修改客户---------------------");
        Customer customer;
        int selection;
        for(; ;){
            System.out.print("请选择待修改客户编号(-1退出):");
            selection = CMUtility.readInt();
            if(selection == -1){
                return ;
            }
            customer = customerList.getCustomer(selection - 1);
            if(customer == null){
                System.out.println("无法找到指定课户!");
            }else{
                break;
            }
        }
        // 修改客户信息
        System.out.print("姓名(" + customer.getName() +"):");
        String name = CMUtility.readString(10,customer.getName());
        System.out.print("性别(" + customer.getGender() +"):");
        char gender = CMUtility.readChar(customer.getGender());
        System.out.print("年龄(" + customer.getAge() +"):");
        int age = CMUtility.readInt(customer.getAge());
        System.out.print("电话(" + customer.getPhone() +"):");
        String phone = CMUtility.readString(13,customer.getPhone());
        System.out.print("邮箱(" + customer.getEmail() +"):");
        String email = CMUtility.readString(32,customer.getEmail());

        Customer newCustomer = new Customer(name, gender, age, phone, email);
        boolean isRepalaced = customerList.replaceCustomer(selection - 1, newCustomer);
        if(isRepalaced){
            System.out.println("---------------------修改完成---------------------");
        }else{
            System.out.println("---------------------修改失败---------------------");
        }
    }
    
    private void deleteCustomer(){
        System.out.println("n---------------------删除客户---------------------");
        Customer customer;
        int selection;
        for(; ;){
            System.out.print("请选择待删除客户编号(-1退出):");
            selection = CMUtility.readInt();
            if(selection == -1){
                return ;
            }
            customer = customerList.getCustomer(selection - 1);
            if(customer == null){
                System.out.println("无法找到指定课户!");
            }else{
                break;
            }

        }
        // 找到了指定用户
        System.out.print("确认是否删除(Y/N):");
        char isDelete = CMUtility.read/confirm/iSelection();
        if(isDelete == 'Y'){
            customerList.deleteCustomer(selection - 1);
            System.out.println("---------------------删除完成---------------------");
        }
    }
    
    private void listAllCustomers(){
        System.out.println("n---------------------------客户列表---------------------------");
        int total = customerList.getTotal();
        if(total == 0){
            System.out.println("没有客户信息!");
        }else{
            System.out.println("编号tt姓名tt性别tt年龄tt电话tttt邮箱");
            Customer[] allCustomers = customerList.getAllCustomers();
            for (int i = 0; i < allCustomers.length; i++) {
                Customer c = allCustomers[i];
                System.out.println((i + 1) + "tt" + c.getName()+ "tt" + 
                                   c.getGender() + "tt" + c.getAge()+ "tt" + 
                                   c.getPhone()+ "tt" + c.getEmail());
            }
        }
        System.out.println("-------------------------客户列表完成-------------------------");
    }

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/318681.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号