Skip to content

Commit 1e2c9b7

Browse files
committed
Add ch09 code
1 parent 315c426 commit 1e2c9b7

21 files changed

+649
-0
lines changed

code/chapter09/.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/chapter09/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/chapter09/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/chapter09/chapter09.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="com.hspedu" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.hspedu.houserent;
2+
3+
import com.hspedu.houserent.view.HouseView;
4+
5+
public class HouseRentApp {
6+
public static void main(String[] args) {
7+
8+
byte b1 = 123;
9+
b1 += 100000; //如果 b1 = b1 + 100000;//编译都不能过
10+
System.out.println(b1);
11+
12+
byte b2 = 123;
13+
b2 = (byte)(b2 + 100000);
14+
System.out.println(b2);
15+
16+
// 创建HouseView匿名对象,并且显示主菜单,是整个程序的入口
17+
// 这里实际上不用接收了,因为接收后用处也不大:
18+
// new hv = new HouseView();
19+
// hv.mainMenu();
20+
new HouseView().mainMenu();
21+
System.out.println("===你退出房屋出租系统==");
22+
}
23+
}
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.hspedu.houserent.domain;
2+
3+
/**
4+
* House的对象表示一个房屋信息
5+
*/
6+
public class House {
7+
//编号 房主 电话 地址 月租 状态(未出租/已出租)
8+
private int id;
9+
private String name;
10+
private String phone;
11+
private String address;
12+
private int rent;
13+
private String state;
14+
//构造器和setter,getter
15+
16+
public House(int id, String name, String phone, String address, int rent, String state) {
17+
this.id = id;
18+
this.name = name;
19+
this.phone = phone;
20+
this.address = address;
21+
this.rent = rent;
22+
this.state = state;
23+
}
24+
25+
public int getId() {
26+
return id;
27+
}
28+
29+
public void setId(int id) {
30+
this.id = id;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name = name;
39+
}
40+
41+
public String getPhone() {
42+
return phone;
43+
}
44+
45+
public void setPhone(String phone) {
46+
this.phone = phone;
47+
}
48+
49+
public String getAddress() {
50+
return address;
51+
}
52+
53+
public void setAddress(String address) {
54+
this.address = address;
55+
}
56+
57+
public int getRent() {
58+
return rent;
59+
}
60+
61+
public void setRent(int rent) {
62+
this.rent = rent;
63+
}
64+
65+
public String getState() {
66+
return state;
67+
}
68+
69+
public void setState(String state) {
70+
this.state = state;
71+
}
72+
//为了方便的输出对象信息,我们实现toString
73+
//编号 房主 电话 地址 月租 状态(未出租/已出租)
74+
@Override
75+
public String toString() {
76+
return id +
77+
"\t\t" + name +
78+
"\t" + phone +
79+
"\t\t" + address +
80+
"\t" + rent +
81+
"\t" + state ;
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.hspedu.houserent.service;
2+
3+
import com.hspedu.houserent.domain.House;
4+
5+
/**
6+
* HouseService.java<=>类 [业务层]
7+
* //定义House[] ,保存House对象
8+
* 1. 响应HouseView的调用
9+
* 2. 完成对房屋信息的各种操作(增删改查c[create]r[read]u[update]d[delete])
10+
*/
11+
public class HouseService {
12+
13+
private House[] houses; // House 数组 保存House对象
14+
private int houseNums = 1; //记录当前有多少个房屋信息
15+
private int idCounter = 1; //记录当前的id增长到哪个值
16+
//构造器
17+
public HouseService(int size) {
18+
//new houses
19+
houses = new House[size];//当创建HouseService对象,指定数组大小
20+
//为了配合测试列表信息,这里初始化一个House对象
21+
houses[0] = new House(1,"jack","112", "海淀区", 2000, "未出租");
22+
}
23+
24+
// findById方法,返回House对象或者null
25+
public House findById(int findId) {
26+
27+
//遍历数组
28+
for(int i = 0; i < houseNums; i++) {
29+
if(findId == houses[i].getId()) {
30+
return houses[i];
31+
}
32+
}
33+
return null;
34+
}
35+
36+
//del方法,删除一个房屋信息
37+
public boolean del(int delId) {
38+
39+
// 应当先找到要删除的房屋信息对应的下标
40+
// 一定要搞清楚 下标和房屋的编号不是一回事
41+
int index = -1;
42+
for(int i = 0; i < houseNums; i++) {
43+
if(delId == houses[i].getId()) {// 要删除的房屋(id),是数组下标为i的元素
44+
index = i;// 就使用index记录i
45+
}
46+
}
47+
48+
if(index == -1) {//说明delId在数组中不存在(有点绕..)
49+
return false;
50+
}
51+
// 如果找到
52+
for(int i = index; i < houseNums - 1; i++) {
53+
houses[i] = houses[i+1];
54+
}
55+
// 把当有存在的房屋信息的最后一个 设置null
56+
houses[--houseNums] = null;
57+
return true;
58+
59+
}
60+
61+
// add方法,添加新对象,返回boolean
62+
public boolean add(House newHouse) {
63+
// 判断是否还可以继续添加(我们暂时不考虑数组扩容的问题) => 能否自己加入数组扩容机制(~~)
64+
if(houseNums == houses.length) {//不能再添加
65+
System.out.println("数组已满,不能再添加了...");
66+
return false;
67+
}
68+
// 把newHouse对象加入到,新增加了一个房屋
69+
houses[houseNums++] = newHouse;
70+
// 我们程序员需要设计一个id自增长的机制, 然后更新 newHouse 的id!
71+
newHouse.setId(++idCounter);
72+
return true;
73+
}
74+
75+
//list方法,返回houses
76+
public House[] list() {
77+
return houses;
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.hspedu.houserent.utils;
2+
3+
public class TestUtility {
4+
public static void main(String[] args) {
5+
6+
//这是一个测试类,使用完毕,就可以删除了
7+
//要求输入一个字符串,长度最大为3
8+
// String s = Utility.readString(3);
9+
// System.out.println("s=" + s);
10+
11+
//要求输入一个字符串,长度最大为10, 如果用户直接回车,就给一个默认值
12+
//老师提示:就把该方法当做一个api使用即可
13+
//"hspedu"
14+
System.out.println("请输入字符串:");
15+
String s2 = Utility.readString(10, "hspedu");
16+
System.out.println("s2=" + s2);
17+
18+
19+
//老师解释一个问题
20+
//这里老师是直接使用 类.方法() => 因为当一个方法是static时,就是一个静态方法
21+
//注意:静态方法可以直接通过类名调用. => 具体细节老师后面在说.
22+
23+
A.cry();
24+
}
25+
}
26+
27+
class A {
28+
public void hi() {
29+
30+
}
31+
public static void cry() {
32+
33+
}
34+
}

0 commit comments

Comments
 (0)