House.java
1 package com.tl.www.House;
2
3 public class House {
4 private String id;//编号
5 private String name;//房主姓名
6 private String phone;//电话
7 private String address;//地址
8 private int rentByMonth;//月租
9 private String state;//状态
10
11 public House() {
12 }
13
14 public House(String id, String name, String phone, String address, int rentByMonth, String state) {
15 this.id = id;
16 this.name = name;
17 this.phone = phone;
18 this.address = address;
19 this.rentByMonth = rentByMonth;
20 this.state = state;
21 }
22
23 public String getId() {
24 return id;
25 }
26
27 public void setId(String id) {
28 this.id = id;
29 }
30
31 public String getName() {
32 return name;
33 }
34
35 public void setName(String name) {
36 this.name = name;
37 }
38
39 public String getPhone() {
40 return phone;
41 }
42
43 public void setPhone(String phone) {
44 this.phone = phone;
45 }
46
47 public String getAddress() {
48 return address;
49 }
50
51 public void setAddress(String address) {
52 this.address = address;
53 }
54
55 public int getRentByMonth() {
56 return rentByMonth;
57 }
58
59 public void setRentByMonth(int rentByMonth) {
60 this.rentByMonth = rentByMonth;
61 }
62
63 public String getState() {
64 return state;
65 }
66
67 public void setState(String state) {
68 this.state = state;
69 }
70
71 @Override
72 public String toString() {
73 return (getId() + "\t\t"
74 + getName() + "\t\t\t"
75 + getPhone() + " \t\t"
76 + getAddress() + "\t\t"
77 + getRentByMonth() + "\t\t\t"
78 + getState())
79 ;
80 }
81 }
主类(应用)
RentHouseApp.java
1 package com.tl.www;
2 import com.tl.www.HouseView.HouseView;
3
4 public class RentHouseApp {
5 public static void main(String[] args) {
6 new HouseView().mainMenu();
7 }
8 }
HouseView.java
1 package com.tl.www.HouseView;
2
3
4 //import com.tl.www.House.House;
5
6 import com.tl.www.House.House;
7 import com.tl.www.Utility;
8 import com.tl.www.HouseService.HouseService;
9
10 public class HouseView {
11 /* 项目界面
12 1、主菜单---mainMenu
13 2、新增房源---addHouse
14 3、查找房源---findHouse
15 4、删除房源---delHouse
16 5、修改房源---updateHouse
17 6、列出房源---listHouse
18 7、退出程序---exit
19 */
20 HouseService houseService = new HouseService();
21 String menuHead = "编号\t\t" + "房主姓名\t\t" + "联系电话\t\t" + "地址\t\t" + "月租费\t\t" + "状态(已出租/未出租)\t";
22 boolean flag = true;
23 House[] houses = new House[1];
24
25
26 //1、主菜单---mainMenu
27 public void mainMenu() {
28 houses[0] = new House("22","22","22","22",22,"22");
29 do {
30 System.out.println("-------------------房屋出租系统--------------------");
31 System.out.println(" 1、新增房源");
32 System.out.println(" 2、查找房源");
33 System.out.println(" 3、删除房源");
34 System.out.println(" 4、修改房源");
35 System.out.println(" 5、列出房源");
36 System.out.println(" 6、退出程序");
37 System.out.println("------------------------------------------------");
38 System.out.print("请输入你的选择:");
39 switch (Utility.readMenuSelection()) {
40 case '1':
41 addHouse();
42 break;
43 case '2':
44 findHouse();
45 break;
46 case '3':
47 delHouse();
48 break;
49 case '4':
50 updateHouse();
51 break;
52 case '5':
53 listHouse();
54 break;
55 case '6':
56 exit();
57 break;
58 }
59 } while (flag);
60 }
61
62 // 2、新增房源---addHouse
63 public void addHouse() {
64 System.out.println("===================添加房屋====================");
65 System.out.print("编号:");
66 String id = Utility.readString(10, "2020140xxx");
67 System.out.print("房主姓名:");
68 String name = Utility.readString(4);
69 System.out.print("电话:");
70 String phone = Utility.readString(11);
71 System.out.print("地址:");
72 String address = Utility.readString(20);
73 System.out.print("月租:");
74 int rentByMonth = Utility.readInt();
75 System.out.print("状态:");
76 String state = Utility.readString(3, "未表明");
77 House houseNew = new House(id, name, phone, address, rentByMonth, state);
78 System.out.println(houseNew);
79 houses = houseService.add(houses, houseNew);//记得要把添加后的新数组重新赋值回去
80 // this.houseService.add(houses,houseNew);
81 }
82
83 // 3、查找房源---findHouse
84 public void findHouse() {
85 System.out.println("请输入你要查找的房源的id号");
86 House houseFound = houseService.findById(houses,Utility.readString(10));
87 System.out.println("房源信息如下:\n" + menuHead + houseFound);
88 }
89
90 // 4、删除房源---delHouse
91 public void delHouse() {
92 System.out.print("请输入你要删除的房源的id号:");
93 String delnum = Utility.readString(10);
94 houses = houseService.Arrdel(houses,delnum);
95 }
96
97 // 5、修改房源---updateHouse
98 public void updateHouse() {
99
100 System.out.println("===================修改房屋====================");
101 System.out.print("请输入你要修改的id编号");
102 String updateNum = Utility.readString(10);
103 int updateIndex = houseService.findById_int(houses,updateNum);
104 System.out.println("房源信息如下:+\n" + menuHead+"\n" + houses[updateIndex]);
105
106 System.out.println("编号("+houses[updateIndex].getId()+"):");
107 String id = Utility.readString(10, "2020140xxx");
108 System.out.print("房主姓名("+houses[updateIndex].getName()+"):");
109 String name = Utility.readString(4);
110 System.out.print("电话("+houses[updateIndex].getPhone()+"):");
111 String phone = Utility.readString(11);
112 System.out.print("地址("+houses[updateIndex].getAddress()+"):");
113 String address = Utility.readString(20);
114 System.out.print("月租("+houses[updateIndex].getRentByMonth()+"):");
115 int rentByMonth = Utility.readInt();
116 System.out.print("状态("+houses[updateIndex].getState()+"):");
117 String state = Utility.readString(3, "未表明");
118 houses[updateIndex] = new House(id,name,phone,address,rentByMonth,state);
119 }
120
121 // 6、列出房源---listHouse
122 public void listHouse() {
123 System.out.println(menuHead);
124 for (int i = 0; i < houses.length; i++) {
125 if (houses[i]!=null){
126 System.out.println(houses[i]);
127 }
128 }
129 }
130
131 public void exit() {
132 System.out.println("确定要退出吗?");
133 Utility.readConfirmSelection();
134 flag = false;
135 }
136 }
HouseService.java
1 package com.tl.www.HouseService;
2
3 import com.tl.www.House.House;
4
5 public class HouseService {
6 House[] houses = new House[1];//保存从House来的对象
7
8 public HouseService() {
9 this.houses = houses;
10 }
11
12 // 查找房源ById号
13 public House findById(House[] houses,String id){
14 int index = -1;
15 for (int i = 0; i < houses.length; i++) {
16 if (id.equals(houses[i].getId())) {//能用equals就用
17 index = i;
18 }
19 }
20 return houses[index];
21 }
22
23 // 返回查找的下标值index
24 public int findById_int(House[] houses,String id){
25 int index = -1;
26 for (int i = 0; i < houses.length; i++) {
27 if (houses[i]!=null){
28 if (id.equals(houses[i].getId())) {//能用equals就用
29 index = i;
30 }
31 }
32
33 }
34 return index;
35 }
36
37 // 动态增长House数组的元素
38 public House[] add(House[] houses, House house) {//输入数组和添加的对象
39 if (houses[0] == null) {
40 houses[0] = house;
41 } else if (houses[0] != null) {
42 houses = ArrAdd(houses);
43 // System.out.println(houses[houses.length-1]);
44 for (int i = 1; i < houses.length; i++) {
45 if (houses[i] == null) {
46 houses[i] = house;
47 break;
48 }
49 }
50 }
51 return houses;
52 }
53
54 // 数组扩容和缩减
55 public House[] ArrAdd(House[] houses) {
56 House[] add = new House[houses.length + 1];
57 for (int i = 0; i < houses.length; i++) {
58 add[i] = houses[i];
59 }
60 return add;
61 }
62 /*
63 == 比较的是变量(栈)内存中存放的对象的(堆)内存地址,用来判断两个对象的地址是否相同,
64 即是否是指相同一个对象。比较的是真正意义上的指针操作。
65
66 equals用来比较的是两个对象的内容是否相等,由于所有的类都是继承自java.lang.Object类的,
67 所以适用于所有对象,如果没有对该方法进行覆盖的话,调用的仍然是Object类中的方法,
68 而Object中的equals方法返回的却是==的判断。
69 */
70
71 // 将删除的元素置空,输出时候跳过null元素
72 public House[] Arrdel(House[] houses, String id) {//删除一个元素
73 // House[] del = new House[houses.length - 1];
74 for (int i = 0; i < houses.length; i++) {
75 if (id.equals(houses[i].getId())) {//能用equals就用
76 houses[i] = null;
77 }
78 }
79 return houses;
80 }
81 }
2021-10-27---晚上17.57