09、房屋出租系统项目:
项目需求说明:
实现基于文本界面的《房屋出租软件》
能够实现对房屋信息的添加、修改和删除(用数组实现),并能打印房屋明细表。
项目界面:
- 主菜单:
- ----房屋出租系统—
- 新增房屋
- 查找房屋
- 删除房屋
- 修改房屋信息
- 房屋列表
- 退出
- 子功能界面——新增房屋:
- ----添加房屋----
- 姓名:
- 电话:
- 地址:
- 月租:
- 状态(未出租/已出租)
- ----添加完成----
- 子功能界面——查找房源:
- ----查找房屋—
- 请输入你要查找的id:
- 显示该id对应的房屋信息;
- 子功能界面——删除房源:
- ----删除房屋----;
- 请选择删除房屋编号(-1退出):
- 确认是否删除(Y/N):请小心选择;
- 请输入你的选择(Y/N);
- ----删除完成—。
- 子功能界面——修改房源(若不希望修改某个形象,直接回车):
- ----修改客户----
- 请选择修改房屋编号(-1退出):
- 姓名(当前姓名):
- 电话(当前电话):
- 地址(当前地址):
- 月租(当前月租):
- 状态(当前状态):
- ----修改完成—
- 子功能界面——房屋列表:
- —房屋列表—;
- 房屋
- —房屋列表完成
- 子功能界面——退出系统:
- 请输入你的选择(Y/N)——一直检验到为y或n才行
- 你已经退出了程序。
设计:
项目设计-程序框架图(分层设计(后面还会有很多模式) -》当软件比较复杂,需要模式管理)。
- 明确系统有哪些类(文件),现在只有三层,以后学完数据库之类的层会增多;
- 明确类与类之间的调用关系
实现:
首先是对工具类的一些解释与说明:
工具类(Utility)是用于对用户收入处理的类,一般是已经写好的,直接调用即可(就像我们平时用的汉字,我们直接用它造句就好了,没有必要每次造句前把汉字再造一遍)
这边附上韩顺平老师的Utility类(记得去b站给韩老师的视频点赞投币收藏呀!讲的真的很细致!)
package houserent.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;
}
}
好的,那么我们现在已经完成了一些基本工具的实现,下一步应该做什么呢。我们可以结合一下我们平时用软件第一眼看到的是什么?是界面!所以我们先实现界面功能,并完成界面对功能选择的简单实现。
界面的实现:
我们来思考一下界面的意义是什么?我个人对界面层的理解就是,软件和用户的所有交互,都应该在这一层里实现,所以我们需要完成如下的功能:
- 显示界面;
- 接收用户输入(交互);
- 调用HouseService.java完成对房屋信息的各种操作。
package houserent.view;
import houserent.utils.Utility;
public class HouseView {
private boolean loop = true;
private char key = ' ';
public void mainmenu(){
do{
System.out.println("\n\n----------房屋出租系统菜单----------");
System.out.println("\t\t\t1 新 增 房 源");
System.out.println("\t\t\t2 查 找 房 屋");
System.out.println("\t\t\t3 删 除 房 屋");
System.out.println("\t\t\t4 修 改 房 屋 信 息");
System.out.println("\t\t\t5 房 屋 列 表");
System.out.println("\t\t\t6 退 出");
System.out.println("请选择(1-6):");
key = Utility.readChar();
switch(key){
case '1':
System.out.println("----------新 增 房 源----------");
break;
case '2':
System.out.println("----------删 除 房 源----------");
break;
case '3':
System.out.println("----------删 除 房 屋----------");
break;
case '4':
System.out.println("----------修 改 房 屋 信 息----------");
break;
case '5':
System.out.println("----------房 屋 列 表----------");
break;
case '6':
System.out.println("----------退 出----------");
loop = false;
break;
}
}while(loop);
}
}
这样,我们就初步完成了对界面的实现,但是内部的具体功能还需要完善。
在App中调用一下,看看能否正常显示:
package houserent;
import houserent.view.HouseView;
public class HouseRentApp {
public static void main(String[] args) {
new HouseView().mainmenu();
System.out.println("房屋租赁系统已退出...");
}
}
House的实现:
package houserent.domain;
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 getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
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;
}
@Override
public String toString() {
return id +
"\t\t" + name +
"\t" + phone +
"\t\t" + address +
"\t" + rent +
"\t" + state;
}
}
实现房屋列表功能:
需要完成HouseService中的list方法,和HouseView中的houseList方法(该方法调用HouseService中的list方法)。
private HouseService houseService = new HouseService(2);
// 5.房屋列表功能
public void houseList(){
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].toString());
}
System.out.println("=========显示完毕=========");
}
HouseView中:
public HouseService(int size) {
houses = new House[size];
houses[0] =new House(1,"天佑斋", "189", "郫都区", 1200, "已出租");
}
// 房屋列表
public House[] list(){
return houses;
}
完成后,在App中尝试运行一下,没有问题,继续编写。
需要注意的是不能将new House[size]写在 list()函数中,不然的话,每次调用list()都会创建一个新的House数组。
完成新增房源:
需要完成HouseService中的add方法,和HouseView中的houseAdd方法(该方法调用HouseService中的add方法)。我们来思考一下,首先我们知道界面是用来和用户交互的,所以houseAdd方法应该接收用户的输入,然后将参数传递给HouseService中的add方法,同时,用户是不会输入房屋编号的,需要引入一个系统自增的id。
// 1.新增房屋功能:
public void houseAdd(){
System.out.println("----------新 增 房 源----------");
System.out.print("姓名:");
String name = Utility.readString(6);
System.out.print("电话:");
String phone = Utility.readString(11);
System.out.print("地址:");
String address = Utility.readString(11);
System.out.print("月租:");
int rent = Utility.readInt();
System.out.print("状态:");
String type = Utility.readString(3);
House newHouse = new House(0, name, phone, address, rent, type);
if(houseService.add(newHouse)){
System.out.println("=========添加成功========");
}else{
System.out.println("=========添加失败========");
}
}
// 添加
public boolean add(House newhouse){
// 但由于我们现在的数组没有引入动态扩容机制,所以不能直接将其插入
// 需要一个属性去记录当前数组中有几个元素(不是数组的大小);
if(houseNum == houses.length){
return false;
}else{
houses[houseNum++] = newhouse; // 让个数自增;
newhouse.setId(++id);
return true;
}
}
删除房屋:
// 3.删除房屋功能:
public void houseDel(){
System.out.println("----------删 除 房 源----------");
System.out.print("请输入你要删除的房屋id:");
int delId = Utility.readInt();
if(houseService.del(delId)){
System.out.println("----------删 除 成 功----------");
}else{
System.out.println("----------删 除 失 败----------");
}
}
// 删除
public boolean del(int delId){
int index = -1;
for(int i=0;i<houseNum;i++){
if(houses[i].getId() == delId){
index = i;
break;
}
}
if(index != -1){
for(;index < houseNum-1;index++){
houses[index] = houses[index++];
}
houses[--houseNum] = null;
return true;
}else{
return false;
}
}
查找房屋:
这边find的返回类型最好是House,因为在修改的时候需要用上。
// 2.查找房屋功能:
public void houseFind(){
System.out.println("----------查 找 房 屋----------");
System.out.println("请输入你要查找的房屋id:");
int findId = Utility.readInt();
houseService.find(findId);
}
// 查找:
public House find(int findId){
int index = -1;
for (int i = 0; i < houseNum; i++) {
if(houses[i].getId() == findId){
index = i;
break;
}
}
if(index != -1){
System.out.println(houses[index]);
System.out.println("查找成功。");
return houses[index];
}else{
System.out.println("没有找到。");
return null;
}
}
修改房屋:
// 4.修改(更新)房屋功能:
public void houseUpdate(){
System.out.println("----------修 改 房 屋 信 息----------");
System.out.print("请选择待修改房屋编号(-1退出):");
int upId = Utility.readInt();
if(upId == -1){
System.out.println("退出成功。");
return;
}
House updateHouse = houseService.find(upId);
if(updateHouse == null){
System.out.println("id为" + upId +"的房屋不存在。");
return;
}
System.out.print("姓名(" + updateHouse.getName() + "):");
String name = Utility.readString(6,"");
if(!"".equals(name)){
updateHouse.setName(name);
}
System.out.print("电话(" + updateHouse.getPhone() + "):");
String phone = Utility.readString(11,"");
if(!"".equals(phone)){
updateHouse.setPhone(phone);
}
System.out.print("地址(" + updateHouse.getAddress() + "):");
String address = Utility.readString(11,"");
if(!"".equals(address)){
updateHouse.setAddress(address);
}
System.out.print("租金(" + updateHouse.getRent() + "):");
int newRent = Utility.readInt();
if(newRent != 0){
updateHouse.setRent(newRent);
}
System.out.print("状态(" + updateHouse.getState() + "):");
String state = Utility.readString(3,"");
if(!"".equals(state)){
updateHouse.setState(state);
}
}
退出:
// 6.退出功能:
public void exit(){
System.out.println("----------退 出----------");
char choice = Utility.readConfirmSelection();
if(choice == 'Y'){
loop = false;
System.out.println("----------退 出 成 功----------");
}
}
完整代码:
domain层:
package houserent.domain;
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 getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
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;
}
@Override
public String toString() {
return id +
"\t\t" + name +
"\t" + phone +
"\t\t" + address +
"\t" + rent +
"\t" + state;
}
}
界面层:
完成接收用户的输入,生成对应的对象,传递给Service。
package houserent.view;
import houserent.domain.House;
import houserent.service.HouseService;
import houserent.utils.Utility;
public class HouseView {
private boolean loop = true;
private char key = ' ';
private HouseService houseService = new HouseService(2);
public void mainmenu(){
do{
System.out.println("\n\n----------房屋出租系统菜单----------");
System.out.println("\t\t\t1 新 增 房 源");
System.out.println("\t\t\t2 查 找 房 屋");
System.out.println("\t\t\t3 删 除 房 屋");
System.out.println("\t\t\t4 修 改 房 屋 信 息");
System.out.println("\t\t\t5 房 屋 列 表");
System.out.println("\t\t\t6 退 出");
System.out.println("请选择(1-6):");
key = Utility.readChar();
switch(key){
case '1':
houseAdd();
break;
case '2':
houseFind();
break;
case '3':
houseDel();
break;
case '4':
houseUpdate();
break;
case '5':
houseList();
break;
case '6':
exit();
break;
}
}while(loop);
}
// 1.新增房屋功能:
public void houseAdd(){
System.out.println("----------新 增 房 源----------");
System.out.print("姓名:");
String name = Utility.readString(6);
System.out.print("电话:");
String phone = Utility.readString(11);
System.out.print("地址:");
String address = Utility.readString(11);
System.out.print("月租:");
int rent = Utility.readInt();
System.out.print("状态:");
String type = Utility.readString(3);
House newHouse = new House(0, name, phone, address, rent, type);
if(houseService.add(newHouse)){
System.out.println("=========添加成功========");
}else{
System.out.println("=========添加失败========");
}
}
// 2.查找房屋功能:
public void houseFind(){
System.out.println("----------查 找 房 屋----------");
System.out.println("请输入你要查找的房屋id:");
int findId = Utility.readInt();
houseService.find(findId);
}
// 3.删除房屋功能:
public void houseDel(){
System.out.println("----------删 除 房 源----------");
System.out.print("请输入你要删除的房屋id:");
int delId = Utility.readInt();
if(houseService.del(delId)){
System.out.println("----------删 除 成 功----------");
}else{
System.out.println("----------删 除 失 败----------");
}
}
// 4.修改(更新)房屋功能:
public void houseUpdate(){
System.out.println("----------修 改 房 屋 信 息----------");
System.out.print("请选择待修改房屋编号(-1退出):");
int upId = Utility.readInt();
if(upId == -1){
System.out.println("退出成功。");
return;
}
House updateHouse = houseService.find(upId);
if(updateHouse == null){
System.out.println("id为" + upId +"的房屋不存在。");
return;
}
System.out.print("姓名(" + updateHouse.getName() + "):");
String name = Utility.readString(6,"");
if(!"".equals(name)){
updateHouse.setName(name);
}
System.out.print("电话(" + updateHouse.getPhone() + "):");
String phone = Utility.readString(11,"");
if(!"".equals(phone)){
updateHouse.setPhone(phone);
}
System.out.print("地址(" + updateHouse.getAddress() + "):");
String address = Utility.readString(11,"");
if(!"".equals(address)){
updateHouse.setAddress(address);
}
System.out.print("租金(" + updateHouse.getRent() + "):");
int newRent = Utility.readInt();
if(newRent != 0){
updateHouse.setRent(newRent);
}
System.out.print("状态(" + updateHouse.getState() + "):");
String state = Utility.readString(3,"");
if(!"".equals(state)){
updateHouse.setState(state);
}
}
// 5.房屋列表功能
public void houseList(){
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].toString());
}
System.out.println("=========显示完毕=========");
}
// 6.退出功能:
public void exit(){
System.out.println("----------退 出----------");
char choice = Utility.readConfirmSelection();
if(choice == 'Y'){
loop = false;
System.out.println("----------退 出 成 功----------");
}
}
}
业务层:
接收来自界面层的指令,完成对数据(domain)的一些操作,如增删改查(crud)。
package houserent.service;
import houserent.domain.House;
public class HouseService {
private House[] houses; // 定义一个House类型的列表,为houses
private int id = 1; // 这里设置为1是因为我们在初始化列表的时候暂时添加了一个值。
private int houseNum = 1;// 也是因为数组中默认有有一个值
public HouseService(int size) {
houses = new House[size];
houses[0] =new House(1,"天佑斋", "189", "郫都区", 1200, "已出租");
}
// 房屋列表
public House[] list(){
return houses;
}
// 添加
public boolean add(House newhouse){
// 但由于我们现在的数组没有引入动态扩容机制,所以不能直接将其插入
// 需要一个属性去记录当前数组中有几个元素(不是数组的大小);
if(houseNum == houses.length){
return false;
}else{
houses[houseNum++] = newhouse; // 让个数自增;
newhouse.setId(++id);
return true;
}
}
// 查找:
public House find(int findId){
int index = -1;
for (int i = 0; i < houseNum; i++) {
if(houses[i].getId() == findId){
index = i;
break;
}
}
if(index != -1){
System.out.println(houses[index]);
System.out.println("查找成功。");
return houses[index];
}else{
System.out.println("没有找到。");
return null;
}
}
// 删除
public boolean del(int delId){
int index = -1;
for(int i=0;i<houseNum;i++){
if(houses[i].getId() == delId){
index = i;
break;
}
}
if(index != -1){
for(;index < houseNum-1;index++){
houses[index] = houses[index++];
}
houses[--houseNum] = null;
return true;
}else{
return false;
}
}
}
初次之外还有工具类也在该层:
package houserent.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;
}
}
App:
我们测试代码的地方:
package houserent;
import houserent.view.HouseView;
public class HouseRentApp {
public static void main(String[] args) {
new HouseView().mainmenu();
System.out.println("房屋租赁系统已退出...");
}
}