Java基础(七) 房屋出租系统

Java基础(七)

房屋出租系统

项目需求

实现基于文本界面的《房屋出租系统》

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

项目界面-主菜单

image-20220722201232995

新增房源

image-20220722201331889

查找房源

image-20220722201400980

删除房源

image-20220722201432485

修改房源

如果不希望修改某个信息则直接回车

image-20220722201625571

房屋列表

image-20220722201720193

退出系统

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yVnp4X4E-1658578187689)(https://s2.loli.net/2022/07/22/OTmkNwvexRW7Bbc.png)]

房屋出租系统的实现

程序框架图(分层模式)

  1. 系统有哪些类【文件】
  2. 明确类与类的调用关系

image-20220723200834627

准备工具类Utility,提高开发效率

在实际开发中,公司会提供相应的工具类和开发库,可以提高开发效率,程序员也需要能够看懂别人写的代码,并能够正确调用。

  1. 了解Utility类的使用
  2. 测试Utility类

系统设计

image-20220723200335322

源码:

House:

package com.lzlp.projcet.houserent.domain;

public class House {
    //编号 房主 电话 地址 月租 状态(未出租/已出租)
    private int i;
    private String name;
    private String phone;
    private String address;
    private double rent;
    private String state;

    public House() {
    }

    public House(int i, String name, String phone,
                 String address, double rent, String state) {
        this.i = i;
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.rent = rent;
        this.state = state;
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    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 double getRent() {
        return rent;
    }

    public void setRent(double rent) {
        this.rent = rent;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    //为了方便的输出对象的信息,实现toString()
    @Override
    public String toString() {
        return  i +
                "\t\t" + name +
                "\t" + phone +
                "\t\t" + address +
                "\t\t" + rent +
                "\t" + state ;
    }
}

HouseRentView:

package com.lzlp.projcet.houserent.houserentview;

import com.lzlp.projcet.houserent.domain.House;
import com.lzlp.projcet.houserent.service.HouseService;
import com.lzlp.projcet.houserent.utils.Utility;

/**
 * 1.显示界面
 * 2.接收用户的输入
 * 3.调用HouseService完成对房屋信息的各种操作
 */
public class HouseRentView {
    private boolean out = true;
    private char k;//接收用户选择
    private HouseService houseService = new HouseService(2);

    //编写listHouses()显示房屋列表
    public 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){//如果数组元素为null,就不在输出
                break;
            }
            System.out.println(houses[i]);
            //System.out.println(i);
        }
        System.out.println("---------房屋列表显示完毕---------");
    }

    //addHouse()接收输入,创建House对象,调用add方法
    public void addHouse(){
        System.out.println("-----------添加房屋-----------");
        System.out.print("姓名:");
        String name = Utility.readString(8);
        System.out.print("电话:");
        String phone = Utility.readString(11);
        System.out.print("地址:");
        String address = Utility.readString(16);
        System.out.print("月租:");
        double 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("添加失败");
        }
    }

    //delHouse()删除房屋信息,显示界面,输入id
    public void delHouse(){
        System.out.println("-----------删除房屋信息-----------");
        System.out.println("请输入待删除的房屋的编号,(-1表示退出):");
        int delId = Utility.readInt();
        if (delId == -1){
            System.out.println("已放弃删除");
            return;//直接结束方法
        }
        char c = Utility.readConfirmSelection();
        //该方法本身就有循环判断的逻辑,必须输入Y/N,否则无法退出循环
        if (c == 'Y'){//真的要删除,调用HouseService中的del()
           if (houseService.del(delId)){
               System.out.println("-----------删除信息成功-----------");
           }else {
               System.out.println("房屋编号不存在,删除失败");
           }
        }else {
            System.out.println("-----------放弃删除信息-----------");
        }

    }
    //退出
    public void exit(){
        char c = Utility.readConfirmSelection();
        if (c == 'Y'){
            out = false;
        }
    }
    //查询房屋信息
    public void findHouse(){
        System.out.println("-----------查找房屋息-----------");
        System.out.print("请输入你要查找的id:");
        int fid = Utility.readInt();
        House house = houseService.find(fid);
        if (house == null){
            System.out.println("未能找到相应id的房屋");
        }else {
            System.out.println(house);
        }
        System.out.println("-----------查找房屋息-----------");
    }
    //修改房屋信息
    public void upHouse(){
        System.out.println("-----------修改房屋信息-----------");
        System.out.println("请输入待修改的房屋的编号,(-1表示退出):");
        int upId = Utility.readInt();
        if (upId == -1){
            System.out.println("已放弃修改");
            return;//直接结束方法
        }
        House house = houseService.up(upId);
        if (house == null){
            System.out.println("未能找到相应id的房屋");
            return;
        }
        //输入时如果直接回车,就不会进行修改
        System.out.print("姓名(" + house.getName() + "):");
        house.setName(Utility.readString(8,house.getName()));
        System.out.print("电话(" + house.getPhone() + "):");
        house.setPhone(Utility.readString(11,house.getPhone()));
        System.out.print("地址(" + house.getAddress() + "):");
        house.setAddress(Utility.readString(16,house.getAddress()));
        System.out.print("租金(" + house.getRent() + "):");
        house.setRent(Utility.readInt((int) house.getRent()));
        System.out.print("状态(" + house.getState() + "):");
        house.setState(Utility.readString(3,house.getState()));
        System.out.println("-----------修改房屋信息完成-----------");
    }
    //显示主菜单
    public void mainMenu(){

        do {
            System.out.println("\n----------房屋出租系统----------");
            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.println("请输入操作(1-6)");
            k = Utility.readChar();
            switch (k){
                case '1':
                    addHouse();
                    break;
                case '2':
                    findHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    upHouse();
                    break;
                case '5':
                    listHouses();
                    break;
                case '6':
                    exit();
                    break;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }while (out);


    }
}

