Java学习第十五天(小总结)

一、今天干了什么

完成了客户信息管理系统!虽然磕磕绊绊,断断续续做了好几天,中途思路经常受阻,还得回头看尚硅谷的教学视频,不过做完之后成就感爆棚。

(测试截图太多,不往上放了)

分成了四个包,每个包里有一个类,通过import关键字来让这几个类相互关联、相互调用。

 

Customer类用于存储用户信息;CustomerList类是对象管理模块,提供检验增加客户、修改指定客户信息、删除指定客户信息、查看所有客户信息、获取存储的客户数量是否成功的布尔类型方法以便CustomerView类调用;CustomerView负责菜单的显示和客户操作方面的方法,并且主方法放在这里;CMUtitlity类是工具类(难度较大,没有自己写,直接从网上扒的尚硅谷老师的代码)。

 

 

 

代码如下:

Customer类:

package pers.yan.PersonInformation;

public class Customer {
    private String name;
    private char sex;
    private int age;
    private String phone;
    private String email;
    public Customer()
    {
        
    }
    public Customer(String name,char sex,int age,String phone,String email)
    {
        this.age = age;
        this.name = name;
        this.sex = sex;
        this.phone = phone;
        this.email = email;
    }

    public void setAge(int age)
    {
        this.age = age;
    }
    public int getAge()
    {
        return age;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return name;
    }
    public void setSex(char sex)
    {
        this.sex = sex;
    }
    public char getSex()
    {
        return sex;
    }
    public void setPhone(String phone)
    {
        this.phone = phone;
    }
    public String getPhone()
    {
        return phone;
    }
    public void setEmail(String email)
    {
        this.email = email;
    }
    public String getEmail()
    {
        return email;
    }
}

CustomerList类:

package pers.yan.service;

import pers.yan.PersonInformation.Customer;

public class CustomerList {
    private Customer[] customers;
    private int total = 0;    //已保存客户对象的数量
    
    public CustomerList(int num)    //构造器
    {
        customers = new Customer[num];
    }
    public boolean addCustomer(Customer customer)    //添加用户
    {
        if(total >= customers.length)
        {
            return false;
        }
        customers[total] = customer;
        total++;
        return true;
    }
    public boolean replaceCustomer(int index, Customer cust)    //修改用户信息
    {
        if(index < 0 || index >= total)
        {
            return false;
        }
        customers[index] = cust;
        return true;
    }
    
    public boolean deleteCustomer(int index)    //删除用户信息
    {
        if(index < 0 || index >= total)
        {
            return false;
        }
        for(int i = index;i < total -1;i++)
        {
            customers[i]=customers[i+1];
        }
        customers[total - 1] = null;
        total--;
        return true;
    }
    
    public Customer[] getAllCustomers()    
    {
        Customer[] custs = new Customer[total];
        for(int i = 0;i < total;i++)
        {
            custs[i] = customers[i];
        }
        return custs;
    }
    
    public Customer getCustomer(int index)
    {
        if(index < 0 || index >= total)
        {
            return null;
        }
        return customers[index];
    }
    
    public int getTotal()    //获取存储的客户数量
    {
        return total;
    }
    
}

CustomerView类:

package pers.yan.ui;

import pers.yan.PersonInformation.*;
import pers.yan.service.CustomerList;
import pers.yan.util.*;


public class CustomerView {
    private  CustomerList customerList = new CustomerList(10);
    
