本文主要是介绍ch09:房屋出租系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- 界面
- 实现
- 设计
- 实现
- 代码部分
- Utility
界面
实现
设计
实现
代码部分
package com.houserent.domain;public class House {//编号 房主 电话 地址 月租 状态(未出租/已出租)private int id;private String name;private String phone;private String address;private int rent;private String state;//为了方便的输出对象信息,我们实现toString//编号 房主 电话 地址 月租 状态(未出租/已出租)@Overridepublic String toString() {return id +"\t\t\t" + name +"\t\t" + phone +"\t\t\t" + address +"\t\t" + rent +"\t\t" + state ;}//构造器和setter,getterpublic 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;}
}
package com.houserent.service;import com.houserent.domain.House;
import hsp.houserent.utils.Utility;public class HouseService {private House[] houses;private int houseNums = 1; //记录当前有多少个房屋信息private int idCounter = 1; //记录当前的id增长到哪个值public HouseService(int size) {houses = new House[size]; //创建HouseService时,指定House数组的大小//初始1个数据,测试houses[0] = new House(1, "jack", "112", "海淀区", 2000, "未出租");}//1.list方法, 返回housespublic House[] list() {return houses;}//2.添加房屋信息public boolean add(House newHouse){//添加失败if (houseNums == houses.length) {System.out.println("数组已满,添加失败........");return false;}//添加成功houses[houseNums++] = newHouse; //移动当前的下标//我们程序员需要设计一个id自增长的机制 然后更新newHouse的idnewHouse.setId(++idCounter);return true;}//3.删除房屋信息public boolean del(int delId){//找到删除的下标int index = -1;for (int i = 0; i < houseNums; i++) {if (delId == houses[i].getId()){index = i; //找到的是 数组下标break;}}//未找到if (index == -1){return false;}//找到//数组覆盖for (int i = index; i < houseNums - 1; i++) { //houseNums非空的元素houses[i] = houses[i + 1];}//最后1个置空,并且减少1个(删掉了)houses[--houseNums] = null;return true;}//4.查找public House findById(int findID){for (int i = 0; i < houseNums; i++) {if (findID == houses[i].getId()){return houses[i];}}return null;}}
Utility
package com.houserent.utils;/**工具类的作用:处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。
*/import java.util.Scanner;/***/
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;}
}
package com.houserent.view;import com.houserent.domain.House;
import com.houserent.service.HouseService;
import com.houserent.utils.Utility;public class HouseView {private boolean loop = true;private char key = ' ';private HouseService houseService = new HouseService(2);//1.显示房屋列表public void listHouses(){System.out.println("=============房屋列表============");System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");//获取房子对象数组House house[] = houseService.list();//输出房屋对象信息for (int i = 0; i < house.length; i++) {//空的时候,跳出if (house[i] == null){break;}//直接输出对象是 调用 toString()System.out.println(house[i]);}System.out.println("=============房屋列表显示完毕============");}//2.添加房屋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===========添加房屋成功===========");}else{System.out.println("===========添加房屋失败===========");}}//3.删除public void delHouse(){System.out.println("=============删除房屋信息============");System.out.print("请输入待删除房屋的编号(-1退出):");int delId = Utility.readInt();//放弃删除if(delId == -1){System.out.println("=============放弃删除房屋信息============");return;}//3.确认删除char choice = Utility.readConfirmSelection(); //该方法本身有循环判断Y/N的逻辑if (choice == 'Y'){if (houseService.del(delId)) {System.out.println("=============删除房屋信息成功============");} else {System.out.println("=============房屋编号不存在,删除失败============");}}else {System.out.println("=============放弃删除房屋信息============");}}//4.退出public void exit(){char c = hsp.houserent.utils.Utility.readConfirmSelection();if (c == 'Y'){loop = false;}}//5.查找public void findHouse(){System.out.println("=============查找房屋信息============");System.out.println("请输入要查找的id:");int findID = Utility.readInt();//查找id的业务代码House house = houseService.findById(findID);if (house != null) {System.out.println(house);}else {System.out.println("=============查找房屋信息id不存在============");}}//6.修改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.println("姓名(" + house.getName() + "): ");String name = Utility.readString(8,"");//直接回车表示不修改 默认""if (!"".equals(name)){house.setName(name);}System.out.print("电话(" + house.getPhone() + "):");String phone = hsp.houserent.utils.Utility.readString(12, "");if (!"".equals(phone)) {house.setPhone(phone);}System.out.print("地址(" + house.getAddress() + "): ");String address = hsp.houserent.utils.Utility.readString(18, "");if (!"".equals(address)) {house.setAddress(address);}System.out.print("租金(" + house.getRent() + "):");int rent = hsp.houserent.utils.Utility.readInt(-1);if (rent != -1) {house.setRent(rent);}System.out.print("状态(" + house.getState() + "):");String state = hsp.houserent.utils.Utility.readString(3, "");if (!"".equals(state)) {house.setState(state);}System.out.println("=============修改房屋信息成功============");}public void mainMenu(){do{System.out.println("\n=============房屋出租系统菜单============");System.out.println("\t\t\t1 新 增 房 源");System.out.println("\t\t\t2 查 找 房 屋");System.out.println("\t\t\t3 删 除 房 屋 信 息");System.out.println("\t\t\t4 修 改 房 屋 信 息");System.out.println("\t\t\t5 房 屋 列 表");System.out.println("\t\t\t6 退 出");System.out.print("请输入你的选择(1-6): ");key = Utility.readChar();switch (key){case '1':
// System.out.println("添 加");addHouse();break;case '2':
// System.out.println("查 找");findHouse();break;case '3':
// System.out.println("删 除");delHouse();break;case '4':
// System.out.println("修 改");update();break;case '5':
// System.out.println("房 屋 列 表");listHouses();break;case '6':exit();break;}}while (loop);}
}
package com.houserent;import com.houserent.view.HouseView;public class HouseRentApp {public static void main(String[] args) {new HouseView().mainMenu();}
}
这篇关于ch09:房屋出租系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!