package com.mosun.houserent.view;
import com.mosun.houserent.domain.House;
import com.mosun.houserent.service.HouseService;
import com.mosun.houserent.utils.Utility;
// 显示界面
// 接受输入
// 完成各个操作(1.新增房源 2. 查找房屋 3.删除房屋 4.修改房屋信息 5.房屋列表 6.退出)
public class HouseView {
// 显示菜单
private boolean loop =true; //控制显示菜单
private char key= ' '; //接受用户选择
private HouseService houseService=new HouseService(8);
public void findHouse(){//查找房屋
System.out.println("**********查找房屋信息**********");
System.out.print("请输入要找出的房屋ID:");
int findId= Utility.readInt();
House house = houseService.findById(findId);
if(house!=null){
System.out.println(house);
}else{
System.out.println("*********没有查到相应房屋信息**********");
}
}
public void addHouse(){//添加房屋
System.out.println("**********添加房屋**********");
System.out.println("姓名:");
String name=Utility.readString(8);
System.out.println("电话:");
String phone=Utility.readString(12);
System.out.println("地址:");
String address=Utility.readString(16);
System.out.println("月租:");
int rent=Utility.readInt();
System.out.println("状态(填已出租/未出租):");
String state=Utility.readString(3);
//creat new house
House newHouse=new House(0,name,phone,address,rent,state);
if(houseService.add(newHouse)){
System.out.println("**********添加房屋**********");
}else{
System.out.println("**********添加房屋失败**********");
}
}
public void delHouse(){//删除房屋
System.out.println("**********删除房屋**********");
System.out.println("输入待删除房屋的编号");
int delId=Utility.readInt();
if(delId==-1){
System.out.println("**********取消删除房屋**********");
return;
}
if(delId==1){
System.out.println("请再确认一次(Y/N):");
char choice=Utility.readConfirmSelection();
if(choice=='Y'){
if(houseService.del(delId)){
System.out.println("**********删除房屋信息成功**********");
}
else {
System.out.println("**********删除房屋编号不存在**********");
}
}else{
System.out.println("**********取消删除房屋**********");
}
}
}
public void listHouses(){//房屋列表
System.out.println("**********房屋列表**********");
System.out.println("编号\t\t房主名\t\t电话\t\t地址\t\t\t月租\t\t\t状态(有无出租)");
House[] houses= houseService.list();
for (int i = 0; i < houses.length; i++) {
if(houses[i]==null)
break;
System.out.println(houses[i]);
}
System.out.println("**********房屋列表显示完毕**********");
}
public void update(){//修改房屋信息
System.out.println("**********修改房屋信息**********");
System.out.println("选择待修改房屋的编号");
int updateId = Utility.readInt();
if(updateId==-1){
System.out.println("**********放弃房屋信息**********");
return;
}
House house = houseService.findById(updateId);
if(house==null){
System.out.println("**********修改的房屋信息不存在**********");
return;
}
System.out.println("姓名("+house.getName()+"):");
String name=Utility.readString(8,"");//如果直接回车表示不修改该信息
if(!"".equals(name)){
house.setName(name);//house.setXXX()只会修改HouseService中houses数组的元素
}
System.out.println("电话("+house.getPhone()+"):");
String phone=Utility.readString(13,"");//如果直接回车表示不修改该信息
if(!"".equals(phone)){
house.setPhone(phone);
}
System.out.println("地址("+house.getAddress()+"):");
String address=Utility.readString(15,"");//如果直接回车表示不修改该信息
if(!"".equals(address)){
house.setAddress(address);
}
System.out.println("租金("+house.getRent()+"):");
int rent=Utility.readInt(-1);//如果直接回车表示不修改该信息
if(rent!=-1){
house.setRent(rent);
}
System.out.println("状态("+house.getState()+"):");
String state=Utility.readString(5,"");//如果直接回车表示不修改该信息
if(!"".equals(state)){
house.setState(state);
}
System.out.println("**********修改房屋信息成功**********");
}
public void mainMenu(){//主菜单列表
do {
System.out.println("**********房屋出租系统**********");
System.out.println("\t\t\t1. 新增房源");
System.out.println("\t\t\t2. 查找房屋");
System.out.println("\t\t\t3. 删除房屋");
System.out.println("\t\t\t4. 修改房屋信息");
System.out.println("\t\t\t5. 房屋列表");
System.out.println("\t\t\t6. 退 出");
System.out.println("请输入选择(1-6):");
key = Utility.readChar();
switch (key){
case '1':
addHouse();
break;
case'2':
findHouse();
break;
case'3':
delHouse();
break;
case'4':
update();
break;
case'5':
listHouses();
break;
case'6':
exit();
break;
}
}while ((loop));
}
public void exit(){//退出系统
char c=Utility.readConfirmSelection();
if(c=='Y'){
loop=false;
}
}
}