栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

面向对象

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

面向对象

Customer: java Bean (包含属性 get set 构造器)


public class Customer {
    private String Name;
    private char Gender;
    private int Age ;
    private String Phone;
    private String Email;

    public void  setName(String Name){
        this.Name = Name;
    }
    public String getName(){
        return   Name;
    }

    public void setGender(char Ggender){

        this.Gender = Ggender;

    }
    public char getGender(){
        return Gender;

    }
    public void setAge(int Age){

        this.Age = Age;
    }
    public int getAge(){


        return Age;
    }
    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;
    }

    public Customer(){

    }
    public Customer(String name ,char Gender,int Age,String Phone,String Email){

        this.Name = name;
        this.Age = Age;
        this.Gender = Gender;
        this.Phone = Phone;
        this.Email = Email;

    }

}

CustomerList:一些对CustomerList处理的方法


public class CustomerList {
    private Customer[] customers;   //客户对象数组
    int total = 0; //计数

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

    public boolean addCustomer(Customer customer) {
        
        if (total >= customers.length) {
            return false;
        }
        else{

        customers[total] = customer;
            total++;
            return true;
        }



    }
    public boolean replaceCustomer(int index ,Customer customer){
        

        if(index<0 || index >=total){
            return false;
        }
        customers[index]   = customer;
        return true;
    }
    public boolean deleteCustomer(int index){
        
        if(index<0 || index>=total){
            return false;
        }
        for(int i=index;i<=total-1;i++){                //将index到最后一个往前移一位,此时倒数第一 第二相同
            customers[i] = customers[i+1];
        }
        customers[total-1] = null;                      //将最后一个客户信息置为空
        total--;
        return true;
    }
    public Customer[] getAllCustomers(){
        
        Customer[] customers1  = new   Customer[total];
        for(int i = 0;i< total;i++){
            customers1[i] = customers[i];
        }
        return customers1;
    }
    public Customer getCustomer(int index){
        
        if(index<0 || index>=total){
            return null;
        }
        return customers[index];
    }
    public  int getTotal(){
        return  total;
    }
}

CustomerView: 交互界面

import java.util.Scanner;


public class CustomerView {

    private CustomerList customerList= new CustomerList(10);

    Scanner scan = new Scanner(System.in);
    public  void enterMainMenu(){
        
        boolean flag = true;

        while(flag) {

            int i = 0;
            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.println("      选则(1~5):");
            System.out.println("--------------------------客户信息管理--------------------------");

            i = scan.nextInt();


            switch  (i) {
                case 1:
                    addNewCustomer();
                    break;

                case 2:

                    modifyCustomer();
                    break;

                case 3:

                    deleteCustomer();
                    break;
                case 4:

                    listAllCustomers();
                    break;

                case 5:
                    String char1 ;
                    System.out.println("是否退出(Y/N):");
                    char1 = scan.next();
                    if(char1.equals("Y")){
                        flag = false;
                    }
                    break;


            }


        }

    }

    private void addNewCustomer(){
        System.out.println("--------------------------添加客户--------------------------");
        System.out.print("姓名:");
        String name = scan.next();
        System.out.print("性别:");
        char Gender = scan.next().charAt(0);
        System.out.print("年龄:");
        int age = scan.nextInt();
        System.out.print("电话:");
        String  Phone = scan.next();
        System.out.print("邮箱:");
        String Email = scan.next();
        Customer customer = new Customer(name,Gender,age,Phone,Email);// 将客户信息封装在一个对象中
        boolean isSeccess =  customerList.addCustomer(customer);
        if (isSeccess){
            System.out.println("--------------------------添加成功--------------------------n");
        }
        else{
            System.out.println("--------------------------添加失败--------------------------n");
        }
    }



    private  void modifyCustomer() {
        listAllCustomers();
        System.out.println("修改客户对应编号:");
        int num;
        for (; ; ) {            //类似于while(1)
            System.out.println("-1退出");
            num = scan.nextInt();
            if (num == -1) {
                return;
            }
            Customer cust = customerList.getCustomer(num - 1);
            if (cust == null) {
                System.out.println("不存在");
            } else {
                break;
            }
        }
            System.out.print("姓名:");
            String name = scan.next();
            System.out.print("性别:");
            char Gender = scan.next().charAt(0);
            System.out.print("年龄:");
            int age = scan.nextInt();
            System.out.print("电话:");
            String  Phone = scan.next();
            System.out.print("邮箱:");
            String Email = scan.next();
            Customer newcustomer = new Customer(name,Gender,age,Phone,Email);// 将客户信息封装在一个对象中
            boolean isReplace =  customerList.replaceCustomer(num-1,newcustomer);
            if(isReplace){
                System.out.println("--------------------------修改成功--------------------------n");
            }else{
                System.out.println("--------------------------修改失败--------------------------n");
            }

    }

    private void deleteCustomer(){
        listAllCustomers();
        System.out.println("删除客户对应编号:");
        int num;
        for (; ; ) {
            System.out.println("-1退出");
            num = scan.nextInt();
            if (num == -1) {
                return;
            }
            Customer cust = customerList.getCustomer(num - 1);
            if (cust == null) {
                System.out.println("不存在");
            } else {
                break;
            }
        }
        boolean isDelete =  customerList.deleteCustomer(num-1);
        if(isDelete){
            System.out.println("--------------------------删除成功--------------------------n");
        }else{
            System.out.println("--------------------------删除失败--------------------------n");
        }

    }

    public CustomerView() { //构造一个初始用户
        Customer customer = new Customer("陈封",'男',23,"13402951750","1162196987@qq.com");
        customerList.addCustomer(customer);

    }

    private void listAllCustomers(){
        int j = customerList.getTotal();
        if(j==0){
            System.out.println("没有客户信息!");
        }
        else{

            System.out.println("--------------------------客户列表--------------------------");
            System.out.println("编号tt姓名tt性别tt年龄tt电话tt        邮箱");
            Customer[] customers1 =  customerList.getAllCustomers();


            for (int i = 0; i < customers1.length; i++) {
                System.out.println((i+1) + "tt"+customers1[i].getName() + "tt"
                        +customers1[i].getGender()+"tt"+customers1[i].getAge()+"tt"
                        +customers1[i].getPhone()+"tt"+customers1[i].getEmail());
            }
            System.out.println("--------------------------客户列表--------------------------");


        }

    }

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

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/691704.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号