程序整体分析
示例代码
package houserent;import java.util.Scanner;public class HouseRentSYS {public static void main(String[] args) {new HouseRentSYS().MainMenu(); }private boolean loop = true; private char key = ' '; private HouseService houseService = new HouseService(5); private void MainMenu() {do {System.out.println("\n=============房屋出租系统菜单============");System.out.println("\t\t\t1 新 增 房 源");System.out.println("\t\t\t2 查 找 房 屋");System.out.println("\t\t\t3 删 除 房 屋 信 息");System.out.println("\t\t\t4 修 改 房 屋 信 息");System.out.println("\t\t\t5 房 屋 列 表");System.out.println("\t\t\t6 退 出");System.out.print("请输入你的选择(1-6): ");key = Util.readChar(); switch (key) {case '1':this.addHouse();break;case '2':this.findHouse();break;case '3':this.delHouse();break;case '4':this.update();break;case '5':this.listHouses();break;case '6':this.exit();break;default:}} while (loop);}private void addHouse() {System.out.println("=============添加房屋============");System.out.print("姓名:");String name = Util.readString(8);System.out.print("电话:");String phone = Util.readString(12);System.out.print("地址:");String address = Util.readString(16);System.out.print("月租:");int rent = Util.readInt();System.out.print("状态:");String state = Util.readString(3);House newHouse = new House(0, name, phone, address, rent, state);if (houseService.add(newHouse)) {System.out.println("=============添加房屋成功============");} else {System.out.println("=============添加房屋失败============");}}private void findHouse() {System.out.println("=============查找房屋信息============");System.out.printf("请输入要查找的id:");int findId = Util.readInt();House house = houseService.findByID(findId);if (house != null) System.out.println(house.toString());else System.out.println("=============查找房屋信息id不存在============");}private void delHouse() {System.out.println("=============删除房屋信息============");System.out.printf("请输入要删除的房屋id(-1退出):");int delId = Util.readInt();if (delId == -1) {System.out.println("放弃删除房屋信息");return; }char choice = Util.readConfirmSelection();if (choice == 'Y') {if (houseService.del(delId)) {System.out.println("=============删除房屋信息成功============");} else {System.out.println("=============房屋编号不存在,删除失败============");}} else {System.out.println("=============放弃删除房屋信息============");}}private void update() {System.out.println("=============修改房屋信息============");System.out.println("请选择待修改房屋编号(-1表示退出):");int updateId = Util.readInt();if (updateId == -1) {System.out.println("放弃修改房屋信息");return; }House house = houseService.findByID(updateId);if (house == null) {System.out.println("房屋信息不存在");return;}System.out.print("姓名(" + house.getName() + "):");String name = Util.readString(8, ""); if (!name.equals("")) {house.setName(name);}System.out.print("电话(" + house.getPhone() + "):");String phone = Util.readString(12, ""); if (!phone.equals("")) {house.setPhone(phone);}System.out.print("地址(" + house.getAddress() + "):");String address = Util.readString(18, ""); if (!address.equals("")) {house.setAddress(address);}System.out.print("租金(" + house.getRent() + "):");int rent = Util.readInt(-1);if (rent != -1) {house.setRent(rent);}System.out.print("状态(" + house.getState() + "):");String state = Util.readString(3, ""); if (!state.equals("")) {house.setState(state);}System.out.println("房屋信息修改成功");}private void listHouses() {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("房屋列表显示完成");}private void exit() {char c = Util.readConfirmSelection(); if (c == 'Y') {loop = false; }}
}
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 void setId(int id) {this.id = id;}@Overridepublic String toString() {return id +"\t\t" + name +"\t" + phone +"\t\t" + address +"\t" + rent +"\t" + state;}public void setName(String name) {this.name = name;}public void setPhone(String phone) {this.phone = phone;}public void setAddress(String address) {this.address = address;}public void setRent(int rent) {this.rent = rent;}public void setState(String state) {this.state = state;}public int getId() {return id;}public String getName() {return name;}public String getPhone() {return phone;}public String getAddress() {return address;}public int getRent() {return rent;}public String getState() {return state;}
}
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, "张三", "112", "朝阳区", 2600, "未出租");}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; for (int i = index; i < houseNums - 1; i++) {houses[i] = houses[i + 1];}houses[--houseNums] = null;return true;}public boolean add(House newHouse) {if (houseNums == houses.length) {System.out.println("数组已满,不能在添加了..");return false;}houses[houseNums++] = newHouse;newHouse.setId(++idCounter);return true;}public House[] list() {return houses;}
}
class Util {private static Scanner scanner = new Scanner(System.in);public static char readMenuSelection() {char c;while (true) {String str = readKeyBoard(1, false); c = str.charAt(0);if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6') {System.out.println("选择错误,重新输入:");} else {break;}}return c;}public static char readConfirmSelection() {System.out.println("请输入你的选择(Y/N):");char c;while (true) {String str = readKeyBoard(1, false).toUpperCase();c = str.charAt(0);if (c == 'Y' || c == 'N') {break;} else {System.out.println("输入的不是(Y/N),请重新输入:");}}return c;}public static char readChar() {String str = readKeyBoard(1, false); return str.charAt(0); }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 int readInt() {int n;while (true) {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;while (true) {String str = readKeyBoard(10, true); if (str.equals("")) { return defaultValue; }try {n = Integer.parseInt(str); break; } catch (NumberFormatException e) {System.out.print("数据输入错误,请重新输入:");}}return n; }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;}break;}return line; }
}
样例输出