亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java實(shí)現(xiàn)客戶信息管理系統(tǒng)

 更新時(shí)間:2021年02月15日 09:17:36   作者:xue 加油  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)客戶信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)客戶信息管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

一、CMUtility工具類

講不同的功能封裝為方法,就是可以直接通過(guò)調(diào)用方法使用它的功能,而無(wú)需考慮具體的功能實(shí)現(xiàn)細(xì)節(jié)。

代碼如下:

package com.p2.util;

import java.util.*;

/**
 * 工具類
 * 講不同的功能封裝為方法,就是可以直接通過(guò)調(diào)用方法使用它的功能,而無(wú)需考慮具體的功
 * 能實(shí)現(xiàn)細(xì)節(jié)。
 * 
 **/
public class CMUtility {
 private static Scanner scanner = new Scanner(System.in);
 /**
 * 用于界面菜單的選擇,該方法讀取鍵盤,如果用戶鍵入"1"-"5"中的任意字符,則方法返回。
 * 返回值為用戶選擇的數(shù)字。
 **/
 public static char readMenuSelection(){
 char c;
 for (; ; ){
 String str = readaKeyBoard(1, false);
 c = str.charAt(0);
 if (c != '1' && c!= '2' && c!= '3' && c!= '4'
 && c!= '5'){
 System.out.println("選擇錯(cuò)誤,請(qǐng)重新輸入:");
 }else break;
 }
 return c;
 }
 /**
 * 從鍵盤讀取一個(gè)字符,并將其作為方法的返回值。
 **/
 public static char readChar(){
 String str = readaKeyBoard(1, false);
 return str.charAt(0);
 }
 /**
 * 從鍵盤讀取一個(gè)字符,并將其作為方法的返回值。
 * 如果用戶不輸入字符而直接回車,方法將以defaultValue 作為返回值。
 **/
 public static char readChar(char defaultValue){
 String str = readaKeyBoard(1, true);
 return (str.length() == 0) ? defaultValue :str.charAt(0);
 }
 /**
 * 從鍵盤讀取一個(gè)長(zhǎng)度不超過(guò)2位的整數(shù),并將其作為方法的返回值。
 **/
 public static int readInt(){
 int n;
 for (; ; ){
 String str = readaKeyBoard(2, false);
 try{
 n = Integer.parseInt(str);
 break;
 }catch(NumberFormatException e){
 System.out.println("數(shù)字輸入錯(cuò)誤,請(qǐng)重新輸入:");
 }
 }
 return n;
 }
 /**
 * 從鍵盤讀取一個(gè)長(zhǎng)度不超過(guò)2位的整數(shù),并將其為方法的返回值。
 * 如果用戶不輸入字符而直接回車,方法將以defaultValue 作為返回值。
 **/
 public static int readInt(int defaultValue){
 int n;
 for(; ; ){
 String str = readaKeyBoard(2, true);
 if (str.equals("")){
 return defaultValue;
 }
 try{
 n = Integer.parseInt(str);
 break;
 }catch(NumberFormatException e){
 System.out.println("數(shù)字輸入錯(cuò)誤,請(qǐng)重新輸入:");
 }
 }
 return n;
 }
 /**
 * 從鍵盤 讀取一個(gè)長(zhǎng)讀不超過(guò)limit的字符串,并將其作為方法的返回值。
 **/
 public static String readString (int limit){
 return readaKeyBoard(limit,false);
 }
 
 public static String readString (int limit, String defaultValue){
 return readaKeyBoard(limit,false);
 }
 /**
 * 從鍵盤讀取一個(gè)瘡毒不超過(guò)limit的字符串,將其作為方法的返回值。
 * 如果用戶不輸入字符而直接回車,方法以defaultValue作為返回值。
 **/
 public static char readComfirmSelection(){
 char c;
 for (; ; ){
 String str = readaKeyBoard(1, false).toUpperCase();
 c = str.charAt(0);
 if (c == 'Y' || c == 'N'){
 break;
 }else{
 System.out.println("選擇錯(cuò)誤,請(qǐng)重新輸入:");
 }
 }
 return c;
 }
 private static String readaKeyBoard(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("輸入長(zhǎng)度(不大于" + limit + ")錯(cuò)誤,請(qǐng)重新輸入:");
 continue;
 }
 break;
 }
 return line;
 }
}

二、Customer

代碼如下:

package com.p2.bean;
/**
 * 把對(duì)象封裝到方法里面
 * 
 * 
 **/
public class Customer {
 private String name;//客戶姓名
 private char gender;//性別
 private int age; //年齡
 private String phone;//電話號(hào)碼
 private String email;//電子郵箱
 //構(gòu)造器
 
