房屋出租系统
项目需求
项目界面
-
主菜单
-
新增房源
-
查找房屋
-
删除房屋
-
修改房源
如果不希望修改某个信息,则直接回车即可
-
房屋列表
-
退出系统
项目设计
项目开发
1、创建包
一个层建立一个包,因为一个层下面往往不止一个类
2、导入工具类
导入工具类,并且可以测试工具类中的一些方法
3、 编写House类(数据层)
属性、构造器、getter和setter、toString
@Override
public String toString() {
return id +
"\t" + name +
"\t" + phone +
"\t" + address +
"\t" + rent +
"\t" + state ;
}
4、显示主菜单
HouseView.java
import com.rxli.houserent.utils.Utility;
/**
* 1. 显示界面
* 2. 接收用户输入
* 3. 调用HouseService完成对房屋信息的各种操作
*/
public class HouseView {
private boolean loop = true; //控制显示菜单
private char key = ' ';//接受用户选择
//显示主菜单
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.print("请输入你的选择(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);
}
}
HouseRentApp.java
import com.rxli.houserent.view.HouseView;
public class HouseRentApp {
public static void main(String[] args) {
//创建HouseView对象,并且显示主菜单,是程序的入口
new HouseView().mainMenu();
System.out.println("====你退出了房屋出租系统====");
}
}
5、显示房屋列表
思路:房屋列表放在数组里,在显示菜单层调用service里的方法,将列表读取出来。view层负责显示这个列表
=》service里需要有House[]属性。
=》service同时负责构造器初始化数组
=》view需要遍历数组以显示列表
=》view需要一个House[]属性接受service传来的数组
HouseService.java
public class HouseService {
private House[] houses; //保存House对象
//构造器
public HouseService(int size){
houses = new House[size]; //当创建HouseService对象,指定数组大小
//测试列表信息
houses[0] = new House(1,"jack","112","海淀区",2000,"未出租");
}
//list方法,返回houses
public House[] list(){
return houses;
}
}
HouseView.java
private HouseService houseService = new HouseService(1);
//编写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++) {//注意数组大小和实际有数据的元素个数不一致问题
if(houses[i] == null){
break;
}
System.out.println(houses[i]);
}
System.out.println("=============房屋列表显示完毕=============");
}
6、添加房屋信息
思路:
view里需要一个addHouse()方法,接收用户输入
service里需要一个add(House newHouse)方法,接收新添加的房屋信息,存储到房屋列表数组中,返回bool代表添加成功或失败
HouseView.java
//编写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对象,注意id是系统分配的,用户不能输入
//新添加的房屋id自增长
House house = new House(0, name, phone, address, rent, state);
if(houseService.add(house)){
System.out.println("=============添加房屋成功=============");
} else {
System.out.println("=============添加房屋失败=============");
}
}
HouseService.java
private int houseNums = 1; //记录当前有多少个房屋信息
private int idCounter = 1; //初始化有一个列表了,所以初始编号为1
//add方法,添加新房屋对象,返回boolean(因为完成了扩容功能,所以也可以返回void)
public boolean add(House newHouse){
//先判断数组是否已满->需要记录当前有多少个房屋信息(即数组实际数据量)
if(houseNums == houses.length){ //数组已满,需扩容
houses = Utility.arrayExpansion(houses);
}
houses[houseNums++] = newHouse;
//程序员需要设置一个id自增长机制
newHouse.setId(++idCounter); //idCounter先自增,然后赋值给对象的id
return true;
}
Utility.java
/**
* 功能:数组扩容,扩容10个长度
* @return 扩容后的数组
*/
public static House[] arrayExpansion(House[] houses){
House[] newHouses = new House[houses.length + 10];
for (int i = 0; i < houses.length; i++) {
newHouses[i] = houses[i];
}
return newHouses;
}
7、删除房屋
思路:
view类,接收用户id
service类,方法del(int delId);完成真正的删除,返回一个boolean
核心:在删除时,首先要找到对应编号在数组中的下标
=》遍历数组,getId()
删除时要将后面的数组覆盖到前面(整体左移一位),最后多出来的空将其置空null
HouseView.java
//编写delHouse() 接收输入的id,调用service的del方法
public void delHouse() {
System.out.println("=============删除房屋=============");
System.out.println("请输入待删除房屋的编号(-1退出):");
int delId = Utility.readInt();
if(delId == -1){
System.out.println("=============放弃删除房屋信息=============");
return;
}
//该方法本身就有循环判断的逻辑,必须输出Y/N
char choice = Utility.readConfirmSelection();
if(choice == 'Y'){
if(houseService.del(delId)){
System.out.println("=============删除房屋信息成功=============");
} else {
System.out.println("=============房屋编号不存在,删除失败=============");
}
} else {
System.out.println("=============放弃删除房屋信息=============");
}
}
HouseService.java
//del方法,删除指定id房屋,返回boolean
public boolean del(int delId) {
//应当先找到要删除的房屋信息对应的下标(下标和房屋编号不一样!)
int index = -1;
for (int i = 0; i < houseNums; i++) {
if(delId == houses[i].getId()){ //要删除的房屋id,是数组下标为i的元素下标
index = i;
}
}
if(index == -1){ //说明delId在数组中不存在
return false;
}
//如果找到,则进行覆盖,后面的元素往前挪
for (int i = index; i < houseNums-1; i++) {//因为houses[i+1]最多是等于houses[houseNums-1]
houses[i] = houses[i + 1];
}
// houses[houseNums-1] = null;
// houseNums--; //少一个
// 这两句等同于下面的一句:
houses[--houseNums] = null; //个数减一,正好此时houses[houseNums]就是原先数组的最后一个有效元素
return true;
}
Utility.java
/**
* 功能:读取键盘输入的确认选项,Y或N
* 将小的功能,封装到一个方法中.
* @return Y或N
*/
public static char readConfirmSelection() {
System.out.println("请输入你的选择(Y/N): 请小心选择");
char c;
for (; ; ) {//无限循环
//在这里,将接受到字符,转成了大写字母
//y => Y n=>N
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
8、完善退出系统确认功能
HouseView.java
//完成退出确认功能
public void exit(){
//使用Utility提供的方法完成确认
char c = Utility.readConfirmSelection();
if(c == 'Y'){
loop = false;
}
}
9、查找房屋信息
HouseView.java
//编写searchHouse,输入id,调用search
public void searchHouse(){
System.out.println("=============查找房屋=============");
System.out.print("请输入你要查找的房屋编号:");
int searchId = Utility.readInt();
if(houseService.search(searchId) == null){
System.out.println("=============没有该房屋=============");
return;
}
System.out.println(houseService.search(searchId));
}
HouseService.java
//search方法,输入房屋编号,返回House
public House search(int id){
for(int i = 0; i < houseNums ; i++){
if(houses[i].getId() == id){
return houses[i];
}
}
return null;
}
10、修改房屋信息
思路:
在view中先调用search判断id是否存在
在view中直接调用setName,setRent等方法
注意:
用户直接回车表示不修改该信息。
=》直接回车默认是""
=》判断"".equals(name),决定是否要set
HouseView.java
//编写updateHouse,输入id,给出提示信息,调用service层
public void updateHouse(){
System.out.println("=============修改房屋信息=============");
System.out.print("请输入待修改的房屋编号(-1退出):");
int updateId = Utility.readInt();
if(updateId == -1){
System.out.println("=============你放弃修改房屋信息=============");
return;
}
House curHouse = houseService.search(updateId);
if(curHouse == null){
System.out.println("=============你要修改的房屋不存在=============");
return;
}
System.out.print("姓名(" + curHouse.getName() + "):");
String curName = Utility.readString(10, "");
if(!curName.equals("")){ //不是默认值,即没有直接回车
curHouse.setName(curName);
}
System.out.print("电话(" + curHouse.getPhone() + "):");
String curPhone = Utility.readString(13, "");
if(!curPhone.equals("")){
curHouse.setPhone(curPhone);
}
System.out.print("地址(" + curHouse.getAddress() + "):");
String curAddress = Utility.readString(20, "");
if(!curAddress.equals("")){
curHouse.setAddress(curAddress);
}
System.out.print("租金(" + curHouse.getRent() + "):");
int curRent = Utility.readInt(-1);
if(curRent != -1){
curHouse.setRent(curRent);
}
System.out.print("状态(" + curHouse.getState() + "):");
String curState = Utility.readString(6, "");
if(!curState.equals("")){
curHouse.setState(curState);
}
System.out.println("=============修改房屋信息完成=============");
}