【Java房屋出租系统】韩顺平java学习房屋出租系统

房屋出租系统效果图示

在这里插入图片描述

房屋出租系统主类

package hspedu.houseRent;

import java.util.Map;

public class HouseRentSys {
    public static void main(String[] args) {
        House house = new House(new Map[0]);
        Menus menus = new Menus();
        for (; true; ) {
            menus.whichMenus();
            if ("6".equals(menus.getWhichType())){
                break;
            }
            switch (menus.getWhichType()){
                case "1":
                    house.addHouse();
                    break;
                case "2":
                    house.findHouseById();
                    break;
                case "3":
                    house.deleteHouseById();
                    break;
                case "4":
                    house.updateHouseById();
                    break;
                case "5":
                    house.infoShow();
                    break;
            }
        }
    }
}

菜单类

package hspedu.houseRent;

import java.util.Scanner;

public class Menus {
    private String whichType;
    public void show(boolean pass){
        System.out.println("------------房屋出租系统----------");
        System.out.println("          1 新增房源");
        System.out.println("          2 查找房屋");
        System.out.println("          3 删除房屋");
        System.out.println("          4 修改房屋信息");
        System.out.println("          5 房屋列表");
        System.out.println("          6 退出");
        System.out.print(pass?"请输入(1-6):":"输入错误,请重新输入(1-6)");
    }
    public String getWhichType() {
        return whichType;
    }

    public void whichMenus() {
        boolean pass;
        Scanner s = new Scanner(System.in);
        show(true);
        for (;true;){
            pass = true;
            whichType = s.next();
            if (whichType.length() != 1) {
                pass = false;
            } else if (!whichType.matches("[1-6]")) {
                pass = false;
            }
            if(pass){
                break;
            }else {
                show(false);
            }
        }
    }
}

房屋类

package hspedu.houseRent;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class House {
    private String name;
    private String tel;
    private String address;
    private String rentFees;
    private String status;
    private Map[] info;
    Scanner s = new Scanner(System.in);

    public House(Map[] info) {
        this.info = info;
    }
    public void addHouse() {
        System.out.println("-------------增加房屋-------------");
        this.writeInfo();
        this.UpdateHouseInfo();
        System.out.println("-------------添加完成-------------");
        this.infoShow();
    }
    public void deleteHouseById(){
        System.out.println("-------------删除房屋-------------");
        this.infoShow();
        System.out.print("请输入您要删除的房屋id:");
        String id = s.next();
        if (!(Boolean) this.matchHouse(id).get("matchOk")){
            System.out.println("很抱歉,未搜寻到id为"+id+"的内容");
        }else {
            this.info[Integer.parseInt(id) - 1].put("delete", true);
            System.out.println("-------------删除成功-------------");
            this.infoShow();
        }
    }
    public void updateHouseById(){
        System.out.println("-------------修改房屋-------------");
        this.infoShow();
        System.out.print("请输入您要修改的房屋id:");
        String id = s.next();
        if (!(Boolean) this.matchHouse(id).get("matchOk")){
            System.out.println("很抱歉,未搜寻到id为"+id+"的内容");
        } else if ((Boolean) this.info[Integer.parseInt(id) - 1].get("delete")) {
            System.out.println("很抱歉,您查找的id号为" + id + "的内容已被删除");
        } else {
            this.infoShow(Integer.parseInt(id) - 1);
            this.writeInfo();
            this.UpdateHouseInfo(Integer.parseInt(id) - 1);
            System.out.println("-------------修改完成-------------");
            this.infoShow();
        }
    }
    public void findHouseById(){    //根据id查找房屋
        System.out.println("-------------查找房屋-------------");
        System.out.print("请输入你要查找的id:");
        String id = s.next();
        if (!(Boolean) this.matchHouse(id).get("matchOk")) {
            System.out.println("很抱歉,未搜寻到id为" + id + "的内容");
        } else if ((Boolean) this.info[Integer.parseInt(id) - 1].get("delete")) {
            System.out.println("很抱歉,您查找的id号为" + id + "的内容已被删除");
        } else {
            System.out.println("-------------查找成功-------------");
            this.infoShow(Integer.parseInt(id) - 1);
        }
    }
    void writeInfo(){
        System.out.print("姓名:");
        this.name = validName();
        System.out.print("电话:");
        this.tel = validTel();
        System.out.print("地址:");
        this.address = s.next();
        System.out.print("月租:");
        this.rentFees = validRentFees();
        System.out.print("状态(未出租0/已出租1):");
        this.status = validStatus();
    }
    void titleShow(){
        System.out.println("id\tname\ttel\taddress\trentFees\tstatus");
    }
    void infoShow(){
        this.titleShow();
        for (int i = 0; i < this.info.length; i++) {
            if (!(Boolean) this.info[i].get("delete")) {
                System.out.println(
                        this.info[i].get("id") + "\t"
                                + this.info[i].get("name") + "\t"
                                + this.info[i].get("tel") + "\t"
                                + this.info[i].get("address") + "\t"
                                + this.info[i].get("rentFees") + "\t"
                                + (this.info[i].get("status").equals("0") ? "未出租" : "已出租"));
            }
        }
    }
    void infoShow(int which){
        this.titleShow();
        System.out.println(
                this.info[which].get("id")+"\t"
                +this.info[which].get("name")+"\t"
                +this.info[which].get("tel")+"\t"
                +this.info[which].get("address")+"\t"
                +this.info[which].get("rentFees")+"\t"
                +(this.info[which].get("status").equals("0")?"未出租":"已出租"));
    }
    Map matchHouse(String id){
        int whichId = 0;
        boolean matchOk = false;
        for (int i = 0; i < this.info.length; i++) {
            if (id.equals(this.info[i].get("id").toString())) {
                whichId = i;
                matchOk = true;
            }
        }
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("whichId", whichId);
        res.put("matchOk", matchOk);
        return res;
    }
    void UpdateHouseInfo(){
        Map[] arr = new Map[this.info.length + 1];
        for (int i = 0; i < this.info.length; i++) {    //如果info有内容就先复制
            arr[i] = this.info[i];
        }
        arr[arr.length - 1] = this.createData();
        this.info = arr;
    }
    void UpdateHouseInfo(int which){
        this.info[which] = this.createData();
    }
    Map createData(){
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("id", this.info.length + 1);
        data.put("delete", false);
        data.put("name", this.name);
        data.put("tel", this.tel);
        data.put("address", this.address);
        data.put("rentFees", this.rentFees);
        data.put("status", this.status);
        return data;
    }
    String validStatus(){
        String status;
        for (; true; ) {
            status = s.next();
            if(status.matches("[0-1]")){
                break;
            }else {
                System.out.println("请输入正确的状态!!!");
                System.out.print("状态(未出租0/已出租1):");
            }
        }
        return status;
    }
    String validRentFees(){
        String money;
        for (; true; ) {
            money = s.next();
            if (money.matches("[+-]?[0-9]+(\\.[0-9]+)?")) {
                break;
            }else {
                System.out.println("请输入正确的月租费用!!!");
                System.out.print("月租:");
            }
        }
        return money;
    }
    String validTel(){
        String tel;
        for (; true; ) {
            tel = s.next();
            if(tel.matches("^1[3,4,5,6,7,8,9]\\d{9}$")){
                break;
            }else {
                System.out.println("请输入正确的手机号码!!!");
                System.out.print("电话:");
            }
        }
        return tel;
    }
    String validName() {
        String name = s.next();
        return name;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值