    public CustomerView()
    {
        Customer customer = new Customer("闫世豪",'男',19,"13242314256","3332831867@qq.com");
        customerList.addCustomer(customer);
    }
    public void enterMainMenu()        //显示《客户信息管理系统》页面
    {
        boolean isFlag = true;
        while(isFlag)
        {
            System.out.println("\n---------------------客户信息管理软件-----------------------");
            System.out.println("                       1.添加客户");
            System.out.println("                       2.修改客户");
            System.out.println("                       3.删除客户");
            System.out.println("                       4.客户列表");
            System.out.println("                       5.退出");
            System.out.println("                    请选择(1-5):");
            
            char menu = CMUtitlity.readMenuSelection();
            switch(menu)
            {
            case '1':
                addNewCustomer();
                break;
            case '2':
                modifyCustomer();
                break;
            case '3':
                deleteCustomer();
                break;
            case '4':
                listAllCustomers();
                break;
            case '5':
                System.out.print("确认是否退出(Y/N)");
                char isExit = CMUtitlity.readConfirm();
                if(isExit == 'Y')
                {
                    isFlag = false;
                    
                }
                
                break;
            }
            
        }
    }
    private void addNewCustomer()    //添加客户
    {
        System.out.println("添加客户的操作");
        System.out.println("------------------------------添加客户----------------------------");
        
        System.out.println("姓名:");
        String name = CMUtitlity.readString(10,null);
        
        System.out.println("性别:");
        char gender = CMUtitlity.readChar('0');
        
        System.out.println("年龄:");
        int age = CMUtitlity.readInteger(0);
        
        System.out.println("电话:");
        String phone = CMUtitlity.readString(13, null);
        
        System.out.println("邮箱:");
        String email = CMUtitlity.readString(30, null);
        
        Customer newone = new Customer(name,gender,age,phone,email);//封装进入一个新的对象
        
        
        boolean isSuccess = customerList.addCustomer(newone);
        if(isSuccess)
        {
            System.out.println("----------------------------添加完成-----------------------------");
        }
        else
        {
            System.out.println("----------------------------客户目录已满,添加失败---------------------------");
        }
    }
    private void modifyCustomer()    //修改客户信息
    {
        System.out.println("修改客户信息的操作");
        System.out.println("----------------------------修改客户-----------------------------");
        int number;
        Customer dust;
        for(;;)
        {
        System.out.println("请选择待修改客户的编号(-1退出):");
        number = CMUtitlity.readInteger(0);
        
            if(number == -1)
            {
                return;
            }
            
            dust = customerList.getCustomer(number - 1);
            
            if(dust == null)
            {
                System.out.println("无法找到指定客户!");
            }
            else    //找到了对应客户
            {
                break;
            }
        }
        //修改客户信息
        System.out.println("姓名(" + dust.getName() + ")");
        String name = CMUtitlity.readString(10,dust.getName());
        
        System.out.println("性别(" + dust.getSex() + ")");
        char sex = CMUtitlity.readChar(dust.getSex());
        
        System.out.println("年龄(" + dust.getAge() + ")");
        int age = CMUtitlity.readInteger(dust.getAge());
        
        System.out.println("电话(" + dust.getPhone() + ")");
        String phone = CMUtitlity.readString(13,dust.getPhone());
        
        System.out.println("邮箱(" + dust.getEmail() + ")");
        String email = CMUtitlity.readString(30,dust.getEmail());
        
        Customer newCust = new Customer(name,sex,age,phone,email);
        
        
        boolean isReplaced = customerList.replaceCustomer(number - 1,newCust);
        if(isReplaced)
        {
            System.out.println("-------------------------------修改完成-------------------------------");
        }
        else
        {
            System.out.println("-------------------------------修改失败-------------------------------");
        }
    }
    
