功能
1、新增房源;
2、查找房屋;
3、删除房屋;
4、删除房屋信息;
5、房屋列表;
6、登录;
7、退出;
数据库8.0以上,功能完善~代码简洁明了!
项目源码链接
运行截图
在这里插入图片描述
部分源码
房屋类:存储房屋基本信息
package entity;
/**
* 房屋类
*/
public class House {
//编号 房主 电话 地址 月租 状态(已出租/未出租) 简介
private int id;
private String name;
private String phone;
private String address;
private double rent;
private String state;
private String introduce;
public House(int id, String name, String phone, String address, double rent, String state, String introduce) {
this.id = id;
this.name = name;
this.phone = phone;
this.address = address;
this.rent = rent;
this.state = state;
this.introduce =introduce;
}
public House() {
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
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;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
@Override
public String toString() {
return id
+"\t" +name
+"\t\t" +phone
+"\t" +address
+"\t\t"+rent
+"\t\t"+state
+"\t\t"+introduce;
}
}
房屋业务层:处理房屋的基本 *** 作
package controller;
import entity.House;
import impl.HouseDaoImpl;
import tool.Utility;
import java.util.List;
/**
* 业务层
*/
public class HouseSevice {
private List<House> houses;//保存house对象
public List<House> list(){
HouseDaoImpl houseDao = new HouseDaoImpl();
List<House> houses = houseDao.selectAll();
return houses;//因为重写了tostring
}
//添加房屋信息
public boolean add(House house){
HouseDaoImpl houseDao = new HouseDaoImpl();
int t = houseDao.add(house);
if (t == 1){
return true;
}
return false;
}
//删除房屋
public boolean del(int Id){
HouseDaoImpl houseDao = new HouseDaoImpl();
int b = houseDao.delete(Id);
if(b==1){
return true;
}else {
return false;
}
}
//查找房屋
public House Find(int id){
HouseDaoImpl houseDao = new HouseDaoImpl();
House house = houseDao.query(id);
return house;
}
//修改房屋信息
public int updata(int up){
int t = 0;
HouseDaoImpl houseDao = new HouseDaoImpl();
House house=Find(up);
if(house==null){
System.out.println("该房屋不存在");
}else {
System.out.print("姓名:("+house.getName()+"):");
String name= Utility.readString(8,"");//用户如果直接回车代表不修改,默认值为""
if(!name.equals("")){
house.setName(name);//将用户输入的name覆盖原来的name
}
System.out.print("手机号:("+house.getPhone()+"):");
String phone= Utility.readString(11,"");//用户如果直接回车代表不修改,默认值为0
if(!phone.equals("")){
house.setPhone(phone);//将用户输入的name覆盖原来的name
}
System.out.print("地址:("+house.getAddress()+"):");
String address= Utility.readString(8,"");
if(!address.equals("")){
house.setAddress(address);
}
System.out.print("月租:("+house.getRent()+"):");
double rent= Utility.readDouble();
if(!(rent==0)){
house.setRent(rent);
}
System.out.print("状态:("+house.getState()+"):");
String state= Utility.readString(8,"");
if(!state.equals("")){
house.setState(state);//
}
System.out.print("简介:("+house.getIntroduce()+"):");
String introduce= Utility.readString(8,"");
if(!introduce.equals("")){
house.setState(introduce);//
}
t = houseDao.update(house);
}
return t;
}
}
房屋数据库处理
package impl;
import dao.DBConnection;
import dao.HouseDao;
import entity.House;
import entity.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class HouseDaoImpl implements HouseDao {
//新增
@Override
public int add(House house) {
try {
Connection con = DBConnection.getConnection();
//新增语句
String sql = "INSERT into house(NAME,phone,address,rent,state,introduce) VALUES(?,?,?,?,?,?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, house.getName());
ps.setString(2, house.getPhone());
ps.setString(3, house.getAddress());
ps.setDouble(4, house.getRent());
ps.setString(5, house.getState());
ps.setString(6, house.getIntroduce());
ps.executeUpdate();
DBConnection.close(null,ps,con);
} catch (SQLException e) {
e.printStackTrace();
}
return 1;
}
//查询房屋出租列表
@Override
public List<House> selectAll() {
List<House> list = new ArrayList<>();
House house = null;
try {
Connection con = DBConnection.getConnection();
String sql = "select * from house";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()){
house = new House();
house.setId(rs.getInt(1));
house.setName(rs.getString(2));
house.setPhone(rs.getString(3));
house.setAddress(rs.getString(4));
house.setRent(rs.getDouble(5));
house.setState(rs.getString(6));
house.setIntroduce(rs.getString(7));
list.add(house);
}
DBConnection.close(rs,ps,con);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//根据房屋的id编号查看房屋信息
@Override
public House query(int id) {
House house = null;
List<House> list = new ArrayList<>();
try {
Connection con = DBConnection.getConnection();
String sql = "SELECT * FROM house WHERE id = ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1,id);
ResultSet rs = ps.executeQuery();
while (rs.next()){
house = new House();
house.setId(rs.getInt(1));
house.setName(rs.getString(2));
house.setPhone(rs.getString(3));
house.setAddress(rs.getString(4));
house.setRent(rs.getDouble(5));
house.setState(rs.getString(6));
house.setIntroduce(rs.getString(7));
list.add(house);
}
DBConnection.close(rs,ps,con);
} catch (SQLException e) {
e.printStackTrace();
}
return house;
}
//修改房屋出租信息
@Override
public int update(House house) {
try {
Connection con = DBConnection.getConnection();
String sql = "UPDATE house SET `name` = ?,phone = ?,address = ?,rent = ?,state = ?,introduce = ? where id = ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,house.getName());
ps.setString(2,house.getPhone());
ps.setString(3,house.getAddress());
ps.setDouble(4,house.getRent());
ps.setString(5,house.getState());
ps.setString(6,house.getIntroduce());
ps.setInt(7,house.getId());
ps.executeUpdate();
DBConnection.close(null,ps,con);
} catch (SQLException e) {
e.printStackTrace();
}
return 1;
}
//删除房屋出租信息
@Override
public int delete(int id) {
try {
Connection con = DBConnection.getConnection();
String sql = "delete from house where id = ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1,id);
ps.executeUpdate();
DBConnection.close(null,ps,con);
} catch (SQLException e) {
e.printStackTrace();
}
return 1;
}
}
房屋界面,处理用户 *** 作
package view;
import controller.HouseSevice;
import controller.UserSevice;
import entity.House;
import entity.User;
import tool.Utility;
import java.util.List;
/**
界面
*/
public class HouseView {
private boolean loop=true;
private char key=' ';
//显示房屋列表
private HouseSevice house_sevice=new HouseSevice();//设置数组大小为10
private UserSevice userSevice = new UserSevice();
private User user;
//编写addhouse(),接收输入,创建addhouse对象,调用add方法
public void addhouse(){
if(user == null){
System.out.println("非常抱歉~您还没有登录,不支持添加");
return;
}
System.out.println("---------添加房屋-------");
System.out.print("姓名:");
String name= Utility.readString(6);
System.out.print("电话:");
String phone= Utility.readString(11);
System.out.print("地址:");
String address= Utility.readString(14);
System.out.print("月租:");
double rent= Utility.readDouble();
System.out.print("状态:");
String state= Utility.readString(3);
System.out.print("简介:");
String introduce= Utility.readString(4);
//注意id是系统分配的
House newhouse=new House(0,name,phone,address,rent,state,introduce);
if(house_sevice.add(newhouse)){
System.out.println("添加房屋成功");
}else {
System.out.println("添加房屋失败");
}
}
//查找房屋
public void find(){
System.out.println("---------查找房屋-------");
System.out.print("请输入要查找的房屋编号:");
int f= Utility.readInt();
if(house_sevice.Find(f)==null){
System.out.println("没有找到该房屋的信息");
}else {
System.out.println("编号 房主 \t电话 \t\t地址 \t月租 \t状态(出租/未出租)\t简介\n"+house_sevice.Find(f));
}
}
//编写delhosue方法,接收输入的id,调用service的del方法
public void delhouse(){
if(user == null){
System.out.println("非常抱歉~您还没有登录,不支持删除");
return;
}
System.out.println("---------删除房屋-------");
System.out.print("请输入要删除的房屋编号(-1退出):");
int delid= Utility.readInt();
if(delid==-1){
System.out.println("---------退出删除-------");
return;
}
char c= Utility.readConfirmSelection();//调用该方法,一直循环直到用户输入y或n
if(c=='Y'){
if(house_sevice.del(delid)){
System.out.println("---------删除成功-------");
}else {
System.out.println("---------删除失败-------");
}
}else {
System.out.println("---------退出删除-------");
return;
}
}
//登录系统
public void register(){
System.out.println("---------登录系统-------");
System.out.print("请输入要您的用户名:");
String name= Utility.readString(6);
System.out.print("请输入要您的密码:");
String password= Utility.readString(10);
user = userSevice.Find(name,password);
if (user==null){
System.out.println("没有找到该用户的信息");
}else {
System.out.println("欢迎"+user.getName()+"登录系统");
}
}
//修改房屋信息
public void update(){
if(user == null){
System.out.println("非常抱歉~您还没有登录,不支持修改");
return;
}
System.out.println("---------修改房屋信息-------");
System.out.print("请输入要修改的房屋编号(-1退出):");
int up= Utility.readInt();
if(up==-1){
System.out.println("---------退出修改-------");
return;
}
int b = house_sevice.updata(up);
if (b==1) {
System.out.println("---------修改成功-------");
}else{
System.out.println("---------修改失败-------");
}
}
//房屋列表
public void listHouse(){
System.out.println("---------房屋出租列表-------");
System.out.println("编号 房主 \t\t电话 \t地址 \t月租 \t\t状态 \t\t简介");
List<House> houses=house_sevice.list();//得到所有房屋的信息
for (House house:houses) {
if(house == null){
break;
}
System.out.println(house);
}
}
//主菜单
public void main_menu(){
do{
System.out.println("---------房屋出租系统-------");
System.out.println("\t\t1 新 增 房 源");
System.out.println("\t\t2 查 找 房 屋");
System.out.println("\t\t3 删 除 房 屋");
System.out.println("\t\t4 修 改 房 屋 信 息");
System.out.println("\t\t5 房 屋 列 表");
System.out.println("\t\t6 登 录");
System.out.println("\t\t7 退 出");
System.out.print("请输入你的选择:");
key= Utility.readChar();
System.out.println(key);
switch (key){
case '1':
addhouse();
break;
case '2':
find();
break;
case '3':
delhouse();
break;
case '4':
update();
break;
case '5':
listHouse();
break;
case '6':
register();
break;
case '7':
char n= Utility.readConfirmSelection();
if(n=='Y'){
loop=false;
}
break;
default:
System.out.println("输入错误!");
}
}while (loop);
}
}
项目源码链接
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)