java基础[项目:客户信息管理系统]

1.问题描述:

        实现客户信息管理系统,包括对用户信息的增删改查功能。

2.包结构

com.hike.cms.view        --        UI模块,和用户进行交互,连接用户和服务组件
    CustomerView            视图层,实现与用户的交互
    CMUtility                工具类,实现接收用户在键盘输入的数据
    
com.hike.cms.service    --        核心服务组件包,管理器
    CustomerList            包含操作的方法
        
com.hike.cms.domin        --         实体包,实体类,就是javabean
    Customer                实体类对象
    
com.hike.cms.main        --        项目入口
    CustomerMain
        main

3.代码

3.1 com.hike.cms.domin  --  Customer

package com.hike.cms.domin;

public class Customer {

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

		public void setGender(String 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;
		}
		
		public String say() {
			return  name+"\t" + gender + "\t" + age+ "\t" +	phone+ "\t" + email + "\t";
		}
}

3.2 com.hike.cms.service    --       CustomerList

package com.hike.cms.service;

import com.hike.cms.domin.Customer;

public class CustomerList {
	
	private Customer[] customers; 	//使用数组保存客户信息
	private int realCount = 0;		//计数器

	public CustomerList(int totalCount) {
		this.customers = new Customer[totalCount];
	}
	
//	删除客户信息
	public boolean deleteCustomer(int index) {
		
		if(index < 0 || index > realCount) {
			return false;
		}else {
			for(int i = index; i < realCount; i++) {
				customers[i-1] = customers[i];
			}
		}
		realCount--;
		return true;
	}
	
//	获取某一个客户的信息
	public Customer getCustomer(int index) {
		if(index > realCount || index <= 0) {
			return null;
		}else {
			return this.customers[index - 1];			
		}
	}
	
//	返回所有客户信息,没有空洞
	public Customer[] getAllCustomers() {
		Customer[] newArr = new Customer[realCount];
		for(int i = 0; i < realCount; i++) {
			newArr[i] = customers[i];
		}
		return newArr;
	}
	
//	添加客户信息,将客户信息添加到最后一个客户信息之后
//	有空洞
	public boolean addCustomer(Customer customer) {
		//数组已满
		if(realCount == customers.length) {
			return false;
		}
		customers[realCount++] = customer;
		return true;
	}
}

3.3 com.hike.cms.main        --        CustomerMain

package com.hike.cms.main;

import com.hike.cms.domin.Customer;
import com.hike.cms.service.CustomerList;
import com.hike.cms.view.CustomerView;

public class CustomerMain {

	public static void main(String[] args) {
		CustomerView customerView = new CustomerView();
		customerView.enterMainMenu();
	}
	
	public static void main1(String[] args) {
		
		System.out.println("##################类模板测试###############");
		
		Customer cus1 = new Customer("张三0","男",25,"11122233340","zs0@qq.com");
		Customer cus2 = new Customer("张三1","女",23,"11122233341","zs1@qq.com");
		Customer cus3 = new Customer("张三2","男",22,"11122233342","zs2@qq.com");
		Customer cus4 = new Customer("张三3","女",21,"11122233343","zs3@qq.com");
		Customer cus5 = new Customer("张三4","男",24,"11122233344","zs4@qq.com");
		Customer cus6 = new Customer("张三5","女",26,"11122233345","zs5@qq.com");
		Customer cus7 = new Customer("张三6","男",27,"11122233346","zs6@qq.com");
		Customer cus8 = new Customer("张三7","女",28,"11122233347","zs7@qq.com");
		
		System.out.println(cus1.say());
		System.out.println(cus2.say());
		System.out.println(cus3.say());
		System.out.println(cus4.say());
		System.out.println(cus5.say());
		System.out.println(cus6.say());
		System.out.println(cus7.say());
		System.out.println(cus8.say());
		
		System.out.println("###############添加客户信息测试##################");
		
		CustomerList customerList = new CustomerList(10);
		
		boolean b1 = customerList.addCustomer(cus1);
		boolean b2 = customerList.addCustomer(cus2);
		boolean b3 = customerList.addCustomer(cus3);
		boolean b4 = customerList.addCustomer(cus4);
		
		System.out.println(b1);
		System.out.println(b2);
		System.out.println(b3);
		System.out.println(b4);
		
//		Customer[] arr = customerList.getCustomer();
//		for(int i = 0 ; i < arr.length ; i++) {
//			if(arr[i] != null) {
//				System.out.println(arr[i].say());
//			}else {
//				System.out.println(arr[i]);
//			}
//		}
		
		System.out.println("###############获取客户信息测试##################");
		Customer[] arr = customerList.getAllCustomers();
		for(int i = 0; i < arr.length; i++) {
			System.out.println(arr[i].say());
		}
		System.out.println("#######获取某一个客户信息###########");
		Customer customer = customerList.getCustomer(200);
		if(customer != null) {
			System.out.println(customer.say());
		}else {
			System.out.println("下标不合法");
		}
		
		System.out.println("######删除一个客户信息########");
		
		boolean b = customerList.deleteCustomer(4);
		System.out.println(b);
		arr = customerList.getAllCustomers();
		for(int i = 0; i < arr.length; i++) {
			System.out.println(arr[i].say());
		}
	}
}

3.4

com.hike.cms.view        --        CustomerView          

package com.hike.cms.view;

import com.hike.cms.domin.Customer;
import com.hike.cms.service.CustomerList;