    private void deleteCustomer()
    {
        System.out.println("删除客户的操作");
        System.out.println("------------------------------------删除客户---------------------------------");
        int num;
        for(;;)
        {
            System.out.println("请选择删除客户编号(-1退出):");
            num = CMUtitlity.readInteger(0);
            
            if(num == -1)
            {
                return ;
            }
            Customer customer = customerList.getCustomer(num - 1);
            if(customer == null)
            {
                System.out.println("无法找到指定客户!");
            }
            else
            {
                break;
            }
        }
        //找到了指定客户
        System.out.print("确认是否删除(Y/N)");
        char isDelete = CMUtitlity.readConfirm();
        if(isDelete == 'Y')
        {
            boolean deleteSuccess = customerList.deleteCustomer(num -1);
            if(deleteSuccess)
            {
                System.out.println("-------------------------------删除完成-------------------------------");
            }
            else
            {
                System.out.println("-------------------------------删除失败-------------------------------");
            }
        }
        else
        {
            return;
        }
    }
    private void listAllCustomers()
    {
        //System.out.println("显示客户列表的操作");
        System.out.println("------------------------------客户列表----------------------------");
        int total = customerList.getTotal();
        if(total == 0)
        {
            System.out.println("没有客户记录");
        }
        else
        {
            System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
            Customer[] custs = customerList.getAllCustomers();
            for(int i = 0;i < total;i++)
            {
                Customer cust = custs[i];
                System.out.println((i+1) + "\t" + cust.getName() +"\t" 
                                    + cust.getSex() + "\t" + cust.getAge() +"\t" + cust.getPhone() 
                                    + "\t" + cust.getEmail());
            }
        }
        
        System.out.println("-----------------------------客户列表完成----------------------------");
        
    }
    public static void main(String[] args)
    {
        CustomerView view = new CustomerView();
        view.enterMainMenu();
        
    }
}

CMUtitlity类:

package pers.yan.util;

import java.util.*;

public class CMUtitlity {
    

     
    /**
     * 将不同的方法封装到工具类,外部直接调用无需考虑具体实现
     */
        private static Scanner scanner = new Scanner(System.in);
        /**
         * 读取长度不超过limit长度的用户输入,用户回车后返回字符串
         */
        public 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.println("请输入字符串长度应不大于" + limit + ",请重新输入");
                    continue;
                }
                break;
            }
            return line;
        }
        /**
         * 获取用户输入1-5的字符:如果用户输入1-5的字符,则返回用户输入值
         */
         public static char readMenuSelection(){
             char c;
             for (;;){
                 String str = readKeyBoard(1,true);
                 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(char defaultValue){
            String str = readKeyBoard(1,true);
            return (str.length() == 0)?defaultValue:str.charAt(0);
        }
        /**
         *获取长度小于2的整数,返若未输入则返回默认值
         */
        public static int readInteger(int defaultValue){
            int n;
            for (;;){
                String str = readKeyBoard(2,false);
                if(str.equals("")){
                    return defaultValue;
                }
                try{
                    n = Integer.parseInt(str);
                    break;
                }catch (NumberFormatException e){
                    System.out.println("输入错误,请输入2位整数");
                }
            }
            return n;
        }
        /**
         * 指定长度范围,获取用户输入的字符串,并返回;用户不输入则返回默认值
         */
        public static String readString(int limit,String defaultValue){
            String str = readKeyBoard(limit,true);
            return str.equals("")?defaultValue:str;
        }
        /**
         * 用于确认选择的输入,从键盘中读取‘Y’ ‘N’,并返回
         */
        public static char readConfirm()
        {
            char c;
            for(;;){
                String str =  readKeyBoard(1,true).toUpperCase();
                 c = str.charAt(0);
                 if(c == 'Y' || c == 'N'){
                     break;
                 }else {
                     System.out.println("选择错误,请重新输入");
                 }
            }
            return c;
        }
}

 

一定要注意:

面向对象!封装!

二、明天要干什么

进入对继承性的学习

三、近几天遇到的问题:

1.构思不够明确导致后边写代码的时候很乱,不知道该怎么用;

2.自己写的时候被CustomerView类中如何判断是否增删改查成功弄的焦头烂额,在以后一定要加强对Customer类中boolean类型方法的使用,真的非常非常nice;

3.增删改客户信息的时候,一定要注意数组下标的问题,总是在这个地方出错;

4.定义多个构造器!

5.给一个构造器初始化以便边写边测试,同时,一定要记住每写一个方法(或者相关联的几个方法)都要进行一个小的测试,及时发现问题并修改;

6.新增用户信息或更改完用户信息之后记得将信息封装进入对象;

7.一定一定要注意编号的关系!!与第三点相通,如以下这两个CustomerList中的方法,非常重要!

 

posted @ 2021-07-21 14:11  闫闫不是那个严  阅读(100)  评论(0编辑  收藏  举报