我正在制作飞机座位预留计划,我正在开设两个班级:乘客和座位。我希望座位上有一个乘客对象,反之亦然,因为在程序中,我希望能够与乘客打印座位列表,或者如果没有乘客,打开,以及带有他们的乘客列表相应的座位。
当我将乘客插入座位时,我是否也应该使用相同的insertPassenger方法将座位插入Passenger中,或者我应该单独调用这两个方法,遵循set方法应该只做一件事的想法?此外,这款Passenger-a-Seat和Seat-a-Passenger的思维方式是否适合我的需求,还是有更好,更有效的方式减少耦合?
public class Seat {
private Passenger passenger;
//more fields and constructor etc.
public void insertPassenger(Passenger passenger) {
this.passenger = passenger;
passenger.setSeat(this); //is this line good or terrible?
}
}
public class Passenger {
//String names and stuff
private Seat seat;
//Seat is left uninitialized in constructor, is this a good idea?
public Passenger(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void setSeat(Seat seat) {
this.seat = seat;
}
}
答案 0 :(得分:0)
不使用集合包的简单解决方案
class Passenger {
private Seat seat;
void setSeat (Seat s){
this.seat = s;
}
assignSeat (Seat s){
s.setPassenger(this)
setSeat(s);
}
}
这符合你的设定者做一件事的标准。但是仅仅为了记录,为了维护你的类的不变量,setter经常需要做多个事情。
但你真正想做的是建立乘客和座位之间的关系。这称为映射。集合包中提供了地图。它需要了解泛型,这超出了本答案的范围。
new HashMap<Passenger, Seat> seatMap = new HashMap<>();
class Passenger {
assignSeat(Seat seat){
seatMap.put(this, seat);
}
}
现在,当您想要列出所有乘客及其座位时,您可以使用
之类的代码 for(Passenger p : seatMap.getKeys()){
System.println("Passenger " + p + " is in seat " seatMap.getValue(p));
}
我实际上没有编译所有这些代码。但我认为它为您提供了映射的含义。这是一种单向映射。这意味着如果您知道乘客,您可以直接找到他们的座位。如果你知道座位,你可以不直接找到乘客。您必须搜索地图中的所有乘客才能找到有座位的乘客。更糟糕的是,在上面的代码中,没有任何东西阻止你把乘客放在同一个座位上(除非检查每个乘客以确保座位还没有被其他人占用)。但所有这些缺点都适用于我的第一个解决方案。
你真正需要的是双向地图。也许Java在集合包中有一个。 :)
答案 1 :(得分:0)
如果是我,我就不会把座位当作对象。我会使用哈希地图来容纳乘客。像HashMap<String, Passenger> seats = new HashMap<String, Passenger>();
这样的东西,你可以分配像:
Passenger person = Passenger("John", "Doe");
Passenger person2 = Passenger("Jane", "Doe");
seats.add("1A", person1);
seats.add("1B", person2);
等等。然后,您可以使用键(座位)或值(乘客)获取座位的值。
使用座位对象的问题在于您没有明确的座位值,并且您也可能有重复的值。如果座位1A是否被带走将很难分辨。通过使用HashMap,您可以保证每个值都是唯一的,因为您只能拥有1个密钥。我会做一个Seat
课,而不是Flight
课。座位在OOP中并没有多大用处。座位就在某人坐的地方。它更具组织性。您使用航班执行的所有操作均基于Passenger
而非基于座位。
但这只是你可以做到这一点的众多方法之一。
答案 2 :(得分:0)
我不完全确定这会对你有用,但如果我这样做,我会这样做:
class Flight{
Set<SeatPassangerMapping> seatPassangerMapping;
}
class SeatPassengerMapping{
Seat seat;
Passenger passenger;
Flight flight;
public boolean equals(Object obj){
if(obj instance of SeatPassangerMapping){
// right your logic here on the basis of flight
}
return false
}
}
因为,乘客在不同的航班上会有不同的床单。
答案 3 :(得分:0)
您正在寻找的术语是循环依赖,甚至是循环参考。有关其他观点,请参阅this answer。
两张地图效果很好。这些将由包含人员和座位的任何东西所拥有(例如飞行课程,无论如何)。这将充当双向映射(在第三方库中也可用作实际集合类型),使您可以全面覆盖每个元素列表以及它们映射到的任何内容(如果有的话)。
由于您可能已经拥有一些确定这些链接的数据来源,因此您可以将其应用于这些地图。
// Linked in case you want to preserve insertion order
Map<Seat, Passenger> seatMap = new LinkedHashMap<>();
Map<Passenger, Seat> passengerMap = new LinkedHashMap<>();
//
// Populate the maps here.
//
// if a seat has no passenger:
// seatMap.put(seat, null);
//
void printSeats() {
for (Map.Entry<Seat, Passenger> entry : seatMap.entrySet()) {
Passenger passenger = entry.getKey();
Seat seat = entry.getValue();
// print the seat
}
}
void printPassengers() {
for (Map.Entry<Passenger, Seat> entry : passengerMap.entrySet()) {
Seat seat = entry.getKey();
Passenger passenger = entry.getValue();
// print the passenger
}
}
答案 4 :(得分:0)
我认为这个不同(只是为了补充其他答案):
航班 座位和乘客,所以我要做的是实施航班类以添加座位乘客逻辑。唯一的诀窍是你必须选择哪个乘客将是非坐位的:
- (seatNumber,null)
- (seatNumber,defaultPassenger,id = 0,name = OPEN)
醇>
我选择了两个选项:
public class Seat {
private int seatNumber;
public Seat ( )
{
}
public Seat ( int seatNumber )
{
this.seatNumber = seatNumber;
}
/**
* @return the seatNumber
*/
public int getSeatNumber ( )
{
return seatNumber;
}
/**
* @param seatNumber the seatNumber to set
*/
public void setSeatNumber ( int seatNumber )
{
this.seatNumber = seatNumber;
}
@Override
public String toString ( )
{
return "Seats [seatNumber=" + seatNumber + "]";
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode ( )
{
final int prime = 31;
int result = 1;
result = prime * result + seatNumber;
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals ( Object obj )
{
if ( this == obj )
return true;
if ( obj == null )
return false;
if ( getClass ( ) != obj.getClass ( ) )
return false;
Seat other = ( Seat ) obj;
if ( seatNumber != other.seatNumber )
return false;
return true;
}
}
public class Seat {
private int seatNumber;
public Seat ( )
{
}
public Seat ( int seatNumber )
{
this.seatNumber = seatNumber;
}
/**
* @return the seatNumber
*/
public int getSeatNumber ( )
{
return seatNumber;
}
/**
* @param seatNumber the seatNumber to set
*/
public void setSeatNumber ( int seatNumber )
{
this.seatNumber = seatNumber;
}
@Override
public String toString ( )
{
return "Seats [seatNumber=" + seatNumber + "]";
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode ( )
{
final int prime = 31;
int result = 1;
result = prime * result + seatNumber;
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals ( Object obj )
{
if ( this == obj )
return true;
if ( obj == null )
return false;
if ( getClass ( ) != obj.getClass ( ) )
return false;
Seat other = ( Seat ) obj;
if ( seatNumber != other.seatNumber )
return false;
return true;
}
}
import java.util.HashMap;
public class Flight
{
private HashMap<Seat, Passenger> seatMap;
public Flight(int numberOfSeats)
{
seatMap = new HashMap<>();
for(int i = 1; i <= numberOfSeats; i++)
{
seatMap.put ( new Seat ( i ) , new Passenger ( ) );
}
}
/**
* @return the seatMap
*/
public HashMap < Seat , Passenger > getSeatMap ( )
{
return seatMap;
}
/**
* @param seatMap the seatMap to set
*/
public void setSeatMap ( HashMap < Seat , Passenger > seatMap )
{
this.seatMap = seatMap;
}
public void sitPassenger (int seatNumber, Passenger passenger)
{
seatMap.put ( new Seat ( seatNumber ) , passenger );
}
@Override
public String toString ( )
{
return "Flight [seatMap=" + seatMap + "]";
}
}
public class mainProgram
{
public static void main(String args [])
{
//set number of seats
Flight myFlight = new Flight ( 3 );
//first passenger
Passenger firstP = new Passenger ( );
firstP.setSocialNumber ( 19993 );
firstP.setName ( "John" );
//second passenger
Passenger secondP = new Passenger ( );
secondP.setSocialNumber ( 19994 );
secondP.setName ( "Brian" );
//sit them
myFlight.sitPassenger ( 1 , firstP );
myFlight.sitPassenger ( 2 , secondP );
//output
System.out.println ( myFlight.toString ( ) );
}
}
飞行[seatMap = {
席位[seatNumber = 1] =乘客[socialNumber = 19993,name = John],
席位[seatNumber = 2] =乘客[socialNumber = 19994,name = Brian],
席位[seatNumber = 3] =乘客[socialNumber = 0,name = OPEN]
}]
此解决方案允许在飞机座位预留计划应该执行的不同航班(座位)上拥有相同的乘客,恕我直言。