package house.sevice;
import house.model.House;
import house.tool.Utility;
/**
* house_Sevice.java<=> 类 [业务层]
* //定义house[],保存house对象
* 1 响应houseView的调用
* 2 完成对房屋信息的各种操作(增删改查)
*/
public class house_Sevice {
private House[] houses;//保存house对象
private int nums=3;//记录数组内的房屋个数
private int count=3;//id
//初始化房屋列表
public house_Sevice(int size){//传入数组大小
houses=new House[size];
houses[0]=new House(1,"Morty",1020,"纽约",111,"未出租");
houses[1]=new House(2,"莱月昴",1021,"东京",222,"未出租");
houses[2]=new House(3,"李星云",1022,"洛阳",333,"未出租");
}
public House[] list(){
return houses;//因为重写了tostring
}
//添加房屋信息
public boolean add(House newhouse){
if(nums==houses.length){
return false;
}else {
houses[nums++]=newhouse;
newhouse.setId(++count);//id自增长机制,更新newhouse的id
return true;
}
}
//删除房屋
public boolean del(int Id){
//找到要删除房屋信息元素对应的下标
int index=-1;
for (int i = 0; i < nums; i++) {
if(Id==houses[i].getId()){
index=i;
}
}
if(index==-1){
return false;
}
for (int i = index; i < houses.length-1; i++) {
houses[i]=houses[i+1];//将该位置之后的元素前移覆盖
}
houses[--nums]=null;//将数组长度减一并将最后一个元素置空
return true;
}
//查找房屋
public House Find(int id){
//找到要查找房屋信息元素对应的下标
for (int i = 0; i < nums; i++) {
if(id==houses[i].getId()){
return houses[i];
}
}
return null;
}
//修改房屋信息
public void updata(int up){
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()+"):");
int phone=Utility.readInt(0);//用户如果直接回车代表不修改,默认值为0
if(!(phone==0)){
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()+"):");
int rent=Utility.readInt(0);
if(!(rent==0)){
house.setRent(rent);
}
System.out.print("状态:("+house.getState()+"):");
String state=Utility.readString(8,"");
if(!state.equals("")){
house.setState(state);//
}
}
}
}
|