房屋出租系统 需求
实现基于文本页面的房屋出租软件
能够实现对房屋信息的添加、修改和删除(数组实现),并能够打印明细表
界面
主菜单
新增房源
查找房源
删除房源
修改房源
房屋列表
退出系统
分层模式
框架图
界面层
HouseView.java
- 显示页面(mainMenu方法)
- 接收用户输入
- 调用其他类完成对房屋的各种操作
public class HouseView {
private boolean loop = true;
private char key = ' ';
private HouseService houseService = new HouseService(10);
public void changeHouse() {
System.out.println("============修改房屋信息============");
System.out.println("========请输入待修改房屋编号(-1退出)========");
int changeId = Utility.readInt();
House house = houseService.find(changeId);
if(house == null) {
System.out.println("此编号不存在!");
return;
}
System.out.println("============请确认是否修改(Y/N),请小心选择============");
char choice = Utility.readConfirmSelection();
if (choice == 'N') {
return;
}
System.out.println("==========请输入修改信息==========");
System.out.print("姓名("+house.getName()+"):");
String name = Utility.readString(8, house.getName());
System.out.print("电话("+house.getPhone()+"):");
String phone = Utility.readString(11, house.getPhone());
System.out.print("地址("+house.getAddress()+"):");
String address = Utility.readString(20, house.getAddress());
System.out.print("租金("+house.getRent()+"):");
int rent = Utility.readInt(house.getRent());
System.out.print("状态(已出租/未出租)("+house.getState()+"):");
String state = Utility.readString(3, house.getState());
house.setName(name);
house.setPhone(phone);
house.setAddress(address);
house.setRent(rent);
house.setState(state);
}
public void findHouse() {
System.out.println("============查找房源============");
System.out.println("请输入待查找房屋编号(-1退出)");
int findId = Utility.readInt();
if (findId == -1) {
System.out.println("============放弃查找房源============");
return;
}
House house = houseService.find(findId);
if (house == null) {
System.out.println("============此编号不存在============");
} else {
System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t租金\t\t状态(未出租/已出租)");
System.out.println(house);
}
}
public void delHouse() {
System.out.println("============添加房屋信息============");
System.out.println("请输入待删除房屋编号(-1退出)");
int delId = Utility.readInt();
if (delId == -1) {
System.out.println("============放弃删除房屋信息============");
return;
}
System.out.println("============请确认是否删除(Y/N),请小心选择============");
char choice = Utility.readConfirmSelection();
if (choice == 'Y') {
if (houseService.del(delId)) {
System.out.println("============删除房屋信息成功============");
} else {
System.out.println("============此编号不存在============");
}
} else {
System.out.println("============放弃删除房屋信息============");
}
}
public void listHouses() {
System.out.println("============房屋列表============");
System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t租金\t\t状态(未出租/已出租)");
House[] houses = houseService.list();
for (House house : houses) {
if (house == null) break;
System.out.println(house);
}
}
public void addHouse() {
System.out.println("=============添加房源=============");
System.out.print("姓名:");
String name = Utility.readString(8);
System.out.print("电话:");
String phone = Utility.readString(11);
System.out.print("地址:");
String address = Utility.readString(20);
System.out.print("租金:");
int rent = Utility.readInt();
System.out.print("状态(已出租/未出租):");
String state = Utility.readString(3);
House house = new House(-1, name, phone, address, rent, state);
if (houseService.add(house)) {
System.out.println("============添加成功============");
} else {
System.out.println("预设空间不够,是否需要扩容?");
char choice = Utility.readConfirmSelection();
if (choice == 'Y') {
System.out.print("请输入需要容量大小:");
int size = Utility.readInt();
extendHouses(size, house);
} else {
System.out.println("============添加失败============");
}
}
}
private void extendHouses(int size, House house) {
HouseService houseService1 = new HouseService(size);
houseService1.copy(houseService.list());
houseService = houseService1;
if (houseService.add(house)) {
System.out.println("============添加成功============");
} else {
System.out.println("预设空间不够,是否需要扩容?");
char choice = Utility.readConfirmSelection();
if (choice == 'Y') {
System.out.print("请输入需要容量大小:");
size = Utility.readInt();
extendHouses(size, house);
} else {
System.out.println("============添加失败============");
}
}
}
public void exitRentApp() {
char choice = Utility.readConfirmSelection();
if (choice == 'Y') {
loop = false;
}
}
public void mainMenu() {
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':
changeHouse();
break;
case '5':
listHouses();
break;
case '6':
exitRentApp();
break;
default:
}
} while (loop);
}
}
业务层
HouseService.java
- 响应HouseView的调用
- 完成对房屋信息的各种操作(CRUD)
- 定义House[],保存House对象
public class HouseService {
private House[] houses;
private int houseNums = 1;
private int idCounter = 1;
public HouseService(int size){
houses = new House[size];
houses[0] = new House(1, "Jack", "186", "海淀区", 3000, "未出租");
}
public House[] list(){
return houses;
}
public boolean add(House house) {
if(houseNums == houses.length){
System.out.println("数组已满,不能再添加了...");
return false;
}
house.setId(++idCounter);
houses[houseNums++] = house;
return true;
}
public boolean copy(House[] houses1){
for (int i = 0; i < houses1.length; i++) {
houses[i] = houses1[i].clone();
}
return true;
}
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;
}else{
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;
}
}
House.java(数据类)
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) {
setId(id);
setName(name);
setPhone(phone);
setAddress(address);
setRent(rent);
setState(state);
}
@Override
public House clone() {
House wt = new House(getId(),getName(),getPhone(),getAddress(),getRent(),getState());
return wt;
}
@Override
public String toString() {
return id + "\t\t\t" +
name + "\t\t" +
phone + "\t\t\t" +
address + "\t\t" +
rent + "\t\t" +
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;
}
}
Tools.java(工具类)
静态方法(static),可以直接类名.方法 来调用
import java.util.*;
public class Utility {
private static Scanner scanner = new Scanner(System.in);
public static char readMenuSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4' && c != '5') {
System.out.print("选择错误,请重新输入:");
} else break;
}
return c;
}
public static char readChar() {
String str = readKeyBoard(1, false);
return str.charAt(0);
}
public static char readChar(char defaultValue) {
String str = readKeyBoard(1, true);
return (str.length() == 0) ? defaultValue : str.charAt(0);
}
public static int readInt() {
int n;
for (; ; ) {
String str = readKeyBoard(10, false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
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;
}
public static String readString(int limit) {
return readKeyBoard(limit, false);
}
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str;
}
public static char readConfirmSelection() {
System.out.println("请输入你的选择(Y/N): 请小心选择");
char c;
for (; ; ) {
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
private static String readKeyBoard(int limit, boolean blankReturn) {
String line = "";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.length() == 0) {
if (blankReturn) return line;
else continue;
}
if (line.length() < 1 || line.length() > limit) {
System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
程序入口
HouseRentApp.java
public class HouseRentApp {
public static void main(String[] args) {
new HouseView().mainMenu();
System.out.println("============你退出了房屋出租系统============");
}
}