韩顺平第一阶段项目-房屋租赁系统
目录结构
![目录结构](https://img-blog.csdnimg.cn/60f0d1810de443409838f855266e5428.png#pic_center)
House.java 房屋类
package com.hanstudy.houseSystem.domain;
public class House {
private int id;
private String name;
private String phone;
private String address;
private double rent;
private String state;
@Override
public String toString() {
return id + "\t\t" + name + "\t\t" + phone + "\t\t" + address +
"\t\t" + rent + "\t\t" + 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(double rent) {
this.rent = rent;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public House(int id, String name, String phone, String address, double rent, String state) {
this.id = id;
this.name = name;
this.phone = phone;
this.address = address;
this.rent = rent;
this.state = state;
}
}
HouseView.java 房屋系统视图内容
package com.hanstudy.houseSystem.view;
import com.hanstudy.houseSystem.domain.House;
import com.hanstudy.houseSystem.service.HouseService;
import com.hanstudy.houseSystem.utils.Utility;
import java.util.Scanner;
public class HouseView {
Scanner sc = new Scanner(System.in);
private boolean loop = true;
private char key = ' ';
private HouseService houseService = new HouseService(2);
public void mainMenu() {
do {
System.out.println("==========房屋租赁系统==========");
System.out.print("\t\t1.新增房源\n");
System.out.print("\t\t2.查找房屋\n");
System.out.print("\t\t3.删除房屋\n");
System.out.print("\t\t4.修改房屋信息\n");
System.out.print("\t\t5.房屋列表\n");
System.out.print("\t\t6.退出系统\n");
System.out.print("请输入你的选择:(1-6)");
key = Utility.readChar();
switch (key) {
case '1':
addHouse();
break;
case '2':
findHouse();
break;
case '3':
delHouse();
break;
case '4':
updataHouse();
break;
case '5':
listHouse();
break;
case '6':
exit();
break;
}
} while (loop);
}
public void listHouse() {
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]);
}
System.out.println("--------------------------");
}
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.print("状态:");
String state = Utility.readString(3);
House h = new House(0, name, phone, address, rent, state);
if (houseService.add(h)) {
System.out.println("**添加房屋成功");
} else {
System.out.println("**添加房屋失败");
}
}
public void delHouse() {
System.out.println("----------删除房屋----------");
System.out.print("请输入待删除房屋的编号(-1退出)");
int delId = Utility.readInt();
if (delId == -1) {
System.out.println("** 放弃删除房屋信息");
return;
}
char choice = Utility.readConfirmSelection();
if (choice == 'Y') {
if (houseService.del(delId)) {
System.out.println("**删除成功");
} else {
System.out.println("房屋id不存在,删除失败。");
}
} else {
System.out.println("放弃删除");
}
}
public void exit() {
char c = Utility.readConfirmSelection();
if (c == 'Y') {
loop = false;
}
}
public void findHouse() {
System.out.println("----------查找房屋----------");
System.out.print("请输入查找房屋的编号(-1退出)");
int findId = Utility.readInt();
if (findId == -1) {
System.out.println("** 放弃删除房屋信息");
return;
}
House house = houseService.find(findId);
if (house != null) {
System.out.println(house);
} else {
System.out.println("没有找到此房屋");
}
}
public void updataHouse() {
System.out.println("----------修改房屋信息----------");
System.out.print("请输入修改房屋的编号(-1退出)");
int findId = Utility.readInt();
if (findId == -1) {
System.out.println("** 放弃修改房屋信息");
return;
}
House house = houseService.find(findId);
if (house == null) {
System.out.println("没有这个房子");
return;
}
System.out.print("姓名(" + house.getName() + "):");
String name = Utility.readString(8, "");
if (!"".equals(name)) {
house.setName(name);
}
System.out.print("电话(" + house.getPhone() + "):");
String phone = Utility.readString(8, "");
if (!"".equals(phone)) {
house.setPhone(phone);
}
System.out.print("地址(" + house.getAddress() + "):");
String address = Utility.readString(8, "");
if (!"".equals(address)) {
house.setAddress(address);
}
System.out.print("月租(" + house.getRent() + "):");
int rent = Utility.readInt(-1);
if (rent != (-1)) {
house.setRent(rent);
}
System.out.print("状态(" + house.getState() + "):");
String state = Utility.readString(8, "");
if (!"".equals(state)) {
house.setState(state);
}
System.out.println("**修改成功");
}
}
HouseService.java 房屋系统内部功能
package com.hanstudy.houseSystem.service;
import com.hanstudy.houseSystem.domain.House;
public class HouseService {
private int houseNum;
private int idCounter = 1;
private House[] houses;
public HouseService(int size) {
houses = new House[size];
}
public House[] list() {
return houses;
}
public boolean add(House newHouse) {
if (houses.length == houseNum) {
System.out.println("数组满了不能添加了");
return false;
}
newHouse.setId(idCounter++);
houses[houseNum++] = newHouse;
return true;
}
public boolean del(int delID) {
int index = -1;
for (int i = 0; i < houseNum; i++) {
if (delID == houses[i].getId()) {
index = i;
}
}
if (index == -1) {
return false;
}
for (int i = index; i < houseNum - 1; i++) {
houses[i] = houses[i + 1];
}
houses[--houseNum] = null;
return true;
}
public House find(int findID){
for (int i = 0; i < houseNum; i++) {
if (findID == houses[i].getId()) {
return houses[i];
}
}
return null;
}
}
Utility.java 开发工具类(韩老师编写)
package com.hanstudy.houseSystem.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(5, 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;
}
}
HouseRentApp.java 系统运行
package com.hanstudy.houseSystem;
import com.hanstudy.houseSystem.view.HouseView;
public class HouseRentApp {
public static void main(String[] args) {
new HouseView().mainMenu();
}
}
我就接受
if (line.length() < 1 || line.length() > limit) {
System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
HouseRentApp.java 系统运行
package com.hanstudy.houseSystem;
import com.hanstudy.houseSystem.view.HouseView;
public class HouseRentApp {
public static void main(String[] args) {
new HouseView().mainMenu();
}
}