App下載

Java項目實戰(zhàn)源代碼 客戶信息管理系統(tǒng)的實現(xiàn)

幼稚園新童鞋 2021-08-07 18:01:41 瀏覽數(shù) (2934)
反饋

一、軟件設(shè)計結(jié)構(gòu)

  • 對于初學(xué)者來說,弄清框架顯得尤為重要
  • 首先該軟件有以下三種模塊組成

二、MVC設(shè)計模式

模型層:Customer處理數(shù)據(jù)

控制層:CustomerList處理業(yè)務(wù)邏輯

視圖層:CustomerView顯示數(shù)據(jù)

以下三點建議結(jié)合代碼理解

1.Customer為實體對象,用于封裝客戶信息

2.CustomerList為Customer對象的管理模塊,內(nèi)部用數(shù)組管理一組Customer對象,并提供相應(yīng)的添加、修改、刪除和遍歷的方法,供CustomerView調(diào)用

3.CustomerView為主模塊,負(fù)責(zé)菜單的顯示和處理用戶操作

四個類都在同一包下

三、Customer

package org.atjinzhao.customer;

public class Customer {
	private String name;//姓名
	private char gender;//性別
	private int age;//年齡
	private String phone;//電話
	private String email;//郵箱
	
	public Customer() {
		
	}
	public Customer(String name, char 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 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;
	}
	
	
}

四、CustomerList

package org.atjinzhao.customer;

public class CustomerList {
	private Customer[] customers;//客戶列表
	private int total = 0;//記錄已保存顧客數(shù)量
	
	//構(gòu)造器
	public CustomerList(int totalCustomer) {
		customers = new Customer[totalCustomer];
	}
	
	//方法
	/**
	 * 添加客戶
	 * return:true添加成功,false:添加失敗
	 */
	public boolean addCustomer(Customer customer){
		if (total < customers.length) {
			customers[total] = customer;
			total++;
			return true;
		}else return false;
		
	}
	/**
	 * 修改指定索引位置上的客戶信息
	 * @param index
	 * @param cust
	 * @return true:修改成功     false修改失敗
	 */
	public boolean replaceCustomer(int index,Customer cust){
		if (index < 0 || index >= total) {
			return false;
		}else{
			customers[index] = cust;
			return true;
		}
		
	}
	/**
	 * 刪除指定索引位置上的客戶
	 * @param index
	 * @return true刪除成功    false刪除失敗
	 */
	public boolean deleteCustomer(int index){
		if (index >= 0 && index < total) {
			for (int i = index; i < total - 1; i++) {
				customers[i] = customers[i+1];
			}
			customers[--total] = null; 
			return true;
		}
		return false;
		
	}
	/**
	 * 獲取所有客戶信息
	 * @return 數(shù)組
	 */
	public Customer[] getAllCustomers(){
		//null的部分不返回
		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 customers[index];
		}else return null;
	}
	/**
	 * 獲取存儲客戶的數(shù)量
	 */
	public int getTotal() {
		return total;
	}
	/**
	 * 獲取最大能儲存客戶的數(shù)量
	 */
	public int getCustomer(){
		return customers.length;
	}
}

五、CustomerView

package org.atjinzhao.customer;

public class CustomerView {
	private CustomerList  customerList = new CustomerList(10);
	

	public CustomerView() {
		Customer cust = new Customer("李明",'男',19,"12349982563","lm@gmail.com");
		customerList.addCustomer(cust);
	}

