Java小项目——房屋出租系统

程序入口

public class HouseRent {
    public static void main(String[] args) {
        new HouseView().Menu();
    }
}

用户界面

public class HouseView {
    boolean loop = true;
    HouseService houseService = new HouseService();
//    显示主菜单
    public void Menu() {
        do {
            System.out.println("----------房屋出租系统----------");
            System.out.println("\t\t1 新增房屋");
            System.out.println("\t\t2 查找房屋");
            System.out.println("\t\t3 删除房屋");
            System.out.println("\t\t4 修改房屋");
            System.out.println("\t\t5 房屋列表");
            System.out.println("\t\t6 退出系统");
            System.out.print("请选择(1-6):");
            char choice = Utility.readChar();
            switch(choice) {
                case '1':
                    addHouse();
                    break;
                case '2':
                    findHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    updateHouse();
                    break;
                case '5':
                    listHouse();
                    break;
                case '6':
                    Exit();
                    break;
                default:
                    System.out.println("输入有误,请重新选择!");
            }
        } while(loop);
        System.out.println("已成功退出房屋出租系统~~");
    }
//    显示房屋列表
    public void listHouse() {
        System.out.println("----------房屋列表----------");
        System.out.println("编号\t\t" + "房主\t\t" + "电话\t\t" + "地址\t\t" + "月租\t\t" + "状态(已出租/未出租)");
        House[] house = houseService.List();
        for (int i = 0; i < house.length; i++) {
            if(house[i] == null) {
                break;
            }
            System.out.println(house[i]);
        }
        System.out.println("房屋列表显示完毕~~");
    }
//    用户输入新增房屋信息
    public void addHouse() {
        System.out.println("----------新增房屋----------");
        System.out.print("姓名:");
        String name = Utility.readString(4);
        System.out.print("电话:");
        String telephone = Utility.readString(11);
        System.out.print("地址:");
        String address = Utility.readString(20);
        System.out.print("月租:");
        int rent = Utility.readInt(4);
        System.out.print("状态(已出租/未出租):");
        String state = Utility.readString(3);
        House newHouse = new House(0, name, telephone, address, rent, state);
        if(houseService.Add(newHouse)) {
            System.out.println("添加房屋成功~~");
        } else {
            System.out.println("添加房屋失败~~");
        }
    }
//    用户输入待查找房屋的id
    public void findHouse() {
        System.out.println("----------查找房屋----------");
        System.out.print("请输入要查找的房屋id:");
        int findId = Utility.readInt(4);
        House house = houseService.findById(findId);
        if(house != null) {
            System.out.println(house);
        } else {
            System.out.println("该房屋编号不存在~~");
        }
    }
//    用户输入待删除房屋的编号
    public void delHouse() {
        System.out.println("----------删除房屋----------");
        System.out.print("请输入待删除房屋的编号(-1退出):");
        int delId = Utility.readInt(4);
        if(delId == -1) {
            System.out.println("放弃删除房屋信息~~");
            return;
        }
        System.out.println("你确定要删除吗?");
        char choice = Utility.readConfirmSelection();
        if(choice == 'Y') {
            if(houseService.Delete(delId)) {
                System.out.println("删除房屋信息成功~~");
            } else {
                System.out.println("房屋编号不存在,无法删除~~");
            }
        } else {
            System.out.println("放弃删除房屋信息~~");
        }
    }
//    用户修改房屋信息
    public void updateHouse() {
        System.out.println("----------修改房屋----------");
        System.out.print("请输入待修改房屋的编号(-1退出):");
        int updateId = Utility.readInt(4);
        if(updateId == -1) {
            System.out.println("放弃修改房屋信息~~");
            return;
        }
        House house = houseService.findById(updateId);
        if(house == null) {
            System.out.println("房屋编号不存在,无法修改~~");
            return;
        } else {
            System.out.print("姓名(" + house.getName() + "):");
            String name = Utility.readString(4, "");
            if (!"".equals(name)) {
                house.setName(name);
            }
            System.out.print("电话(" + house.getTelephone() + "):");
            String telephone = Utility.readString(11, "");
            if(!"".equals(telephone)) {
                house.setTelephone(telephone);
            }
            System.out.print("地址(" + house.getAddress() + "):");
            String address = Utility.readString(20, "");
            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 Exit() {
        char isExit = Utility.readConfirmSelection();
        if(isExit == 'Y') {
            loop = false;
        }
    }
}

业务逻辑类

public class HouseService {
    House[] house = new House[1000];
    int houseNum = 0;
    int idCount = 0;

    //    返回房屋信息
    public House[] List() {
        return house;
    }

    //    新增房屋
    public boolean Add(House newHouse) {
        if (houseNum == house.length) {
            System.out.println("房屋已满,不能再添加~~");
            return false;
        }
        house[houseNum++] = newHouse;
        newHouse.setId(++idCount);  //让房屋编号自增长,从1开始
        return true;
    }

    //    查找房屋
    public House findById(int findId) {
        for (int i = 0; i < houseNum; i++) {
            if (findId == house[i].getId()) {
                return house[i];
            }
        }
        return null;
    }

    //    删除房屋
    public boolean Delete(int delId) {
        int index = -1;
        for (int i = 0; i < houseNum; i++) {
            if (delId == house[i].getId()) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        }
        for (int i = index; i < houseNum - 1; i++) {
            house[i] = house[i + 1];
        }
        house[--houseNum] = null;
        return true;
    }
}

房屋类

public class House {
    private int  id;
    private String name;
    private String telephone;
    private String address;
    private int rent;
    private String state;

    public House(int id, String name, String telephone, String address, int rent, String state) {
        this.id = id;
        this.name = name;
        this.telephone = telephone;
        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 getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public double 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\t" + telephone + "\t\t"
                + address + "\t\t" + rent + "\t\t" + state;
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值