项目代码
https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_chapter09/src/com/yinhai/houserent
一、项目需求说明
实现基于文本界面的房屋出租系统,能够实现对房屋信息的添加、修改和删除(用数组实现),并能够打印房屋明细表
项目界面 - 主菜单
        
1.新增房源
.        
2.查找房源
        
3.删除房源
        
4.修改房源
如果不希望修改某个信息 则回车即可
        
5.房屋列表
        
6.退出系统
        
二、项目设计
项目设计-程序框架图(分层模式 =》当软件比较复杂时,需要模式管理

三、项目实现
1.准备工具类Utility 提高开发效率
package com.yinhai.houserent.utlity;
/**
	工具类的作用:
	处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。
*/
import java.util.*;
/**
	
*/
public class Utility {
	//静态属性。。。
    private static Scanner scanner = new Scanner(System.in);
    
    /**
     * 功能:读取键盘输入的一个菜单选项,值:1——5的范围
     * @return 1——5
     */
	public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);//包含一个字符的字符串
            c = str.charAt(0);//将字符串转换成字符char类型
            if (c != '1' && c != '2' && 
                c != '3' && c != '4' && c != '5') {
                System.out.print("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }
	/**
	 * 功能:读取键盘输入的一个字符
	 * @return 一个字符
	 */
    public static char readChar() {
        String str = readKeyBoard(1, false);//就是一个字符
        return str.charAt(0);
    }
    /**
     * 功能:读取键盘输入的一个字符,如果直接按回车,则返回指定的默认值;否则返回输入的那个字符
     * @param defaultValue 指定的默认值
     * @return 默认值或输入的字符
     */
    
    public static char readChar(char defaultValue) {
        String str = readKeyBoard(1, true);//要么是空字符串,要么是一个字符
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }
	
    /**
     * 功能:读取键盘输入的整型,长度小于2位
     * @return 整数
     */
    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(10, false);//一个整数,长度<=10位
            try {
                n = Integer.parseInt(str);//将字符串转换成整数
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
    /**
     * 功能:读取键盘输入的 整数或默认值,如果直接回车,则返回默认值,否则返回输入的整数
     * @param defaultValue 指定的默认值
     * @return 整数或默认值
     */
    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;
    }
    /**
     * 功能:读取键盘输入的指定长度的字符串
     * @param limit 限制的长度
     * @return 指定长度的字符串
     */
    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }
    /**
     * 功能:读取键盘输入的指定长度的字符串或默认值,如果直接回车,返回默认值,否则返回字符串
     * @param limit 限制的长度
     * @param defaultValue 指定的默认值
     * @return 指定长度的字符串
     */
	
    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }
	/**
	 * 功能:读取键盘输入的确认选项,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;
    }
    /**
     * 功能: 读取一个字符串
     * @param limit 读取的长度
     * @param blankReturn 如果为true ,表示 可以读空字符串。 
     * 					  如果为false表示 不能读空字符串。
     * 			
	 *	如果输入为空,或者输入大于limit的长度,就会提示重新输入。
     * @return
     */
    private static String readKeyBoard(int limit, boolean blankReturn) {
        
		//定义了字符串
		String line = "";
		//scanner.hasNextLine() 判断有没有下一行
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();//读取这一行
           
			//如果line.length=0, 即用户没有输入任何内容,直接回车
			if (line.length() == 0) {
                if (blankReturn) return line;//如果blankReturn=true,可以返回空串
                else continue; //如果blankReturn=false,不接受空串,必须输入内容
            }
			//如果用户输入的内容大于了 limit,就提示重写输入  
			//如果用户如的内容 >0 <= limit ,我就接受
            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }
        return line;
    }
}
2、完成House类 domain/model
有这几个属性 编号 房主 电话 地址 月租 状态(已出租/未出租)
package com.yinhai.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;
    }
    //为了方便输出对象 实现重写toString方法
    @Override
    public String toString() {
        //编号 房主 电话 地址 月租 状态(已出租/未出租)
        return id + "\t" + name + '\t' + phone + "\t" + + address + '\t' + rent + "\t" + state;
    }
}
3、完成显示主菜单和退出软件功能
实现功能三部曲【明确完成功能 - 思路分析 - 代码实现】
功能说明:用户打开软件,可以看到主菜单,可以退出软件
思路分析:在HouseView中编写一个方法mainMenu显示菜单
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.println("请输入你的选择(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);
    }
}
4、完成显示房屋列表的功能
思路分析 需要编写HouseView 和 HouseService
HouseService 业务层
定义house[]保存house对象 编写一个方法list()返回所有的房屋信息
    //编写listHouses房屋列表
    public void listHouse(){
        System.out.println("===================房屋列表===================");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t\t月租\t\t状态(已出租/未出租)");
        House[] houses = houseService.list();//list返回的也是数组
        for (int i = 0; i < houses.length; i++) {//但是length长度是10 会输出很多空
            //所以当 == null时可以直接break
            if(houses[i] == null){//如果为空 就不再显示
                break;
            }
            System.out.println(houses[i] );
        }
    }
