整體結構

工具類——Utility
package oop.houserentexercise; /** 工具類的作用: 處理各種情況的用戶輸入,并且能夠按照程式員的需求,得到用戶的控制臺輸入, */ import java.util.*; /** */ public class Utility { //靜態屬性,,, private static Scanner scanner = new Scanner(System.in); /** * 功能:讀取鍵盤輸入的一個選單選項,值:1——5的范圍 * @return 1——5 */ public static char readMenuSelection() { char c; for (; ; ) { String str = readKeyBoard(1, false);//包含一個字符的字串 c = str.charAt(0);//將字串轉換成字符char型別 if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5'&& c != '6') { System.out.print("選擇錯誤,請重新輸入:"); } else break; } return c; } /** * 功能:讀取鍵盤輸入的一個字符 * @return 一個字符 */ public static char readChar() { String str = readKeyBoard(1, false);//就是一個字符 return str.charAt(0); } /** * 功能:讀取鍵盤輸入的一個字符,如果直接按回車,則回傳指定的默認值;否則回傳輸入的那個字符 * @param defaultValue 指定的默認值 * @return 默認值或輸入的字符 */ public static char readChar(char defaultValue) { String str = readKeyBoard(1, true);//要么是空字串,要么是一個字符 return (str.length() == 0) ? defaultValue : str.charAt(0); } /** * 功能:讀取鍵盤輸入的整型,長度小于2位 * @return 整數 */ public static int readInt() { int n; for (; ; ) { String str = readKeyBoard(10, false);//一個整數,長度<=10位 try { n = Integer.parseInt(str);//將字串轉換成整數 break; } catch (NumberFormatException e) { System.out.print("數字輸入錯誤,請重新輸入:"); } } return n; } /** * 功能:讀取鍵盤輸入的 整數或默認值,如果直接回車,則回傳默認值,否則回傳輸入的整數 * @param defaultValue 指定的默認值 * @return 整數或默認值 */ public static int readInt(int defaultValue) { int n; for (; ; ) { String str = readKeyBoard(10, true); if (str.equals("")) { return defaultValue; } //例外處理... try { n = Integer.parseInt(str); break; } catch (NumberFormatException e) { System.out.print("數字輸入錯誤,請重新輸入:"); } } return n; } /** * 功能:讀取鍵盤輸入的指定長度的字串 * @param limit 限制的長度 * @return 指定長度的字串 */ public static String readString(int limit) { return readKeyBoard(limit, false); } /** * 功能:讀取鍵盤輸入的指定長度的字串或默認值,如果直接回車,回傳默認值,否則回傳字串 * @param limit 限制的長度 * @param defaultValue 指定的默認值 * @return 指定長度的字串 */ public static String readString(int limit, String defaultValue) { String str = readKeyBoard(limit, true); return str.equals("")? defaultValue : str; } /** * 功能:讀取鍵盤輸入的確認選項,Y或N * 將小的功能,封裝到一個方法中. * @return Y或N */ public static char readConfirmSelection() { System.out.println("請輸入你的選擇(Y/N): 請小心選擇"); char c; for (; ; ) {//無限回圈 //在這里,將接受到字符,轉成了大寫字母 //y => Y n=>N String str = readKeyBoard(1, false).toUpperCase(); c = str.charAt(0); if (c == 'Y' || c == 'N') { break; } else { System.out.print("選擇錯誤,請重新輸入:"); } } return c; } /** * 功能: 讀取一個字串 * @param limit 讀取的長度 * @param blankReturn 如果為true ,表示 可以讀空字串, * 如果為false表示 不能讀空字串, * * 如果輸入為空,或者輸入大于limit的長度,就會提示重新輸入, * @return */ private static String readKeyBoard(int limit, boolean blankReturn) { //定義了字串 String line = ""; //scanner.hasNextLine() 判斷有沒有下一行 while (scanner.hasNextLine()) { line = scanner.nextLine();//讀取這一行 //如果line.length=0, 即用戶沒有輸入任何內容,直接回車 if (line.length() == 0) { if (blankReturn) return line;//如果blankReturn=true,可以回傳空串 else continue; //如果blankReturn=false,不接受空串,必須輸入內容 } //如果用戶輸入的內容大于了 limit,就提示重寫輸入 //如果用戶如的內容 >0 <= limit ,我就接受 if (line.length() < 1 || line.length() > limit) { System.out.print("輸入長度(不能大于" + limit + ")錯誤,請重新輸入:"); continue; } break; } return line; } }
domain包物體類——house
將house的各項屬性封裝到類中
package oop.houserentexercise.domain; public class House { private int id;//編號 private String name;//房主姓名 private String phone;//電話 private String address;//地址 private int rentByMonth;//月租 private String state;//狀態 public House(int id, String name, String phone, String address, int rentByMonth, String state) { this.id = id; this.name = name; this.phone = phone; this.address = address; this.rentByMonth = rentByMonth; this.state = state; } @Override public String toString() { return (getId() + "\t\t" + getName() + "\t" + getPhone() + "\t\t" + getAddress() + "\t" + getRentByMonth() + "\t\t" + getState()); } 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 getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getRentByMonth() { return rentByMonth; } public void setRentByMonth(int rentByMonth) { this.rentByMonth = rentByMonth; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
service類——HouseService
實作各項功能
package oop.houserentexercise.service; import oop.houserentexercise.domain.House; public class HouseService { private int houseNums = 1; //記錄當前有多少個房屋資訊 // private int idCounter = 1; //記錄當前的id增長到哪個值 public HouseService(int houseNums, int idCounter) { this.houseNums = houseNums; // this.idCounter = idCounter; } public void list(House[] houses) { for (int i = 0; i < houses.length; i++) {//大家想想,這里老韓有什么?雷,坑 if (houses[i] == null) {//如果為null,就不用再顯示了 break; } System.out.println(houses[i]); } } public House[] add(House[] houses, House house) { if (houseNums == houses.length) { House[] num = new House[houses.length + 1]; for (int i = 0; i < houses.length; i++) { num[i] = houses[i]; } num[houses.length] = house; houses = num; System.out.println(houses[houseNums] + "添加成功!"); houseNums++; } else { houses[houseNums] = house; houseNums++; } return houses; } public House[] del(House house[], int id) { House[] delhouse = new House[house.length-1]; for (int i = id; i < house.length; i++) { if (house[i].getId() == id) { house[i] = null; break; } } for (int i = 0; i < id; i++) { delhouse[i]=house[i]; } for (int i = id; i < house.length; i++) { delhouse[i-1]=house[i]; } house=delhouse; return house; } public House findById(House[] houses, int id) { int index = -1; for (int i = 0; i < houses.length; i++) { if (id== (houses[i].getId())) {//能用equals就用 index = i; break; } } return houses[index]; } public int findById_int(House[] houses, int updateNum) { int index=0; for (int i = 0; i < houses.length; i++) { if (updateNum== (houses[i].getId())) {//能用equals就用 index = i; break; } } return index; } }
界面類
呼叫service類的函式并在界面上實作
package oop.houserentexercise.view; import oop.houserentexercise.Utility; import oop.houserentexercise.domain.House; import oop.houserentexercise.service.HouseService; public class HouseView { /* 專案界面 12 1、主選單---mainMenu 13 2、新增房源---addHouse 14 3、查找房源---findHouse 15 4、洗掉房源---delHouse 16 5、修改房源---updateHouse 17 6、列出房源---listHouse 18 7、退出程式---exit 19 */ boolean loop = true; String menuHead = "編號\t\t" + "房主姓名\t\t" + "聯系電話\t\t" + "地址\t\t" + "月租費\t\t" + "狀態(已出租/未出租)\t"; House[] houses = new House[3]; HouseService houseService = new HouseService(1, 1); public void mainMenu() { houses[0] = new House(1, "jack", "13703259980", "四社區C-417", 1500, "未出租"); do { 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(" 6、退出程式"); System.out.println("------------------------------------------------"); System.out.print("請輸入你的選擇:"); switch (Utility.readMenuSelection()) { case '1': addHouse(); break; case '2': findHouse(); break; case '3': delHouse(); break; case '4': updateHouse(); break; case '5': listHouse(); break; case '6': exit(); break; } } while (loop); System.out.println("您已成功退出系統!"); } public void exit() { char choice = ' '; while (true) { System.out.println("是否確認退出?(y/n)"); choice = Utility.readChar(); if (choice == 'n' || choice == 'y') break; } if (choice == 'y') { loop = false; return; } return; } public void listHouse() { System.out.println("=============房屋串列============"); System.out.println(menuHead); houseService.list(houses);//得到所有房屋資訊 System.out.println("=============房屋串列顯示完畢============"); } public void addHouse() { System.out.println("===================添加房屋===================="); System.out.print("編號:"); int id = Utility.readInt(); System.out.print("房主姓名:"); String name = Utility.readString(4); System.out.print("電話:"); String phone = Utility.readString(11); System.out.print("地址:"); String address = Utility.readString(20); System.out.print("月租:"); int rentByMonth = Utility.readInt(); System.out.print("狀態:"); String state = Utility.readString(3, "未表明"); House houseNew = new House(id, name, phone, address, rentByMonth, state); houses = houseService.add(houses, houseNew);//記得要把添加后的新陣列重新賦值回去 } public void delHouse() { System.out.print("請輸入你要洗掉的房源的id號:"); int id = Utility.readInt(10); houses = houseService.del(houses, id); } public void findHouse() { System.out.println("請輸入你要查找的房源的id號"); House houseFound = houseService.findById(houses, Utility.readInt()); System.out.println("房源資訊如下:\n" + menuHead + "\n" + houseFound); } public void updateHouse() { System.out.println("===================修改房屋===================="); System.out.print("請輸入你要修改的id編號"); int updateNum = Utility.readInt(10); int updateIndex = houseService.findById_int(houses, updateNum); System.out.println("房源資訊如下:+\n" + menuHead + "\n" + houses[updateIndex]); System.out.println("編號(" + houses[updateIndex].getId() + "):"); int id = Utility.readInt(); System.out.print("房主姓名(" + houses[updateIndex].getName() + "):"); String name = Utility.readString(4); System.out.print("電話(" + houses[updateIndex].getPhone() + "):"); String phone = Utility.readString(11); System.out.print("地址(" + houses[updateIndex].getAddress() + "):"); String address = Utility.readString(20); System.out.print("月租(" + houses[updateIndex].getRentByMonth() + "):"); int rentByMonth = Utility.readInt(); System.out.print("狀態(" + houses[updateIndex].getState() + "):"); String state = Utility.readString(3, "未表明"); houses[updateIndex] = new House(id, name, phone, address, rentByMonth, state); } }
運行類
程式的運行入口
package oop.houserentexercise; import oop.houserentexercise.view.HouseView; public class HouseRentApp { public static void main(String[] args) { HouseView houseView = new HouseView(); houseView.mainMenu(); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/396023.html
標籤:Java
上一篇:提升卡方反演
