房屋出租系统的主类
package com.java.HouseRentApp;
import com.java.HouseRentApp.houseService;
class house {
private int id; //编号
private String name; //姓名
private String phone; //年龄
private String address; //地址
private int rent; //房租
private String condition; //状态(已出租,未出租)
public house()
{}
public house(int id,String name,String phone,String address,int rent,String condition)
{
this.id = id;
this.name = name;
this.phone = phone;
this.address = address;
this.rent = rent;
this.condition = condition;
}
public void setId(int id)
{
this.id = id;
}
public int getId()
{
return this.id;
}
public String getName()
{
return this.name;
}
public String getPhone()
{
return this.phone;
}
public String getAddress()
{
return this.address;
}
public int getRent()
{
return this.rent;
}
public String getCondition()
{
return this.condition;
}
public void changeInfo(String name,String phone) //更换住户信息
{
this.name = name;
this.phone = phone;
}
public void chanRent(int rent)
{
this.rent = rent;
}
public void chanCondition(String condition)
{
this.condition = condition;
}
public String toString()
{
return " " + this.id + "tt" + this.name
+ "tt" + this.phone
+ "tt" + this.address
+ "tt" + this.rent
+ "tt" + this.condition;
}
}
class menu {
private boolean loop = true;
private char key = ' ';
private houseService houses = new houseService(4); //设置数组的大小
public void mainMenu() {
do {
System.out.println();
System.out.println(" =============房屋出租系统=============");
System.out.println("|ttt1 新 增 房 源ttt|");
System.out.println("|ttt2 查 找 房 屋ttt|");
System.out.println("|ttt3 删 除 房 屋 信 息tt|");
System.out.println("|ttt4 修 改 房 屋 信 息tt|");
System.out.println("|ttt5 房 屋 信 息ttt|");
System.out.println("|ttt6 退 出ttt|");
System.out.println(" ===================================");
System.out.println();
System.out.println("请输入你的选择(1-6): ");
key = Utilis.readChar();
switch (key) {
case '1':
System.out.println("ttt [ 当前业务 ]");
System.out.println("ttt1 新 增 房 源");
addHouse();
break;
case '2':
System.out.println("ttt [ 当前业务 ]");
System.out.println("ttt2 查 找 房 屋");
findHouse();
break;
case '3':
System.out.println("ttt [ 当前业务 ]");
System.out.println("ttt3 删 除 房 屋 信 息");
deleHouse();
break;
case '4':
System.out.println("ttt [ 当前业务 ]");
System.out.println("ttt4 修 改 房 屋 信 息");
changeInformationHouse();
break;
case '5':
System.out.println("ttt [ 当前业务 ]");
System.out.println("ttt5 房 屋 信 息");
listHouse();
break;
case '6':
System.out.println("ttt6 退 出");
char c = Utilis.readConfirmSelection();
if (c == 'Y') {
loop = false;
}
break;
}
} while (loop);
}
//编写listHouse()方法输出房屋信息
public void listHouse() {
System.out.println(" =============房屋出租系统=============");
System.out.println(" 编号t房主tt电话tt地址ttt月租tt状态(已出租/未" + "未出租)");
house[] houses1 = houses.list();
for (int i = 0; i < houses1.length; i++) {
if (houses1[i] != null) {
System.out.println(houses1[i]);
}
}
System.out.println(" ===========房屋列表显示完毕===========");
}
//编写findHouse()方法查找房屋 分为编号查找、名字查找
public void findHouse() {
System.out.println(" =============查找房屋=============");
System.out.println("请你选择 1名字查找 2房屋编号查找");
int key = Utilis.readInt();
while(key != 1 && key !=2)
{
System.out.println("你的输入有误!!");
System.out.println("请你重新输入!!");
key = Utilis.readInt();
}
switch (key) {
case 1 -> findHouseOfName();
case 2 -> findHouseOfNums();
}
}
//按照编号查找房屋
public void findHouseOfNums()
{
System.out.print("请你输入想要查找的房屋的编号:");
int houseId = Utilis.readInt();
if (houses.findHouseNum(houseId)) {
System.out.println(" =============查找房屋成功=============");
System.out.println(houses.findHouseNumReturn(houseId));
} else {
System.out.println(" =============查找失败,编号有误=============");
}
}
//按照姓名查找房屋
public void findHouseOfName()
{
System.out.println("请你输入想要查找的房屋主人的姓名");
String name = Utilis.readString(8);
if(houses.fineHouseName(name)!=null)
{
System.out.println("现在为你输出信息!!!");
System.out.println(" 编号t房主tt电话tt地址ttt月租tt状态(已出租/未" + "未出租)");
System.out.println(houses.fineHouseName(name));
}
else
{
System.out.println(" =============查找失败,姓名有误=============");
}
}
//编写方法修改房屋信息
public void changeInformationHouse() {
System.out.println(" =============修改房屋=============");
System.out.print("请你输入修改房屋的编号:");
int nums = Utilis.readInt();
if (houses.findHouseNum(nums)) {
System.out.println("请你选择修改 1房客姓名以及联系方式 2月租信息 3出租与否");
int test = Utilis.readInt();
while (test != 1 && test != 2 && test != 3) //判断房客输入的信息是否合法
{
System.out.println("输入错误!!");
System.out.println("请你正确输入!!");
test = Utilis.readInt();
}
if (test == 1)
{
System.out.print("[ 姓名 ]: ");
String name = Utilis.readString(8);
System.out.print("[ 电话 ]: ");
String phone = Utilis.readString(8);
houses.changeNamePhone(name,phone,nums);
}
else if(test == 2)
{
System.out.print("[ 月租 ]: ");
int rent = Utilis.readInt();
houses.changeRent(rent,nums);
}
else
{
System.out.println("出租与否:");
String condition = Utilis.readString(8);
houses.changeCondition(condition,nums);
}
}
else
{
System.out.println(" =============编号有误,查找失败=============");
}
}
//编写addHouse()方法接受输入创建house对象
public void addHouse()
{
System.out.println(" =============添加房屋=============");
System.out.println("[ 现在进行信息录入.... ]");
System.out.println("请你输入房客的姓名,联系方式,月租以及房屋出租与否");
System.out.print("[ 姓名 ]: ");
String n1 = Utilis.readString(8);
System.out.print("[ 电话 ]: ");
String phone1 = Utilis.readString(8);
System.out.print("[ 地址 ]: ");
String address1 = Utilis.readString(8);
System.out.print("[ 月租 ]: ");
int rent = Utilis.readInt();
System.out.print("[ 状态 ]: ");
String condition = Utilis.readString(3);
//注意id是系统分配的,用户不能输入,
house information1 = new house(0,n1,phone1,address1,rent,condition);
if(houses.add(information1))
{
System.out.println(" =============添加房屋成功=============");
}
else
{
System.out.println(" =============房屋添加失败=============");
}
}
//编写delHouse() 接收输入的id,调用houseService的del方法
public void deleHouse()
{
System.out.print("请输入待删除房屋的编号:(-1退出)");
int delId = Utilis.readInt();
if(delId==-1)
{
System.out.println(" =============放弃删除房屋信息=============");
return;
}
//工具类的方法本身就有循环
char choice = Utilis.readConfirmSelection();
if(choice=='Y') {
if (houses.del(delId)) {
System.out.println(" =============房屋删除成功=============");
}
else
{
System.out.println(" =============编号有误,删除失败=============");
}
}
}
}
class houseService {
private house[] houses; //保存house对象
private int houseNumber=1; //记录当前有多少个房屋信息
private int idCount=1; //记录id的自增值
//可以考虑加入一个数组扩容机制
public houseService(int size)
{
houses = new house[size];
houses[0] = new house(1,"non","116","海淀区",5000,"已出租");
}
//add方法,添加新对象,返回Boolean型
public boolean add(house newHouse)
{
//判断是否可以继续添加
if(houseNumber == houses.length)
{
System.out.println("[ 空间已被占满,不能在添加了..... ]");
return false;
}
//把newHouse对象加入到数组中
houses[houseNumber++] = newHouse;
//需要设置一个id自增长的程序
newHouse.setId(++idCount);
return true;
}
public house[] list() //设置程序输出房屋信息
{
return this.houses;
}
public boolean del(int id) //设置程序返回房屋信息是否存在,存在返回true确认可以删除
{
int index =-1;
//要找到要删除的房屋对应的下标
for(int i=0;i
房屋出租系统的工具类
package com.java.HouseRentApp;
import java.util.*;
public class Utilis {
//静态属性。。。
public static Scanner scanner = new Scanner(System.in);
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;
}
public static char readChar() {
String str = readKeyBoard(1, false);//就是一个字符
return str.charAt(0);
}
public static char readChar(char defaultValue) {
String str = readKeyBoard(1, true);//要么是空字符串,要么是一个字符
return (str.length() == 0) ? defaultValue : str.charAt(0);
}
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;
}
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;
}
public static String readString(int limit) {
return readKeyBoard(limit, false);
}
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str;
}
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;
}
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;
}
public static String scanner() {
return scanner.next();
}
}



