專案-房屋出租系統
筆記目錄:(https://www.cnblogs.com/wenjie2000/p/16378441.html)
房屋出租系統-需求
●專案需求說明
實作基于文本界面的《房屋出租軟體》,
能夠實作對房屋資訊的添加、修改和洗掉(用陣列實作),并能夠列印房屋明細表,
●專案界面-主選單

●專案界面-1新增房源

●專案界面-2查找房源

●專案界面-3洗掉房源

●專案界面-4修改房源
如果不希望修改某個資訊,則直接回車

●專案界面-5房屋串列

●專案界面-6退出系統

專案結構

結構細節

程式檔案結構

代碼
程式答案不唯一
HouseRentApp
package com.hspedu.houserent;
import com.hspedu.houserent.view.HouseView;
public class HouseRentApp {
public static void main(String[] args) {
//整個程式入口
new HouseView().mainMenu();
System.out.println("已退出系統");
}
}
House
package com.hspedu.houserent.domain;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utils.Utility;
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;
}
@Override
public String toString() {
return id +
"\t\t" + name +
"\t\t" + phone +
"\t\t" + address +
"\t\t" + rent +
"\t\t" + state;
}
}
HouseService
package com.hspedu.houserent.service;
import com.hspedu.houserent.domain.House;
import com.hspedu.houserent.utils.Utility;
public class HouseService {
private House[] houses;//保存House物件
private int houseNums = 1;//記錄當前有多少個房屋資訊
private int idCounter = 1;//記錄當前的id增長到哪個值
public HouseService(int size) {
//new houses
houses = new House[size];
houses[0] = new House(1, "jack", "112", "海淀區", 2000, "未出租");
}
public boolean modify(int modifyId){
int index = -1;
for (int i = 0; i < houseNums; i++) {
if (modifyId == houses[i].getId()) {//要洗掉的房屋(id),是陣列下標為i的元素
index = i;//就使用index記錄i
break;
}
}
if (index == -1) {//說明delId在陣列中不存在(有點繞..)
return false;
}
System.out.print("姓名("+houses[index].getName()+"):");
String name = Utility.readString(8);
houses[index].setName(name);
System.out.print("電話("+houses[index].getPhone()+"):");
String phone = Utility.readString(12);
houses[index].setPhone(phone);
System.out.print("地址("+houses[index].getAddress()+"):");
String address = Utility.readString(16);
houses[index].setAddress(address);
System.out.print("月租("+houses[index].getRent()+"):");
int rent = Utility.readInt();
houses[index].setRent(rent);
System.out.print("狀態("+houses[index].getState()+"):");
String state = Utility.readString(3);
houses[index].setState(state);
return true;
}
public boolean find(int findId){
int index = -1;
for (int i = 0; i < houseNums; i++) {
if (findId == houses[i].getId()) {//要洗掉的房屋(id),是陣列下標為i的元素
index = i;//就使用index記錄i
break;
}
}
if (index == -1) {//說明delId在陣列中不存在(有點繞..)
return false;
}
//如果找到,輸出
System.out.println(houses[index].toString());
return true;
}
// add方法,添加新物件,回傳boolean
public boolean add(House newHouse) {
//判斷是否還可以繼續添加(我們暫時不考慮陣列擴容的問題)
if (houseNums == houses.length) {//不能再添加
System.out.println("陣列已滿,不能再添加了...");
return false;
}
//把newHouse物件加入到,新增加了一個房屋
houses[houseNums++] = newHouse;
//我們程式員需要設計一個id自增長的機制,然后更新newHouse的id
newHouse.setId(++idCounter);
return true;
}
// del方法,洗掉一個房屋資訊
public boolean del(int delId) {
//應當先找到要洗掉的房屋資訊對應的下標
//老韓強調,一定要搞清楚下標和房屋的編號不是一回事
int index = -1;
for (int i = 0; i < houseNums; i++) {
if (delId == houses[i].getId()) {//要洗掉的房屋(id),是陣列下標為i的元素
index = i;//就使用index記錄i
}
}
if (index == -1) {//說明delId在陣列中不存在(有點繞..)
return false;
}
//如果找到,這里需要小伙伴動腦筋
for (int i = index; i < houseNums - 1; i++) {
houses[i] = houses[i + 1];
}
//把當有存在的房屋資訊的最后一個設定null
houses[--houseNums] = null;
return true;
}
//list方法,回傳houses
public House[] list() {
return houses;
}
}
Utility
package com.hspedu.houserent.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=>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;
}
}
HouseView
package com.hspedu.houserent.view;
import com.hspedu.houserent.domain.House;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utils.Utility;
public class HouseView {
private boolean loop = true;//控制顯示選單
private char key;//接受用戶選擇
private HouseService houseService = new HouseService(10);
//顯示主選單
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.println("\n請選擇(1-6):");
key = Utility.readChar();
switch (key) {
case '1':
System.out.println("1 新增房源");
addHouse();
break;
case '2':
System.out.println("2 查找房屋");
findHouse();
break;
case '3':
System.out.println("3 洗掉房屋");
delHouse();
break;
case '4':
System.out.println("4 修改房屋資訊");
modifyHouse();
break;
case '5':
System.out.println("5 房屋串列");
listHouses();
break;
case '6':
System.out.println("6 退出");
exit();
break;
}
} while (loop);
}
//1 添加房子
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物件,注意id是系統分配的,
House newHouse = new House(0, name, phone, address, rent, state);
if (houseService.add(newHouse)) {
System.out.println("\n========房屋添加成功=====\n");
} else {
System.out.println("\n========房屋添加失敗=====\n");
}
}
//2查找
public void findHouse(){
System.out.println("=============查找房屋資訊============");
System.out.println("請輸入需要查找的房屋id(-1退出):");
int findId=Utility.readInt();
if (findId == -1) {
System.out.println("=============放棄查找房屋資訊============");
return;
}
if (!houseService.find(findId)) {//查找不成功
System.out.println("房屋編號不存在,查找失敗");
}
}
//3 洗掉房屋 撰寫delHouse()接收輸入的id,呼叫Service 的del方法
public void delHouse() {
System.out.println("=============洗掉房屋資訊============");
System.out.print("請輸入待洗掉房屋的編號(-1退出):");
int delId = Utility.readInt();
if (delId == -1) {
System.out.println("=============放棄洗掉房屋資訊============");
return;
}
//注意該方法本身就有回圈判斷的邏輯,必須輸出Y/N
char choice = Utility.readConfirmSelection();
if (choice == 'Y') {//真的洗掉
if (houseService.del(delId)) {
System.out.println("洗掉房屋資訊成功");
} else {
System.out.println("房屋編號不存在,洗掉失敗");
}
} else {
System.out.println("=============放棄洗掉房屋資訊============");
}
}
//4修改
public void modifyHouse(){
System.out.println("-----------------修改資訊-----------------");
System.out.print("請輸入修改房屋的編號(-1退出):");
int modifyId = Utility.readInt();
if (modifyId == -1) {
System.out.println("=============放棄查找房屋資訊============");
return;
}
if (!houseService.modify(modifyId)){//未找到
System.out.println("房屋編號不存在");
}
}
//5 撰寫listHouse()顯示房屋串列
public void listHouses() {
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("\n=============房屋串列顯示完畢============\n");
}
//6 退出
//完成退出確認
public void exit() {
//這里我們使用Utility提供方法,完成確認
char c = Utility.readConfirmSelection();
if (c == 'Y') {
loop = false;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/500500.html
標籤:Java
上一篇:零基礎學Java(13)方法引數
