客户信息管理软件

本文介绍了一个简单的Java实现的客户管理系统,包括客户信息的增删改查功能。系统通过控制台交互方式,利用Scanner类读取用户输入,并实现了基本的错误处理。

1.工具类

package com.oracle.comm;
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;
    }
    //只读取一个char
    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;
    }
    //读取2位以内整数。若不输入返回默认值。
    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;
    }
    //输出Str 至少limit 位
    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }
    //至少limit位 字符串,若不输入默认为defaultValue,  
    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }
    //验证结果是'Y' or 'N'
    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;
    }
    //获取输入的字符串,如果blankReturn = true,输入字符为空,
    //               balnkReturn = flase,  输入字符    取值范围为(1-limit)  
    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;
    }
}

2.实体类

package com.oracle.model;

public class Customer {

    private String name;
    private char sex;
    private int age;
    private String tel;
    private String email;

    public Customer(String name, char sex, int age, String tel, String email) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.tel = tel;
        this.email = email;
    }
    public Customer() {
        // TODO Auto-generated constructor stub
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public char getSex() {
        return sex;
    }
    public void setSex(char sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

3.customerList类

package com.oracle.customerList;

import com.oracle.model.Customer;

public interface CustomerListimpl {

    public boolean addCustomer(Customer customer) ;
    public boolean replaceCustomer(int index, Customer cust);
    public boolean deleteCustomer(int index);
    public Customer[] getAllCustomers() ;
    public Customer getCustomer(int index);
}
package com.oracle.customerList;

import com.oracle.model.Customer;

public class CustomerList implements CustomerListimpl{
    Customer[] customers;//用来保存客户对象的数组
    public int total = 0  ;//记录已保存客户对象的数量
    //使用构造方法初始化数组
    public CustomerList(int totalCustomer) {
        customers= new Customer[totalCustomer];
        customers[0]=new Customer("张三", '男', 30, "010-52776920","bc@email.com" );
        customers[1]=new Customer("莉莉", '男', 30, "010-52776920","bc@email.com" );
        customers[2]=new Customer("王芳", '女', 30, "010-52776920","bc@email.com" );
        total =3;  
    }
    //添加客户
    @Override
    public boolean addCustomer(Customer customer) {
        // TODO Auto-generated method stub
        if(total>=0&&total<customers.length){
            customers[total++]=customer;

            return true;
        }else{
            return false;
        }
    }
    //修改客户
    @Override
    public boolean replaceCustomer(int index, Customer cust) {
        // TODO Auto-generated method stub
        if(index<=total&&total>0){
            customers[index-1] =cust;
            return true;    
        }
        return false;
    }
    //删除客户
    @Override
    public boolean deleteCustomer(int index) {
        // TODO Auto-generated method stub
        if(total<=customers.length){
            if(index > 0&&index<=total){
                for(int i =index-1; i<total-1;i++){
                    customers[i] = customers[i+1];

                } 
                total--;
                return true;
            }else if(index==total){
                customers[index-1] = null;
                total--;
                return true;
            }
        }
        return false;

    }
    //查询所有客户
    @Override
    public Customer[] getAllCustomers() {
        // TODO Auto-generated method stub

        return customers;
    }
    //查询指定id客户
    @Override
    public Customer getCustomer(int index) {
        // TODO Auto-generated method stub

            if(index>0&&index<=total){
                return customers[index-1];
            }

        return null;
    }


}

4.列表内容

package com.oracle.customerview;

import com.oracle.comm.CMUtility;
import com.oracle.customerList.CustomerList;
import com.oracle.model.Customer;

public class ViewCustomer {

    public static void main(String[] args) {
        //判断程序进程
        boolean booflag=true;

        //实例化CustomerList
        CustomerList customerList = new CustomerList(10);

        do{//使用循环
            meun();
            new CMUtility();
            char num=CMUtility.readMenuSelection();
            //选择使用具体的方法
            switch (num) {
            case '1':
                booflag=customerList.addCustomer(addCustomer());
                break; 
            case '2':
                booflag=updateCustomer(customerList);
                break;
            case '3':
                booflag=deleteCustomer(customerList);
                break;
            case '4':
                booflag=findCustomer(customerList);
                break;
            default:
                booflag=exit();
                break;
            }

        }while(booflag);
    }

    //主菜单
    public static  void meun(){
        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.print("请选择(1-5)—— ");
    }
    //添加客户菜单
    public  static Customer addCustomer(){
        System.out.println("---------------------添加客户---------------------");

        System.out.print("姓名:");
        String name = CMUtility.readString(6);

        System.out.print("性别:");
        char sex = CMUtility.readChar('男');

        System.out.print("年龄:");
        int age=CMUtility.readInt(0);

        System.out.print("电话:");
        String tel = CMUtility.readString(11);

        System.out.print("邮箱:");
        String email = CMUtility.readString(28);

        System.out.println("---------------------添加完成---------------------");
        return new Customer(name, sex, age, tel, email);
    }
    //修改客户菜单
    public static boolean updateCustomer(CustomerList customerList){
        System.out.println("---------------------修改客户---------------------");
        System.out.println("请选择待修改客户编号(-1退出):");

        int id = CMUtility.readInt();//输入2位以内整数
        if((id<0&&id>customerList.total)){
            System.out.println("请重新输入修改客户编号:");
            System.out.println("提示:输入(1-"+customerList.total+")的编号");
        }else if(id==-1){
            System.out.println("---------------------退出(返回主菜单)---------------------");

        }else {
            Customer c=customerList.getCustomer(id);
            System.out.print("姓名("+ c.getName()+")");
            String name = CMUtility.readString(6);
            c.setName(name);

            System.out.print("性别("+ c.getSex()+")");
            char sex = CMUtility.readChar(c.getSex());
            c.setSex(sex);

            System.out.print("年龄("+ c.getAge()+")");
            int age=CMUtility.readInt();
            c.setAge(age);

            System.out.print("电话("+ c.getTel()+")");
            String tel = CMUtility.readString(11);
            c.setTel(tel);
            System.out.print("邮箱("+ c.getEmail()+")");
            String email = CMUtility.readString(28);
            c.setEmail(email);

            System.out.println("---------------------修改完成---------------------");

        }

        return true;
    }

    //删除客户菜单
    public static boolean deleteCustomer(CustomerList customerList){
        System.out.println("---------------------删除客户---------------------");
        System.out.print("请选择待删除客户编号(-1退出):");
        int id = CMUtility.readInt();

        if((id<0&&id>customerList.total)){
                System.out.println("请重新输入删除客户编号:");
                System.out.println("提示:请输入(0-"+customerList.total+")的编号");
        }else if(id==-1){
                System.out.println("---------------------退出(返回主菜单)---------------------");

        }else {
            System.out.println("确认是否删除Y/N?");
            char yesOrNo = CMUtility.readConfirmSelection();
            if(yesOrNo=='Y'){
                customerList.deleteCustomer(id);
                System.out.println("---------------------删除完成---------------------");
            }else{
                    System.out.println("---------------------删除取消---------------------");
            }

        }
        return true;
    }

    //查询客户列表
    public static boolean findCustomer(CustomerList customerList){

        System.out.println("\n---------------------客户列表---------------------");
        System.out.print("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
        Customer[] allCustomers = customerList.getAllCustomers();
        System.out.println(customerList.total);
        for (int i = 1; i <= customerList.total; i++) {
            Customer c =allCustomers[i-1];
            System.out.println(i+"\t"+c.getName()+"\t"+c.getSex()+"\t"+c.getAge()+"\t"+c.getTel()+"\t\t"+c.getEmail());
        }
        System.out.println("---------------------客户列表完成---------------------");
        return true;
    }

    //退出系统
    public static boolean exit(){
        System.out.println("---------------------退出系统---------------------");
        return false;
    }

}

项目需求:传送们
升级版优秀代码项目源码:传送门

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值