package houserent.view;
import houserent.domain.House;
import houserent.service.HouseService;
import houserent.utils.Utility;
/*
*1.显示界面
*2.接收用户的输入
*3.调用HouseService完成对房屋信息的各种操作
* */
public class Houseview {
//1.显示主菜单
private boolean loop = true;
private char key = ' ';
private HouseService houseService = new HouseService(10);
public void updateHouse(){
System.out.println("=================修改房屋===================");
System.out.println("=================请选择修改的房屋编号(-1退出)===================");
int UpdateId = Utility.readInt();
if (UpdateId == -1){
return;
}
House house = houseService.findById(UpdateId);
if (house == null){
System.out.println("=================你要修改的房屋信息不存在无法修改===================");
return;
}
//值得注意的是,引用类型是存在堆里的,谁指向他都能直接修改。
//返回的是引用类型也就是数组中的元素 ,因此在后对house.setXX(),就会对HouseService中的houses数组进行修改
System.out.println("姓名("+ house.getName() +")");
//读取修改的信息,有可能直接回车,如果是空的,那么就是不修改,如果不是空的那么就修改
String name = Utility.readString(8,"");//这里如果用户直接回车的话,表示不修改信息
if(!"".equals(name)){
house.setName(name);
}
System.out.println("电话(" + house.getPhone() + ")");
String phone = Utility.readString(12,"");
if(!"".equals(phone)){
house.setPhone(phone);
}
System.out.println("地址(" + house.getAddress() + ")");
String address = Utility.readString(18,"");
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(18,"");
if(!"".equals(address)){
house.setState(state);
}
System.out.println("=================修改房屋信息成功===================");
}
public void findHouse(){
System.out.println("=================查找房屋===================");
System.out.println("=================请输入你要查找的id===================");
int findId = Utility.readInt();
//调用方法
House house = houseService.findById(findId);
if(house != null){
System.out.println(house);
System.out.println("=================查找成功===================");
}
else{
System.out.println("=================查找的id不存在===================");
}
}
public void exit(){
char c = Utility.readConfirmSelection();
if(c == 'Y'){
loop = false;
}
}
public void delHouse(){
System.out.println("=================删除房屋===================");
System.out.println("============请选择房屋编号(-1退出)==============");
int detId = Utility.readInt();
if(detId == -1){
System.out.println("===========放弃删除房屋===================");
return;
}
//注意该方法本身就有循环判断的逻辑,只能输出Y或者N
char choice = Utility.readConfirmSelection();
if(choice == 'Y'){//真的删除了
if(houseService.delHouse(detId)){
System.out.println("===========删除房屋成功===================");
}
else{
System.out.println("===========删除房屋失败,编号不存在===================");
}
}
}
public void addHouses(){
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(18);
System.out.println("月租:");
int rent = Utility.readInt();
System.out.println("状态:");
String state = Utility.readString(3);
//创建一个新的House对象,注意id是系统分配的
House newHouse = new House(0, name, phone, address, rent, state);
if(houseService.addHouse(newHouse)){
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状态(未出租/已出租)" );
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("==================房屋列表显示完毕===================");
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("请输入你的选择");
key = Utility.readChar();
switch (key){
case '1':
addHouses();
break;
case '2':
findHouse();
break;
case '3':
delHouse();
break;
case '4':
updateHouse();
break;
case '5':
listHouses();
break;
case '6':
System.out.println("退出");
exit();
break;
}
}
while(loop);
}
}