java11-项目1:用户管理系统
创始人
2024-05-29 16:40:23
0

1.项目需求:

 2.项目设计:

 3.项目架构:

 4.具体实现代码:

1)工具类:CMUtility

import java.util.*;

public class CMUtility {
    
    private static Scanner scanner = new Scanner(System.in);
    public  static char readMenuSelection() {
        char c;
        for(;;) {
            String str=readKeyBord(1,false);
            c=str.charAt(0);
            if(c!='1'&&c!='2'&&c!='3'&&c!='4'&c!='5') {
                System.out.println("选择错误请重新输入");
            }else  break;
        }
        return c;
            
        }
    //从键盘读取一个字符,并将其作为方法的返回值
    public static char readChar() {
        String str=readKeyBord(1,false);
        return str.charAt(0);
    }
    //从键盘读取一个字符,并将其作为方法的返回值,如果用户不输入字符而直接回车,方法将以defalutValue作为返回值
    public static char readChar(char defalutValue) {
        String str=readKeyBord(1,true);
        return (str.length()==0) ?defalutValue:str.charAt(0);
    }
    //从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值
    public static int readInt() {
        int n;
        for(;;) {
            String str=readKeyBord(2,false);
            try {
                n =Integer.parseUnsignedInt(str);
                break;
        }catch(NumberFormatException e) {
            System.out.println("数字输入错误,请重新输入");
        }
        
    }
    return n;
    }
    //从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值,如果用户不输入而直接回车,方法将以defalutValue作为返回值
        public static int readInt(int defalutValue) {
            int n;
            for(;;) {
                String str=readKeyBord(2,false);
                try {
                    n =Integer.parseUnsignedInt(str);
                    break;
            }catch(NumberFormatException e) {
                System.out.println("数字输入错误,请重新输入");
            }
            
        }
        return n;
        }
        //从键盘读取一长度不超过limit的字符串,并将其作为方法的返回值
        public static String readString(int limit) {
        
            return readKeyBord(limit,false);
}
        
        //从键盘读取一长度不超过limit的字符串,并将其作为方法的返回值,如果用户不输入字符而直接回车,方法将以defalutValue作为返回值
        public static String readString(int limit,String defalutValue ) {
            String str=readKeyBord(limit,true);
            return str.equals("") ? defalutValue:str;
}
        //用于确认选择的输入。该方法从键盘读取‘Y’或‘N’,并将其作为方法的返回值
        
        public  static char readConfirmSelection() {
            char c;
            for(;;) {
            
            String str=readKeyBord(1,false).toUpperCase();
            c=str.charAt(0);
            if(c=='Y'||c=='N') {
                break;}
            else {
                System.out.println("选择错误,请重新输入");
            }
            }
            return c;
        
    
        }
        
private static  String  readKeyBord(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.println("输入长度(不大于" + limit + ")错误,请重新输入");
                    continue;
                }
                break;
                
                }
            return line;
            }
            
        }
    2)    javabean:Customer

public class Customer {

     private String name;
     private char gender;
     private int age;
     private String phone;
     private String email;
     
     public Customer() {
        super();
    }
     
    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;
    }

    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;
    }
    
}
3)后台服务类:CustomerList 

public class CustomerList {
    private Customer[] customers;//用来保存客户对象的数组
    private int total=0;//记录保存客户的数量
    //初始化数组长度
    public CustomerList(int totalCustomer) {
        customers= new Customer[totalCustomer];
    }
    //添加客户
    public boolean addCustomer(Customer cust) {
        if(total>=customers.length) {
            return false;
        }
        customers[total]=cust;
        total++;
        return true;
    }//修改客户
    public boolean replaceCustoer(int index,Customer cust) {
        if(index<0||index>=total) {
            return false;
        }
        customers[index]=cust;
        return true;
        
    }//删除客户
    public boolean delteCustomer(int index) {
        if(index<0||index>total) {
            return false;
        }
          for(int i=index;i
              customers[i]=customers[i+1];
              
          }
          customers[total-1]=null;
          total--;
          return true;
    }//获取所有客户
    public Customer[] getAllCustomers() {
        Customer[] cust= new Customer[total];
        for (int i=0;i
            cust[i]=customers[i];
        }
        
        return cust;
    }
    //获取指定客户
    public  Customer getCustomer(int index) {
        if(index<0||index>total) {
            return null;
        }
        return customers[index];
        
    }//获取客户数量
    public int getTotal() {
        return total;
    }
    

}
4)前端展示类:CustomerView 

