帮我设计一个基于java mysql的房屋管理系统
时间: 2024-01-14 20:05:09 浏览: 35
好的,以下是一个简单的基于 Java 和 MySQL 的房屋管理系统的设计:
### 数据库设计
数据库名:house_management
表名:user
| 字段名 | 类型 | 描述 |
| ------ | ---- | ---- |
| id | int | 用户编号 |
| username | varchar(50) | 用户名 |
| password | varchar(50) | 密码 |
| role | int | 用户角色(0:管理员,1:普通用户) |
表名:house_info
| 字段名 | 类型 | 描述 |
| ------ | ---- | ---- |
| id | int | 房屋编号 |
| address | varchar(100) | 房屋地址 |
| area | double | 房屋面积 |
| price | double | 房屋单价 |
| status | int | 房屋状态(0:未出租,1:已出租) |
表名:rent_info
| 字段名 | 类型 | 描述 |
| ------ | ---- | ---- |
| id | int | 租赁信息编号 |
| house_id | int | 房屋编号 |
| user_id | int | 租户编号 |
| rent_date | date | 租赁日期 |
| rent_days | int | 租赁天数 |
### Java 代码实现
#### User 类
```java
public class User {
private int id;
private String username;
private String password;
private int role;
public User() {}
public User(int id, String username, String password, int role) {
this.id = id;
this.username = username;
this.password = password;
this.role = role;
}
// getter and setter methods
}
```
#### HouseInfo 类
```java
public class HouseInfo {
private int id;
private String address;
private double area;
private double price;
private int status;
public HouseInfo() {}
public HouseInfo(int id, String address, double area, double price, int status) {
this.id = id;
this.address = address;
this.area = area;
this.price = price;
this.status = status;
}
// getter and setter methods
}
```
#### RentInfo 类
```java
public class RentInfo {
private int id;
private int houseId;
private int userId;
private Date rentDate;
private int rentDays;
public RentInfo() {}
public RentInfo(int id, int houseId, int userId, Date rentDate, int rentDays) {
this.id = id;
this.houseId = houseId;
this.userId = userId;
this.rentDate = rentDate;
this.rentDays = rentDays;
}
// getter and setter methods
}
```
#### UserDao 类
```java
public class UserDao {
private Connection conn;
public UserDao() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/house_management";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
}
public User login(String username, String password) {
User user = null;
try {
String sql = "SELECT * FROM user WHERE username=? AND password=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setRole(rs.getInt("role"));
}
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
// 其他数据库操作方法
}
```
#### HouseInfoDao 类
```java
public class HouseInfoDao {
private Connection conn;
public HouseInfoDao() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/house_management";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
}
public List<HouseInfo> findAll() {
List<HouseInfo> list = new ArrayList<>();
try {
String sql = "SELECT * FROM house_info";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
HouseInfo houseInfo = new HouseInfo();
houseInfo.setId(rs.getInt("id"));
houseInfo.setAddress(rs.getString("address"));
houseInfo.setArea(rs.getDouble("area"));
houseInfo.setPrice(rs.getDouble("price"));
houseInfo.setStatus(rs.getInt("status"));
list.add(houseInfo);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
// 其他数据库操作方法
}
```
#### RentInfoDao 类
```java
public class RentInfoDao {
private Connection conn;
public RentInfoDao() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/house_management";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
}
public List<RentInfo> findAll() {
List<RentInfo> list = new ArrayList<>();
try {
String sql = "SELECT * FROM rent_info";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
RentInfo rentInfo = new RentInfo();
rentInfo.setId(rs.getInt("id"));
rentInfo.setHouseId(rs.getInt("house_id"));
rentInfo.setUserId(rs.getInt("user_id"));
rentInfo.setRentDate(rs.getDate("rent_date"));
rentInfo.setRentDays(rs.getInt("rent_days"));
list.add(rentInfo);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
// 其他数据库操作方法
}
```
#### HouseManagementSystem 类
```java
import java.util.List;
import java.util.Scanner;
public class HouseManagementSystem {
private UserDao userDao;
private HouseInfoDao houseInfoDao;
private RentInfoDao rentInfoDao;
private User currentUser;
public HouseManagementSystem() {
userDao = new UserDao();
houseInfoDao = new HouseInfoDao();
rentInfoDao = new RentInfoDao();
}
public void start() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("欢迎使用房屋管理系统!");
System.out.println("1. 登录");
System.out.println("2. 退出");
System.out.print("请选择:");
int choice = sc.nextInt();
switch (choice) {
case 1:
login();
break;
case 2:
System.out.println("谢谢使用,再见!");
System.exit(0);
break;
default:
System.out.println("输入有误,请重新选择!");
break;
}
}
}
private void login() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入用户名:");
String username = sc.nextLine();
System.out.print("请输入密码:");
String password = sc.nextLine();
currentUser = userDao.login(username, password);
if (currentUser == null) {
System.out.println("用户名或密码错误!");
} else {
System.out.println("登录成功!");
if (currentUser.getRole() == 0) {
adminMenu();
} else {
userMenu();
}
}
}
private void adminMenu() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("管理员菜单:");
System.out.println("1. 查询房屋信息");
System.out.println("2. 添加房屋信息");
System.out.println("3. 修改房屋信息");
System.out.println("4. 删除房屋信息");
System.out.println("5. 查询租赁信息");
System.out.println("6. 添加租赁信息");
System.out.println("7. 修改租赁信息");
System.out.println("8. 删除租赁信息");
System.out.println("9. 修改密码");
System.out.println("10. 退出登录");
System.out.print("请选择:");
int choice = sc.nextInt();
switch (choice) {
case 1:
List<HouseInfo> houseInfoList = houseInfoDao.findAll();
for (HouseInfo houseInfo : houseInfoList) {
System.out.println(houseInfo);
}
break;
case 2:
// 添加房屋信息
break;
case 3:
// 修改房屋信息
break;
case 4:
// 删除房屋信息
break;
case 5:
// 查询租赁信息
break;
case 6:
// 添加租赁信息
break;
case 7:
// 修改租赁信息
break;
case 8:
// 删除租赁信息
break;
case 9:
// 修改密码
break;
case 10:
System.out.println("退出登录成功!");
currentUser = null;
return;
default:
System.out.println("输入有误,请重新选择!");
break;
}
}
}
private void userMenu() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("用户菜单:");
System.out.println("1. 查询房屋信息");
System.out.println("2. 租赁房屋");
System.out.println("3. 修改密码");
System.out.println("4. 退出登录");
System.out.print("请选择:");
int choice = sc.nextInt();
switch (choice) {
case 1:
List<HouseInfo> houseInfoList = houseInfoDao.findAll();
for (HouseInfo houseInfo : houseInfoList) {
System.out.println(houseInfo);
}
break;
case 2:
// 租赁房屋
break;
case 3:
// 修改密码
break;
case 4:
System.out.println("退出登录成功!");
currentUser = null;
return;
default:
System.out.println("输入有误,请重新选择!");
break;
}
}
}
public static void main(String[] args) {
new HouseManagementSystem().start();
}
}
```
以上是一个简单的基于 Java 和 MySQL 的房屋管理系统的设计,具体实现可以根据实际需求进行调整。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)