 //方法(封裝)
 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 Customer() {
 
 }
 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 com.p2.service;
/**
 * 最主要的
 * 
 **/
import com.p2.bean.Customer;

public class CustomerList {
 private Customer[] customers;
 private int total = 0;
 //構(gòu)造器
 /**
 * 用來(lái)初始化customers數(shù)組的構(gòu)造器
 * @param totalCustomer: 指定數(shù)組的長(zhǎng)度
 **/
 public CustomerList (int totalCustomer){//給正在創(chuàng)建的對(duì)象進(jìn)行初始化
 customers = new Customer[totalCustomer];
 }
 //方法 
 /**
 * 將指定的客戶添加到數(shù)組中
 * @param customer
 * @return成功true,失敗:false
 **/
 public boolean addCustomer(Customer customer){//增
 if(total < 0 || total >= customers.length){
 return false;
 }
 customers[total++] = customer;
 return true;
 }
 /**
 * 修改指定位置的客戶信息
 * @param index
 * @param cust
 * @return
 **/
 public boolean replaceCustomer(int index, Customer cust){//改
 if(index <0 || index >= total){
 return false;
 }
 customers[index] = cust;
 return true;
 }
 /**
 * 刪除指定位置上的客戶
 * @param index
 * @return
 **/
 public boolean deleteCustsomer(int index){//刪除
 if(index < 0 || index >= total){
 return false;
 }
 for(int i = index; i < total - 1; i++){
 customers[i] = customers[i + 1];
 }
 //最后有數(shù)據(jù)的元素需要置空
// customers[total - 1] =null;
// total --;
 customers[-- total] = null;
 return true;
 
 }
 /**
 * 獲得所有的客戶信息
 * @return
 **/
 public Customer[] getAllCustomers() {//查詢所有
 Customer[] custs = new Customer[total];
 for(int i = 0; i < total; i++){
 custs[i] = customers[i];
 }
 return custs;
 }
 /**
 * 獲取指定位置上的客戶信息
 * @param index
 * @return
 **/
 public Customer getCustomer(int index) {//查詢一個(gè)
 if(index < 0 || index >= total){
 return null;
 }
 return customers[index];
 }
 //封裝
 /**
 * 獲取存儲(chǔ)客戶的的數(shù)量
 * @return
 **/
 public int getTotal() {//統(tǒng)計(jì)數(shù)據(jù)
 return total;
 }
}

四、CustomerView

代碼如下:

package com.p2.ui;

import com.p2.bean.Customer;
import com.p2.service.CustomerList;
import com.p2.util.CMUtility;

/**
 * 
 * 用戶交互
 * 
 * 
 * 
 **/
public class CustomerView {
 private CustomerList customerList = new CustomerList(100);
 
 public CustomerView(){
 Customer customer = new Customer("王濤",'男', 23, "13213123","wt@gmail.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.退出\n");
 System.out.print(" 請(qǐng)選擇(1-5):");
 
 char menu = CMUtility.readMenuSelection();
 switch(menu){
 case '1':
 addNewCustomer();
 break;
 case '2':
 modifyCustomer();
 break;
 case '3':
 deleteCustomer();
 break;
 case '4':
 listAllCustomers();
 
 break;
 case '5':
// System.out.println("退出");
 
 System.out.println("確認(rèn)是否退出(Y/N):");
 char isExit = CMUtility.readComfirmSelection();
 if(isExit =='Y'){
 isFlag = false;
 }
// break;
 }
 
// isFlag = false;
 }
 
 }
 /**
 * 添加客戶
 * 
 **/
 private 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);
 
 //將上述數(shù)據(jù)封裝到對(duì)象中
 Customer customer = new Customer(name, gender, age, phone, email);
 
 boolean isSucccess = customerList.addCustomer(customer);
 if(isSucccess){
 System.out.println("------添加完成------");
 }else{
 System.out.println("------客戶已滿------");
 } 
 
 }
 /**
 * 修改客戶
 **/
 private void modifyCustomer(){
 System.out.println("---------修改客戶-------");
 Customer cust;
 int number;
 for(;;){
 System.out.print("選擇要修改的客戶編號(hào)(-1退出):");
 number = CMUtility.readInt();
 
 if (number == -1){
 return;
 }else{
 cust = customerList.getCustomer(number - 1);
 if(cust == null){
 System.out.println("無(wú)法找到指定客戶?。?);
 }else{//找到相應(yīng)的客戶
 break;
 }
 }
 }
 //修改客戶信息
 System.out.print("姓名:(" + cust.getName() + "):");
 String name = CMUtility.readString(10, cust.getName());
 System.out.print("性別:(" + cust.getGender() + "):");
 char gender = CMUtility.readChar(cust.getGender());
 System.out.print("年齡:(" + cust.getAge() + "):");
 int age = CMUtility.readInt(cust.getAge());
 System.out.print("電話:(" + cust.getPhone() + "):");
 String phone = CMUtility.readString(13, cust.getPhone());
 System.out.print("郵箱:(" + cust.getEmail() + "):");
 String email = CMUtility.readString( 30,cust.getEmail());
 
 Customer newcust = new Customer(name, gender, age, phone, email);
 
 boolean isRepalaced = customerList.replaceCustomer(number - 1, newcust);
 if(isRepalaced){
 System.out.println("修改成功?。?);
 }else{
 System.out.println("修改失?。?!");
 }
 
 }
 /**
 * 刪除客戶
 **/
 private void deleteCustomer(){
 System.out.println("-------刪除客戶-------");
 int number;
 for(;;){
 System.out.print("請(qǐng)選擇呆刪除客戶的編號(hào)(-1退出):");
 number = CMUtility.readInt();
 
 if (number ==-1){
 return;
 }
 Customer customer =customerList.getCustomer(number -1);
 if (customer == null){
 System.out.println("無(wú)法找到指定客戶??!");
 }else{
 break;
 }
 }
 //找到客戶了
 
 System.out.println("是否確認(rèn)刪除(Y/N):");
 char isDelete = CMUtility.readComfirmSelection(); 
 if(isDelete == 'Y'){
 boolean deleteSuccess = customerList.deleteCustsomer(number - 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("編號(hào)\t姓名\t性別\t年齡\t電話\t\t郵箱");
 Customer[] custs = customerList.getAllCustomers();
 for(int i = 0;i<custs.length; i++){
 Customer cust = custs[i];
 System.out.println((i + 1) +"\t"+ cust.getName() +"\t" +cust.getGender() 
 + "\t"+cust.getAge() +
 "\t" + cust.getPhone() + "\t" + cust.getEmail());
 }
 }
 
 
 System.out.println("---------客戶列表結(jié)束-------");
 }
 
 public static void main(String[] args) {
 CustomerView view = new CustomerView();
 view.enterMainMenu();
 }

}

五、運(yùn)行結(jié)果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論