目录
架构设计
House类
HouseView类
HouseService类
HouseRentApp类
public class House {
//编号,房主,电话,地址,月租,出租状态(已出租/未出租)
private int id;
private String name;
private int phone;
private String address;
private int rent;
private String state;
public House(int id, String name, int phone, String address, int rent, String state) {
this.id = id;
this.name = name;
this.phone = phone;
this.address = address;
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 int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getRent() {
return rent;
}
public void setRent(int 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" + address +
"\t" + rent +
"\t" + state ;
}
}
public class HouseView {
private boolean loop=true;//控制显示菜单
private final HouseService houseService=new HouseService(10);//设置房屋数组大小为10
//根据id修改房源信息
public void update(){
System.out.println("================修改房源信息================");
System.out.println("请输入你要修改的房源的编号:(-1退出)");
int updateId = Utility.readInt();
//-1退出
if(updateId==-1){
System.out.println("===============放弃修改房源信息===============");
return;
}
//根据输入得到的updateId,查找对象
House updateHouse = houseService.findById(updateId);
if(updateHouse==null){
System.out.println("================你要修改的房源id不存在================");
return;
}
//name
System.out.println("姓名("+updateHouse.getName()+")更改为:");
//如果回车默认返回空字符串""
String updateName = Utility.readString(12, "");
if(!"".equals(updateName)){
updateHouse.setName(updateName);
}
//phone
System.out.println("电话("+updateHouse.getPhone()+")更改为:");
//如果直接回车默认返回-1
int updatePhone = Utility.readInt(-1);
if(updatePhone!=-1){
updateHouse.setPhone(updatePhone);
}
//address
System.out.println("地址("+updateHouse.getAddress()+")更改为:");
//如果回车默认返回空字符串""
String updateAddress = Utility.readString(12, "");
if(!"".equals(updateAddress)){
updateHouse.setName(updateAddress);
}
//rent
System.out.println("租金("+updateHouse.getRent()+")更改为:");
//如果直接回车默认返回-1
int updateRent = Utility.readInt(-1);
if(updateRent!=-1){
updateHouse.setPhone(updateRent);
}
//state
System.out.println("状态("+updateHouse.getState()+")更改为:");
//如果回车默认返回空字符串""
String updateState = Utility.readString(12, "");
if(!"".equals(updateState)){
updateHouse.setName(updateState);
}
System.out.println("==============修改房源信息成功==============");
}
//根据id查找房源信息
public void findHouse(){
System.out.println("================查找房源信息================");
System.out.println("请输入要查找房源的id:");
int findId = Utility.readInt();
//调用findById()方法
House goalHouse= houseService.findById(findId);
if(goalHouse!=null){
System.out.println(goalHouse);
}else{
System.out.println("================你查找的房源id不存在================");
}
}
//完成退出确认
public void exit(){
char c = Utility.readConfirmSelection();
if(c=='Y'){
this.loop=false;
System.out.println("================退出房屋出租系统================");
}
}
//编写delHouse() 接收输入的id,调用Service的del方法
public void delHouse(){
System.out.println("================删除房屋================");
System.out.println("输入待删除房屋的编号(-1退出):");
//输入编号
int delId=Utility.readInt();
if(delId==-1){
System.out.println("================放弃删除房屋信息================");
return;
}
//选择是否删除某一房屋信息
char choice = Utility.readConfirmSelection();
if(choice=='Y'){
if(houseService.del(delId)){
System.out.println("================删除房屋信息成功================");
}else{
System.out.println("================房屋编号不存在,删除失败================");
}
}else{
System.out.println("================放弃删除房屋信息================");
}
}
//编写addHouses()方法来直接添加House对象,方法体内需调用add方法
public void addHouses(){
System.out.println("================添加房屋================");
System.out.println("输入姓名:");
String name = Utility.readString(8);//要求name长度8以内
System.out.println("输入电话:");
int phone = Utility.readInt();
System.out.println("输入地址:");
String address = Utility.readString(8);
System.out.println("输入月租:");
int rent = Utility.readInt();
System.out.println("输入状态:");
String state = Utility.readString(8);
//创建一个新的House对象,注意id是由1开始的自增长序列,这里先将id置0,随后重新赋值
House newHouse = new House(0, name, phone, address, rent, state);
if(houseService.add(newHouse)){
System.out.println("================添加房屋成功================");
}else{
System.out.println("================添加房屋失败================");
}
}
//编写listHouses()显示房屋列表
public void listHouses(){
System.out.println("================房屋列表================");
System.out.println("编号\t房主\t电话\t地址\t月租\t出租状态(未出租/已出租)");
House[] houses = houseService.list();//得到所有房屋信息
for(int i=0;i
public class HouseService {
private House[] houses;//保存House对象
private int houseNum=1;//记录当前有多少个房屋信息,随着增删而动态增减
private int idCounter=1;//记录当前已经添加了idCounter个房屋信息
public HouseService(int size){//带参构造方法
houses=new House[size];//初始化房子数组的size
houses[0]=new House(1,"jack",123,"海淀区",2000,"未出租");
}
//find()方法,返回House对象
public House findById(int findId){
//遍历数组
for(int i=0;i
public class HouseRentApp {
public static void main(String[] args) {
//创建 HouseView对象,调用方法显示主菜单,是整个程序的入口
new HouseView().mainMenu();
}
}