HouseService

package com.lzlp.projcet.houserent.service;

import com.lzlp.projcet.houserent.domain.House;
import com.lzlp.projcet.houserent.utils.Utility;

/**
 * 【业务层】
 * //定义House[],保存House对象
 * 1.响应HouseRentView调用
 * 完成对房屋信息的操作,(增删改查crud)
 */
public class HouseService {
    private House[] houses;//保存House对象
    private int housesNum = 1;//记录已有多少房屋信息
    private int idCounter = 1;//记录id增长情况
    //构造器(数组长度),创建House数组,并输入第一个元素默认值进行功能验证
    public HouseService(int size){
        houses = new House[size];//在创建HouseService对象时,指定数组的大小
        houses[0] =new House (1,"jack","122",
                "承德",3000,"未出租");
    }
    //list()返回所有房屋信息
    public House[] list(){
        return houses;
    }
    //add()添加新对象,返回boolean
    public boolean add(House newhouse){
        //添加数组扩容机制,在数组已满时,增加数组容量
        //判断是否还可以继续添加
        if(housesNum == houses.length){
            House[] houses1 = new House[this.houses.length + 1];
            for (int i = 0; i < houses.length; i++) {
                houses1[i] = houses[i];
            }
            houses = houses1;
            System.out.println("数组已满,数组长度+1");

        }
        houses[housesNum++] = newhouse;
        //设计id子增长机制,更新newHouse的id
        idCounter++;
        newhouse.setI(idCounter);
        return true;
    }

    //del()根据房屋id,删除房屋信息,有删除和没删除两种情况,返回boolean
    public boolean del(int delId){
        //先找到删除的房屋信息的下标
        //下标和房屋编号不是一回事,有时编号可能是乱序的
        int index = -1;
        for (int i = 0; i < housesNum; i++) {
            if (delId == houses[i].getI()){//要删除的是房屋id,i是数组下标
                index = i;//记录下标i
            }
        }
        if (index == -1){//在数组中没有要删除的id
            return false;
        }
        //如果找到
        for (int i = index; i < housesNum-1; i++) {
            houses[i] = houses[i+1];
        }
       /*
       houses[housesNum-1] = null;
       housesNum--;
        */
        houses[--housesNum] = null;
        return true;
    }

    //find()用id查找房屋
    public House find(int fid){
        for (int i = 0; i < housesNum; i++) {
            if (fid == houses[i].getI()){
                return houses[i];
            }
        }
        return null;
    }
    //up()修改房屋信息
   /* public boolean up(int upId){
        int index = -1;
        for (int i = 0; i < housesNum; i++) {
            if (upId == houses[i].getI()){
                index = i;
                break;
            }
        }
        if (index == -1){
            System.out.println("未找到相应id房屋的信息");
            return false;
        }else {
            System.out.print("姓名(" + houses[index].getName() + "):");
            houses[index].setName(Utility.readString(8));
            System.out.print("电话(" + houses[index].getPhone() + "):");
            houses[index].setPhone(Utility.readString(11));
            System.out.print("地址(" + houses[index].getAddress() + "):");
            houses[index].setAddress(Utility.readString(16));
            System.out.print("租金(" + houses[index].getRent() + "):");
            houses[index].setRent(Utility.readInt());
            System.out.print("状态(" + houses[index].getState() + "):");
            houses[index].setState(Utility.readString(3));
        }
        return true;
    }*/
    public House up(int upId){
        House house = find(upId);
        return house;
    }

}

Utility

package com.lzlp.projcet.houserent.utils;


/**
 工具类的作用:
 处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。
 */

import java.util.*;
/**


 */
public class Utility {
    //静态属性。。。
    private static Scanner scanner = new Scanner(System.in);


    /**
     * 功能:读取键盘输入的一个菜单选项,值:1——5的范围
     * @return 1——5
     */
    //静态方法,可以直接用类名调用Utility.readMenuSelection();
    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;
    }
}



HouseRentApp

package com.lzlp.projcet.houserent;

import com.lzlp.projcet.houserent.houserentview.HouseRentView;

public class HouseRentApp {
    public static void main(String[] args) {
        //创建HouseView对象,并显示主菜单,是整个程序的入口
        new HouseRentView().mainMenu();
        System.out.println("==你已退出房屋租赁系统==");
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值