Java实现客户信息管理

目录

项目场景

代码

Customers类

工具类

CustomerList类

CustomerView类


 

项目场景

1模拟实现一个基于文本界面的《客户信息管理软件》。

2该软件能够实现对客户对象的插入、修改和删除(用数组实现),并能够打印客户明细表。

开始页面:

1添加客户:

4显示客户列表:

 

 

 

代码

Customers类

用来封装客户信息

package bean;

/**
 * @author dd
 * @Description 
 * @date 2022/5/3 16:28
 */
public class Customers {
    String name;
    char gender;
    int age;
    String phone;
    String email;

    public Customers(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 getDetail(){
        return name + "\t" + gender + "\t" + age + "\t\t" + phone + "\t" + email;
    }
}

工具类

提供多种方法方便地实现键盘访问。

package util;

import java.util.Locale;
import java.util.Scanner;

/**
 * @author
 * @Description
 * @date 2022/5/3 16:50
 */
public class Util {
    private static Scanner scan = new Scanner(System.in);
    /**
     用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。
     */
    public static char readMenuSelect(){
        char c ;
        for (;;){
            String choice = readKeyBroad(1,false);//回车的话没反应并且没结束
            c = choice.charAt(0);
            if (c != '1' && c != '2' && c != '3' && c != '4' &&c != '5' ){
                System.out.println("输入错误,请输入正确的选项");
            }
            else break;
        }
        return c;
    }
    public static char readConfirmSelection(){
        char s = 0;
     for (;;){
         String select = readKeyBroad(1,false).toLowerCase(Locale.ROOT);
         s = select.charAt(0);
         if (s != 'y' && s != 'n'){
             System.out.println("请输入正确的选项");
         }
         else break;
     }
     return s;
    }
    public static int readInt(){
        int n;
        for (;;){
            String str = readKeyBroad(3,false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.println("请输入正确的数字");
            }
        }
        return n;
    }
    public static int readInt(int defaultValue){
        int n;
        for (;;){
            String str = readKeyBroad(3,true);
            if (str.equals(""))
                return defaultValue;
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.println("请输入正确的数字");
            }
        }
        return n;
    }

    /**
     *
     * @param limit 限制输出字符的长度
     * @return 输出输入的字符串
     */
    public static String readString(int limit){
        return readKeyBroad(limit,false);
    }

    /**
     *
     * @param limit 限制输出字符串的长度
     * @param defaultValue 如果输入空字符串(回车)则返回默认值
     * @return
     */
    public static String readString(int limit,String defaultValue){
        String str = readKeyBroad(limit,true);
        return (str.equals("")) ? defaultValue : str;
    }

    /**
     *
     * @return 输出输入的字符
     */
    public static char readChar(int limit){
        return readKeyBroad(1,false).charAt(0);
    }
    public static char readChar(int limit,char defaultValue){
        String str = readKeyBroad(1,true);
        return (str.equals("")) ? defaultValue : str.charAt(0);
    }

    /**
     *
     * @param limit 长度限制
     * @param blankReturn 是否需要返回值 如果是false则没有返回值
     * @return 键盘输入的值
     */

