一、房屋出租系统-需求&&界面
1项目需求说明
- 实现基于文本界面的《房屋出租软件》。
- 能够实现对房屋信息的添加、修改和删除(用数组实现),并能够打印房屋明细表
2 房屋出租系统-界面
2.1 项目界面 - 主菜单
2.2项目界面- 新增房源
2.3项目界面- 查找房源
2.4项目界面- 删除房源
2.5项目界面- 修改房源
2.6项目界面- 房屋列表
2.7项目界面- 退出系统
二、房屋出租系统-设计
项目设计-程序框架图 (分层模式=>当软件比较复杂,需要模式管理)
三、房屋出租系统-实现
1准备工具类 Utility,提高开发效率
- 在实际开发中,公司都会提供相应的工具类和开发库,可以提高开发效率,程序员也需要能够看懂别人写的代码, 并能够正确的调用。
- 1) 了解 Utility 类的使用
- 2) 测试 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=>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.hspedu.houserent.utils;public class TestUtility {public static void main(String[] args) {//这是一个测试类,使用完毕就可删除//要求输入一个字符串,长度最大为3//String s = Utility.readString(3);//System.out.println("s="+s);//要求输入一个字符串,长度最大为10,如果用户直接回车,就给一个默认值//"liushaolin",就把方法当做api使用即可String s2 = Utility.readString(10,"liushaolin");System.out.println("s2="+s2);//类.方法()=》因为静态方法是static时,就是一个静态方法//注意:静态方法可以直接通过类名调用}
}
2项目功能实现-完成 House 类
- 编号 房主 电话 地址 月租 状态(未出租/已出租
package com.hspedu.houserent.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\t" + phone + "\t\t" + address + "\t\t" + rent +"\t\t" + state;}
}
3.显示主菜单和完成退出软件功能
- 实现功能的三部曲 [明确完成功能->思路分析->代码实现]
功能说明
用户打开软件, 可以看到主菜单,可以退出软件
思路分析:
在 HouseView.java 中,编写一个方法 mainMenu,显示菜单
代码实现:
package com.hspedu.houserent.view;import com.hspedu.houserent.utils.Utility;/*** 1.显示页面* 2.接收用户信息* 3。调用HouseService完成对房屋的各种操作*/
public class HouseView {//显示主菜单private boolean loop =true;//控制显示菜单private char key =' ';//接收用户选择public void mainMenue(){do {System.out.println("============房屋出租系统===============");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.println("请输入你的选择(1-6)");key = Utility.readChar();switch (key){case '1':System.out.println("新增");break;case '2':System.out.println("查找");break;case '3':System.out.println("删除");break;case '4':System.out.println("修改");break;case '5':System.out.println("显示");//房屋列表break;case '6':System.out.println("退 出");loop = false;break;}}while(loop);}
}
package com.hspedu.houserent;import com.hspedu.houserent.view.HouseView;public class HouseRentApp {public static void main(String[] args) {new HouseView().mainMenue();}
}
4.完成显示房屋列表的功能
package com.hspedu.houserent.service;import com.hspedu.houserent.domain.House;/*** HouseService=>类【业务层】* 1.相应HouseView的调用* 2.完成对房屋信息的各种操作【增删改查】**/
public class HouseService {private House[] houses;//保存House对象public HouseService(int size){houses =new House[size];//当创建HouseService对象,指定数组大小//为了测试列表信息,初始化列表对象houses[0] = new House(1,"Jach","112","海定区",2000,"未出租");}//list 方法,返回所有房屋信息public House [] list(){return houses;}
}
package com.hspedu.houserent.view;import com.hspedu.houserent.domain.House;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utils.Utility;/*** 1.显示页面* 2.接收用户信息* 3。调用HouseService完成对房屋的各种操作*/
public class HouseView {//显示主菜单private boolean loop =true;//控制显示菜单private char key =' ';//接收用户选择private HouseService houseService = new HouseService(10);//编写listHouse()显示房屋列表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++) {//代码不够完善,实际房屋可能没有10个,进行判断if (houses[i]==null)break;System.out.println(houses[i].toString());}System.out.println("===================房屋列表显示完毕==============");}public void mainMenue(){do {System.out.println("============房屋出租系统===============");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.println("请输入你的选择(1-6)");key = Utility.readChar();switch (key){case '1':System.out.println("新增");break;case '2':System.out.println("查找");break;case '3':System.out.println("删除");break;case '4':System.out.println("修改");break;case '5':listHouse()//房屋列表break;case '6':System.out.println("退 出");loop = false;break;}}while(loop);}
}
5.添加房屋信息的功能
package com.hspedu.houserent.view;import com.hspedu.houserent.domain.House;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utils.Utility;/*** 1.显示页面* 2.接收用户信息* 3。调用HouseService完成对房屋的各种操作*/
public class HouseView {//显示主菜单private boolean loop =true;//控制显示菜单private char key =' ';//接收用户选择private HouseService houseService = new HouseService(10);//编写listHouse()显示房屋列表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++) {//代码不够完善,实际房屋可能没有10个,进行判断if (houses[i]==null)break;System.out.println(houses[i].toString());}System.out.println("===================房屋列表显示完毕==============");}//addHouse()接收输入,创建House对象,调用add方法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对象,系统分配House house = new House(0, name, phone, address, rent, state);if (houseService.add(house)){System.out.println("===================添加房屋成功==============");}else {System.out.println("===================添加房屋失败==============");}}public void mainMenue(){do {System.out.println("============房屋出租系统===============");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.println("请输入你的选择(1-6)");key = Utility.readChar();switch (key){case '1':addHouse();break;case '2':System.out.println("查找");break;case '3':System.out.println("删除");break;case '4':System.out.println("修改");break;case '5':listHouse();//房屋列表break;case '6':System.out.println("退 出");loop = false;break;}}while(loop);}
}
package com.hspedu.houserent.service;import com.hspedu.houserent.domain.House;/*** HouseService=>类【业务层】* 1.相应HouseView的调用* 2.完成对房屋信息的各种操作【增删改查】**/
public class HouseService {private House[] houses;//保存House对象private int houseNumbers = 1;//记录当前有多少房屋信息private int idCounter = 1;//记录当前的id增长到哪个值public HouseService(int size){houses =new House[size];//当创建HouseService对象,指定数组大小//为了测试列表信息,初始化列表对象houses[0] = new House(1,"Jach","112","海定区",2000,"未出租");}//list 方法,返回所有房屋信息public House [] list(){return houses;}//add方法public boolean add(House newhouse){//判断是否还可以添加(暂时不考虑数组扩容)if (houseNumbers==houses.length)//不能再添加{System.out.println("数组已满不能再添加");return false;}else{//把newHouse对象加入到数组空间houses[houseNumbers]=newhouse;houseNumbers++;//还需要设计一个id自增长idCounter++;newhouse.setId(idCounter);//更新newhouse的idreturn true;}}
}
6.完成删除房屋信息的功能
package com.hspedu.houserent.view;import com.hspedu.houserent.domain.House;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utils.Utility;/*** 1.显示页面* 2.接收用户信息* 3。调用HouseService完成对房屋的各种操作*/
public class HouseView {//显示主菜单private boolean loop =true;//控制显示菜单private char key =' ';//接收用户选择private HouseService houseService = new HouseService(10);//编写delHouse()接收输入的idpublic void delHouse(){System.out.println("===================删除房屋信息==============");System.out.print("请输入待删除房屋的编号(-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("===================放弃删除房屋信息==============");return;}}//编写listHouse()显示房屋列表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++) {//代码不够完善,实际房屋可能没有10个,进行判断if (houses[i]==null)break;System.out.println(houses[i].toString());}System.out.println("===================房屋列表显示完毕==============");}//addHouse()接收输入,创建House对象,调用add方法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对象,系统分配House house = new House(0, name, phone, address, rent, state);if (houseService.add(house)){System.out.println("===================添加房屋成功==============");}else {System.out.println("===================添加房屋失败==============");}}public void mainMenue(){do {System.out.println("============房屋出租系统===============");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.println("请输入你的选择(1-6)");key = Utility.readChar();switch (key){case '1':addHouse();break;case '2':System.out.println("查找");break;case '3':delHouse();break;case '4':System.out.println("修改");break;case '5':listHouse();//房屋列表break;case '6':System.out.println("退 出");loop = false;break;}}while(loop);}
}
package com.hspedu.houserent.service;import com.hspedu.houserent.domain.House;/*** HouseService=>类【业务层】* 1.相应HouseView的调用* 2.完成对房屋信息的各种操作【增删改查】**/
public class HouseService {private House[] houses;//保存House对象private int houseNumbers = 1;//记录当前有多少房屋信息private int idCounter = 1;//记录当前的id增长到哪个值public HouseService(int size){houses =new House[size];//当创建HouseService对象,指定数组大小//为了测试列表信息,初始化列表对象houses[0] = new House(1,"Jach","112","海定区",2000,"未出租");}//del方法 删除一个房屋信息public boolean del(int delId){//应该先找到删除的房屋信息对应的下标int index=-1;for (int i = 0; i <houseNumbers; i++) {if(houses[i].getId()==delId){index=i;break;}}if(index==-1){//不存在该房屋IdSystem.out.println("房屋对应id的信息不存在");return false;}//如果找到for (int i = index; i <houseNumbers-1; i++) {houses[i]=houses[i+1];}houses[houseNumbers-1]=null;houseNumbers--;return true;}//list 方法,返回所有房屋信息public House [] list(){return houses;}//add方法public boolean add(House newhouse){//判断是否还可以添加(暂时不考虑数组扩容)if (houseNumbers==houses.length)//不能再添加{System.out.println("数组已满不能再添加");return false;}else{//把newHouse对象加入到数组空间houses[houseNumbers]=newhouse;houseNumbers++;//还需要设计一个id自增长idCounter++;newhouse.setId(idCounter);//更新newhouse的idreturn true;}}
}
7.-完善退出确认功能
把输入要求改写成了Y或者N
package com.hspedu.houserent.view;import com.hspedu.houserent.domain.House;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utils.Utility;/*** 1.显示页面* 2.接收用户信息* 3。调用HouseService完成对房屋的各种操作*/
public class HouseView {//显示主菜单private boolean loop =true;//控制显示菜单private char key =' ';//接收用户选择private HouseService houseService = new HouseService(10);//退出public void exit(){char c =Utility.readConfirmSelection();if (c=='Y'){loop = false;}}//编写delHouse()接收输入的idpublic void delHouse(){System.out.println("===================删除房屋信息==============");System.out.print("请输入待删除房屋的编号(-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("===================放弃删除房屋信息==============");return;}}//编写listHouse()显示房屋列表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++) {//代码不够完善,实际房屋可能没有10个,进行判断if (houses[i]==null)break;System.out.println(houses[i].toString());}System.out.println("===================房屋列表显示完毕==============");}//addHouse()接收输入,创建House对象,调用add方法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对象,系统分配House house = new House(0, name, phone, address, rent, state);if (houseService.add(house)){System.out.println("===================添加房屋成功==============");}else {System.out.println("===================添加房屋失败==============");}}public void mainMenue(){do {System.out.println("============房屋出租系统===============");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.println("请输入你的选择(1-6)");key = Utility.readChar();switch (key){case '1':addHouse();break;case '2':System.out.println("查找");break;case '3':delHouse();break;case '4':System.out.println("修改");break;case '5':listHouse();//房屋列表break;case '6':exit();break;}}while(loop);}
}
8.完成根据 id 查找房屋信息的功能
package com.hspedu.houserent.view;import com.hspedu.houserent.domain.House;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utils.Utility;/*** 1.显示页面* 2.接收用户信息* 3。调用HouseService完成对房屋的各种操作*/
public class HouseView {//显示主菜单private boolean loop =true;//控制显示菜单private char key =' ';//接收用户选择private HouseService houseService = new HouseService(10);//查找public void findHouse(){System.out.println("===================查找房屋信息==============");System.out.print("请输入待查找房屋的编号");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()接收输入的idpublic void delHouse(){System.out.println("===================删除房屋信息==============");System.out.print("请输入待删除房屋的编号(-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("===================放弃删除房屋信息==============");return;}}//编写listHouse()显示房屋列表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++) {//代码不够完善,实际房屋可能没有10个,进行判断if (houses[i]==null)break;System.out.println(houses[i].toString());}System.out.println("===================房屋列表显示完毕==============");}//addHouse()接收输入,创建House对象,调用add方法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对象,系统分配House house = new House(0, name, phone, address, rent, state);if (houseService.add(house)){System.out.println("===================添加房屋成功==============");}else {System.out.println("===================添加房屋失败==============");}}public void mainMenue(){do {System.out.println("============房屋出租系统===============");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.println("请输入你的选择(1-6)");key = Utility.readChar();switch (key){case '1':addHouse();break;case '2':findHouse();break;case '3':delHouse();break;case '4':System.out.println("修改");break;case '5':listHouse();//房屋列表break;case '6':exit();break;}}while(loop);}
}
package com.hspedu.houserent.service;import com.hspedu.houserent.domain.House;/*** HouseService=>类【业务层】* 1.相应HouseView的调用* 2.完成对房屋信息的各种操作【增删改查】**/
public class HouseService {private House[] houses;//保存House对象private int houseNumbers = 1;//记录当前有多少房屋信息private int idCounter = 1;//记录当前的id增长到哪个值public HouseService(int size){houses =new House[size];//当创建HouseService对象,指定数组大小//为了测试列表信息,初始化列表对象houses[0] = new House(1,"Jach","112","海定区",2000,"未出租");}//find 方法 返回House对象或者为空public House findById(int findId){for (int i = 0; i < houseNumbers; i++) {if(houses[i].getId()==findId){return houses[i];}}return null;}//del方法 删除一个房屋信息public boolean del(int delId){//应该先找到删除的房屋信息对应的下标int index=-1;for (int i = 0; i <houseNumbers; i++) {if(houses[i].getId()==delId){index=i;break;}}if(index==-1){//不存在该房屋IdSystem.out.println("房屋对应id的信息不存在");return false;}//如果找到for (int i = index; i <houseNumbers-1; i++) {houses[i]=houses[i+1];}houses[houseNumbers-1]=null;houseNumbers--;return true;}//list 方法,返回所有房屋信息public House [] list(){return houses;}//add方法public boolean add(House newhouse){//判断是否还可以添加(暂时不考虑数组扩容)if (houseNumbers==houses.length)//不能再添加{System.out.println("数组已满不能再添加");return false;}else{//把newHouse对象加入到数组空间houses[houseNumbers]=newhouse;houseNumbers++;//还需要设计一个id自增长idCounter++;newhouse.setId(idCounter);//更新newhouse的idreturn true;}}
}
9.完成修改房屋信息的功能
package com.hspedu.houserent.view;import com.hspedu.houserent.domain.House;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utils.Utility;/*** 1.显示页面* 2.接收用户信息* 3。调用HouseService完成对房屋的各种操作*/
public class HouseView {//显示主菜单private boolean loop =true;//控制显示菜单private char key =' ';//接收用户选择private HouseService houseService = new HouseService(10);//修改public void updateHouse(){System.out.println("===================修改房屋信息==============");System.out.print("请输入待修改房屋的编号(-1退出)");int updateId =Utility.readInt();if (updateId==-1){System.out.println("===================放弃修改房屋信息==============");return;}//根据输入的updateId查找对象House house =houseService.findById(updateId);if(house==null){System.out.println("==============修改房屋信息编号不存在,无法修改==============");return;}System.out.print("姓名("+house.getName()+"):");String name =Utility.readString(8,house.getName());//如果用户直接回车,表示不修改该字段if(!house.getName().equals(name)){//修改house.setName(name);}System.out.print("电话("+house.getPhone()+"):");String phone =Utility.readString(12,house.getPhone());//如果用户直接回车,表示不修改该字段if(!house.getPhone().equals(phone)){//修改house.setPhone(phone);}System.out.print("地址("+house.getAddress()+"):");String address = Utility.readString(18,house.getAddress());if(!house.getAddress().equals(address)){//修改house.setAddress(address);}System.out.print("租金("+house.getRent()+"):");int Rent =Utility.readInt(house.getRent());if(!(Rent==house.getRent())){house.setRent(Rent);}System.out.print("状态("+house.getState()+"):");String state = Utility.readString(3,house.getState());if(!house.getState().equals(state)){//修改house.setState(state);}System.out.println("==============修改房屋信息成功==============");}//查找public void findHouse(){System.out.println("===================查找房屋信息==============");System.out.print("请输入待查找房屋的编号");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()接收输入的idpublic void delHouse(){System.out.println("===================删除房屋信息==============");System.out.print("请输入待删除房屋的编号(-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("===================放弃删除房屋信息==============");return;}}//编写listHouse()显示房屋列表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++) {//代码不够完善,实际房屋可能没有10个,进行判断if (houses[i]==null)break;System.out.println(houses[i].toString());}System.out.println("===================房屋列表显示完毕==============");}//addHouse()接收输入,创建House对象,调用add方法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对象,系统分配House house = new House(0, name, phone, address, rent, state);if (houseService.add(house)){System.out.println("===================添加房屋成功==============");}else {System.out.println("===================添加房屋失败==============");}}public void mainMenue(){do {System.out.println("============房屋出租系统===============");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.println("请输入你的选择(1-6)");key = Utility.readChar();switch (key){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);}
}
package com.hspedu.houserent.service;import com.hspedu.houserent.domain.House;/*** HouseService=>类【业务层】* 1.相应HouseView的调用* 2.完成对房屋信息的各种操作【增删改查】**/
public class HouseService {private House[] houses;//保存House对象private int houseNumbers = 1;//记录当前有多少房屋信息private int idCounter = 1;//记录当前的id增长到哪个值public HouseService(int size){houses =new House[size];//当创建HouseService对象,指定数组大小//为了测试列表信息,初始化列表对象houses[0] = new House(1,"Jach","112","海定区",2000,"未出租");}//find 方法 返回House对象或者为空public House findById(int findId){for (int i = 0; i < houseNumbers; i++) {if(houses[i].getId()==findId){return houses[i];}}return null;}//del方法 删除一个房屋信息public boolean del(int delId){//应该先找到删除的房屋信息对应的下标int index=-1;for (int i = 0; i <houseNumbers; i++) {if(houses[i].getId()==delId){index=i;break;}}if(index==-1){//不存在该房屋IdSystem.out.println("房屋对应id的信息不存在");return false;}//如果找到for (int i = index; i <houseNumbers-1; i++) {houses[i]=houses[i+1];}houses[houseNumbers-1]=null;houseNumbers--;return true;}//list 方法,返回所有房屋信息public House [] list(){return houses;}//add方法public boolean add(House newhouse){//判断是否还可以添加(暂时不考虑数组扩容)if (houseNumbers==houses.length)//不能再添加{System.out.println("数组已满不能再添加");return false;}else{//把newHouse对象加入到数组空间houses[houseNumbers]=newhouse;houseNumbers++;//还需要设计一个id自增长idCounter++;newhouse.setId(idCounter);//更新newhouse的idreturn true;}}
}