java-房屋出租系统实现

发布时间 2023-08-02 22:57:59作者: hhxxlrkq

房屋出租系统

项目需求

能够实现对房屋信息的添加、修改和删除(用数组实现),并能够打印房屋明细表

项目界面

主菜单

新增房源

查找房源

删除房源

修改房源

房屋列表

退出系统

项目设计

项目实现

显示主菜单

在HouseView.java 中,编写一个方法mainMenu,显示菜单

点击查看代码
    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 (true);
    }

显示房屋列表

点击查看代码
    private 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("============房屋列表显示完毕============");
    }

添加房屋信息

点击查看代码
//HouseView
    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 是系统分配的,
        House newHouse = new House(0, name, phone, address, rent, state);
        if (houseService.add(newHouse)) {
            System.out.println("=============添加房屋成功============");
        } else {
            System.out.println("=============添加房屋失败============");
        }

    }

//HouseService
    public boolean add(House newHouse) {
        //判断是否可以继续添加(不考虑数组扩容)
        if (houseNums == houses.length) {  //不能再添加
            System.out.println("数组已满,不能再添加了.....");
            return false;
        }
        //将newHouse对象加入,新增加了一个房屋
        houses[houseNums++] = newHouse;
        //id自动增长
        newHouse.setId(++idCounter);
        return true;
    }

删除房屋信息

点击查看代码
//HouseService
    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) {
            return false;  //说明delId在数组中不存在
        }
        for (int i = index; i < houseNums - 1; i++) {
            houses[i] = houses[i + 1];
            //往前移
        }
        //把当有存在的房屋信息的最后一个  设置null
        houses[--houseNums] = null;
        return true;

    }

//HouseView
    public void delHouse() {
        System.out.println("=============删除房屋信息============");
        System.out.print("请输入待删除房屋的编号(-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("=============放弃删除房屋信息============");
        }

    }

根据id查找房屋信息

点击查看代码
//HouseView
    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
    public House findById(int findId) {

        //遍历数组
        for (int i = 0; i < houseNums; i++) {
            if (findId == houses[i].getId()) {
                return houses[i];
            }
        }

        return null;

    }

修改房屋信息

点击查看代码
//HouseView
   public void update() {
        System.out.println("=============修改房屋信息============");
        System.out.println("请选择待修改房屋编号(-1表示退出)");
        int updateId = Utility.readInt();
        if (updateId == -1) {
            System.out.println("=============你放弃修改房屋信息============");
            return;
        }

        //根据输入得到updateId,查找对象

        //返回的是引用类型[即:就是数组的元素]
        //在后面对house.setXxx() ,就会修改HouseService中houses数组的元素!
        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("=============修改房屋信息成功============");


    }

退出

点击查看代码
//HouseView
    public void exit() {
        char c = Utility.readConfirmSelection();
        if (c == 'Y') {
            loop = false;
        }
    }

完整代码

参考

韩顺平零基础学java