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);//
}
}
}
}
House
package house.model;
/**
* house类的对象表示一个房屋的信息
*/
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" +name
+"\t" +phone
+"\t" +address
+"\t\t"+rent
+"\t\t"+state;
}
}
App
package house.view;
import house.model.House;
public class App {
public static void main(String[] args) {
//创建View对象,并显示主菜单,是整个程序的入口
new houseView().main_menu();//创建一个虚拟对象调用显示主菜单的方法
System.out.println("程序已退出");
}
}