public class CustomerView {
    private CustomerList customerlist= new CustomerList(10);
    
    public CustomerView() {
        Customer cust =new Customer("张三",'男',20,"13917342345","zhangsan@qq.com");
        customerlist.addCustomer(cust);
    }
    public static void main(String[] args) {
        CustomerView view =new CustomerView();
        view.enterMainMenum();
        
    }

    public void enterMainMenum() {
        
        boolean isflag=true;
        while(isflag) {
        
        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:");
         char  menu=com.cn.project01.util.CMUtility.readMenuSelection();
         switch(menu) {
         case '1':
             addNewCustomer();
             break;
         case '2':
             modifyCustomer(); 
             break;
         case '3':
             deleteCustomer();
             break;
         case '4':
             listAllCustomer();
             break;
         case '5':
             System.out.println("确认退出请输入Y,否输入N \n");
         char readConfirmSelection = CMUtility.readConfirmSelection();
         
         if(readConfirmSelection=='Y') {
             isflag=false;
         }
             
         }
    }
    }
    //新增客户
    public void addNewCustomer() {
        System.out.println("***************添加客户***************************");
        System.out.println("姓名:\n");
        String name=CMUtility.readString(10);
        System.out.println("性别:\n");
        char gender=CMUtility.readChar();
        System.out.println("年龄:\n");
        int age=CMUtility.readInt();
        System.out.println("电话:\n");
        String phone=CMUtility.readString(11);
        System.out.println("邮箱:\n");
        String email=CMUtility.readString(20);
        Customer cust =new Customer(name,gender,age,phone,email);
    boolean addCustomer = customerlist.addCustomer(cust);
        if(addCustomer) {
            System.out.println("添加成功!");
        }else {
            System.out.println("列表已满,不能添加!");
        }
        
    }
    //修改客户
    public void modifyCustomer() {
         Customer customer;
         int number;
        for(;;) {
        System.out.println("请输入修改客户的编号(-1退出)");
         number=CMUtility.readInt();
        if(number==-1) {
            return ;
        }
         customer = customerlist.getCustomer(number-1);
         if(customer ==null) {
             System.out.println("无该客户");
         }else {
             break;
         }
        
    }
        //修改客户信息
        System.out.println("姓名:"+customer.getName());
        String name=CMUtility.readString(10, customer.getName());
        System.out.println("性别:"+customer.getGender());
        char gender=CMUtility.readChar(customer.getGender());
        System.out.println("年龄:"+customer.getAge());
        int age =CMUtility.readInt(customer.getAge());
        System.out.println("电话:"+customer.getPhone());
        String phone=CMUtility.readString(10, customer.getPhone());
        System.out.println("邮箱:"+customer.getEmail());
        String email=CMUtility.readString(10, customer.getEmail());
        Customer newcust= new Customer(name,gender,age,phone,email);
        boolean replaceCustoer = customerlist.replaceCustoer(number-1, newcust);
        if(replaceCustoer) {
            System.out.println("修改成功!");
        }else {System.out.println("修改失败!");}
         
        
    }
    
    
    //查询客户
    public  void listAllCustomer() {
        
        System.out.println("*******客户信息开始展示******************");
        
         int total =customerlist.getTotal();
         if(total==0) {
             System.out.println("客户信息不存在");
         }else {
             System.out.println("编号 \t姓名\t 性别 \t 年龄 \t 手机号 \t 邮箱");
             Customer[] custs=customerlist.getAllCustomers();
             for(int i=0;i
                 Customer cust= custs[i];
                 System.out.println((i+1)+ "\t"+cust.getName()+ "\t"+cust.getGender()+ "\t"+cust.getAge()+ "\t"+cust.getPhone()+ "\t"+cust.getEmail());
             }
         }
            
        System.out.println("*******客户信息结束展示******************");
    }
    //删除客户
    public void deleteCustomer() {
        int number;
        for(;;) {
        System.out.println("************删除客户***********");
    
        System.out.println("请输入修改客户的编号(-1退出)");
        number =CMUtility.readInt();
        if(number==-1) {
            return ;
        }else {
            break;
        }
    }
        System.out.println("确认是否删除 确认Y ,不删除N");
        char readConfirmSelection = CMUtility.readConfirmSelection();
        if(readConfirmSelection=='Y') {
            boolean delteCustomer = customerlist.delteCustomer(number-1);
            if(delteCustomer) {
                System.out.println("删除成功!");
            }else {
                System.out.println("删除失败!");
            }
            
        }
        
        
    }
    
}
5)测试:

