思维导图:业务逻辑
类的存放:
工具类 Utility
java
package study.houserent.util;
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类
java
package study.houserent.domain;
/*
house类的对象代表一个信息
*/
public class House {
private int id;
private String name;
private String iphone;
private int rent;
//ctrl +a
private String 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 getIphone() {
return iphone;
}
public void setIphone(String iphone) {
this.iphone = iphone;
}
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;
}
public House(int id, String name, String iphone, int rent, String state) {
this.id = id;
this.name = name;
this.iphone = iphone;
this.rent = rent;
this.state = state;
}
@Override
public String toString() {
return id + "\t"+ name + "\t" + iphone + "\t" + rent + "\t"+ state ;
}
}
HouseSerive类
java
package study.houserent.serive;
import study.houserent.domain.House;
import study.houserent.atil.Utility;
public class HouseService {
private House[] houses;
private int houseNums=1;
private int idCounter=1;
public boolean add(House newhouse){
if(houseNums==houses.length){
System.out.println("已满");
return false;
}
houses[houseNums]=newhouse;
houseNums++;
idCounter++;
newhouse.setId(idCounter);
// newhouse.setId(++idCounter);
return true;
}
public House changeID(int change){
for(int i=0;i<houseNums;i++){
if(houses[i].getId()==change){
System.out.print("姓名:");
String name= Utility.readString(8);
System.out.print("电话:");
String phone=Utility.readString(12);
System.out.print("月租: ");
int rent=Utility.readInt();
System.out.println("状态:");
String state =Utility.readString(3);
houses[i].setName(name);
houses[i].setIphone(phone);
houses[i].setRent(rent);
houses[i].setState(state);
return houses[i];
}
}
return null;
}
public House findById(int findid){
for(int i=0;i<houseNums;i++){
if(findid==houses[i].getId()){
return houses[i];
}
}
return null;
}
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;
}
else{
for(int i=index;i<houseNums-1;i++){
houses[i]=houses[i+1];
}
houses[--houseNums]=null;
return true;
}
}
public HouseService(int size){
houses =new House[size];
houses[0]=new House(1,"A","100",2000,"未出租");
}
public House[] list() {
return houses;
}
}
HouseView类
java
package study.houserent.views;
import study.houserent.domain.House;
import study.houserent.serive.HouseService;
import study.houserent.atil.Utility;
public class HouseView {
private boolean loop=true;
private char key=' ';
public HouseService houseService=new HouseService(10);
public void exit(){
char c=Utility.readConfirmSelection();
if(c=='Y'){
loop=false;
}
}
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.println("状态:");
String state =Utility.readString(3);
House newhouse = new House(0,name, phone, rent, state);
if(houseService.add(newhouse)){
System.out.println("==========添加房屋=========");
}
else{
System.out.println("========添加房屋失败=======");
}
}
public void findHouse(){
System.out.println("=========查找房屋信息=======");
System.out.println("输入查找的房间号:");
int findid=Utility.readInt();
House house=houseService.findById(findid);
System.out.println((house==null)?"=======查找失败=====":house);
}
public void listHouses(){
System.out.println("============房屋出租系统=======");
System.out.println("编号 房主 电话 地址 状态");
House[] houses=houseService.list();
for(Object q:houses){
if(q==null);
else System.out.println(q);
}
System.out.println("房屋列表显示完毕");
}
public void changeHouses(){
System.out.println("=====修改房屋信息========");
System.out.println("修改的房间号:");
int change=Utility.readInt();
House house=houseService.changeID(change);
System.out.println((house==null)?"=======不存在=====":house);
}
public void delHouse(){
System.out.println("================删除房屋============");
System.out.println("请输入待删除房屋的编号:");
int deTid=Utility.readInt();
if(deTid==-1){
System.out.println("放弃删除房屋");
return;
}
char choice=Utility.readConfirmSelection();
if(choice=='Y'){
//在调用方法
if(houseService.del(deTid)){
System.out.println("===========删除房屋信息成功=========");
}
else{
System.out.println("=============删除失败===========");
}
}
else{
System.out.println("放弃删除房屋");
}
}
public void mainMenu (){
do{
System.out.println("=============房屋租赁系统===========");
System.out.println("\t\t\t[新 增 房 源]");
System.out.println("\t\t\t[查 找 房 间]");
System.out.println("\t\t\t[删 除 房 屋 信 息]");
System.out.println("\t\t\t[修 改 房 屋 信 息]");
System.out.println("\t\t\t[房 屋 列 表]");
System.out.println("\t\t\t 其输入你得选择【1-6】");
key= Utility.readChar();
switch( key){
case '1':
System.out.println("新 增");
addhouse();
listHouses();
break;
case '2':
System.out.println("查 找 房 间");
findHouse();
listHouses();
break;
case '3':
System.out.println("删除房屋信息");
delHouse();
listHouses();
break;
case '4':
System.out.println("修改房屋信息");
changeHouses();
listHouses();
break;
case '5':
System.out.println("房屋列表");
listHouses();
break;
case '6':
exit();
System.out.println("退出");
listHouses();
}
}while(loop);
}
}
HouserentApp类 运行入口
java
package study.houserent;
import study.houserent.domain.House;
import study.houserent.views.HouseView;
public class houseRentApp {
public static void main(String[] args) {
//创造程序入口
new HouseView().mainMenu();
System.out.println("=========你退出房屋出租系统===============");
}
}