目录

一.需求说明

二.出租设计(韩顺平老师总结)

三.实现代码的具体步骤(Utility工具类的代码发在最后)

1.House类

2.创建主菜单

3.出租列表

4.添加功能

5.删除功能

6.退出功能

7.查找功能

8.修改功能

五.总结

六.完整代码

1.HouserentAPP部分

2.House类

3.Houseservice部分

4.Houseview部分

5.utility部分


一.需求说明

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

主要包括:主菜单,新增房源,查找房屋信息,修改房屋信息,删除房屋信息

主菜单

添加房屋信息

查找房屋信息

删除房屋信息

修改房屋信息

房屋列表

退出系统

二.出租设计(韩顺平老师总结)

在这里总结一下韩老师的设计思路,首先我们需要一个与用户交互的界面(view),其次我们需要对创建一个解决crud的(service)方法,我们可以在view界面中可以调用service方法,我们还需要一个House类来储存房屋的属性,在这期间我们也需要工具类完成一下特定功能(比如输入,判断是否退出等功能),最后就需要一个App来调用该对象。

三.实现代码的具体步骤(Utility工具类的代码发在最后)

1.House类

package 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//编号 房主 电话 地址 月租 状态(出租或未出租)//这里重写了tostring方法,原本tostring方法的作用使返回拼接对象的地址值@Overridepublic String toString() {return  id +"\t\t" + name +"\t" + phone +"\t\t" + address +"\t" + rent +"\t" + state ;}
}

2.创建主菜单

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.print("请输入你的选择(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':this.listHouse();break;case '6':exit();loop = false;break;}}while (loop);

3.出租列表

//service界面
public House[] list(){return houses;}//view界面
public void listHouse(){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){break;}System.out.println(houses[i]);}System.out.println("========房屋列表显示完毕========");}

4.添加功能

