在写房屋出租系统之前,我们先回顾一下零钱通项目的整体思路:
将所有代码写在一个main里 -->将不同的业务放到不同的方法里,main调用方法
一、项目需求说明
实现基于文本界面的房屋出租软件
能够实现对房屋信息的添加、修改和删除(用数组存储房屋信息),并能够打印房屋明细表
二、项目界面
主菜单
1. 新增房源
2. 查找房屋
3. 删除房屋
4. 修改房屋信息
5. 房屋列表
6. 退出系统
三、项目设计
(一)、程序框架图(分层模式)
1. 分层模式的好处?
答:方便让系统方便管理和项目结构更加清晰
2. 为什么用分层模式?
答:软件比较复杂,需求比较多,所以需要模式管理
3. 程序框架图怎么写?
答:系统有哪些类【文件】、明确类与类的调用关系
(二) 、房屋出租工具类
1. 工具类的作用?
在实际开发中,公司都会提供相应的工具类和开发库,可以提高开发效率,程序员也需要能够看懂别人写的代码,并能够正确的调用
类比:相当于现实中的斧头、扳手之类的工具,你开啤酒瓶可能会桌子、用筷子、用牙齿,但如果我们要有专业的工具-启瓶器,是不是就更快更省力
工具的出现都是为了能够提升效率
2.已有Scanner输入,为什么还要单独用工具类来接受输入呢?
此项目需要输入的地方很多,将输入封装到一个类中方便使用;Scanner输入不带有数据判定,此工具类可以判定输入,比如:可以传入参数规定输入长度
package com.wpy.project.renthouse.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=>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;
}
}
(三) 、房屋出租House类
1. 怎么判断House类的属性
根据项目的需求,此项目查看房屋列表需求图可发现需要:编号、房主、电话、地址、月租、状态(未出租/已出租)
package com.wpy.project.renthouse.domain;
public class House {
private int id;
// 姓名
private String name;
// 電話
private int phone;
// 地址
private String location;
// 月租
private int rent;
// 狀態(未出租/已出租)
private String status;
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 int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public House() {
}
public House(int id, String name, int phone, String location, int rent, String status) {
this.id = id;
this.name = name;
this.phone = phone;
this.location = location;
this.rent = rent;
this.status = status;
}
@Override
public String toString() {
return
id +"\t\t"+ name + "\t\t" +
phone +"\t\t" + location + "\t" +
rent +"\t" +
status + '\t'
;
}
}
(四)、房屋出租主菜单
1. 项目很大,项目功能的实现从哪里着手?
从用户角度出发,用户首先看到的是项目的主菜单
2.实现功能三部曲
明确完成功能-思路分析-代码实现
3. 房屋出租主菜单代码实现分析
编写一个mainMenu方法,可以显示主菜单