java11-项目1:用户管理系统

1.项目需求:

java11-项目1:用户管理系统_第1张图片

 2.项目设计:

java11-项目1:用户管理系统_第2张图片

 3.项目架构:

java11-项目1:用户管理系统_第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","[email protected]");
        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 类,根据界面提示进行 新增,修改,查询,删除等操作

你可能感兴趣的:(java,java,servlet,算法)