Java房屋出租系统

该代码示例展示了一个简单的Java应用,用于管理房屋出租信息。包括House类用于表示房屋对象,HouseService类处理房屋的增删查改操作,以及HouseView类负责用户交互界面。程序提供了添加、查找、删除、修改房屋信息及显示房屋列表的功能。
摘要由CSDN通过智能技术生成
package com.houserent;

import com.houserent.view.HouseView;

public class HouseRentApp {
    public static void main(String[] args) {
        new HouseView().mainMenu();
        System.out.println("=====你退出房屋出租系统======");
    }
}
package com.houserent.domain;

/**
 * House的对象表示一个房屋信息
 */
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;
    }
}
package com.houserent.service;

import com.houserent.domain.House;

public class HouseService {
    private House[] houses;
    private int houseNum = 1;//记录当前有多少个房屋信息
    private int idCounter = 1;//记录当前id
    public  HouseService(int size) {
         houses = new House[size];
         houses[0] = new House(1,"mark","132","云岩区",2000,"未出租" );
    }

    public House findById(int findId){
        for (int i = 0; i < houseNum; i++) {
            if (findId == houses[i].getId());
            return houses[i];
        }
        return null;
    }

    public boolean del(int delId){
        //应当先找到要删除的房屋信息对应的下标
        int index = -1;
        for (int i = 0; i < houseNum; i++) {
            if (delId == houses[i].getId()){//要删除的房屋(id),是数组下标为i的元素
                index = i;
            }
        }
        if (index == -1){
            return false;
        }
        for (int i = index; i < houseNum - 1 ; i++) {
            houses[i] = houses[i+1];
        }
        houses[--houseNum] = null;
        return true;
    }

    public boolean add(House newHouse){
        if(houseNum == houses.length){
            System.out.println("数组已满,不满再添加了...");
            return false;
        }
        houses[houseNum++] = newHouse;
        idCounter++;
        newHouse.setId(++idCounter);
        return true;
    }

    public House[] list(){
        return houses;
}
}
package com.houserent.view;

import com.houserent.HouseRentApp;
import com.houserent.domain.House;
import com.houserent.service.HouseService;
import com.houserent.utils.Utility;

/**
 * 1.显示界面
 * 2.接受用户输入
 * 3.调用HouseService完成对房屋信息的各种操作
 */
public class HouseView {
    private boolean loop = true;//控制显示菜单
    private char key = ' ';//接受用户输入
    private HouseService houseService = new HouseService(5);

    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.println("姓名("+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 (!"".equals(rent)){
            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.println("请输入要查找的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;
        }
    }

    private void delHouse() {
        System.out.println("=======添加房屋信息=======");
        System.out.println("请输入要删除房屋的编号(-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.println("姓名:");
        String name = Utility.readString(8);
        System.out.println("电话:");
        String phone = Utility.readString(12);
        System.out.println("地址:");
        String address = Utility.readString(16);
        System.out.println("月租:");
        int rent = Utility.readInt(3);
        System.out.println("状态:");
        String state = Utility.readString(3);
        House addhouse = new House(0, name, phone, address, rent, state);
        if (houseService.add(addhouse)) {
            System.out.println("=========添加房屋成功=========");
        } else {
            System.out.println("=========添加房屋失败=========");
        }
    }

    public void listHouse() {
        System.out.println("\t======房屋列表======");
        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]);
        }
    }

    //显示主菜单
    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.println("请输入你的选择(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':
                    listHouse();
                    break;
                case '6':
                    exit();
            }
        } while (loop);
    }
}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yu_Xiaoguang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值