JAVA 集合框架 之 ArrayList 应用
房屋信息:房主名称, 价格, 描述)
1 . 添加房屋
2 . 列出房屋的所有信息
3 . 根据 房主名称 修改房屋
4 . 按房主名称查询房屋
5 . 根据 房主名称 删除房屋
http://cn.honoit.com/home/?com=detail&id=11113296120378
import java.util.List;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author zhangd
*/
public class Test {
/**
* List 实现房地产公司对, 出租的房屋信息的管理
(房屋信息:房主名称, 价格, 描述)
1 . 添加房屋
2 . 列出房屋的所有信息
3 . 根据 房主名称 修改房屋
4 . 按房主名称查询房屋
5 . 根据 房主名称 删除房屋
*/
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());
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author zhangd
*/
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;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author zhangd
*/
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;
}
}