房屋出租系统-java
package com.zhou.java.Houserent.domain;
public class House {
private int id;
private String name;
private String phone;
private String address;
private String rent;
private String state;
public House(int id, String name, String phone, String address, String 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 String getRent() {
return rent;
}
public void setRent(String rent) {
this.rent = rent;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Override
public String toString() {
return id +
"\t\t" + name +
"\t\t" + phone +
"\t\t" + address +
"\t\t\t" + rent +
"\t\t\t" + state;
}
}
package com.zhou.java.Houserent.Service;
import com.zhou.java.Houserent.domain.House;
public class HouseService {
private House[] houses;
private int houseNums = 1;
private int idCounter = 1;
public HouseService(int size) {
houses = new House[size];
houses[0] = new House(1, "张三", "123", "新乡学院", "2000", "未出租");
}
public House[] list() {
return houses;
}
public boolean add(House newHouse) {
if (houseNums >= houses.length) {
System.out.println("数据已经满了,无法继续添加数据");
return false;
}
houses[houseNums] = newHouse;
houseNums++;
newHouse.setId(++idCounter);
return true;
}
public boolean delete(int idDel) {
int index = -1;
for (int i = 0; i < houseNums; i++) {
if (idDel == houses[i].getId()) {
index = i;
}
}
if (index == -1) {
return false;
}
for (int i = index; i < houses.length - 1; i++) {
houses[i] = houses[i + 1];
}
houses[houseNums] = null;
houseNums--;
return true;
}
public House find(int findId) {
for (int i = 0; i < houseNums; i++) {
if (findId == houses[i].getId()) {
return houses[i];
}
}
return null;
}
}
package com.zhou.java.Houserent.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(2, 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;
}
}
package com.zhou.java.Houserent.View;
import com.zhou.java.Houserent.Service.HouseService;
import com.zhou.java.Houserent.domain.House;
import com.zhou.java.Houserent.utils.Utility;
public class HouseView {
private boolean loop = true;
private char key = ' ';
private HouseService houseService = new HouseService(10);
public void listHouse() {
System.out.println("=================房屋列表=================");
System.out.println("编号\t\t房主\t\t电话\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(10);
System.out.print("电话: ");
String phone = Utility.readString(10);
System.out.print("地址: ");
String address = Utility.readString(10);
System.out.print("月租: ");
String rent = Utility.readString(10);
System.out.print("状态(未出租/已出租): ");
String state = Utility.readString(3);
House newHouse = new House(0, name, phone, address, 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(10);
House house = houseService.find(findId);
if (house != null){
System.out.println(house);
}else {
System.out.println("你输入的房屋编号没有房屋信息");
}
}
public void deleteHouse() {
System.out.println("=================删除房屋=================");
System.out.println("请输入删除房屋序号(-1退出): ");
int delId = Utility.readInt();
if (delId == -1) {
System.out.println("=================放弃删除房屋=================");
return;
}
char choice = Utility.readConfirmSelection();
if (choice == 'Y') {
if (houseService.delete(delId)){
System.out.println("=================删除房屋成功=================");
}else {
System.out.println("=================房屋编号不存在,删除失败=================");
}
} else {
System.out.println("=================放弃删除房屋=================");
}
}
public void updateHouse(){
System.out.println("=================修改房屋信息=================");
System.out.println("请输入修改房屋序号(-1退出): ");
int updateId = Utility.readInt();
if (updateId ==-1){
System.out.println("你放弃修改房屋信息");
return;
}
House house = houseService.find(updateId);
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()+"): ");
String rent = Utility.readString(8, "");
if (!"".equals(rent)){
house.setRent(rent);
}
System.out.print("状态("+house.getState()+"): ");
String state = Utility.readString(8, "");
if (!"".equals(state)){
house.setState(state);
}
System.out.println("房屋信息修改成功");
}
public void exit(){
char exit = Utility.readConfirmSelection();
if (exit=='Y'){
loop =false;
}
}
public void HouseMenu() {
do {
System.out.println("\n===============房屋出租系统菜单===============");
System.out.println("\t\t\t\t1.新增房源");
System.out.println("\t\t\t\t2.查找房源");
System.out.println("\t\t\t\t3.删除房源");
System.out.println("\t\t\t\t4.修改房屋信息");
System.out.println("\t\t\t\t5.房屋列表");
System.out.println("\t\t\t\t6.退 出");
System.out.println("请输入你的选择(1-6):");
key = Utility.readChar();
switch (key) {
case '1':
addHouse();
break;
case '2':
findHouse();
break;
case '3':
deleteHouse();
break;
case '4':
updateHouse();
break;
case '5':
listHouse();
break;
case '6':
exit();
break;
default:
System.out.println("输入错误,请重新输入");
}
} while (loop);
}
}
package com.zhou.java.Houserent;
import com.zhou.java.Houserent.View.HouseView;
public class HouseTest {
public static void main(String[] args) {
new HouseView().HouseMenu();
System.out.println("成功退出系统 ");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?