JAVA 房屋出租系统(韩顺平)
目錄
一.需求說明?
二.出租設計(韓順平老師總結)
?三.實現代碼的具體步驟(Utility工具類的代碼發在最后)
1.House類
2.創建主菜單
3.出租列表
4.添加功能
5.刪除功能
6.退出功能
7.查找功能
8.修改功能
五.總結
六.完整代碼
1.HouserentAPP部分
2.House類
3.Houseservice部分
4.Houseview部分
5.utility部分
一.需求說明?
能夠實現對房屋信息的添加、修改和刪除(用數組實現),并能夠打印房屋明細表
主要包括:主菜單,新增房源,查找房屋信息,修改房屋信息,刪除房屋信息
主菜單
添加房屋信息?
?
查找房屋信息
刪除房屋信息
?修改房屋信息
?房屋列表
?退出系統
二.出租設計(韓順平老師總結)
在這里總結一下韓老師的設計思路,首先我們需要一個與用戶交互的界面(view),其次我們需要對創建一個解決crud的(service)方法,我們可以在view界面中可以調用service方法,我們還需要一個House類來儲存房屋的屬性,在這期間我們也需要工具類完成一下特定功能(比如輸入,判斷是否退出等功能),最后就需要一個App來調用該對象。
?三.實現代碼的具體步驟(Utility工具類的代碼發在最后)
1.House類
package domain;/* House 的 對象表示一個房屋信息*/ public class House {private int id;private String name;private String phone;private String address;private int rent;private String state;//構造器public House(int id, String name, String phone, String address, int rent, String state) {this.id = id;this.name = name;this.phone = phone;this.address = address;this.rent = rent;this.state = state;}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 getRent() {return rent;}public void setRent(int rent) {this.rent = rent;}public String getState() {return state;}public void setState(String state) {this.state = state;}//為方便的輸出對象的信息,我們實現toString//編號 房主 電話 地址 月租 狀態(出租或未出租)//這里重寫了tostring方法,原本tostring方法的作用使返回拼接對象的地址值@Overridepublic String toString() {return id +"\t\t" + name +"\t" + phone +"\t\t" + address +"\t" + rent +"\t" + state ;} }2.創建主菜單
public void MainMenu(){do {System.out.println("\n=========房屋出租系統=========");System.out.println("\t\t1.新 增 房 源");System.out.println("\t\t2.查 找 房 源");System.out.println("\t\t3.刪 除 房 屋 信 息");System.out.println("\t\t4.修 改 房 屋 信 息");System.out.println("\t\t5.房 屋 列 表");System.out.println("\t\t6.退 出");System.out.print("請輸入你的選擇(1-6)");key = Utility.readChar();switch (key){case '1':addHouse();break;case '2':findHouse();break;case '3':delHouse();break;case '4':update();break;case '5':this.listHouse();break;case '6':exit();loop = false;break;}}while (loop);3.出租列表
//service界面 public House[] list(){return houses;}//view界面 public void listHouse(){System.out.println("=========房屋列表=========");System.out.println("編號\t\t房主\t\t電話\t\t地址\t\t月租\t\t狀態(未出租/已出租)");House[] houses = houseService.list();for (int i = 0; i < houses.length; i++) {if (houses[i] == null){break;}System.out.println(houses[i]);}System.out.println("========房屋列表顯示完畢========");}4.添加功能
//service界面 public boolean add(House newHouse){//判斷是否還可以繼續添加if (houseNums == houses.length){System.out.println("數組已滿,不能再添加");return false;}houses[houseNums++] = newHouse;newHouse.setId(++idCounter);return true;}view界面 //addHouse()接收輸入public void addHouse(){System.out.println("=========添加房屋=========");System.out.print("姓名:");String name = Utility.readString(8);//括號表示字符串長度System.out.print("電話:");String phone = Utility.readString(12);System.out.print("地址:");String address = Utility.readString(16);System.out.print("月租:");int rent = Utility.readInt();System.out.print("狀態:");String state = Utility.readString(3);House house = new House(0, name, phone, address, rent, state);if (houseService.add(house)){System.out.println("添加房屋成功");}else {System.out.println("添加房屋失敗");}}5.刪除功能
//service界面 //del方法 刪除一個房屋信息public boolean del(int delId){int index = -1;for (int i = 0; i < houseNums; i++) {if (delId == houses[i].getId()){//判斷房屋號是否存在index = i;}}if (index == -1){return false;}for (int i = index; i < houseNums - 1; i++) {houses[i] = houses[i + 1];}houses[--houseNums] = null;return true;}//view界面//編寫delHouse()接受輸入的id,調用Service的del方法public void delHouse() {System.out.println("=========刪除房屋=========");System.out.println("請輸入待刪除房屋的編號(-1退出)");int delId = Utility.readInt();if (delId == -1) {System.out.println("==========放棄刪除房屋信息=========");return;}char choice = Utility.readConfirmSelection();//可以將y轉化為Y,判斷是否放棄或是否退出if (choice == 'Y') {//確定刪除if (houseService.del(delId)) {System.out.println("=========刪除房屋信息成功=========");} else {System.out.println("=========房屋編號不存在,刪除失敗");}} else {System.out.println("=========放棄刪除房屋信息=========");}}6.退出功能
//view界面 //退出public void exit(){char c = Utility.readConfirmSelection();//大家可以ctrl + B調看這個方法的源碼if (c == 'Y'){loop = false;}}7.查找功能
//service界面//findbyId方法 返回House對象或nullpublic House findById(int findId){for (int i = 0; i < houseNums; i++) {//判斷房屋號是否存在if (findId == houses[i].getId()){return houses[i];}}return null;}//view界面 //根據id查找房屋信息public void findHouse(){System.out.println("=========查找房屋信息=========");System.out.println("請輸入要查找的id");int findId = Utility.readInt();//調用方法House house = houseService.findById(findId);if (house != null){System.out.println(house);}else {System.out.println("=========查找房屋信息id不存在=========");}}8.修改功能
//service界面 //del方法 刪除一個房屋信息public boolean del(int delId){int index = -1;for (int i = 0; i < houseNums; i++) {if (delId == houses[i].getId()){index = i;}}if (index == -1){return false;}for (int i = index; i < houseNums - 1; i++) {houses[i] = houses[i + 1];}houses[--houseNums] = null;return true;}//view界面 //修改public void update(){System.out.println("=========修改房屋信息=========");System.out.println("請選擇待修改房屋編號(-1表示退出)");int updateId = Utility.readInt();if (updateId == -1){System.out.println("=========放棄修改房屋信息=========");return;}House house = houseService.findById(updateId);if (house == null) {System.out.println("=========修改房屋信息編號不存在=========");return;}System.out.print("姓名(" + house.getName() + "): ");//這里如果用戶直接回車表示不修改信息 默認""String name = Utility.readString(8, "");if (!"".equals(name)) {//修改house.setName(name);}System.out.print("電話(" + house.getPhone() + "):");String phone = Utility.readString(12, "");if (!"".equals(phone)) {house.setPhone(phone);}System.out.print("地址(" + house.getAddress() + "):");String address = Utility.readString(18, "");if (!"".equals(address)) {house.setAddress(address);}System.out.print("租金(" + house.getRent() + "):");int rent = Utility.readInt(-1);if (rent != -1) {house.setRent(rent);}System.out.println("狀態(" + house.getState() + "):");String state = Utility.readString(3, "");if (!"".equals(state)) {house.setState(state);}System.out.println("===========修改房屋信息成功=========");}五.總結
最后總結一下,當我們面對這個比較復雜的房屋出租系統的問題時,我們可以采用分層模式來化繁為簡,即我們就需要考慮兩個問題,我們需要哪些類,類和類直接的調用關系是什么(筆者感覺這個項目中的類與類之間的關系比較復雜),具體框架可以看文章最開始的圖片,這里我們再介紹一下utility工具類,這個也就是我們俗稱的輪子,公司一般會提前封裝好,我們直接調用就行了。完整代碼發在下面啦(O ^ ~ ^ O)
六.完整代碼
1.HouserentAPP部分
import houserent.View.HouseView;public class HouserentAPP {public static void main(String[] args) {new HouseView().MainMenu();System.out.println("==退出房屋出租系統==");} }2.House類
package domain;/* House 的 對象表示一個房屋信息*/ public class House {private int id;private String name;private String phone;private String address;private int rent;private String state;//構造器public House(int id, String name, String phone, String address, int rent, String state) {this.id = id;this.name = name;this.phone = phone;this.address = address;this.rent = rent;this.state = state;}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 getRent() {return rent;}public void setRent(int rent) {this.rent = rent;}public String getState() {return state;}public void setState(String state) {this.state = state;}//為方便的輸出對象的信息,我們實現toString//編號 房主 電話 地址 月租 狀態(出租或未出租)@Overridepublic String toString() {return id +"\t\t" + name +"\t" + phone +"\t\t" + address +"\t" + rent +"\t" + state ;} }3.Houseservice部分
package Service;import domain.House;public class HouseService {private House[] houses;private int houseNums = 1;private int idCounter = 1;public HouseService(int size){houses = new House[size];houses[0] = new House(1,"jack","123","金水區",2000,"未出租");}//findbyId方法 返回House對象或nullpublic House findById(int findId){for (int i = 0; i < houseNums; i++) {if (findId == houses[i].getId()){return houses[i];}}return null;}//del方法 刪除一個房屋信息public boolean del(int delId){int index = -1;for (int i = 0; i < houseNums; i++) {if (delId == houses[i].getId()){index = i;}}if (index == -1){return false;}for (int i = index; i < houseNums - 1; i++) {houses[i] = houses[i + 1];}houses[--houseNums] = null;return true;}public boolean add(House newHouse){//判斷是否還可以繼續添加if (houseNums == houses.length){System.out.println("數組已滿,不能再添加");return false;}houses[houseNums++] = newHouse;newHouse.setId(++idCounter);return true;}public House[] list(){return houses;}}4.Houseview部分
package houserent.View;import Service.HouseService; import domain.House; import utils.Utility;/* 1.顯示界面 2.接受用戶的輸入 3.調用HOuseService完成對房屋信息的各種操作*/ public class HouseView {private boolean loop = true;private char key = ' ';//接受用戶選擇private HouseService houseService = new HouseService(10);//修改public void update(){System.out.println("=========修改房屋信息=========");System.out.println("請選擇待修改房屋編號(-1表示退出)");int updateId = Utility.readInt();if (updateId == -1){System.out.println("=========放棄修改房屋信息=========");return;}House house = houseService.findById(updateId);if (house == null) {System.out.println("=========修改房屋信息編號不存在=========");return;}System.out.print("姓名(" + house.getName() + "): ");//這里如果用戶直接回車表示不修改信息 默認""String name = Utility.readString(8, "");if (!"".equals(name)) {//修改house.setName(name);}System.out.print("電話(" + house.getPhone() + "):");String phone = Utility.readString(12, "");if (!"".equals(phone)) {house.setPhone(phone);}System.out.print("地址(" + house.getAddress() + "):");String address = Utility.readString(18, "");if (!"".equals(address)) {house.setAddress(address);}System.out.print("租金(" + house.getRent() + "):");int rent = Utility.readInt(-1);if (rent != -1) {house.setRent(rent);}System.out.println("狀態(" + house.getState() + "):");String state = Utility.readString(3, "");if (!"".equals(state)) {house.setState(state);}System.out.println("===========修改房屋信息成功=========");}//根據id查找房屋信息public void findHouse(){System.out.println("=========查找房屋信息=========");System.out.println("請輸入要查找的id");int findId = Utility.readInt();//調用方法House house = houseService.findById(findId);if (house != null){System.out.println(house);}else {System.out.println("=========查找房屋信息id不存在=========");}}//退出public void exit(){char c = Utility.readConfirmSelection();if (c == 'Y'){loop = false;}}//編寫delHouse()接受輸入的id,調用Service的del方法public void delHouse() {System.out.println("=========刪除房屋=========");System.out.println("請輸入待刪除房屋的編號(-1退出)");int delId = Utility.readInt();if (delId == -1) {System.out.println("==========放棄刪除房屋信息=========");return;}char choice = Utility.readConfirmSelection();if (choice == 'Y') {//確定刪除if (houseService.del(delId)) {System.out.println("=========刪除房屋信息成功=========");} else {System.out.println("=========房屋編號不存在,刪除失敗");}} else {System.out.println("=========放棄刪除房屋信息=========");}}//addHouse()接收輸入public void addHouse(){System.out.println("=========添加房屋=========");System.out.print("姓名:");String name = Utility.readString(8);System.out.print("電話:");String phone = Utility.readString(12);System.out.print("地址:");String address = Utility.readString(16);System.out.print("月租:");int rent = Utility.readInt();System.out.print("狀態:");String state = Utility.readString(3);House house = new House(0, name, phone, address, rent, state);if (houseService.add(house)){System.out.println("添加房屋成功");}else {System.out.println("添加房屋失敗");}}public void listHouse(){System.out.println("=========房屋列表=========");System.out.println("編號\t\t房主\t\t電話\t\t地址\t\t月租\t\t狀態(未出租/已出租)");House[] houses = houseService.list();for (int i = 0; i < houses.length; i++) {if (houses[i] == null){break;}System.out.println(houses[i]);}System.out.println("========房屋列表顯示完畢========");}//顯示主菜單public void MainMenu(){do {System.out.println("\n=========房屋出租系統=========");System.out.println("\t\t1.新 增 房 源");System.out.println("\t\t2.查 找 房 源");System.out.println("\t\t3.刪 除 房 屋 信 息");System.out.println("\t\t4.修 改 房 屋 信 息");System.out.println("\t\t5.房 屋 列 表");System.out.println("\t\t6.退 出");System.out.print("請輸入你的選擇(1-6)");key = Utility.readChar();switch (key){case '1':addHouse();break;case '2':findHouse();break;case '3':delHouse();break;case '4':update();break;case '5':this.listHouse();break;case '6':exit();loop = false;break;}}while (loop);} }5.utility部分
package utils;/**工具類的作用:處理各種情況的用戶輸入,并且能夠按照程序員的需求,得到用戶的控制臺輸入。 */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') {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=>NString 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;} }總結
以上是生活随笔為你收集整理的JAVA 房屋出租系统(韩顺平)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mybatis查询传递单个参数和传递多个
- 下一篇: 使多个线程循环输出0-99-0-99