1、项目需求说明
2、界面
2.1、项目界面 - 主菜单
2.2、项目界面 - 新增房源
2.3、项目界面 - 查找房源
2.4、项目界面 - 删除房源
2.5、项目界面 - 修改房源
2.6、项目界面 - 房屋列表
2.7、项目界面 - 退出系统
3、设计
3.1、项目设计 - 程序框架图(分层模式=>当软件比较复杂, 需要模式管理)
4、实现
4.1、准备工具类Utility, 提高开发效率
4.2、项目功能实现 - 完成House类
package com.cc.houserenttest.domain;
public class House {
private int id; //编号
private String name; //房主
private String phone; //电话
private String address; //地址
private int rent; //月租
private String state; //状态
//打印房屋信息
public void printHouse() {
System.out.println("编号\t房主\t电话\t地址\t月租\t状态(未出租/已出租)\n" +
id + "\t\t" + name + "\t" + phone + "\t\t" + address
+ "\t" + rent + "\t" + 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;
}
}
package com.cc.houserenttest;
import com.cc.houserenttest.domain.House;
public class HouseRentApp {
public static void main(String[] args) {
House house = new House(2, "none", "116", "昌平区", 5000, "已出租");
house.printHouse();
}
}
4.3、项目功能实现 - 显示主菜单和完成退出软件功能
package com.cc.houserenttest.houseview;
import java.util.Scanner;
public class HouseView {
Scanner sc = new Scanner(System.in);
String choose = "";
boolean flag = true;
//显示主菜单
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): ");
choose = sc.next();
switch (choose) {
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("退出");
flag = false;
break;
default :
System.out.println("范围为1-6, 请重新输入");
}
} while (flag);
}
}
package com.cc.houserenttest;
import com.cc.houserenttest.domain.House;
import com.cc.houserenttest.houseview.HouseView;
public class HouseRentApp {
public static void main(String[] args) {
/*House house = new House(2, "none", "116", "昌平区", 5000, "已出租");
house.printHouse();*/
HouseView houseView = new HouseView();
houseView.mainMenu();
}
}
4.4、项目功能实现 - 完成显示房屋列表的功能
4.5、项目功能实现 - 添加房屋信息的功能
4.6、项目功能实现 - 完成删除房屋信息的功能
4.7、项目功能实现 - 完善退出确认功能
4.8、项目功能实现 - 完成根据id查找房屋信息的功能
4.9、项目功能实现 - 完成修改房屋信息的功能
4.10、项目效果展示
**代码: **
数据层
package com.cc.houserent.domain;
public class House {
private int id; //编号
private String name; //房主
private String phone; //电话
private String address; //地址
private int rent; //月租
private String state; //状态
//打印房屋信息
public void printHouse() {
System.out.println(id + "\t\t" + name + "\t" + phone + "\t\t" + address
+ "\t" + rent + "\t" + 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;
}
}
业务层
package com.cc.houserent.houseservice;
import com.cc.houserent.domain.House;
public class HouseService {
private House[] houses;
private int houseNums; //房屋个数, 默认为0
private int idCount; //id
public HouseService(int size) {
houses = new House[size];
}
//返回房屋信息的方法
public House[] list() {
return houses;
}
//返回增加房屋信息的方法
public boolean add(House newHouse) {
for (int i = 0; i < houseNums; i++) {
if (houses.length == houseNums) {
return false;
}
}
houses[houseNums] = newHouse;
houseNums++;
idCount++;
newHouse.setId(idCount);
return true;
}
//返回删除房屋信息的方法
int index = -1;
public boolean del(int deleteId) {
for (int i = 0; i < houseNums; i++) {
if (deleteId == 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 - 1] = null;
houseNums--;
return true;
}
//返回查找房屋信息的方法
public House find(int findId) {
for (int i = 0; i < houseNums; i++) {
if (findId == houses[i].getId()) {
return houses[i];
}
}
return null;
}
}
界面层
package com.cc.houserent.houseview;
import com.cc.houserent.domain.House;
import com.cc.houserent.houseservice.HouseService;
import java.util.Scanner;
public class HouseView {
Scanner sc = new Scanner(System.in);
String choose = "";
boolean flag = true;
HouseService hs = new HouseService(2);
//显示主菜单
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): ");
choose = sc.next();
switch (choose) {
case "1":
addHouse();
break;
case "2":
findById();
break;
case "3":
deleteHouse();
break;
case "4":
updateHouse();
break;
case "5":
listHouse();
break;
case "6":
exit();
break;
default:
System.out.println("范围为1-6, 请重新输入");
}
} while (flag) ;
}
//房屋列表
public void listHouse() {
System.out.println("---------------房屋列表---------------");
System.out.println("编号\t房主\t电话\t地址\t月租\t状态(未出租/已出租)");
House[] list = hs.list();
for (int i = 0; i < list.length; i++) {
if (list[i] == null) {
break;
}
list[i].printHouse();
}
System.out.println("---------------房屋列表完成---------------");
}
//新增房源
public void addHouse() {
System.out.println("---------------添加房屋---------------");
System.out.print("姓名: ");
String name = sc.next();
System.out.print("电话: ");
String phone = sc.next();
System.out.print("地址: ");
String address = sc.next();
System.out.print("月租: ");
int rent = sc.nextInt();
System.out.print("状态(未出租/已出租): ");
String state = sc.next();
//把添加的信息放在新数组里
House newHouse = new House(0, name, phone, address, rent, state);
if (hs.add(newHouse)) {
System.out.println("---------------添加完成---------------");
} else {
System.out.println("---------------房屋列表已存满, 不能增加---------------");
}
}
//删除房屋
public void deleteHouse() {
System.out.println("---------------删除房屋---------------");
System.out.print("请选择待删除房屋编号(-1)退出: ");
int delId = sc.nextInt();
if (delId == -1) {
System.out.println("---------------退出删除房屋---------------");
return;
}
System.out.println("确认是否删除(Y/N): 请小心选择: ");
System.out.println("请输入你的选择(Y/N): ");
choose = sc.next();
if (choose.equals("Y")) {
if (hs.del(delId)) {
System.out.println("---------------删除完成---------------");
} else {
System.out.println("---------------没有该房屋编号---------------");
}
} else if (choose.equals("N")) {
System.out.println("---------------退出删除房屋---------------");
return;
} else {
System.out.println("---------------输入错误---------------");
}
}
//退出系统
public void exit() {
while (true) {
System.out.println("请输入你的选择(Y/N): ");
choose = sc.next();
if (!choose.equals("Y") && !choose.equals("N")) {
System.out.println("选择错误, 请重新输入: ");
}
if (choose.equals("Y")) {
System.out.println("你退出了程序");
flag = false;
break;
}
if (choose.equals("N")) {
System.out.println("退出程序失败");
break;
}
}
}
//查找房屋信息
public void findById() {
System.out.println("---------------查找房屋---------------");
System.out.print("请输入你要查找的id: ");
int findId = sc.nextInt();
House house = hs.find(findId);
if (house != null) {
house.printHouse();
return;
}
System.out.println("---------------房屋列表没有此编号房屋---------------");
}
//修改房源
public void updateHouse() {
System.out.println("---------------修改房屋信息---------------");
System.out.print("请选择待修改房屋编号(-1退出): ");
int updateId = sc.nextInt();
if (updateId == -1) {
System.out.println("---------------退出修改房屋信息---------------");
return;
}
House house = hs.find(updateId);
if (house == null) {
System.out.println("---------------修改房屋信息不存在---------------");
return;
}
System.out.print("姓名(" + house.getName() + "): ");
String name = sc.next();
house.setName(name);
System.out.print("电话(" + house.getPhone() + "): ");
String phone = sc.next();
house.setPhone(phone);
System.out.print("地址(" + house.getAddress() + "): ");
String address = sc.next();
house.setAddress(address);
System.out.print("租金(" + house.getRent() + "): ");
int rent = sc.nextInt();
house.setRent(rent);
System.out.print("状态(" + house.getState() + "): ");
String state = sc.next();
house.setState(state);
System.out.println("---------------修改完成---------------");
}
}
测试层
package com.cc.houserent;
import com.cc.houserent.houseview.HouseView;
public class HouseRentApp {
public static void main(String[] args) {
HouseView houseView = new HouseView();
houseView.mainMenu();
}
}
效果图:
新增房源
当前房屋列表为空, 最大可以存放两个房屋列表, 所有可以添加
当添加king的时候, 前面已经添加了两个房屋信息了, 所以不能在添加
显示当前房屋列表信息
查找房屋
当前房屋列表只要id为1和2的信息, 当输入其它的会显示没有此编号
当查找id为1或者2的信息时, 则会返回相对应的信息
删除房屋
当输入-1的时候, 就可以退出删除房屋
删除房屋操作前, 会提醒是否确定删除房屋, 如果输入N, 则退出删除房屋
删除房屋操作前, 会提醒是否确定删除房屋, 如果输入Y, 则删除房屋
当输入的删除房屋编号没有时, 则提示没有该房屋编号
修改房屋信息
当输入的修改房屋信息编号不存在时, 则会提示修改房屋信息不存在
当修改房屋信息编号存在时, 则继续下一步
退出系统
当输入的不是Y或者N时, 会提示你重新输入, 直到输入Y或者N为止
输入的是Y则退出程序