public class CustomerView {

//	关联核心的管理器
	private CustomerList customerList = new CustomerList(10);
	
//	程序的真实入口 
	public void enterMainMenu() {
//		不能随便返回
		boolean loopFlag = true;
		while(loopFlag) {
			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();
			System.out.println("请选择1-5:");
			char choice = CMUtility.readMenuSelection();
			switch(choice) {
				case '1' : addNewCustomer();break;
				case '2' : modifyCustomer();break;
				case '3' : deleteCustomer();break;
				case '4' : listAllCustomer();break;
				case '5' : 
					System.out.println("确认退出(Y/N):");
					char confirm = CMUtility.readConfirmSelection();
					if(confirm == 'Y') {
						loopFlag = false;
						System.out.println("退出系统成功,欢迎下次使用");
					}
					break;
			}
		}
	}
	
	private void addNewCustomer() {
		System.out.println("addNewCustomer");
		System.out.println("--------------------添加客户--------------------");
		//通过工具类读出name
		System.out.println("姓名:");
		String name = CMUtility.readString(10);
		//通过工具类读出gender
		System.out.println("性别:");
		String gender = CMUtility.readString(10);
		System.out.println("年龄:");
		int age = CMUtility.readInt();
		System.out.println("手机:");
		String phone = CMUtility.readString(10);
		System.out.println("邮箱:");
		String email = CMUtility.readString(10);
		//创建一个实体对象
		Customer customer = new Customer(name,gender,age,phone,email);
		//将此对象添加到管理器customerList中,前面已经关联CustomerList
		boolean b = customerList.addCustomer(customer);
		
		if(b == true) {
			System.out.println("--------------------添加成功--------------------");
		}else {
			System.out.println("---------添加失败,已达最大存储容量-----------");
		}
		listAllCustomer();
	}
	
	private void modifyCustomer() {
		System.out.println("modifyCustomer");
		System.out.println("--------------------修改客户--------------------");
		System.out.println("请选择要修改的编号(-1)退出:");
		listAllCustomer();
		//获取用户输入的编号
		int index = CMUtility.readInt();
		if(index == -1) {
			System.out.println("---------------修改失败,用户选择撤销删除操作-----------------");
			return;
		}
		System.out.println("确认是否修改(Y/N):");
		System.out.println("直接回车不修改");
		char confirm = CMUtility.readConfirmSelection();
		Customer target = customerList.getCustomer(index);
		if(confirm == 'Y') {
			//直接回车表示不修改此元素
			if(target != null) {
				
				System.out.println("姓名(" + target.getName() + "):");		//姓名(张三)
				String name = CMUtility.readString(10, target.getName());		//获取新数据,重载readString方法实现【直接回车不修改】功能
				target.setName(name);		//无条件修改元素
				
				System.out.println("性别(" + target.getGender() + "):");
				String gender = CMUtility.readString(10,target.getGender());
				target.setGender(gender);
				
				System.out.println("年龄(" + target.getAge() + "):");
				int age = CMUtility.readInt(target.getAge());
				target.setAge(age);
				
				System.out.println("手机(" + target.getPhone() + "):");
				String phone = CMUtility.readString(10,target.getPhone());
				target.setPhone(phone);
				
				System.out.println("邮箱(" + target.getEmail() + "):");
				String email = CMUtility.readString(10,target.getEmail());
				target.setEmail(email);
							
				System.out.println("--------------------修改成功--------------------");
			}else {
				System.out.println("--------------------修改失败,编号输入错误--------------------");
			}
		}else {
			System.out.println("--------------------修改失败,用户选择撤销删除操作--------------------");
		}
		listAllCustomer();
	}
	
	private void deleteCustomer() {
		System.out.println("deleteCustomer");
		System.out.println("--------------------删除客户--------------------");
		listAllCustomer();
		System.out.println("请选择要删除的编号(-1)退出:");
		//获取用户输入的编号
		int index = CMUtility.readInt();
		if(index == -1) {
			System.out.println("---------------删除失败,用户选择撤销删除操作-----------------");
			return;
		}
		System.out.println("确认是否删除(Y/N):");
		char confirm = CMUtility.readConfirmSelection();
		if(confirm == 'Y') {
			boolean b = customerList.deleteCustomer(index);
			if(b == true) {
				System.out.println("--------------------删除成功--------------------");
			}else {
				System.out.println("--------------------删除失败,编号输入错误--------------------");
			}
		}else {
			System.out.println("--------------------删除失败,用户选择撤销删除操作--------------------");
		}
		listAllCustomer();
	}
	
	private void listAllCustomer() {
		System.out.println("listAllCustomer");
		System.out.println("--------------------客户列表--------------------");
		System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱");
		//从管理器中获取所有有效对象,调用相应方法
		Customer[] arr = customerList.getAllCustomers();
		int i = 0;
		for(Customer temp : arr) {
			System.out.println((i + 1) + "\t" + temp.say());
			i++;
		}
		System.out.println("--------------------客户列表完成--------------------");
	}
}


  3.6   CMUtility

package com.hike.cms.view;

import java.util.*;

public class CMUtility {
    private static Scanner scanner = new Scanner(System.in);
    
	public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '1' && c != '2' && 
                c != '3' && c != '4' && c != '5') {
                System.out.print("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }

    public static char readChar() {
        String str = readKeyBoard(1, false);
        return str.charAt(0);
    }

    public static char readChar(char defaultValue) {
        String str = readKeyBoard(1, true);
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }

    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }

    public static int readInt(int defaultValue) {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, true);
            if (str.equals("")) {
                return defaultValue;
            }

            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }

    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }

    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }

    public static char readConfirmSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("选择错误,请重新输入:");
            }
        }
        return c;
    }

    private 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.print("输入长度(不大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }

        return line;
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OneTenTwo76

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值