本文主要是介绍JAVA实现房屋出租系统(超详细),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
运行展示:
1.新增房源
当列表空间满时,会无法添加(这里我设空间为3组,方便展示)
2.查找房源
3.删除房屋
4.修改房屋信息
5.退出
构思[分层模式]:
包的创建:
工具类代码(Utility):
public class Utility {public static char readChar() {//static静态对象可以直接调用,不用newScanner scanner = new Scanner(System.in);String key = scanner.next();return key.charAt(0);//只会返回字符串第一个字符,比如123只返回1}public static String readString() {Scanner scanner = new Scanner(System.in);String key = scanner.next();return key;}public static int readInt() {Scanner scanner = new Scanner(System.in);int key = scanner.nextInt();return key;}
}
模型类代码(House) :
设置House中存在哪些属性
/**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\t" + name +"\t\t\t" + phone +"\t\t\t" + address +"\t\t\t" + rent +"\t\t\t" + state;}
}
程序入口(main)
public class HouseRentApp {public static void main(String[] args) {//创建一个HouseView对象,并显示主菜单,是整个程序的入口new HouseView().mainMenu();//匿名对象}
}
主类实现代码:
1.HouseView.java 类[界面]
显示主菜单:
mainMenu方法,可以显示主菜单
新增房屋信息:
addHouse方法,界面,接收用户的输入信息,添加房屋信息
查找房屋信息:
seekHouse方法,界面,查找房屋信息
删除房屋信息:
deleteHouse方法,界面,删除房屋信息
修改房屋信息:
alterHouse方法,界面,修改房屋信息
房屋列表:
listHouse方法,界面,清单
退出系统:
outMenu方法,退出系统
类中添加属性
private boolean loop = true;
private char key = ' ';
private HouseService houseService = new HouseService(3);//设置列表空间
private int id = 0;
mainMenu方法,可以显示主菜单
public void mainMenu() {do {System.out.println("\n=============房屋出租系统==============");System.out.println("\t\t\t" + "1.新 增 房 源");System.out.println("\t\t\t" + "2.查 找 房 屋");System.out.println("\t\t\t" + "3.删 除 房 屋");System.out.println("\t\t\t" + "4.修 改 房 屋 信 息");System.out.println("\t\t\t" + "5.房 屋 列 表");System.out.println("\t\t\t" + "6.退 出");System.out.println("请输出(1~6):");key = Utility.readChar();switch (key) {case '1':addHouse();break;case '2':seekHouse();break;case '3':deleteHouse();break;case '4':alterHosue();break;case '5':listHouse();break;case '6':outMenu();break;default:System.out.println("输入有误,返回菜单页面");break;}} while (loop);}
addHouse方法,界面,接收用户的输入信息,添加房屋信息
/*** 添加信息方法*/public void addHouse() {boolean loop = houseService.addLoop();if (!loop) {System.out.println("无法添加,表格已满");}else {//编号自增,不能自取id++;System.out.println("房主:");String name = Utility.readString();System.out.println("电话(6位):");String phone = Utility.readString();System.out.println("地址:");String address = Utility.readString();System.out.println("月租:");int rent = Utility.readInt();System.out.println("状态(未出租/已出租):");String state = Utility.readString();House house = new House(id, name, phone, address, rent, state);houseService.add(house);System.out.println("添加成功");}}
seekHouse方法,界面,查找房屋信息
/*** 查找房屋信息方法*/public void seekHouse() {System.out.println("请输入你要查找的房屋编号(输入-1返回菜单)");int seekId = Utility.readInt();if (seekId == -1) {System.out.println("成功返回菜单");} else {houseService.seekLoop(seekId);}}
deleteHouse方法,界面,删除房屋信息
/*** 删除房屋信息方法*/public void deleteHouse() {System.out.println("请输入你要删除的房屋编号(输入-1返回菜单)");int deleteId = Utility.readInt();if (deleteId == -1) {System.out.println("成功返回菜单");} else {houseService.deleteLoop(deleteId);}}
alterHouse方法,界面,修改房屋信息
/*** 修改房屋信息方法*/public void alterHosue() {System.out.println("请输入你要修改的房屋编号(输入-1返回菜单)");int alterId = Utility.readInt();if (alterId == -1) {System.out.println("成功返回菜单");} else {houseService.alterLoop(alterId);}}
listHouse方法,界面,清单
/*** 列表清单方法*/public void listHouse() {House[] houses = houseService.list();if (houses[0] == null) {System.out.println("列表中无信息");}else {System.out.println("----------------------------------房屋列表清单----------------------------------");System.out.println("编号" + "\t\t\t" +"房主" + "\t\t\t" +"电话" + "\t\t\t" +"地址" + "\t\t\t" +"月租" + "\t\t\t" +"状态(未出租/已出租)");for (int i = 0; i < houses.length; i++) {if (houses[i] == null) {//如果为空,就不输出break;}System.out.println(houses[i]);}System.out.println("----------------------------------房屋列表完毕----------------------------------");}}
outMenu方法,退出系统
/*** 退出菜单方法*/public void outMenu() {System.out.println("确定要退出系统吗?");do {System.out.println("请输入:是(y/Y)/否(n/N)");char out = Utility.readChar();switch (out) {case 'y':case 'Y':System.out.println("成功退出系统");loop = false;break;case 'n':case 'N':System.out.println("成功返回菜单");break;default:System.out.println("输入有误,重新输入");break;}}while(loop);}
2. HouseService.java 类[业务层]
新增房屋信息:
add(House[] newHouse)方法,把新的house对象添加进House数组中
addLoop()方法,判断能否继续添加房屋信息
查找房屋信息:
seek(int index)方法,输出编号对应的房屋信息
seekLoop(int seekId)方法,判断查询编号存不存在
删除房屋信息:
delete(int index)方法,删除编号对应的房屋信息
deleteLoop(int deleteId)方法,判断能否删除对应编号的房屋信息
修改房屋信息:
alter(int index)方法,修改编号对应的房屋信息
alterLoop(int alterId)方法,判断修改编号存不存在
房屋列表:
list方法,返回所有房屋信息
a.新增房屋信息
/*** 添加房屋信息* @param newHouse*/public void add(House newHouse) {houses[HouseSum] = newHouse;HouseSum++;}/*** 返回是否能添加房屋* @return*/public boolean addLoop() {if (HouseSum == houses.length) {return false;} else {return true;}}
b.查找房屋信息
/*** 输出查询节点对应编号* @param index*/public void seek(int index) {if (index == -1) {System.out.println("查询编号不存在");} else {System.out.println("----------------------------------查询房屋信息----------------------------------");System.out.println("编号" + "\t\t\t" +"房主" + "\t\t\t" +"电话" + "\t\t\t" +"地址" + "\t\t\t" +"月租" + "\t\t\t" +"状态(未出租/已出租)");System.out.println(houses[index]);System.out.println("----------------------------------信息输出完毕----------------------------------");}}/*** 判断查询编号存不存在* @param seekId*/public void seekLoop(int seekId) {int index = -1;for (int i = 0; i < HouseSum; i++) {if (seekId == houses[i].getId()) {index = i;}}seek(index);}
c.删除房屋信息
/*** 删除房屋信息* @param index*/public void delete(int index) {for (int i = index; i < HouseSum - 1; i++) {houses[i] = houses[i + 1];}houses[HouseSum - 1] = null;HouseSum--;}/*** 判断删除编号存不存在* @param deleteId*/public void deleteLoop(int deleteId) {int index = -1;//节点for (int i = 0; i < HouseSum; i++) {if (houses[i].getId() == deleteId) {index = i;}}if (index == -1) {System.out.println("未查找到对应编号的房屋信息,无法删除");} else {System.out.println("已查找到对应编号的房屋信息,确定删除?");System.out.println("请输入是(y/Y)否(n/N):");boolean doLoop = true;do {char loop = Utility.readChar();switch (loop) {case 'y':case 'Y':delete(index);System.out.println("成功删除对应编号的房屋信息");doLoop = false;break;case 'n':case 'N':System.out.println("成功返回菜单");doLoop = false;break;default:System.out.println("输出错误,重新输入");}}while (doLoop);}}
d.修改房屋信息
/*** 修改编号对应房屋信息* @param index*/public void alter(int index) {if (index == -1) {System.out.println("修改编号不存在");} else{System.out.println("输入你要修改的信息:");System.out.println("房主(" + houses[index].getName() + "):");String name = Utility.readString();System.out.println("电话(" + houses[index].getPhone() + "):");String phone = Utility.readString();System.out.println("地址(" + houses[index].getAddress() + "):");String address = Utility.readString();System.out.println("月租(" + houses[index].getRent() + "):");int rent = Utility.readInt();System.out.println("状态(" + houses[index].getState() + "):");String state = Utility.readString();houses[index] = new House(houses[index].getId(), name, phone, address, rent, state);}}/*** 判断修改编号存不存在* @param alterId*/public void alterLoop(int alterId) {int index = -1;for (int i = 0; i < HouseSum; i++) {if (alterId == houses[i].getId()) {index = i;}}alter(index);}
e.房屋列表
/*** 清单* @return*/public House[] list() {return houses;}
目录
运行展示:
1.新增房源
2.查找房源
3.删除房屋
4.修改房屋信息
5.退出
构思[分层模式]:
包的创建:
工具类代码(Utility):
模型类代码(House) :
程序入口(main)
主类实现代码:
1.HouseView.java 类[界面]
类中添加属性
mainMenu方法,可以显示主菜单
addHouse方法,界面,接收用户的输入信息,添加房屋信息
seekHouse方法,界面,查找房屋信息
deleteHouse方法,界面,删除房屋信息
alterHouse方法,界面,修改房屋信息
listHouse方法,界面,清单
outMenu方法,退出系统
2. HouseService.java 类[业务层]
a.新增房屋信息
b.查找房屋信息
c.删除房屋信息
d.修改房屋信息
e.房屋列表
这篇关于JAVA实现房屋出租系统(超详细)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!