java管理系统[最完善]【大一上】

1.工具类

import java.util.Scanner;

public class CMUtity {
    public static Scanner scanner = new Scanner(System.in);

    public static String readKeyBoard(int limit, boolean broken) {
        String line = "";
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            if (line.length() == 0) {
                if (broken) {
                    return line;
                } else {
                    continue;
                }
            }
            if (line.length() < 1 || line.length() > limit) {
                System.out.println("输入的数据错误,请重新输入");
                continue;
            }
            break;
        }
        return line;
    }

    public static char readNumber() {
        char c;
        for (; ; ) {
            String arr = readKeyBoard(1, false);
            c = arr.charAt(0);
            if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5') {
                System.out.println("输入错误,请重新输入");
            } else {

                break;
            }

        }
        return c;
    }

    public static char comfirmChar() {
        char c;
        for (; ; ) {
            String arr = readKeyBoard(1, false).toUpperCase();
            c = arr.charAt(0);
            if (c != 'Y' && c != 'N') {
                System.out.println("输入错误,请重新输入");
            } else {
                break;
            }
        }
        return c;
    }

    public static String readString(int limit) {
        String arr = readKeyBoard(limit, false);
        return arr;
    }

    public static String readString(int limit, String broken) {
        String arr = readKeyBoard(limit, true);
        return (arr.length() == 0) ? broken : arr;
    }

    public static int readInt() {
        int c;
        for (; ; ) {
            try {
                String arr = readKeyBoard(2, false);
                c = Integer.parseInt(arr);
                break;
            } catch (NumberFormatException e) {
                System.out.println("输入错误");
            }
        }
        return c;
    }

    public static int readInt(int broken) {
        int n;
        for (; ; ) {
            String arr = readKeyBoard(2, true);
            if (arr.equals("")) {
                return broken;
            }
            try {
                n = Integer.parseInt(arr);
                break;
            } catch (NumberFormatException e) {
                System.out.println("输入的数据有错,重新输入");
            }
        }
        return n;
    }

    public static char readChar() {
        String arr = readKeyBoard(1, false);
        return arr.charAt(0);
    }

    public static char readChar(char broken)
    {
        String arr = readKeyBoard(1,true);
        return (arr.length() == 0)?broken:arr.charAt(0);
    }
}

2.bean类

public class Customer
{
    String name;
    char gender;
    int age;
    String phone;
    String 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 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;
    }
}

3.处理业务层

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

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

    public boolean addCustomer(Customer customer)
    {
        if (total >= customers.length)
        {
            return false;
        }
        customers[total++] = customer;
        return true;
    }

    public boolean deleteCustomer(int index)
    {
        if (index < 0 || index > total)
        {
            return false;
        }
        for (int i = index;i < total;i++)
        {
            customers[index] = customers[index++];
        }
        customers[--total] = null;
        return true;
    }

    public boolean reapCustomer(int index,Customer cust)
    {
        if (index < 1 || index > total)
        {
            return false;
        }
        customers[index] = cust;
        return true;
    }

    public Customer[] ListAllCustomer()
    {
        Customer customer[];
        customer = customers;
        return customer;
    }

    public Customer getCustomer(int index)
    {
        return customers[index];
    }

    public int getTotal()
    {
        return total;
    }
}

4.页面层

import java.nio.charset.StandardCharsets;

public class CustomerView
{
    public static CustomerList customerList = new CustomerList(100);

    public static void main(String[] args) {
        CustomerView customerView = new CustomerView();
        customerView.enterMenu();

    }

    private void enterMenu()
    {
        boolean isFlsg = true;
        while (isFlsg)
        {
            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("输入操作编号:");
            int num = CMUtity.readNumber();
            switch (num)
            {
                case '1':
                    addCustomer();
                    break;
                case '2':
                    deleteCuotomer();
                    break;
                case '3':
                    reapCustomer();
                    break;
                case '4':
                    ListAllCustomer();
                    break;
                case '5':
                    System.out.print("是否选择退出(Y/N)");
                    char isExit = CMUtity.comfirmChar();
                    if (isExit == 'Y')
                    {
                        break;
                    }
            }
        }
    }

    private void addCustomer()
    {
        System.out.println("*********增加客户************");
        System.out.print("姓名:");
        String name = CMUtity.readString(5);
        System.out.print("性别:");
        char gender = CMUtity.readChar();
        System.out.print("年龄:");
        int age = CMUtity.readInt();
        System.out.print("电话:");
        String phone = CMUtity.readString(30);
        System.out.print("邮箱:");
        String email = CMUtity.readString(40);
        Customer customer = new Customer(name,gender,age,phone,email);
        boolean isFlag = customerList.addCustomer(customer);
        if (isFlag)
        {
            System.out.println("添加成功");
        }else
        {
            System.out.println("添加失败");
        }
    }

    private void deleteCuotomer() {
        System.out.println("删除客户");
        int total = customerList.getTotal();
        int index;
        for (; ; ) {
            System.out.print("请选择编号(-1退出):");
            index = CMUtity.readInt();
            if (index-1 < 0 || index -1>= total) {
                System.out.println("输入编号错误");
            } else if (index == -1) {
                return;
            } else {
                break;
            }
        }
            System.out.print("是否选择删除(Y/N):");
            char a = CMUtity.comfirmChar();
            if (a == 'Y') {
                {
                    boolean isSucced = customerList.deleteCustomer(index - 1);
                    if (isSucced) {
                        System.out.println("删除成功");
                    } else {
                        System.out.println("删除失败");
                    }
                }
            }}


            private void reapCustomer()
            {
                System.out.println("**********修改客户信息**********");
                Customer customer;
                int num;
                int total = customerList.getTotal();
                for (;;)
                {
                    System.out.print("输入客户编号(-1退出)");
                    num = CMUtity.readInt();
                    if (num == -1)
                    {
                        return;
                    }
                    customer = customerList.getCustomer(num - 1);
                    if (customer == null)
                    {
                        System.out.println("无法找到该用户");
                    }else
                    {
                        break;
                    }
                    }
                System.out.print("姓名:");
                String name = CMUtity.readString(5,customer.getName());
                System.out.print("性别:");
                char gender = CMUtity.readChar(customer.getGender());
                System.out.print("年龄:");
                int age = CMUtity.readInt(customer.getAge());
                System.out.print("电话:");
                String phone = CMUtity.readString(30,customer.getPhone());
                System.out.print("邮箱:");
                String email = CMUtity.readString(30,customer.getEmail());

                Customer customer1 = new Customer(name,gender,age,phone,email);
                boolean isSucced = customerList.reapCustomer(num,customer1);
                if (isSucced)
                {
                    System.out.println("修改成功");
                }else
                {
                    System.out.println("修改失败");
                }
            }

            private void ListAllCustomer ()
            {
                System.out.println("*********客户列表*********");
                int n = customerList.getTotal();
                if (n == 0)
                {
                    System.out.println("没有客户信息");
                }else
                {
                    Customer[] cust = customerList.ListAllCustomer();
                    Customer customer;
                    System.out.println("姓名\t\t性别\t年龄\t电话\t\t\t邮箱");
                    for (int i = 0;i < n;i++)
                    {
                        customer = cust[i];
                        System.out.println(customer.getName() + "\t" + customer.getGender() + "\t" + customer.getAge()+ "\t" + customer.getPhone() + "\t" + customer.getEmail());
                    }
                    System.out.println("客户列表完成");
                }
            }
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海边的彩虹与你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值