//service界面
public boolean add(House newHouse){//判断是否还可以继续添加if (houseNums == houses.length){System.out.println("数组已满,不能再添加");return false;}houses[houseNums++] = newHouse;newHouse.setId(++idCounter);return true;}view界面
//addHouse()接收输入public void addHouse(){System.out.println("=========添加房屋=========");System.out.print("姓名:");String name = Utility.readString(8);//括号表示字符串长度System.out.print("电话:");String phone = Utility.readString(12);System.out.print("地址:");String address = Utility.readString(16);System.out.print("月租:");int rent = Utility.readInt();System.out.print("状态:");String state = Utility.readString(3);House house = new House(0, name, phone, address, rent, state);if (houseService.add(house)){System.out.println("添加房屋成功");}else {System.out.println("添加房屋失败");}}

5.删除功能

//service界面
//del方法 删除一个房屋信息public boolean del(int delId){int index = -1;for (int i = 0; i < houseNums; i++) {if (delId == houses[i].getId()){//判断房屋号是否存在index = i;}}if (index == -1){return false;}for (int i = index; i < houseNums - 1; i++) {houses[i] = houses[i + 1];}houses[--houseNums] = null;return true;}//view界面//编写delHouse()接受输入的id,调用Service的del方法public void delHouse() {System.out.println("=========删除房屋=========");System.out.println("请输入待删除房屋的编号(-1退出)");int delId = Utility.readInt();if (delId == -1) {System.out.println("==========放弃删除房屋信息=========");return;}char choice = Utility.readConfirmSelection();//可以将y转化为Y,判断是否放弃或是否退出if (choice == 'Y') {//确定删除if (houseService.del(delId)) {System.out.println("=========删除房屋信息成功=========");} else {System.out.println("=========房屋编号不存在,删除失败");}} else {System.out.println("=========放弃删除房屋信息=========");}}

6.退出功能

//view界面
//退出public void exit(){char c = Utility.readConfirmSelection();//大家可以ctrl + B调看这个方法的源码if (c == 'Y'){loop = false;}}

7.查找功能

    //service界面//findbyId方法 返回House对象或nullpublic House findById(int findId){for (int i = 0; i < houseNums; i++) {//判断房屋号是否存在if (findId == houses[i].getId()){return houses[i];}}return null;}//view界面
//根据id查找房屋信息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不存在=========");}}

8.修改功能

//service界面
//del方法 删除一个房屋信息public boolean del(int delId){int index = -1;for (int i = 0; i < houseNums; i++) {if (delId == houses[i].getId()){index = i;}}if (index == -1){return false;}for (int i = index; i < houseNums - 1; i++) {houses[i] = houses[i + 1];}houses[--houseNums] = null;return true;}//view界面
//修改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.print("姓名(" + 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 (rent != -1) {house.setRent(rent);}System.out.println("状态(" + house.getState() + "):");String state = Utility.readString(3, "");if (!"".equals(state)) {house.setState(state);}System.out.println("===========修改房屋信息成功=========");}

五.总结

最后总结一下,当我们面对这个比较复杂的房屋出租系统的问题时,我们可以采用分层模式来化繁为简,即我们就需要考虑两个问题,我们需要哪些类,类和类直接的调用关系是什么(笔者感觉这个项目中的类与类之间的关系比较复杂),具体框架可以看文章最开始的图片,这里我们再介绍一下utility工具类,这个也就是我们俗称的轮子,公司一般会提前封装好,我们直接调用就行了。完整代码发在下面啦(O ^ ~ ^ O)

六.完整代码

1.HouserentAPP部分

import houserent.View.HouseView;public class HouserentAPP {public static void main(String[] args) {new HouseView().MainMenu();System.out.println("==退出房屋出租系统==");}
}

2.House类

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

3.Houseservice部分

package Service;import domain.House;public class HouseService {private House[] houses;private int houseNums = 1;private int idCounter = 1;public HouseService(int size){houses = new House[size];houses[0] = new House(1,"jack","123","金水区",2000,"未出租");}//findbyId方法 返回House对象或nullpublic House findById(int findId){for (int i = 0; i < houseNums; i++) {if (findId == houses[i].getId()){return houses[i];}}return null;}//del方法 删除一个房屋信息public boolean del(int delId){int index = -1;for (int i = 0; i < houseNums; i++) {if (delId == houses[i].getId()){index = i;}}if (index == -1){return false;}for (int i = index; i < houseNums - 1; i++) {houses[i] = houses[i + 1];}houses[--houseNums] = null;return true;}public boolean add(House newHouse){//判断是否还可以继续添加if (houseNums == houses.length){System.out.println("数组已满,不能再添加");return false;}houses[houseNums++] = newHouse;newHouse.setId(++idCounter);return true;}public House[] list(){return houses;}}

4.Houseview部分

package houserent.View;import Service.HouseService;
import domain.House;
import utils.Utility;/*
1.显示界面
2.接受用户的输入
3.调用HOuseService完成对房屋信息的各种操作*/
public class HouseView {private boolean loop = true;private char key = ' ';//接受用户选择private HouseService houseService = new HouseService(10);//修改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.print("姓名(" + 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 (rent != -1) {house.setRent(rent);}System.out.println("状态(" + house.getState() + "):");String state = Utility.readString(3, "");if (!"".equals(state)) {house.setState(state);}System.out.println("===========修改房屋信息成功=========");}//根据id查找房屋信息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;}}//编写delHouse()接受输入的id,调用Service的del方法public 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("=========放弃删除房屋信息=========");}}//addHouse()接收输入public void addHouse(){System.out.println("=========添加房屋=========");System.out.print("姓名:");String name = Utility.readString(8);System.out.print("电话:");String phone = Utility.readString(12);System.out.print("地址:");String address = Utility.readString(16);System.out.print("月租:");int rent = Utility.readInt();System.out.print("状态:");String state = Utility.readString(3);House house = new House(0, name, phone, address, rent, state);if (houseService.add(house)){System.out.println("添加房屋成功");}else {System.out.println("添加房屋失败");}}public void listHouse(){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){break;}System.out.println(houses[i]);}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.print("请输入你的选择(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':this.listHouse();break;case '6':exit();loop = false;break;}}while (loop);}
}

5.utility部分

package utils;/**工具类的作用:处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。
*/import java.util.*;
/***/
public class Utility {//静态属性。。。private static Scanner scanner = new Scanner(System.in);/*** 功能:读取键盘输入的一个菜单选项,值:1——5的范围* @return 1——5*/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=>NString 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;}
}

JAVA 房屋出租系统(韩顺平)相关推荐

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

    房屋出租系统效果图示 房屋出租系统主类 package hspedu.houseRent;import java.util.Map;public class HouseRentSys {public ...

  2. 【韩顺平JAVA】房屋出租系统

    程序整体分析 示例代码 package houserent;import java.util.Scanner;public class HouseRentSYS {public static void ...

  3. B站韩顺平java学习笔记(八)-- 房屋出租系统(项目)章节

    目录 一 项目需求说明 1 项目界面 二  房屋租赁程序框架图 ​三  系统实现 1  完成House类 2  显示主菜单和完成退出软件的功能 3  完成显示房屋列表的功能 4  添加房屋信息的功能 ...

  4. java学习笔记(9) 第9章 Java项目-房屋出租系统

    Java项目-房屋出租系统 代码打包--百度网盘链接: 9.1 房屋出租系统-需求 9.1.1 项目需求说明 9.2 房屋出租系统-界面 9.3 房屋出租系统-设计(!!) 9.4 房屋出租系统-实现 ...

  5. Java笔记——11.房屋出租系统

    11.房屋出租系统 项目需求说明 实现基于文本界面的"房屋出租系统" 能够实现对房屋信息的添加.修改和删除(用数组实现),并且能够打印房屋明细表 主菜单页面: 新增房源页面: 查找 ...

  6. Java/java程序设计:房屋出租系统:要求实现:新增房源,查找房屋信息,修改房屋信息,删除房屋信息,显示所有房屋列表,退出房屋管理系统;

    Java/java程序设计:房屋出租系统: 一.前言: 一.1. 框架图 二.各类包下的代码实现: 1. 主文件(运行文件HouseApp.java) 2. 房屋类文件(House.java) 3. ...

  7. 基于javaweb的公寓房屋出租系统(java+ssm+jsp+easyui+echarts+mysql)

    基于javaweb的公寓房屋出租系统(java+ssm+jsp+easyui+echarts+mysql) 运行环境 Java≥8.MySQL≥5.7.Tomcat≥8 开发工具 eclipse/id ...

  8. Java 第一阶段建立编程思想 【房屋出租系统】

    Java 第一阶段建立编程思想 [房屋出租系统] 1. 项目需求说明 2. 项目界面 1. 主菜单 2. 新增房源 3. 查找房源 4. 删除房源 5. 修改房源 6. 房屋列表 7. 退出系统 3. ...

  9. 计算机毕业设计Java房屋出租(源码+系统+mysql数据库+lw文档)

    计算机毕业设计Java房屋出租(源码+系统+mysql数据库+lw文档) 计算机毕业设计Java房屋出租(源码+系统+mysql数据库+lw文档) 本源码技术栈: 项目架构:B/S架构 开发语言:Ja ...

  10. 基于java房屋出租计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署

    基于java房屋出租计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署 基于java房屋出租计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署 本源码技术栈: 项目架构:B/S ...

最新文章

  1. js之数据类型及类型转换
  2. 【转】MyBatis缓存机制
  3. 那些查了无数遍的问题
  4. 布隆过滤算法c语言,通过实例解析布隆过滤器工作原理及实例
  5. android网络框架
  6. 服务器网站打开慢跟什么有关系吗,浏览器访问网站的速度很慢,跟服务器的好差有关系吗?跟域名有关系吗?...
  7. 【德】博多·费舍尔 - 小狗钱钱2(2013年7月27日)
  8. 使用powershell执行在线脚本的具体示例
  9. Android学习笔记之如何将数据保存到SDCard
  10. 【申博攻略】三.北交计算机学院学术型博士“申请-考核”攻略(经验分享篇)
  11. ubuntu16.04安装caffe教程(仅cpu)
  12. FastStone Capture—截图功能
  13. DNS域传送漏洞(CVE-2015-5254)
  14. 夏令营501-511NOIP训练18
  15. Django写一个登录注册---001创建项目以及设计数据库
  16. 真实机下 ubuntu 18.04 安装anaconda+cuDNN+pytorch以及其版本选择(亲测非常实用)
  17. 2022-2028全球隔离式CAN收发器行业调研及趋势分析报告
  18. Python开发环境Spyder3安装方法
  19. 面向对象程序设计六大原则
  20. 聚焦计算机视觉前沿,蚂蚁技术研究院4篇论文入选顶会NeurIPS

热门文章

  1. 如何在CAD中输入带圈序号?
  2. 77GHz毫米波雷达快速chirp信号技术(三):测角原理
  3. 「雕爷学编程」Arduino动手做(32)——雨滴传感器模块
  4. 2022数学建模“五一杯”B题
  5. matlab 卷积神经网络 图像去噪 对抗样本修复
  6. C语言 三角函数用法
  7. fluent-bit 本地安装及配置
  8. web网页设计实例作业 ——校园文化(7页) html大学生网站开发实践作业
  9. PLSQL Developer破解注册码
  10. .NET Reflector 反射下载