JAVA实现房屋出租系统(超详细)

2023-12-27 05:59

本文主要是介绍JAVA实现房屋出租系统(超详细),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

运行展示:

1.新增房源

当列表空间满时,会无法添加(这里我设空间为3组,方便展示)

2.查找房源 

3.删除房屋 

4.修改房屋信息 

5.退出 

构思[分层模式]:

        

 包的创建:

工具类代码(Utility):

public class Utility {public static char readChar() {//static静态对象可以直接调用,不用newScanner scanner = new Scanner(System.in);String key = scanner.next();return key.charAt(0);//只会返回字符串第一个字符,比如123只返回1}public static String readString() {Scanner scanner = new Scanner(System.in);String key = scanner.next();return key;}public static int readInt() {Scanner scanner = new Scanner(System.in);int key = scanner.nextInt();return key;}
}

模型类代码(House) :

设置House中存在哪些属性

/**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//编号   房主   电话   地址   月租   状态(未出租/已出租)@Overridepublic String toString() {return  id +"\t\t\t" + name +"\t\t\t" + phone +"\t\t\t" + address +"\t\t\t" + rent +"\t\t\t" + state;}
}

 程序入口(main)

public class HouseRentApp {public static void main(String[] args) {//创建一个HouseView对象,并显示主菜单,是整个程序的入口new HouseView().mainMenu();//匿名对象}
}

主类实现代码:

1.HouseView.java 类[界面]

显示主菜单:
mainMenu方法,可以显示主菜单


新增房屋信息:
addHouse方法,界面,接收用户的输入信息,添加房屋信息


查找房屋信息:
seekHouse方法,界面,查找房屋信息


删除房屋信息:
deleteHouse方法,界面,删除房屋信息


修改房屋信息:
alterHouse方法,界面,修改房屋信息


房屋列表:
listHouse方法,界面,清单


退出系统:
outMenu方法,退出系统

类中添加属性 

private boolean loop = true;
private char key = ' ';
private HouseService houseService = new HouseService(3);//设置列表空间
private int id = 0;

public void mainMenu() {do {System.out.println("\n=============房屋出租系统==============");System.out.println("\t\t\t" + "1.新 增 房 源");System.out.println("\t\t\t" + "2.查 找 房 屋");System.out.println("\t\t\t" + "3.删 除 房 屋");System.out.println("\t\t\t" + "4.修 改 房 屋 信 息");System.out.println("\t\t\t" + "5.房 屋 列 表");System.out.println("\t\t\t" + "6.退      出");System.out.println("请输出(1~6):");key = Utility.readChar();switch (key) {case '1':addHouse();break;case '2':seekHouse();break;case '3':deleteHouse();break;case '4':alterHosue();break;case '5':listHouse();break;case '6':outMenu();break;default:System.out.println("输入有误,返回菜单页面");break;}} while (loop);}

addHouse方法,界面,接收用户的输入信息,添加房屋信息

    /*** 添加信息方法*/public void addHouse() {boolean loop = houseService.addLoop();if (!loop) {System.out.println("无法添加,表格已满");}else {//编号自增,不能自取id++;System.out.println("房主:");String name = Utility.readString();System.out.println("电话(6位):");String phone = Utility.readString();System.out.println("地址:");String address = Utility.readString();System.out.println("月租:");int rent = Utility.readInt();System.out.println("状态(未出租/已出租):");String state = Utility.readString();House house = new House(id, name, phone, address, rent, state);houseService.add(house);System.out.println("添加成功");}}

seekHouse方法,界面,查找房屋信息

    /*** 查找房屋信息方法*/public void seekHouse() {System.out.println("请输入你要查找的房屋编号(输入-1返回菜单)");int seekId = Utility.readInt();if (seekId == -1) {System.out.println("成功返回菜单");} else {houseService.seekLoop(seekId);}}

deleteHouse方法,界面,删除房屋信息

    /*** 删除房屋信息方法*/public void deleteHouse() {System.out.println("请输入你要删除的房屋编号(输入-1返回菜单)");int deleteId = Utility.readInt();if (deleteId == -1) {System.out.println("成功返回菜单");} else {houseService.deleteLoop(deleteId);}}

alterHouse方法,界面,修改房屋信息

    /*** 修改房屋信息方法*/public void alterHosue() {System.out.println("请输入你要修改的房屋编号(输入-1返回菜单)");int alterId = Utility.readInt();if (alterId == -1) {System.out.println("成功返回菜单");} else {houseService.alterLoop(alterId);}}

listHouse方法,界面,清单

    /*** 列表清单方法*/public void listHouse() {House[] houses = houseService.list();if (houses[0] == null) {System.out.println("列表中无信息");}else {System.out.println("----------------------------------房屋列表清单----------------------------------");System.out.println("编号" + "\t\t\t" +"房主" + "\t\t\t" +"电话" + "\t\t\t" +"地址" + "\t\t\t" +"月租" + "\t\t\t" +"状态(未出租/已出租)");for (int i = 0; i < houses.length; i++) {if (houses[i] == null) {//如果为空,就不输出break;}System.out.println(houses[i]);}System.out.println("----------------------------------房屋列表完毕----------------------------------");}}

outMenu方法,退出系统

    /***  退出菜单方法*/public void outMenu() {System.out.println("确定要退出系统吗?");do {System.out.println("请输入:是(y/Y)/否(n/N)");char out = Utility.readChar();switch (out) {case 'y':case 'Y':System.out.println("成功退出系统");loop = false;break;case 'n':case 'N':System.out.println("成功返回菜单");break;default:System.out.println("输入有误,重新输入");break;}}while(loop);}

 2. HouseService.java 类[业务层]

新增房屋信息:
add(House[] newHouse)方法,把新的house对象添加进House数组中
addLoop()方法,判断能否继续添加房屋信息


查找房屋信息:
seek(int index)方法,输出编号对应的房屋信息
seekLoop(int seekId)方法,判断查询编号存不存在


删除房屋信息:
delete(int index)方法,删除编号对应的房屋信息
deleteLoop(int deleteId)方法,判断能否删除对应编号的房屋信息


修改房屋信息:
alter(int index)方法,修改编号对应的房屋信息
alterLoop(int alterId)方法,判断修改编号存不存在


房屋列表:
list方法,返回所有房屋信息

 a.新增房屋信息

    /*** 添加房屋信息* @param newHouse*/public void add(House newHouse) {houses[HouseSum] = newHouse;HouseSum++;}/*** 返回是否能添加房屋* @return*/public boolean addLoop() {if (HouseSum == houses.length) {return false;} else {return true;}}

b.查找房屋信息

    /*** 输出查询节点对应编号* @param index*/public void seek(int index) {if (index == -1) {System.out.println("查询编号不存在");} else {System.out.println("----------------------------------查询房屋信息----------------------------------");System.out.println("编号" + "\t\t\t" +"房主" + "\t\t\t" +"电话" + "\t\t\t" +"地址" + "\t\t\t" +"月租" + "\t\t\t" +"状态(未出租/已出租)");System.out.println(houses[index]);System.out.println("----------------------------------信息输出完毕----------------------------------");}}/*** 判断查询编号存不存在* @param seekId*/public void seekLoop(int seekId) {int index = -1;for (int i = 0; i < HouseSum; i++) {if (seekId == houses[i].getId()) {index = i;}}seek(index);}

c.删除房屋信息

    /*** 删除房屋信息* @param index*/public void delete(int index) {for (int i = index; i < HouseSum - 1; i++) {houses[i] = houses[i + 1];}houses[HouseSum - 1] = null;HouseSum--;}/*** 判断删除编号存不存在* @param deleteId*/public void deleteLoop(int deleteId) {int index = -1;//节点for (int i = 0; i < HouseSum; i++) {if (houses[i].getId() == deleteId) {index = i;}}if (index == -1) {System.out.println("未查找到对应编号的房屋信息,无法删除");} else {System.out.println("已查找到对应编号的房屋信息,确定删除?");System.out.println("请输入是(y/Y)否(n/N):");boolean doLoop = true;do {char loop = Utility.readChar();switch (loop) {case 'y':case 'Y':delete(index);System.out.println("成功删除对应编号的房屋信息");doLoop = false;break;case 'n':case 'N':System.out.println("成功返回菜单");doLoop = false;break;default:System.out.println("输出错误,重新输入");}}while (doLoop);}}

 d.修改房屋信息

    /*** 修改编号对应房屋信息* @param index*/public void alter(int index) {if (index == -1) {System.out.println("修改编号不存在");} else{System.out.println("输入你要修改的信息:");System.out.println("房主(" + houses[index].getName() + "):");String name = Utility.readString();System.out.println("电话(" + houses[index].getPhone() + "):");String phone = Utility.readString();System.out.println("地址(" + houses[index].getAddress() + "):");String address = Utility.readString();System.out.println("月租(" + houses[index].getRent() + "):");int rent = Utility.readInt();System.out.println("状态(" + houses[index].getState() + "):");String state = Utility.readString();houses[index] = new House(houses[index].getId(), name, phone, address, rent, state);}}/*** 判断修改编号存不存在* @param alterId*/public void alterLoop(int alterId) {int index = -1;for (int i = 0; i < HouseSum; i++) {if (alterId == houses[i].getId()) {index = i;}}alter(index);}

 e.房屋列表

    /*** 清单* @return*/public House[] list() {return houses;}

目录

运行展示:

1.新增房源

2.查找房源 

3.删除房屋 

4.修改房屋信息 

5.退出 

构思[分层模式]:

 包的创建:

工具类代码(Utility):

模型类代码(House) :

 程序入口(main)

主类实现代码:

1.HouseView.java 类[界面]

类中添加属性 

addHouse方法,界面,接收用户的输入信息,添加房屋信息

seekHouse方法,界面,查找房屋信息

deleteHouse方法,界面,删除房屋信息

alterHouse方法,界面,修改房屋信息

listHouse方法,界面,清单

outMenu方法,退出系统

 2. HouseService.java 类[业务层]

 a.新增房屋信息

b.查找房屋信息

c.删除房屋信息

 d.修改房屋信息

 e.房屋列表


这篇关于JAVA实现房屋出租系统(超详细)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/542080

相关文章

springboot控制bean的创建顺序

《springboot控制bean的创建顺序》本文主要介绍了spring-boot控制bean的创建顺序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1、order注解(不一定有效)2、dependsOn注解(有效)3、提前将bean注册为Bea

Java中的ConcurrentBitSet使用小结

《Java中的ConcurrentBitSet使用小结》本文主要介绍了Java中的ConcurrentBitSet使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、核心澄清:Java标准库无内置ConcurrentBitSet二、推荐方案:Eclipse

java中的Supplier接口解析

《java中的Supplier接口解析》Java8引入的Supplier接口是一个无参数函数式接口,通过get()方法延迟计算结果,它适用于按需生成场景,下面就来介绍一下如何使用,感兴趣的可以了解一下... 目录1. 接口定义与核心方法2. 典型使用场景场景1:延迟初始化(Lazy Initializati

Java中ScopeValue的使用小结

《Java中ScopeValue的使用小结》Java21引入的ScopedValue是一种作用域内共享不可变数据的预览API,本文就来详细介绍一下Java中ScopeValue的使用小结,感兴趣的可以... 目录一、Java ScopedValue(作用域值)详解1. 定义与背景2. 核心特性3. 使用方法

spring中Interceptor的使用小结

《spring中Interceptor的使用小结》SpringInterceptor是SpringMVC提供的一种机制,用于在请求处理的不同阶段插入自定义逻辑,通过实现HandlerIntercept... 目录一、Interceptor 的核心概念二、Interceptor 的创建与配置三、拦截器的执行顺

Python中Tkinter GUI编程详细教程

《Python中TkinterGUI编程详细教程》Tkinter作为Python编程语言中构建GUI的一个重要组件,其教程对于任何希望将Python应用到实际编程中的开发者来说都是宝贵的资源,这篇文... 目录前言1. Tkinter 简介2. 第一个 Tkinter 程序3. 窗口和基础组件3.1 创建窗

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi