Java 集合 之 List 酒店房间管理实例
http://www.verejava.com/?id=1699454145722
package com.hotel;
import java.util.List;
public class TestHotel
{
public static void main(String[] args)
{
//实例化酒店
Hotel h=new Hotel("汉庭快捷酒店","北京市朝阳区望京阜通东大街金兴路2号","坐落于北京望京商务圈");
Room bitRoom=new Room("大床房",280);
//添加房间价格种类
bitRoom.addRoomType(new RoomType("大床房A",284));
bitRoom.addRoomType(new RoomType("大床房 含早餐",303));
bitRoom.addRoomType(new RoomType("大床房 含免费宽带",303));
//添加房间种类
h.add(bitRoom);
h.add(new Room("高级大床房",290));
h.add(new Room("双床房",290));
h.add(new Room("家庭房",332));
h.add(new Room("商务房",332));
//显示酒店信息
System.out.println("酒店信息:");
System.out.println(h.getName()+","+h.getAddress()+","+h.getDescription());
System.out.println("酒店房间信息");
List<Room> roomList=h.getRoomList();
for(int i=0;i<roomList.size();i++)
{
Room room=roomList.get(i);
System.out.println(room.getRoomName()+","+room.getStartPrice());
//打印房间种类价格
List<RoomType> roomTypeList=room.getRoomTypeList();
for(int j=0;j<roomTypeList.size();j++)
{
RoomType roomType=roomTypeList.get(j);
System.out.println("----"+roomType.getTypeName()+","+roomType.getTypePrice());
}
}
}
}
package com.hotel;
public class RoomType
{
private String typeName;
private double typePrice;
public RoomType(String typeName, double typePrice)
{
super();
this.typeName = typeName;
this.typePrice = typePrice;
}
public String getTypeName()
{
return typeName;
}
public void setTypeName(String typeName)
{
this.typeName = typeName;
}
public double getTypePrice()
{
return typePrice;
}
public void setTypePrice(double typePrice)
{
this.typePrice = typePrice;
}
}
package com.hotel;
import java.util.ArrayList;
import java.util.List;
public class Room
{
private String roomName;
private double startPrice;
private List<RoomType> roomTypeList;
public Room(String roomName, double startPrice)
{
super();
this.roomName = roomName;
this.startPrice = startPrice;
roomTypeList=new ArrayList<RoomType>();
}
/*
* 添加房间类型
*/
public void addRoomType(RoomType roomType)
{
roomTypeList.add(roomType);
}
public List<RoomType> getRoomTypeList()
{
return roomTypeList;
}
public void setRoomTypeList(List<RoomType> roomTypeList)
{
this.roomTypeList = roomTypeList;
}
public String getRoomName()
{
return roomName;
}
public void setRoomName(String roomName)
{
this.roomName = roomName;
}
public double getStartPrice()
{
return startPrice;
}
public void setStartPrice(double startPrice)
{
this.startPrice = startPrice;
}
}
package com.hotel;
import java.util.ArrayList;
import java.util.List;
public class Hotel
{
private String name;
private String address;
private String description;
private List<Room> roomList;
public Hotel(String name, String address, String description)
{
super();
this.name = name;
this.address = address;
this.description = description;
roomList=new ArrayList<Room>();
}
/*
* 添加房间
*/
public void add(Room room)
{
roomList.add(room);
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public List<Room> getRoomList()
{
return roomList;
}
public void setRoomList(List<Room> roomList)
{
this.roomList = roomList;
}
}