代码打包–百度网盘链接:
链接: https://pan.baidu.com/s/101mJFUlM6ftqeQPyVazW8w 提取码: jw8i
9.1 房屋出租系统-需求
9.1.1 项目需求说明
实现基于文本界面的《房屋出租软件》。 能够实现对房屋信息的添加、修改和删除(用数组实现),并能够打印房屋明细表
9.2 房屋出租系统-界面
- 项目界面- 主菜单
-
项目界面- 新增房源 -
项目界面- 查找房源 -
项目界面- 删除房源 -
项目界面- 修改房源 -
项目界面- 房屋列表 -
项目界面- 退出系统
9.3 房屋出租系统-设计(!!)
项目设计-程序框架图(分层模式=>当软件比较复杂,需要模式管理)
9.4 房屋出租系统-实现
9.4.1 准备工具类Utility,提高开发效率
在实际开发中,公司都会提供相应的工具类和开发库,可以提高开发效率,程序员也需要能够看懂别人写的代码, 并能够正确的调用。
- 了解Utility 类的使用
- 测试Utility 类
测试Utility 类代码:
public class TestUtility {
public static void main(String[] args) {
System.out.println("请输入字符串:");
String s2 = Utility.readString(10, "hspedu");
System.out.println("s2=" + s2);
A.cry();
}
}
class A {
public void hi() {
}
public static void cry() {
}
}
Utility代码:当一个api文档使用,别太深入钻研。
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;
}
}
9.4.2 项目功能实现-完成House 类
编号 id ,房主 name,电话 phone,地址 address,月租 rent,状态 state(未出租/已出租)
代码:
package com.hspedu.houserent.domain;
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;
}
@Override
public String toString() {
return id +
"\t\t" + name +
"\t" + phone +
"\t\t" + address +
"\t" + rent +
"\t" + state ;
}
}
9.4.3 项目功能实现-显示主菜单和完成退出软件功能
说明:实现功能的三部曲[明确完成功能->思路分析->代码实现]
- 功能说明:
用户打开软件, 可以看到主菜单,可以退出软件. - 思路分析:
在HouseView.java 中,编写一个方法mainMenu,显示菜单. - 代码实现:
HouseView代码:
package com.houserent.view;
import com.houserent.utils.Utility;
public class HouseView {
private boolean loop = true;
private char key = ' ';
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("\t 1 新增房源");
break;
case '2' :
System.out.println("\t 2 查找房屋");
break;
case '3' :
System.out.println("\t 3 删除房屋");
break;
case '4' :
System.out.println("\t 4 修改房屋信息");
break;
case '5' :
System.out.println("\t 5 房屋列表");
break;
case '6' :
System.out.println("\t 退出");
loop=false;
break;
}
} while (loop);
}
}
在主类调用,显示主菜单:
package com.houserent;
import com.houserent.view.HouseView;
import java.util.Scanner;
public class HouseRentApp {
public static void main(String[] args) {
new HouseView().mainMenu();
System.out.println("===你退出房屋出租系统==");
}
}
9.4.4 项目功能实现-完成显示房屋列表的功能
HouseService代码:
package com.houserent.service;
import com.houserent.domain.House;
public class HouseService {
private House[] houses;
public HouseService(int size) {
houses = new House[size];
houses[0] = new House(1,"jack","112", "海淀区", 2000, "未出租");
}
public House[] list() {
return houses;
}
}
HouseView代码:这一部分首先设置数组的大小为2,然后编写listHouses()显示房屋列表,记得修改 case ‘5’ listHouses(); break;
package com.houserent.view;
import com.houserent.domain.House;
import com.houserent.service.HouseService;
import com.houserent.utils.Utility;
public class HouseView {
HouseService houseService = new HouseService(10);
public void listHouses() {
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++) {
if (houses[i] == null) {
break;
}
System.out.println(houses[i]);
}
System.out.println("=============房屋列表显示完毕============");
}
private boolean loop = true;
private char key = ' ';
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("\t 1 新增房源");
break;
case '2' :
System.out.println("\t 2 查找房屋");
break;
case '3' :
System.out.println("\t 3 删除房屋");
break;
case '4' :
System.out.println("\t 4 修改房屋信息");
break;
case '5' :
listHouses();
break;
case '6' :
System.out.println("\t 退出");
loop=false;
break;
}
} while (loop);
}
}
主类代码,再次运行输入5 查看房屋列表
package com.houserent;
import com.houserent.view.HouseView;
import java.util.Scanner;
public class HouseRentApp {
public static void main(String[] args) {
new HouseView().mainMenu();
System.out.println("===你退出房屋出租系统==");
}
}
9.4.5 项目功能实现-添加房屋信息的功能
编写listHouses()显示房屋列表 代码:
package com.houserent.view;
import com.houserent.domain.House;
import com.houserent.service.HouseService;
import com.houserent.utils.Utility;
public class HouseView {
HouseService houseService = new HouseService(10);
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 newHouse = new House(0, name, phone, address, rent, state);
if (houseService.add(newHouse)) {
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 (int i = 0; i < houses.length; i++) {
if (houses[i] == null) {
break;
}
System.out.println(houses[i]);
}
System.out.println("=============房屋列表显示完毕============");
}
private boolean loop = true;
private char key = ' ';
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' :
addHouse();
break;
case '2' :
System.out.println("\t 2 查找房屋");
break;
case '3' :
System.out.println("\t 3 删除房屋");
break;
case '4' :
System.out.println("\t 4 修改房屋信息");
break;
case '5' :
listHouses();
break;
case '6' :
System.out.println("\t 退出");
loop=false;
break;
}
} while (loop);
}
}
addHouse方法,添加新对象,返回boolean 代码:
package com.houserent.service;
import com.houserent.domain.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","112", "海淀区", 2000, "未出租");
}
public boolean add(House newHouse) {
if(houseNums == houses.length) {
System.out.println("数组已满,不能再添加了...");
return false;
}
houses[houseNums++] = newHouse;
newHouse.setId(++idCounter);
return true;
}
public House[] list() {
return houses;
}
}
主类方法 运行 选择系统1进行添加测试:
package com.houserent;
import com.houserent.view.HouseView;
import java.util.Scanner;
public class HouseRentApp {
public static void main(String[] args) {
new HouseView().mainMenu();
System.out.println("===你退出房屋出租系统==");
}
}
9.4.6 项目功能实现-完成删除房屋信息的功能
1.编写delHouse() 接收输入的id,调用Service 的 del方法 。 代码如下
package com.houserent.view;
import com.houserent.domain.House;
import com.houserent.service.HouseService;
import com.houserent.utils.Utility;
public class HouseView {
HouseService houseService = new HouseService(2);
public 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("=============放弃删除房屋信息============");
}
}
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 newHouse = new House(0, name, phone, address, rent, state);
if (houseService.add(newHouse)) {
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 (int i = 0; i < houses.length; i++) {
if (houses[i] == null) {
break;
}
System.out.println(houses[i]);
}
System.out.println("=============房屋列表显示完毕============");
}
private boolean loop = true;
private char key = ' ';
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' :
addHouse();
break;
case '2' :
System.out.println("\t 2 查找房屋");
break;
case '3' :
delHouse();
break;
case '4' :
System.out.println("\t 4 修改房屋信息");
break;
case '5' :
listHouses();
break;
case '6' :
System.out.println("\t 退出");
loop=false;
break;
}
} while (loop);
}
}
2.del方法,删除一个房屋信息。有点难。代码如下:
package com.houserent.service;
import com.houserent.domain.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","112", "海淀区", 2000, "未出租");
}
public boolean del(int delId) {
int index = -1;
for(int i = 0; i < houseNums; i++) {
if(delId == 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 boolean add(House newHouse) {
if(houseNums == houses.length) {
System.out.println("数组已满,不能再添加了...");
return false;
}
houses[houseNums++] = newHouse;
newHouse.setId(++idCounter);
return true;
}
public House[] list() {
return houses;
}
}
3.主类测试
package com.houserent;
import com.houserent.view.HouseView;
import java.util.Scanner;
public class HouseRentApp {
public static void main(String[] args) {
new HouseView().mainMenu();
System.out.println("===你退出房屋出租系统==");
}
}
9.4.7 项目功能实现-完善退出确认功能
在HouseView中添加exit ( ) 方法。然后在case '6’里边调用exit()方法。
public void exit() {
char c = Utility.readConfirmSelection();
if (c == 'Y') {
loop = false;
}
}
9.4.8 项目功能实现-完成根据id 查找房屋信息的功能
首先在HouseView中添加findHouse方法。代码如下: 记得修改 case ‘2’: findHouse(); break;
public void findHouse() {
System.out.println("=============查找房屋信息============");
System.out.print("请输入要查找的id: ");
int findId = Utility.readInt();
House house = houseService.findById(findId);
if (house != null) {
System.out.println(house);
} else {
System.out.println("=============查找房屋信息id不存在============");
}
}
然后再HouseService里添加findById方法。代码如下:
public House findById(int findId) {
for(int i = 0; i < houseNums; i++) {
if(findId == houses[i].getId()) {
return houses[i];
}
}
return null;
}
9.4.9 项目功能实现-完成修改房屋信息的功能
这一步加在以下完整代码里边。
完整代码
1、主类代码
package com.hspedu.houserent;
import com.hspedu.houserent.view.HouseView;
public class HouseRentApp {
public static void main(String[] args) {
byte b1 = 123;
b1 += 100000;
System.out.println(b1);
byte b2 = 123;
b2 = (byte)(b2 + 100000);
System.out.println(b2);
new HouseView().mainMenu();
System.out.println("===你退出房屋出租系统==");
}
}
2、HoseView代码
package com.hspedu.houserent.view;
import com.hspedu.houserent.domain.House;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utils.Utility;
public class HouseView {
private boolean loop = true;
private char key = ' ';
private HouseService houseService = new HouseService(2);
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.print("姓名(" + house.getName() + "): ");
String name = Utility.readString(8, "");
if (!"".equals(name)) {
house.setName(name);
}
System.out.print("电话(" + house.getPhone() + "):");
String phone = Utility.readString(12, "");
if (!"".equals(phone)) {
house.setPhone(phone);
}
System.out.print("地址(" + house.getAddress() + "): ");
String address = Utility.readString(18, "");
if (!"".equals(address)) {
house.setAddress(address);
}
System.out.print("租金(" + house.getRent() + "):");
int rent = Utility.readInt(-1);
if (rent != -1) {
house.setRent(rent);
}
System.out.print("状态(" + house.getState() + "):");
String state = Utility.readString(3, "");
if (!"".equals(state)) {
house.setState(state);
}
System.out.println("=============修改房屋信息成功============");
}
public void findHouse() {
System.out.println("=============查找房屋信息============");
System.out.print("请输入要查找的id: ");
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;
}
}
public 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("=============放弃删除房屋信息============");
}
}
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 newHouse = new House(0, name, phone, address, rent, state);
if (houseService.add(newHouse)) {
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 (int i = 0; i < houses.length; i++) {
if (houses[i] == null) {
break;
}
System.out.println(houses[i]);
}
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':
addHouse();
break;
case '2':
findHouse();
break;
case '3':
delHouse();
break;
case '4':
update();
break;
case '5':
listHouses();
break;
case '6':
exit();
break;
}
} while (loop);
}
}
3、HouseService代码
package com.hspedu.houserent.service;
import com.hspedu.houserent.domain.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","112", "海淀区", 2000, "未出租");
}
public House findById(int findId) {
for(int i = 0; i < houseNums; i++) {
if(findId == houses[i].getId()) {
return houses[i];
}
}
return null;
}
public boolean del(int delId) {
int index = -1;
for(int i = 0; i < houseNums; i++) {
if(delId == 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] = null;
return true;
}
public boolean add(House newHouse) {
if(houseNums == houses.length) {
System.out.println("数组已满,不能再添加了...");
return false;
}
houses[houseNums++] = newHouse;
newHouse.setId(++idCounter);
return true;
}
public House[] list() {
return houses;
}
}
4、House代码
package com.hspedu.houserent.domain;
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;
}
@Override
public String toString() {
return id +
"\t\t" + name +
"\t" + phone +
"\t\t" + address +
"\t" + rent +
"\t" + state ;
}
}
5、Unility代码
见本文 9.4.1 节中的测试Utility 类代码。
|