/
public class HouseService {
    private House[] houses;//保存house对象
    public HouseService(int size) {
        houses = new House[size];//当创建HouseService对象 指定大小
        //为了测试列表信息 初始化一个对象
        houses[0] = new House(1,"jack","112","shanghai",2000,"未出租");
    }
    public House[] list(){
        return houses;
    }
}5、完成新增房源功能
在HouseView内 编写addHouse 接受用户输入
在service内 编写add 将新的对象加入到houses数组中
class HouseView{
//编写addHouse() 接受用户输入 调用add方法
    public void addHouse(){
        if(houseService.full()){//提前判断一次 避免打完信息发现满了
            System.out.println("=================添加房屋失败=================");
            while(true){//执行数组扩容
                System.out.println("是否扩容房屋列表y/n");
                key = Utility.readChar();
                if(key != 'y' && key != 'n'){
                    System.out.println("请输入y/n");
                    continue;
                }
                if(key == 'y'){
                    System.out.println("输入想要增加的个数");
                    int size = Utility.readInt();
                    houseService.addArrlength(size);
                }
                break;
            }
            return;
        }
        System.out.println("===================添加房屋===================");
        System.out.print("姓名:");
        String name = Utility.readString(8);
        System.out.println("电话:");
        String phone = Utility.readString(13);
        System.out.println("地址:");
        String address = Utility.readString(13);
        System.out.println("月租:");
        int rent  = Utility.readInt();
        System.out.println("状态:");
        String state = Utility.readString(3);
        //创建一个新的house对象 id应该是系统分配的,用户不能输入
        House newHouse = new House(0,name,phone,address,rent,state);
        if(houseService.add(newHouse)){
            System.out.println("添加房屋成功");
        }
}
class HouseService{
    public boolean add(House newHouse){
        //暂时不考虑数组扩容,判断是否还可以继续添加
        if(count == houses.length){
            System.out.println("数组已满,不能再添加...");
            return false;
        }
        houses[count++] = newHouse;
        newHouse.setId(++idCounter);
        return true;
    }
    public boolean full(){
        if(count == houses.length){
            System.out.println("数组已满,不能再添加...");
            return true;
        }else {
            return false;
        }
    }
    public void addArrlength(int size){//数组扩容
        int num = houses.length;
        House[] temp = new House[num + size];
        for (int i = 0; i < houses.length; i++) {
            temp[i] = houses[i];
        }
        houses = temp;
    }
}6、完成删除房源
给了一个小的作业 加对齐数组 删了一次后面删报了空指针异常,明天想想怎么改吧,应该是数组长度不变化但是循环到null的对象getId报错,考虑缩减数组长度,明天试试
接着调用add里面的houseNum即可
注意setID应该在构建数组的时候查询是否有默认数组 idCounter = size
public boolean del(int delId){
        //应当找到要删除的房屋信息的对应下标 delId和下标没关系,期间有删除会前移等等
        int index = -1;
        for (int i = 0; i < houses.length; i++) {
            if(delId == houses[i].getId()){
                index = i;
            }
        }
        if(index == -1){
            return false;
        }
        if(index == houses.length - 1){
            houses[index] = null;
        }else{
            for (int i = index ; i <= houses.length - 2; i++) {
                houses[i] = houses[i + 1];//煞笔了 用了index做自变量,人家都获取到了下标还懒觉自变
            }
            houses[houses.length - 1] = null;
        }
        return true;
    }
class HouseService{
    public boolean del(int delId){
        //应当找到要删除的房屋信息的对应下标 delId和下标没关系,期间有删除会前移等等
        int index = -1;
        for (int i = 0; i < houses.length; i++) {
            if(delId == houses[i].getId()){
                index = i;
            }
        }
        if(index == -1){
            return false;
        }
        for (int i = index ; i < houseNum - 1; i++) {
            houses[i] = houses[i + 1];//煞笔了 用了index做自变量,人家都获取到了下标还懒觉自变
        }
        houses[--houseNum] = null;
        return true;
    }
    public void addArrlength(int size){
        int num = houses.length;
        House[] temp = new House[num + size];
        for (int i = 0; i < houses.length; i++) {
            temp[i] = houses[i];
        }
        houses = temp;
    }
}
class HouseView{
    public void delHouse(){
        System.out.println("===================删除房屋信息===================");
        System.out.println("请输入待删除房屋的编号(输入-1取消删除)");
        int delId = Utility.readInt();
        if(delId == -1){
            return;
        }
        System.out.println("请确认是否删除编号为" + delId + "的房屋 y/n,请小心选择");
        while(true){
            char choice = Utility.readChar();
            if(choice != 'y' && choice != 'n'){
                System.out.println("输入有误请输入y/n");
                continue;
            }
            if(choice == 'y'){
                if(!houseService.del(delId)){
                    System.out.println("无法找到对应编码,请确认后重试");
                    return;
                }
            }
            if(choice == 'n'){
                return;
            }
            break;
        }
    }
} 
 
 
7、完成退出确认功能
直接调用Uility内的方法readConfirmSelection
写在HouseView类内
class HouseView{
    public void exitView(){
        System.out.println("=================退        出=================");
        char c = Utility.readConfirmSelection();
        if (c == 'Y'){
            loop = false;
        }
        if (c == 'N'){
            System.out.println("=============取消退出 返回主界面==============");
        }
    }
}8.完成根据id查找房屋信息功能
需要写View类内的方法 加 查找数组的Service功能
直接使用del方法内的查找编号代码块即可
class HouseView{
    public void findHouse() {
        System.out.println("===================查找房源===================");
        while (true) {
            System.out.println("请输入待查找房屋的编号(输入-1取消查找)");
            int findId = Utility.readInt();
            if (findId == -1) {
                System.out.println("您已取消查找");
                return;
            }
            if (!houseService.find(findId)) {
                System.out.println("无法找到对应编码,请确认后重试");
                continue;
            }
            break;
        }
    }
}
class HouseService{
    public boolean find(int findId){
        int index = -1;
        for (int i = 0; i < houseNum; i++) {
            if(findId == houses[i].getId()){
                index = i;
            }
        }
        if(index == -1){
            return false;
        }
        System.out.println(houses[index]);
        return true;
    }
}

9.完成修改房屋信息
同上 查找到后输入需要更改的信息即可
更改信息应该给有原来数组的值,即输入回车给原来的值,不进行修改
 class HouseView{   
    public void modifyingHouse(){
        System.out.println("===================修改房源信息===================");
        int modifyId = 0;
        while (true) {
            System.out.println("请输入待修改房屋的编号(输入-1取消修改)");
            int findId = Utility.readInt();
            modifyId = findId;
            if (findId == -1) {
                System.out.println("您已取消修改");
                return;
            }
            if (houseService.find(findId)) {
                break;
            }
            System.out.println("无法找到对应编码,请确认后重试");
            return;
        }
        System.out.println("请确认是否修改编号为" + modifyId + "的房屋 y/n,请小心选择");
        while(true){
            char choice = Utility.readChar();
            if(choice != 'y' && choice != 'n'){
                System.out.println("输入有误请输入y/n");
                continue;
            }
            if(choice == 'y'){
                break;
                }
            if(choice == 'n'){
                return;
            }
        }
        System.out.println("===================输入修改房源的信息===================");
        System.out.println("直接回车,不修改,保持原有");
        System.out.print("姓名:");
        String name = Utility.readString(8,"");
        System.out.println("电话:");
        String phone = Utility.readString(13,"");
        System.out.println("地址:");
        String address = Utility.readString(13,"");
        System.out.println("月租:");
        int rent  = Utility.readInt(0);
        System.out.println("状态:");
        String state = Utility.readString(13,"");
        //创建一个新的house对象 id为查找到的ID
        House newHouse = new House(modifyId,name,phone,address,rent,state);
        if(houseService.modify(newHouse)){
            System.out.println("修改成功");
        }else{
            System.out.println("未进行更改,保持原有属性");
        }
    }
}
class HouseService{
    public boolean modify(House newHouse){
        int index = -1;
        for (int i = 0; i < houseNum; i++) {
            if(newHouse.getId() == houses[i].getId()){
                index = i;
            }
        }
        int modifyCount = -1;
        if(!newHouse.getName().equals("")){
            houses[index].setName(newHouse.getName());
            modifyCount++;
        }
        if(!newHouse.getPhone().equals("")){
            houses[index].setPhone(newHouse.getPhone());
            modifyCount++;
        }
        if(!newHouse.getAddress().equals("")){
            houses[index].setAddress(newHouse.getAddress());
            modifyCount++;
        }
        if(newHouse.getRent() != 0){
            houses[index].setRent(newHouse.getRent());
            modifyCount++;
        }
        if(!newHouse.getState().equals("")){
            houses[index].setState(newHouse.getState());
            modifyCount++;
        }
        if(modifyCount == -1){
            return false;
        }
        return true;
    }
}



四、总结
至此,该程序便圆满完成,面向对象真的蛮方便的,而且很简洁,最后这个程序综合性很强,对象引用也用的很多,以后可以多看看这个程序
总代码
package com.yinhai.houserent;
import com.yinhai.houserent.domain.House;
import com.yinhai.houserent.view.HouseView;
public class HouseRentApp {
    public static void main(String[] args) {
        // 系统入口,创建houseview对象显示主菜单
        new HouseView().mainMenu();
        System.out.println("==============退出了房屋出租系统=============");
    }
}
package com.yinhai.houserent.view;
import com.yinhai.houserent.domain.House;
import com.yinhai.houserent.service.HouseService;
import com.yinhai.houserent.utlity.Utility;
public class HouseView {
    private boolean loop = true;
    private char key = ' ';
    private HouseService houseService = new HouseService(5);
    //显示主菜单
    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':
                    modifyingHouse();
                    break;
                case '5':
                    listHouse();
                    break;
                case '6':
                    exitView();
                    break;
            }
        } while (loop);
    }
    //编写listHouses房屋列表
    public void listHouse(){
        System.out.println("===================房屋列表===================");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t\t月租\t\t状态(已出租/未出租)");
        House[] houses = houseService.list();//list返回的也是数组
        for (int i = 0; i < houses.length; i++) {//但是length长度是10 会输出很多空
            //所以当 == null时可以直接break
            if(houses[i] == null){//如果为空 就不再显示
                break;
            }
            System.out.println(houses[i] );
        }
    }
    //编写addHouse() 接受用户输入 调用add方法
    public void addHouse(){
        System.out.println("===================新增房源===================");
        if(houseService.full()){//提前判断一次 避免打完信息发现满了
            System.out.println("=================添加房屋失败=================");
            while(true){//执行数组扩容
                System.out.println("是否扩容房屋列表y/n");
                key = Utility.readChar();
                if(key != 'y' && key != 'n'){
                    System.out.println("请输入y/n");
                    continue;
                }
                if(key == 'y'){
                    System.out.println("输入想要增加的个数");
                    int size = Utility.readInt();
                    houseService.addArrlength(size);
                }
                break;
            }
            return;
        }
        System.out.println("===================添加房屋===================");
        System.out.print("姓名:");
        String name = Utility.readString(8);
        System.out.println("电话:");
        String phone = Utility.readString(13);
        System.out.println("地址:");
        String address = Utility.readString(13);
        System.out.println("月租:");
        int rent  = Utility.readInt();
        System.out.println("状态:");
        String state = Utility.readString(3);
        //创建一个新的house对象 id应该是系统分配的,用户不能输入
        House newHouse = new House(0,name,phone,address,rent,state);
        if(houseService.add(newHouse)){
            System.out.println("添加房屋成功");
        }
    }
    public void delHouse(){
        System.out.println("===================删除房屋信息===================");
        System.out.println("请输入待删除房屋的编号(输入-1取消删除)");
        int delId = Utility.readInt();
        if(delId == -1){
            System.out.println("您已取消删除");
            return;
        }
        System.out.println("请确认是否删除编号为" + delId + "的房屋 y/n,请小心选择");
        while(true){
            char choice = Utility.readChar();
            if(choice != 'y' && choice != 'n'){
                System.out.println("输入有误请输入y/n");
                continue;
            }
            if(choice == 'y'){
                if(!houseService.del(delId)){
                    System.out.println("无法找到对应编码,请确认后重试");
                    return;
                }
            }
            if(choice == 'n'){
                return;
            }
            break;
        }
    }
    public void exitView(){
        System.out.println("=================退        出=================");
        char c = Utility.readConfirmSelection();
        if (c == 'Y'){
            loop = false;
        }
        if (c == 'N'){
            System.out.println("=============取消退出 返回主界面==============");
        }
    }
    public void findHouse() {
        System.out.println("===================查找房源===================");
        while (true) {
            System.out.println("请输入待查找房屋的编号(输入-1取消查找)");
            int findId = Utility.readInt();
            if (findId == -1) {
                System.out.println("您已取消查找");
                return;
            }
            if (!houseService.find(findId)) {
                break;
            }
            System.out.println("无法找到对应编码,请确认后重试");
        }
    }
    public void modifyingHouse(){
        System.out.println("===================修改房源信息===================");
        int modifyId = 0;
        while (true) {
            System.out.println("请输入待修改房屋的编号(输入-1取消修改)");
            int findId = Utility.readInt();
            modifyId = findId;
            if (findId == -1) {
                System.out.println("您已取消修改");
                return;
            }
            if (houseService.find(findId)) {
                break;
            }
            System.out.println("无法找到对应编码,请确认后重试");
            return;
        }
        System.out.println("请确认是否修改编号为" + modifyId + "的房屋 y/n,请小心选择");
        while(true){
            char choice = Utility.readChar();
            if(choice != 'y' && choice != 'n'){
                System.out.println("输入有误请输入y/n");
                continue;
            }
            if(choice == 'y'){
                break;
                }
            if(choice == 'n'){
                return;
            }
        }
        System.out.println("===================输入修改房源的信息===================");
        System.out.println("直接回车,不修改,保持原有");
        System.out.print("姓名:");
        String name = Utility.readString(8,"");
        System.out.println("电话:");
        String phone = Utility.readString(13,"");
        System.out.println("地址:");
        String address = Utility.readString(13,"");
        System.out.println("月租:");
        int rent  = Utility.readInt(0);
        System.out.println("状态:");
        String state = Utility.readString(13,"");
        //创建一个新的house对象 id为查找到的ID
        House newHouse = new House(modifyId,name,phone,address,rent,state);
        if(houseService.modify(newHouse)){
            System.out.println("修改成功");
        }else{
            System.out.println("未进行更改,保持原有属性");
        }
    }
}
package com.yinhai.houserent.service;
import com.yinhai.houserent.domain.House;
public class HouseService {
    private House[] houses;//保存house对象
    private int houseNum = 1;
    private int idCounter = 1;
    public HouseService(int size) {
        houses = new House[size];//当创建HouseService对象 指定大小
        houseNum = size;
        idCounter = size;
        //为了测试列表信息 初始化一个对象
        houses[0] = new House(1,"jack","112","shanghai",2000,"未出租");
        houses[1] = new House(2,"jack1","112","shanghai",2000,"未出租");
        houses[2] = new House(3,"jack2","112","shanghai",2000,"未出租");
        houses[3] = new House(4,"jack3","112","shanghai",2000,"未出租");
        houses[4] = new House(5,"jack4","112","shanghai",2000,"未出租");
    }
    public House[] list(){
        return houses;
    }
    public boolean add(House newHouse){
        //暂时不考虑数组扩容,判断是否还可以继续添加
        if(houseNum == houses.length){
            System.out.println("数组已满,不能再添加...");
            return false;
        }
        houses[houseNum++] = newHouse;
        newHouse.setId(++idCounter);
        return true;
    }
    public boolean full(){
        if(houseNum == houses.length){
            System.out.println("数组已满,不能再添加...");
            return true;
        }else {
            return false;
        }
    }
    public boolean del(int delId){
        //应当找到要删除的房屋信息的对应下标 delId和下标没关系,期间有删除会前移等等
        int index = -1;
        for (int i = 0; i < houseNum; i++) {
            if(delId == houses[i].getId()){
                index = i;
            }
        }
        if(index == -1){
            return false;
        }
        for (int i = index ; i < houseNum - 1; i++) {
            houses[i] = houses[i + 1];//煞笔了 用了index做自变量,人家都获取到了下标还懒觉自变
        }
        houses[--houseNum] = null;
        return true;
    }
    public void addArrlength(int size){
        int num = houses.length;
        House[] temp = new House[num + size];
        for (int i = 0; i < houses.length; i++) {
            temp[i] = houses[i];
        }
        houses = temp;
    }
    public boolean find(int findId){
        int index = -1;
        for (int i = 0; i < houseNum; i++) {
            if(findId == houses[i].getId()){
                index = i;
            }
        }
        if(index == -1){
            return false;
        }
        System.out.println(houses[index]);
        return true;
    }
    public boolean modify(House newHouse){
        int index = -1;
        for (int i = 0; i < houseNum; i++) {
            if(newHouse.getId() == houses[i].getId()){
                index = i;
            }
        }
        int modifyCount = -1;
        if(!newHouse.getName().equals("")){
            houses[index].setName(newHouse.getName());
            modifyCount++;
        }
        if(!newHouse.getPhone().equals("")){
            houses[index].setPhone(newHouse.getPhone());
            modifyCount++;
        }
        if(!newHouse.getAddress().equals("")){
            houses[index].setAddress(newHouse.getAddress());
            modifyCount++;
        }
        if(newHouse.getRent() != 0){
            houses[index].setRent(newHouse.getRent());
            modifyCount++;
        }
        if(!newHouse.getState().equals("")){
            houses[index].setState(newHouse.getState());
            modifyCount++;
        }
        if(modifyCount == -1){
            return false;
        }
        return true;
    }
}
package com.yinhai.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;
    }
    //为了方便输出对象 实现重写toString方法
    @Override
    public String toString() {
        //编号 房主 电话 地址 月租 状态(已出租/未出租)
        return id + "\t\t" + name + "\t" + phone + "\t\t" + address + "\t" + rent + "\t" + state;
    }
}



















![[③ADRV902x]: Digital Filter Configuration(接收端)](https://img-blog.csdnimg.cn/5b6487b3fcc440b7a53108ee4ef0497e.png)