java項目之Bank銀行代碼
文中所有代碼來自尚硅谷宋紅康老師的Java課程,如有侵權請聯系刪除。
項目說明:
模擬實現一個基于文本界面的 《 客戶信息管理軟件 》。
該軟件能夠實現對客戶對象的插入、修改和刪除(用數組實現),并能夠打印客戶明細表。
項目采用分級菜單方式。主菜單如下:
-----------------客戶信息管理軟件-----------------
1 添 加 客 戶
2 修 改 客 戶
3 刪 除 客 戶
4 客 戶 列 表
5 退?????????? 出
請選擇(1-5):_
? 每個客戶的信息被保存在c ustomer 對象中。
? 以一個c ustomer 類型的數組來記錄當前所有的客戶。
? 每次“添加客戶”(菜單 1 )后,客戶( Customer )對象被添加到數組中。
? 每次“修改客戶”(菜單 2 )后,修改后的客戶( Customer )對象替換數組中原對象。
? 每次“刪除客戶”(菜單 3 )后,客戶( Customer )對象被從數組中清除。
? 執行“客戶列表 ”(菜單 4 )時,將列出數組中所有客戶的信息。
項目代碼
整個項目分為4個Java文件,分別實現功能。
CMUtility文件,實現與客戶的交互
package customer.util;
import java.util.*;
public class CMUtil {
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.print("選擇錯誤,請重新輸入:");
} 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位的整數,并將其作為方法的返回值。
*/
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;
}
/**
從鍵盤讀取一個長度不超過2位的整數,并將其作為方法的返回值。
如果用戶不輸入字符而直接回車,方法將以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("數字輸入錯誤,請重新輸入:");
}
}
return n;
}
/**
從鍵盤讀取一個長度不超過limit的字符串,并將其作為方法的返回值。
*/
public static String readString(int limit) {
return readKeyBoard(limit, false);
}
/**
從鍵盤讀取一個長度不超過limit的字符串,并將其作為方法的返回值。
如果用戶不輸入字符而直接回車,方法將以defaultValue 作為返回值。
*/
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str;
}
/**
用于確認選擇的輸入。該方法從鍵盤讀取‘Y’或’N’,并將其作為方法的返回值。
*/
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;
}
}
customer代碼文件,實現創建客戶信息
package customer.bean;
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 gender) {
this.gender = gender;
}
public char getGender() {
return gender;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = 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.gender=gender;
this.age=age;
this.phone=phone;
this.email=email;
}
}
customerList文件,實現客戶信息的一些操作
package customer.service;
import customer.bean.customer;
public class customerList {
private customer[] customers; //用來保存用戶對象的數組
private int total=0; //記錄已保存的客戶的數量
public customerList(int totalCustomer){
customers=new customer[totalCustomer];
}
public boolean addcustomer(customer customes){
if (total>=customers.length){
return false;
}
customers[total++]=customes;
return true;
}
public boolean replacecustomer(int index,customer cust){
if (index<0 || index>=total){
return false;
}
customers[index]=cust;
return true;
}
public boolean deletecustomer(int index){
if (index<0 || index>=total){
return false;
}
for (int i=0;i customers[i]=customers[i+1]; } customers[--total]=null; return true; } public customer[] getAllCustomers(){ customer[] custs=new customer[total]; for (int i=0;i custs[i]=customers[i]; } return custs; } public customer getCustomer(int index){ if (index<0 || index>=total){ return null; } return customers[index]; } public int getTotal(){ return total; } } customerView文件,實現主界面實現和邏輯交互 package customer.ui; import customer.bean.customer; import customer.service.customerList; import customer.util.CMUtil; public class customerview { private customerList customers=new customerList(10); public customerview(){ customer cust=new customer("張三", '男', 30, "010-56253825", "abc@email.com"); customers.addcustomer(cust); } public void enterMainMenu(){ boolean loopFlag = true; do { System.out.println("\n-----------------客戶信息管理軟件-----------------\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(" 請選擇(1-5):"); char key = CMUtil.readMenuSelection(); System.out.println(); switch (key) { case '1': addNewCustomer(); break; case '2': modifyCustomer(); break; case '3': deleteCustomer(); break; case '4': listAllCustomers(); break; case '5': System.out.print("確認是否退出(Y/N):"); char yn = CMUtil.readConfirmSelection(); if (yn == 'Y') loopFlag = false; break; } } while (loopFlag); } private void addNewCustomer() { System.out.println("---------------------添加客戶---------------------"); System.out.print("姓名:"); String name = CMUtil.readString(4); System.out.print("性別:"); char gender = CMUtil.readChar(); System.out.print("年齡:"); int age = CMUtil.readInt(); System.out.print("電話:"); String phone = CMUtil.readString(15); System.out.print("郵箱:"); String email = CMUtil.readString(30); customer cust = new customer(name, gender, age, phone, email); boolean flag = customers.addcustomer(cust); if (flag) { System.out .println("---------------------添加完成---------------------"); } else { System.out.println("----------------記錄已滿,無法添加-----------------"); } } private void modifyCustomer() { System.out.println("---------------------修改客戶---------------------"); int index = 0; customer cust = null; for (;;) { System.out.print("請選擇待修改客戶編號(-1退出):"); index = CMUtil.readInt(); if (index == -1) { return; } cust = customers.getCustomer(index - 1); if (cust == null) { System.out.println("無法找到指定客戶!"); } else break; } System.out.print("姓名(" + cust.getName() + "):"); String name = CMUtil.readString(4, cust.getName()); System.out.print("性別(" + cust.getGender() + "):"); char gender = CMUtil.readChar(cust.getGender()); System.out.print("年齡(" + cust.getAge() + "):"); int age = CMUtil.readInt(cust.getAge()); System.out.print("電話(" + cust.getPhone() + "):"); String phone = CMUtil.readString(15, cust.getPhone()); System.out.print("郵箱(" + cust.getEmail() + "):"); String email = CMUtil.readString(15, cust.getEmail()); cust = new customer(name, gender, age, phone, email); boolean flag = customers.replacecustomer(index - 1, cust); if (flag) { System.out .println("---------------------修改完成---------------------"); } else { System.out.println("----------無法找到指定客戶,修改失敗--------------"); } } private void deleteCustomer() { System.out.println("---------------------刪除客戶---------------------"); int index = 0; customer cust = null; for (;;) { System.out.print("請選擇待刪除客戶編號(-1退出):"); index = CMUtil.readInt(); if (index == -1) { return; } cust = customers.getCustomer(index - 1); if (cust == null) { System.out.println("無法找到指定客戶!"); } else break; } System.out.print("確認是否刪除(Y/N):"); char yn = CMUtil.readConfirmSelection(); if (yn == 'N') return; boolean flag = customers.deletecustomer(index - 1); if (flag) { System.out .println("---------------------刪除完成---------------------"); } else { System.out.println("----------無法找到指定客戶,刪除失敗--------------"); } } private void listAllCustomers() { System.out.println("---------------------------客戶列表---------------------------"); customer[] custs = customers.getAllCustomers(); if (custs.length == 0) { System.out.println("沒有客戶記錄!"); } else { System.out.println("編號\t姓名\t性別\t年齡\t\t電話\t\t郵箱"); for (int i = 0; i < custs.length; i++) { System.out.println(i + 1 + "\t" + custs[i].getName() + "\t" + custs[i].getGender() + "\t" + custs[i].getAge() + "\t\t" + custs[i].getPhone() + "\t" + custs[i].getEmail()); //System.out.println((i+1) + "\t" + custs[i].getDetails()); } } System.out.println("-------------------------客戶列表完成-------------------------"); } public static void main(String[] args) { customerview cView = new customerview(); cView.enterMainMenu(); } } Java 數據結構
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。