	public void enterMainMenu(){
		//顯示主頁面
		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.println("       請選擇(1-5): ");
			
			char selection = CMUtility.readMenuSelection();
			switch (selection) {
			case '1':
				addNewCustomer();
				break;

			case '2':
				modifyCustomer();
				break;
			case '3':
				
				deleteCustomer();
				break;
			case '4':
				listAllCustomers();
				break;
			case '5':
				System.out.print("是否確認(rèn)退出(Y/N):");
				char isExit = CMUtility.readConfirmSelection();
				if (isExit == 'Y') {
					isFlag = false;
				}
			}
			
		}
		
	}
	/**
	 * 添加客戶
	 */
	public void addNewCustomer(){
		System.out.println("-----------------添加客戶-----------------");
		System.out.print("姓名:");
		String name = CMUtility.readString(10);
		System.out.print("性別:");
		char gender = CMUtility.readChar();
		System.out.print("年齡:");
		int age = CMUtility.readInt();
		System.out.print("電話:");
		String phone = CMUtility.readString(13);
		System.out.print("郵箱:");
		String email = CMUtility.readString(30);
		
		Customer customer = new Customer(name,gender,age,phone,email);
		boolean isSuccess = customerList.addCustomer(customer);
		if(isSuccess){
			System.out.println("-----------------添加成功-----------------");
		}else{
			System.out.println("---------------目錄已滿,添加失敗---------------");
		}		
	}
	/**
	 * 修改客戶
	 */
	public void modifyCustomer(){
		System.out.println("-----------------修改客戶-----------------");
		Customer cust;
		int num;
		for (;;) {
			System.out.print("請輸入要修改的客戶序號(輸入-1退出):");
			num = CMUtility.readInt();
			if (num == -1) {
				return;
			}
			cust = customerList.getCustomer(num - 1);
			if (cust == null) {
				System.out.println("無法找到指定客戶!");
			} else {
				break;
			}
		}
		
		System.out.println("姓名("+cust.getName()+"):");
		String name = CMUtility.readString(10, cust.getName());
		System.out.println("性別("+cust.getGender()+"):");
		char gender = CMUtility.readChar( cust.getGender());
		System.out.println("年齡("+cust.getAge()+"):");
		int age = CMUtility.readInt(cust.getAge());
		System.out.println("電話("+cust.getPhone()+"):");
		String tel = CMUtility.readString(11, cust.getPhone());
		System.out.println("郵箱("+cust.getEmail()+"):");
		String email = CMUtility.readString(15, cust.getEmail());
		
		Customer newCust = new Customer(name,gender,age,tel,email);
		boolean isReplaced = customerList.replaceCustomer(num - 1, newCust);
		if (isReplaced) {
			System.out.println("-----------------修   改  成  功-----------------	");
		} else {
			System.out.println("-----------------修   改  失  敗-----------------	");
		}
		
		
	}
	/**
	 * 刪除客戶
	 */
	public void deleteCustomer(){
		System.out.println("-----------------刪除客戶-----------------");
		Customer cust;
		int index;
		for (;;) {
			System.out.print("請輸入要刪除的客戶序號(輸入-1退出):");
			index = CMUtility.readInt();
			if (index == -1) {
				return;
			}
			cust = customerList.getCustomer(index - 1);
			if (cust == null) {
				System.out.println("無法找到客戶!");
			} else {
				break;
			}
		}
		
		System.out.print("是否確認(rèn)刪除(Y/N):");
		char isDelete = CMUtility.readConfirmSelection();
		if (isDelete == 'Y') {
			boolean deleteSuccess = customerList.deleteCustomer(index - 1);
			if (deleteSuccess) {
				System.out.println("-----------------刪除成功-----------------");
			} else {
				System.out.println("-----------------刪除失敗-----------------");
			}
		}else{
			return;
		}
	}
	/**
	 * 顯示客戶列表的操作
	 */
	public void listAllCustomers(){
		System.out.println("-------------------客  戶  列  表------------------
");
		int total = customerList.getTotal();
		if (total == 0) {
			System.out.println("沒有客戶記錄!");
		}else {
			System.out.println("編號	姓名	性別	年齡	電話		郵箱");
			Customer[] custList = customerList.getAllCustomers();
			for (int i = 0; i < total; i++) {
				System.out.println(i+1 + "	" + custList[i].getName()+
						"	" + custList[i].getGender()+"	" + custList[i].getAge()+
						"	" + custList[i].getPhone()+"	" + custList[i].getEmail()+"	");
			}
		}
		System.out.println("-----------------客戶列表完成-----------------
");
	}
	public static void main(String[] args) {
		CustomerView view = new CustomerView();
		view.enterMainMenu();
	}
	
	
}

六、CMUtility

package org.atjinzhao.customer;

import java.util.*;

public class CMUtility {
	public static void main(String[] args) {
		//System.out.println(readMenuSelection());
	}
	private static Scanner scanner = new Scanner(System.in);
	/**
	 * 用于界面菜單的選擇。該方法讀取鍵盤用戶鍵入的‘1'-‘5'的任意字符,方法返回。
	 * 
	 */
	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.println("選擇錯誤,請重新輸入:");
			} else break;
		}
		return c;
	}
	
	/**
	 * 從鍵盤讀取一個字符,并將其作為方法的返回值。
	 * 獲取性別
	 */
	public static char readChar(){
		String str = readKeyBoard(1,false);
		return str.charAt(0);
	}
	/**
	 *從鍵盤讀取一個字符,并將其作為方法的返回值。
	 *如果用戶不輸入字符而回車,方法將以defaultValue 作為返回值。 
	 *
	 */
	public static char readChar(char defaultValue){
		String str = readKeyBoard(1,true);
		return (str.length()==0)? defaultValue : str.charAt(0);
	}
	/**
	 * 從鍵盤讀取一個長度不超過2位的整數(shù),并將其作為方法的返回值。
	 * 獲取年齡
	 */
	public static int readInt(){
		int n;
		for(;;){
			String str = readKeyBoard(2,false);
			try{
				n = Integer.parseInt(str);
				break;
			}catch (NumberFormatException e) {
				System.out.print("數(shù)字輸入錯誤,請重新輸入:");
			}
		}
		return n;
	}
	/**
	 *從鍵盤讀取一個字符,并將其作為方法的返回值。
	 *如果用戶不輸入字符而回車,方法將以defaultValue 作為返回值。
	 */
	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("數(shù)字輸入錯誤,請重新輸入:");
			}
		}
		return n;
	}
	/**
	 * 從鍵盤讀取一個長度不超過limit的字符串,并將其作為方法的返回值。
	 */
	public static String readString(int limit){
		return readKeyBoard(limit,false);
	}
	/**
	 * 從鍵盤讀取一個長度不超過limit的字符串,并將其作為方法的返回值。
	 * 如果用戶不輸入字符而直接回車,方法將以defaultVaue作為返回值。
	 */
	public static String readString(int limit,String defaultValue){
		//修改姓名、電話、郵箱時,不輸入信息直接回車
		String str = readKeyBoard(limit,true);
		return str.equals("") ? defaultValue : str;
	}
	/**
	 * 用于確認(rèn)選擇的輸入。該方法從鍵盤讀取‘Y'或‘N',并將其作為方法的返回值。
	 */
	public static char readConfirmSelection(){
		//獲取確認(rèn)的輸入
		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.println("輸入長度(不大于" + limit + ")錯誤,請重新輸入“");
				continue;
			}
			break;
		}
		return line;
		
	}

}

本篇關(guān)于使用Java實現(xiàn)客戶信息管理系統(tǒng)的實戰(zhàn)項目就介紹到此結(jié)束了,如果您對Java其他實戰(zhàn)項目有興趣,請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,也希望大家以后多多支持!

0 人點贊