执行CustomerView 类,根据界面提示进行 新增,修改,查询,删除等操作

相关内容

热门资讯

122.(leaflet篇)l... 听老人家说:多看美女会长寿 地图之家总目录(订阅之前建议先查看该博客) 文章末尾处提供保证可运行...
育碧GDC2018程序化大世界... 1.传统手动绘制森林的问题 采用手动绘制的方法的话,每次迭代地形都要手动再绘制森林。这...
育碧GDC2018程序化大世界... 1.传统手动绘制森林的问题 采用手动绘制的方法的话,每次迭代地形都要手动再绘制森林。这...
Vue使用pdf-lib为文件... 之前也写过两篇预览pdf的,但是没有加水印,这是链接:Vu...
PyQt5数据库开发1 4.1... 文章目录 前言 步骤/方法 1 使用windows身份登录 2 启用混合登录模式 3 允许远程连接服...
Android studio ... 解决 Android studio 出现“The emulator process for AVD ...
Linux基础命令大全(上) ♥️作者:小刘在C站 ♥️个人主页:小刘主页 ♥️每天分享云计算网络运维...
再谈解决“因为文件包含病毒或潜... 前面出了一篇博文专门来解决“因为文件包含病毒或潜在的垃圾软件”的问题,其中第二种方法有...
南京邮电大学通达学院2023c... 题目展示 一.问题描述 实验题目1 定义一个学生类,其中包括如下内容: (1)私有数据成员 ①年龄 ...
PageObject 六大原则 PageObject六大原则: 1.封装服务的方法 2.不要暴露页面的细节 3.通过r...
【Linux网络编程】01:S... Socket多进程 OVERVIEWSocket多进程1.Server2.Client3.bug&...
数据结构刷题(二十五):122... 1.122. 买卖股票的最佳时机 II思路:贪心。把利润分解为每天为单位的维度,然后收...
浏览器事件循环 事件循环 浏览器的进程模型 何为进程? 程序运行需要有它自己专属的内存空间࿰...
8个免费图片/照片压缩工具帮您... 继续查看一些最好的图像压缩工具,以提升用户体验和存储空间以及网站使用支持。 无数图像压...
计算机二级Python备考(2... 目录  一、选择题 1.在Python语言中: 2.知识点 二、基本操作题 1. j...
端电压 相电压 线电压 记得刚接触矢量控制的时候,拿到板子,就赶紧去测各种波形,结...
如何使用Python检测和识别... 车牌检测与识别技术用途广泛,可以用于道路系统、无票停车场、车辆门禁等。这项技术结合了计...
带环链表详解 目录 一、什么是环形链表 二、判断是否为环形链表 2.1 具体题目 2.2 具体思路 2.3 思路的...
【C语言进阶:刨根究底字符串函... 本节重点内容: 深入理解strcpy函数的使用学会strcpy函数的模拟实现⚡strc...
Django web开发(一)... 文章目录前端开发1.快速开发网站2.标签2.1 编码2.2 title2.3 标题2.4 div和s...