Java 集合 之 房屋出租实例

http://www.verejava.com/?id=17159857428338

/** 
     * List 实现房地产公司对, 出租的房屋信息的管理 
(房屋信息:房主名称,  价格, 描述) 
1 . 添加房屋 
2 . 列出房屋的所有信息 
3 . 根据 房主名称 修改房屋 
4 . 按房主名称查询房屋 
5 . 根据 房主名称  删除房屋 

     */ 

import java.util.List;


public class Test {

   
    public static void main(String[] args) {
        //实例化房地产公司
       Company company=new Company("链家地产");
       //添加房屋
       company.add(new House("李强",1500,"单间"));
       company.add(new House("王浩",3000,"一室一厅"));
       company.add(new House("张涛",4000,"两室一厅"));
       
       //根据 房主名称 修改房屋
       company.updateByOwner(new House("张涛",4500,"两室一厅"));
       
       //按房主名称查询房屋
       House house=company.findByOwner("王浩");
       System.out.println("您查找的王浩房屋信息:"+house.getOwner()+","+house.getDescription()+","+house.getPrice());
       
       //根据 房主名称  删除房屋
       company.deleteByOwner("张涛");
       
       //列出房屋的所有信息
       List houseList=company.getHouseList();
       for(int i=0;i<houseList.size();i++)
       {
           House h=(House)houseList.get(i);
           System.out.println(h.getOwner()+","+h.getPrice()+","+h.getDescription());
       }
       
    }
}


//房子类
public class House {
    private String owner;
    private double price;
    private String description;

    public House(String owner, double price, String description) {
        this.owner = owner;
        this.price = price;
        this.description = description;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
    
    
}




//出租公司类
import java.util.ArrayList;
import java.util.List;

public class Company {
    private String name;
    // company 1 对 多 house
    private List houseList;

    public Company(String name) {
        this.name = name;
        this.houseList=new ArrayList();
    }
    
    //1 . 添加房屋
    public void add(House h)
    {
        houseList.add(h);
    }
    //2 . 列出房屋的所有信息
    public List getHouseList() {
        return houseList;
    }
    
    //3 . 根据 房主名称 修改房屋
    public boolean updateByOwner(House h)
    {
        for(int i=0;i<houseList.size();i++)
        {
            House house=(House)houseList.get(i);
            if(house.getOwner().equals(h.getOwner()))
            {
                //修改
                houseList.set(i, h);
                return true;
            }
        }
        return false;
    }
    //4 . 按房主名称查询房屋
    public House findByOwner(String owner)
    {
        for(int i=0;i<houseList.size();i++)
        {
            House h=(House)houseList.get(i);
            if(h.getOwner().equals(owner))
            {
                return h;
            }
        }
        return null;
    }
    //5 . 根据 房主名称  删除房屋
    public boolean deleteByOwner(String owner)
    {
        for(int i=0;i<houseList.size();i++)
        {
            House h=(House)houseList.get(i);
            if(h.getOwner().equals(owner))
            {
                houseList.remove(i);
                return true;
            }
        }
        return false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    

    public void setHouseList(List houseList) {
        this.houseList = houseList;
    }
    
    
    
}


http://www.verejava.com/?id=17159857428338

猜你喜欢

转载自www.cnblogs.com/verejava/p/9219554.html