客户管理系统

//customer类
public class customer {
private int id;
private String name;
private String sex;
private int age;
private String phone;
private String email;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    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(String name, String sex, int age, String phone, String email) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.phone = phone;
        this.email = email;
    }
    public customer() {
    }
}
//customerlist类  增、删、改、查、显示功能
public class customerlist {
private customer[] customers;
private int num;

    /**
     * 初始化customer数组
     * @param totalCustomer 指定customer数组的最大空间
     */
    public customerlist (int totalCustomer) {
        customers=new customer[totalCustomer];
}

    /**
     * 用途:将参数customer添加组中最后一个客户对象记录之后
     * @param customer 指定要添加的客户对象
     * @return 添加成功返回true; false表示数组已满,无法添加
     */
    public boolean addCustomer( customer customer) {
        if(num>=customers.length)
            return false;
        else {
            customers[num] = customer;
            num++;
            return true;
        }
    }

    /**
     * 用途:用参数customer替换数组中由index指定的对象
     * @param index 指定所替换对象在数组中的位置,从0开始
     * @param cust 指定替换的新客户对象
     * @return 替换成功返回true; false表示索引无效,无法替换
     */
    public boolean replaceCustomer(int index, customer cust){
    if(index<0||index>=num)
    return false;
    else {
        customers[index]=cust;
        return true;
    }
    }

    /**
     *删除客户
     * @param index 指定删除的新客户对象
     * @return
     */
    public boolean deleteCustomer(int index ){
        if(index<0||index>=num){
            return false;
        }
        else {
            for(int i=index;i<num-1;i++) {
                customers[i] = customers[i + 1];
            }
            customers[num-1]=null;
            num--;
            return true;
        }
    }

    /**
     * 显示所有客户
     * @return
     */
    public customer[] getAllCustomers(){
customer[] cus=new customer[num];
        for(int i=0;i<num;i++){
            cus[i]=customers[i];
        }
        return cus;
    }

    /**
     * 查找指定位置的客户
     * @param index
     * @return
     */
    public customer getCustomer(int index){
        if(index<0||index>=num){
            return null;
        }
        else
            return customers[index];
    }

    /**
     * 获取客户的数量
     * @return
     */
    public int getNum(){
        return num;
    }

}
//customerview类  进行操作的一个类
import java.util.Scanner;

public class customerview {
    private customerlist cust=new customerlist(10);
    Scanner sc=new Scanner(System.in);

    public customerview() {
        customer custu=new customer("小明","男",18,"123456789","qq.com");
        cust. addCustomer(custu);
    }

    /**
     * 主界面
     */
    public void enterMainMenu(){
        boolean flag=true;
        while(flag) {
            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)");
            int n=sc.nextInt();
            switch(n){
                case 1: addNewCustomer();break;
                case 2:modifyCustomer();break;
                case 3:deleteCustomer();break;
                case 4:listAllCustomers();break;
                case 5: System.out.println("是否退出y/n");break;
            }
            if(n==5){
            String str=sc.next();
            if(str.equals("y")||str.equals("Y")){
                flag=false;
            }
        }
        }
    }

    /**
     * 添加客户
     */
    private void addNewCustomer (){
        System.out.println("-----------------添加客户-------------");
        System.out.print("姓名:");
        String str=sc.next();
        System.out.print("性别:");
        String str1=sc.next();
        System.out.print("年龄:");
        int n=sc.nextInt();
        System.out.print("电话:");
        String str2=sc.next();
        System.out.print("邮箱:");
        String str3=sc.next();
        customer custu=new customer(str,str1,n,str2,str3);
        boolean flag= cust. addCustomer(custu);
        if(flag)
            System.out.println("添加成功");
        else
            System.out.println("客户内存满了,添加失败");
    }

    /**
     * 修改客户
     */
    private void modifyCustomer(){
        System.out.println("修改客户");
        System.out.print("要修改的客户编号");
        boolean flag;
        int n;
        do{
            n=sc.nextInt();
        if(n<1||n>cust.getNum()){
            flag=true;
            System.out.println("找不到指定客户");
        }
        else
            flag=false;
        }while(flag);
        customer cus=cust.getCustomer(n-1);
        String str1,str2,str3,str4;
        int m;
                System.out.print("姓名(" + cus.getName() + ")");
                str1 = sc.next();
                System.out.print("姓别(" + cus.getSex() + ")");
                str2 = sc.next();
                System.out.print("年龄(" + cus.getAge() + ")");
                m = sc.nextInt();
                System.out.print("电话(" + cus.getPhone() + ")");
                str3 = sc.next();
                System.out.print("邮箱(" + cus.getEmail() + ")");
                str4 = sc.next();
        customer custu=new customer(str1,str2,m,str3,str4);
        cust.replaceCustomer(n-1,custu);
    }

    /**
     * 删除客户
     */
    private void deleteCustomer(){
        System.out.println("删除客户");
        System.out.println("要删除的客户编号");
        boolean flag;
        int n;
        do{
            n=sc.nextInt();
            if(n<1||n>cust.getNum()){
                flag=true;
                System.out.println("找不到指定客户");
            }
            else
                flag=false;
        }while(flag);
        System.out.println("是否确认删除y/n");
        String str=sc.next();
        if(str.equals("y")||str.equals("Y")){
            cust.deleteCustomer(n-1);
        }

    }

    /**
     * 显示客户信息
     */
    private void listAllCustomers () {
        System.out.println("显示客户");
        if (cust.getNum() == 0) {
            System.out.println("没有客户");
        } else {
            System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t\t邮箱");
            customer[] cus = cust.getAllCustomers();
            for (int i = 0; i < cust.getNum(); i++) {
                customer cust = cus[i];
                System.out.println((i + 1) + "\t" + cust.getName() + "\t" + cust.getSex() + "\t" + cust.getAge() + "\t" + cust.getPhone() + "\t" + cust.getEmail());
            }
        }
    }
}

//test类  测试类
public class test3 {
    public static void main(String[] args) {
        customerview cus=new customerview();
        cus.enterMainMenu();
    }
}
上一篇:C#事件与委托的区别


下一篇:element表格去掉表头的实现方法