房屋出租系统
第九章 房屋出租系统 项目需求 实现基于文本界面的《房屋出租软件》,能够实现对房屋信息的添加、修改和删除(用数组实现),并能够打印房屋明细表。 项目界面 主菜单 
1.新增房源 
2.查找房屋 
3.删除房屋 
4.修改房屋信息 
5.房屋列表 
6.退出 
★项目设计 
项目实现 
工具类Utility.java 见【零基础 快速学Java】韩顺平 零基础30天学会Java
P364 房屋类House.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 package com.f.houserent.domain;public class House { private int id; private String name; private String phone; private String addr; private double rent; private String state; public House () { } public House (int id, String name, String phone, String addr, double rent, String state) { this .id = id; this .name = name; this .phone = phone; this .addr = addr; 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 getAddr () { return addr; } public void setAddr (String addr) { this .addr = addr; } 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; } @Override public String toString () { return id + "\t\t" + name + "\t" + phone + "\t\t" + addr + "\t" + rent + "\t" + state; } }
显示界面HouseView.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 package com.f.houserent.view;import com.f.houserent.domain.House;import com.f.houserent.service.HouseService;import java.util.Scanner;public class HouseView { Scanner scanner = new Scanner (System.in); private boolean flag = true ; private String choice; private HouseService houseService = new HouseService (3 ); public void mainMenu () { do { System.out.println("------------房屋出租系统------------" ); 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):" ); choice = scanner.next(); switch (choice) { case "1" : CreateHouse(); break ; case "2" : RetrieveHouse(); break ; case "3" : DeleteHouse(); break ; case "4" : UpdateHouse(); break ; case "5" : ListHouses(); break ; case "6" : flag = false ; break ; default : System.out.println("您输入的不是 1 - 6,请重新输入\n" ); } } while (flag); } public void CreateHouse () { System.out.println("------------添加房屋信息------------" ); House houseNew = new House (); System.out.print("姓名:" ); houseNew.setName(scanner.next()); System.out.print("电话:" ); houseNew.setPhone(scanner.next()); System.out.print("地址:" ); houseNew.setAddr(scanner.next()); System.out.print("月租:" ); houseNew.setRent(scanner.nextDouble()); System.out.print("状态(未出租/已出租):" ); houseNew.setState(scanner.next()); if (houseService.Create(houseNew)) { System.out.println("------------添加完成------------\n" ); } else { System.out.println("------------添加失败------------\n" ); } } public void RetrieveHouse () { System.out.println("------------查找房屋信息------------" ); System.out.print("请输入要查找的房屋 id:" ); int searchID = scanner.nextInt(); House searchHouse = houseService.Retrieve(searchID); if (searchHouse == null ) { System.out.println("------------没有该房屋------------\n" ); } else { System.out.println(searchHouse); System.out.println(); } } public void DeleteHouse () { System.out.println("------------删除房屋信息------------" ); System.out.print("请选择待删除房屋编号(-1退出):" ); int deleteChoice = scanner.nextInt(); if (deleteChoice == -1 ) { System.out.println("放弃删除房屋信息...\n" ); return ; } System.out.println("确认是否删除(Y/N):请小心选择:" ); System.out.print("请输入你的选择(Y/N)" ); String confirm; do { confirm = scanner.next(); if (confirm.equals("y" ) || confirm.equals("Y" )) { if (houseService.Delete(deleteChoice)) { System.out.println("------------删除成功------------\n" ); } else { System.out.println("------------删除失败------------\n" ); } break ; } else if (confirm.equals("n" ) || confirm.equals("N" )) { System.out.println("放弃删除房屋信息...\n" ); break ; } else { System.out.print("输入不规范,请重新输入(Y/N):" ); } } while (true ); } public void UpdateHouse () { System.out.println("------------修改房屋信息------------" ); System.out.print("请输入待修改的房屋的编号(-1退出):" ); int updateID = scanner.nextInt(); if (updateID == -1 ) { System.out.println("放弃修改房屋信息...\n" ); return ; } House updateHouse = houseService.Retrieve(updateID); if (updateHouse == null ) { System.out.println("所选择的房屋不存在..." ); System.out.println("------------修改失败------------\n" ); } else { System.out.print("姓名" + "(" + updateHouse.getName() + "):" ); updateHouse.setName(scanner.next()); System.out.print("电话" + "(" + updateHouse.getPhone() + "):" ); updateHouse.setPhone(scanner.next()); System.out.print("地址" + "(" + updateHouse.getAddr() + "):" ); updateHouse.setAddr(scanner.next()); System.out.print("月租" + "(" + updateHouse.getRent() + "):" ); updateHouse.setRent(scanner.nextDouble()); System.out.print("状态" + "(" + updateHouse.getState() + "):" ); updateHouse.setState(scanner.next()); System.out.println("------------修改完成------------\n" ); } } public 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("-----------房屋列表完成-----------\n" ); } }
业务功能HouseService.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 package com.f.houserent.service;import com.f.houserent.domain.House;import java.util.Scanner;public class HouseService { Scanner scanner = new Scanner (System.in); private House[] houses; private static int houseID = 1 ; private static int occupation = 0 ; public HouseService (int size) { houses = new House [size]; } public boolean Create (House house) { if (occupation >= houses.length) { System.out.println("空间已满,不能再添加了..." ); return false ; } houses[occupation++] = house; house.setId(houseID++); return true ; } public House Retrieve (int searchID) { for (int i = 0 ; i < occupation; i++) { if (searchID == houses[i].getId()) { return houses[i]; } } return null ; } public boolean Delete (int id) { int index = -1 ; for (int i = 0 ; i < occupation; i++) { if (id == houses[i].getId()) { index = i; } } if (index == -1 ) { System.out.println("输入的 id 所对应的房屋信息不存在" ); return false ; } for (int i = index; i < occupation - 1 ; i++) { houses[i] = houses[i + 1 ]; } houses[occupation - 1 ] = null ; occupation--; return true ; } public House[] List() { return houses; } }
应用入口HouseRentApp.java 1 2 3 4 5 6 7 8 9 10 11 12 13 package com.f.houserent;import com.f.houserent.view.HouseView;public class HouseRentApp { public static void main (String[] args) { new HouseView ().mainMenu(); System.out.println("您退出了房屋出租系统..." ); } }