![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/91782c7bce274522de38ec2cebe0c2b6.png)
House
package homework.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 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 double getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Override
public String toString() {
return "House{" +
"id=" + id +
", name='" + name + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
", rent=" + rent +
", state='" + state + '\'' +
'}';
}
}
House Service
package homework.service;
import homework.domain.House;
import homework.utils.Utility;
public class HouseService {
private House[] houses;
private int houseNum = 1;
private int newId = 1;
public HouseService(int size){
houses = new House[size];
houses[0] = new House(1, "jack", "112",
"上海", 2000, "未出租");
}
public House[] list(){
return houses;
}
public boolean add(House newHouse){
if(houseNum >= houses.length){
System.out.println("数组已满, 无法继续添加");
return false;
}
houses[houseNum++] = newHouse;
newId++;
newHouse.setId(newId);
return true;
}
public boolean del(int delId){
int f = -1;
for(int i = 0; i < houseNum; i++){
if(delId == houses[i].getId()){
f = i;
}
}
if(f == -1){
System.out.println("未找到房屋,删除失败");
}
else{
for(int i = f; i < houseNum-1; i++){
houses[i] = houses[i + 1];
}
houses[houseNum - 1] = null;
houseNum--;
return true;
}
return false;
}
public boolean find(int findId){
if(findId > houseNum){
return false;
}
else{
System.out.println(houses[findId-1]);
return true;
}
}
public boolean update(int findId){
if(findId > houseNum){
return false;
}
else{
System.out.println("输按顺序入修改后的信息:");
System.out.println("房主 电话 地址 月租 状态(回车分开)");
houses[findId - 1].setName(Utility.readString(10));
houses[findId - 1].setPhone(Utility.readString(10));
houses[findId - 1].setAddress(Utility.readString(10));
houses[findId - 1].setRent(Utility.readInt());
houses[findId - 1].setState(Utility.readString(10));
return true;
}
}
}
Utility 工具包
package homework.utils;
import java.util.*;
public class Utility {
private static Scanner scanner = new Scanner(System.in);
public static char readMenuSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
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);
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 (; ; ) {
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 = "";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.length() == 0) {
if (blankReturn) return line;
else continue;
}
if (line.length() < 1 || line.length() > limit) {
System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
House View
package homework.view;
import homework.domain.House;
import homework.service.HouseService;
import homework.utils.Utility;
public class HouseView {
private boolean loop = true;
private char key = ' ';
private HouseService houseService = new HouseService(10);
public void showMenu() {
do {
System.out.println("==========房屋出租系统菜单===========");
System.out.println("\t\t 1.新增房源");
System.out.println("\t\t 2.查找房屋");
System.out.println("\t\t 3.删除房屋信息");
System.out.println("\t\t 4.修改房屋信息");
System.out.println("\t\t 5.显示房屋信息");
System.out.println("\t\t 6.退出系统");
System.out.println("请输入选择编号:");
key = Utility.readChar();
switch (key) {
case '1':
System.out.println("新增房源");
addHouse();
break;
case '2':
System.out.println("查找房屋");
findHouse();
break;
case '3':
System.out.println("删除房屋信息");
delHouse();
break;
case '4':
System.out.println("修改房屋信息");
updateHouse();
break;
case '5':
System.out.println("显示房屋信息");
showHouses();
break;
case '6':
loop = false;
break;
}
} while (loop);
}
public void addHouse(){
System.out.println("============添加房屋=============");
System.out.println("姓名:");
String name = Utility.readString(10);
System.out.println("电话:");
String phone = Utility.readString(10);
System.out.println("地址:");
String address = Utility.readString(10);
System.out.println("月租:");
int rent = Utility.readInt();
System.out.println("状态:");
String state = Utility.readString(10);
House house = new House(0, name, phone, address, rent, state);
if(houseService.add(house)){
System.out.println("添加房屋成功");
}
else{
System.out.println("添加房屋失败");
}
}
public void delHouse(){
System.out.println("============房屋删除=============");
System.out.println("请输入要删除的房屋ID(输入-1放弃)");
int delId = Utility.readInt();
if(delId == -1){
return ;
}
else{
if(houseService.del(delId)){
System.out.println("房屋删除成功");
}
else{
System.out.println("编号有误,删除失败");
}
}
}
public void showHouses(){
System.out.println("============房屋信息=============");
House[] houses = houseService.list();
for(int i = 0; i < houses.length; i ++ ){
if(houses[i] == null){
if(i == 0){
System.out.println("当前无房屋信息");
}
break;
}
System.out.println(houses[i]);
}
}
public void findHouse(){
System.out.println("请输入要查找房屋的ID:");
int findId = Utility.readInt();
if(houseService.find(findId)){
System.out.println("查找成功");
}
else{
System.out.println("查找失败");
}
}
public void updateHouse(){
System.out.println("请输入要修改房屋的ID:");
int findId = Utility.readInt();
if(houseService.update(findId)){
System.out.println("修改成功");
}
else{
System.out.println("修改失败");
}
}
}
主类
import homework.view.HouseView;
public class HouseRentApp {
public static void main(String[] args) {
HouseView houseView = new HouseView();
houseView.showMenu();
}
}