    public static String readKeyBroad(int limit, boolean blankReturn) {
        String line = "";
        while (scan.hasNextLine()) {

            line = scan.nextLine();
            if (line.length() == 0){
                if(blankReturn) return line;
                else continue;
            }
            if (line.length() > limit) {
                System.out.println("输入长度(不大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }
        return line;
    }

}

CustomerList类

为Customer对象的管理模块,内部使用数组管理一组Customer对象

package service;

import bean.Customers;
import util.Util;

/**
 * @author
 * @Description CustomerList为Customer对象的管理模块,
 * 内部用数组管理一组Customer对象,并提供相应的添加、修改、删除和遍历方法,供CustomerView调用
 *
 * 控制层 Controller 处理业务逻辑
 * @date 2022/5/3 16:33
 */
public class CustomerList {
    private  int total = 0;
    private Customers[] customers;

    /**
     * 添加构造器
     * @param totalCustomer total是添加的客户总数 totalCustomer是创建的数组长度
     */
    public CustomerList(int totalCustomer){
        customers = new Customers[totalCustomer];
    }

    /**
     *
     * @param customer 将要添加的某个客户
     * @return 是否能够添加成功
     */
    public boolean addCustomer(Customers customer){
        if (total < customers.length){
            customers[total++] = customer;
            return true;
        }
        return false;

    }
   public Customers getCustomer(int index){
        if (index < 0 || index >= total){
            return null;
        }
        return customers[index];
   }

    /**
     *
     * @param index 要被取代的某客户的索引
     * @param cus 要被取代的某客户的地址
     * @return 是否能取代成功
     */
    public boolean replaceCustomer(int index, Customers cus) {
        if (index < 0 || index >= total) return false;
        customers[index] = cus;
        return true;
    }

    /**
     *
     * @param customer 要被删除的某个用户
     * @return 是否删除成功
     */
    public boolean deleteCustomer(int index,Customers customer){
        if (index < 0 || index >= total){
            return false;
        }
        else {
            for (int i = index;i >= 0;i--){
                customers[i] =customers[i+1];
            }
        }
        customers[--total] = customer;
        return true;
    }
    public Customers[] getAllCustomers() {
        Customers[] custs = new Customers[total];
        for (int i = 0; i < total; i++) {
            custs[i] = customers[i];
        }
        return custs;
    }
}

CustomerView类

package ui;
import bean.Customers;
import service.CustomerList;
import util.Util;
/**
 * @author
 * @Description CustomerView为主模块,负责菜单的显示和处理用户操作
 * @date 2022/5/3 16:49
 */
public class CustomerView {
    CustomerList customerl = new CustomerList(10);
    public CustomerView(){
        Customers cus1 = new Customers("张三",'男',18,"12345678","12345@qq.com");
        customerl.addCustomer(cus1);
    }
    public static void main(String[] args) {
        CustomerView customerView = new CustomerView();
        customerView.enterMenu();
    }
    public void enterMenu() {
        boolean isFlag = true;
        do{
        System.out.println("--------客户信息管理软件--------");
        System.out.println("\t\t1 添 加 客 户");
        System.out.println("\t\t2 修 改 客 户");
        System.out.println("\t\t3 删 除 客 户");
        System.out.println("\t\t4 客 户 列 表");
        System.out.println("\t\t5 退       出");
        System.out.println();
        System.out.println("\t\t请选择(1-5):_");
        char choice = Util.readMenuSelect();
        switch (choice){
            case '1':
                addNewCustomer();
                break;
            case '2':
                modifyCustomer();
                break;
            case '3':
                deleteCustomer();
                break;
            case '4':
                listCustomer();
                break;
            case '5':
                System.out.println("是否确认退出");
                char select = Util.readConfirmSelection();
                if (select == 'y'){
                    isFlag = false;
                }
                else
                    break;


        }
        }while (isFlag);
    }
    public void addNewCustomer(){
        System.out.println("------添加客户--------");
        System.out.print("姓名 : ");
        String name = Util.readString(4);
        System.out.print("性别 : ");
        char gender = Util.readString(1).charAt(0);
        System.out.print("年龄 : ");
        int age = Util.readInt();
        System.out.print("电话 : ");
        String phone = Util.readString(15);
        System.out.println("邮箱 : ");
        String email = Util.readString(15);

        Customers cus = new Customers(name,gender,age,phone,email);
        boolean isTrue =customerl.addCustomer(cus);
        if (isTrue){
            System.out.println("------添加成功------");
        }
        else{
            System.out.println("添加失败,已经没有多余空间了");
        }
    }
    public void modifyCustomer(){
        System.out.println("------修改用户-------");
        int index;
        Customers cus;
        for (;;){
            System.out.println("请选择待修改的客户编号(-1退出)");
            index = Util.readInt();
            if (index == -1){
                return;
            }
            cus = customerl.getCustomer(index -1 );
            if (cus == null){
                System.out.println("无法找到改用户");
            }
            else break;
        }
        System.out.println("姓名("+cus.getName()+"):");
        String name = Util.readString(4,cus.getName());

        System.out.println("性别("+cus.getGender()+"):");
        char gender = Util.readChar(1,cus.getGender());

        System.out.println("年龄("+cus.getAge()+"):");
        int age = Util.readInt(cus.getAge());

        System.out.println("电话("+cus.getPhone()+"):");
        String phone = Util.readString(11,cus.getPhone());

        System.out.println("邮箱("+cus.getEmail()+"):");
        String email = Util.readString(12,cus.getEmail());

        cus = new Customers(name,gender,age,phone,email);
        boolean isTrue = customerl.replaceCustomer(index - 1,cus);
        if (isTrue){
            System.out.println("---------修改成功-------");
        }
        else {
            System.out.println("----------无法找到指定客户,修改失败--------------");
        }
    }
    public void deleteCustomer(){
        int index = 0;
        Customers cus;
        System.out.println("-----删除客户------");
        for (;;){
            System.out.println("请选择待删除客户编号(-1退出)");
            index = Util.readInt();
            if (index == -1){
                return;
            }
            cus = customerl.getCustomer(index - 1);
            if (cus == null){
                System.out.println("不存在此用户");
            }
            else break;
        }
        System.out.println("确认是否删除(Y/N)");
        char select = Util.readConfirmSelection();
        if (select == 'n'){
            return;
        }
        else {
            boolean isTrue = customerl.deleteCustomer(index - 1,cus);
            if (isTrue == true){
                System.out.println("-------删除成功------");
            }
            else {
                System.out.println("未找到改用户");
            }
        }
    }
    public void listCustomer(){
        System.out.println("------客户列表------");
        Customers[] cust = customerl.getAllCustomers();
        if (cust.length == 0){
            System.out.println("没有客户记录");
        }
        else {
                System.out.println("编号\t姓名\t性别\t年龄\t\t电话\t\t邮箱");
                for (int i = 0; i < cust.length; i++) {
//            System.out.println(i + 1 + "\t" + custs[i].getName() + "\t" + custs[i].getGender() + "\t" + custs[i].getAge() + "\t\t" + custs[i].getPhone() + "\t" + custs[i].getEmail());
                    System.out.println((i+1) + "\t" + cust[i].getDetail());
                }
        }

    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值