我一直在做一堆练习,但这一切都很有意义,但是当我不得不从头开始编写代码时,我很困惑,如果有人能帮我一点,那就太好了。
我有:
public class Airplane {
private HashMap<String,Integer> plane;
public Airplane() {
this.plane = new HashMap<String,Integer>();
}
public addAirplane(String id, int capacity) {
this.plane.put(id,capacity);
public class Flight {
private HashMap<Airplane, List<String>> flight;
public Flight() {
this.flight = new HashMap<Airplane,List<String>>();
public addFlight(Airplane airplane, String departurePort, String destinationPort) {
this.flight.put(airplane, Arrays.asList(departurePort, destinationPort));
}
然后我有另一个类,我创建了Plane和Flight的实例,
private Airplane airplane = new Airplane();
private Flight flight = new Flight();
并且在大多数情况下它似乎工作正常,但是我无法弄清楚我应该检索什么并在这里添加..
我从用户那里读到了飞机ID和两个机场代码,我想做这样的事情:
flight.addFlight( //here I have Airplane's name//, departureAirport, destinationAirport);
所以基本上有两个问题:1)如果只有它的名字,我如何检索“飞机”对象。 2)这架“飞机”对象究竟是什么?是HashMap吗?它是HashMap的一个实例吗?如果我尝试将HashMap而不是Airplane传递给方法flight.addFlight(Airplane,String,String),会有区别吗?
答案 0 :(得分:0)
好的,我会尝试解释你想要达到的目标。只需检查这是否是您真正想要的。
假设您要为机场设计应用程序。让我们在这个时间点保持简单。您可以稍后为自己的用例扩展它。
让我们开始设计您的应用程序。
每个Airport
都有Flights
,并且可以在这些航班上运营。因此,机场和飞行及相关对象的简单类设计如下所示:
public class Airport {
private List<Flight> allFlights;
public List<Flight> getAllFlights() {
return allFlights;
}
public void setAllFlights(List<Flight> allFlights) {
this.allFlights = allFlights;
}
/**
* Add flights
* @param flight
*/
public void addFlights(Flight flight){
allFlights.add(flight);
}
/**
* Delete flights
* @param flight
*/
public void deleteFlight(Flight flight){
for(Flight eachFlight : allFlights){
if(eachFlight.equals(flight)){
allFlights.remove(eachFlight);
break;
}
}
}
/**
* Update specific flight info.
*
* @param flightId
* @param airplane
* @param source
* @param destination
* @return
*/
public boolean updateFlight(long flightId, Airplane airplane, String source, String destination){
boolean isUpdated = false;
for(Flight eachFlight : allFlights){
if(eachFlight.getFlightId() == flightId){
eachFlight.setAirplane(airplane);
eachFlight.setSourceName(source);
eachFlight.setDestinationName(destination);
isUpdated = true;
break;
}
}
return isUpdated;
}
}
每个Flight
都有Airplane
以及来源和目的地
飞行和飞机只是普通的旧Java对象(POJO)
public class Flight {
private long flightId;
private Airplane airplane;
private String sourceName;
private String destinationName;
public long getFlightId() {
return flightId;
}
public void setFlightId(long flightId) {
this.flightId = flightId;
}
public Airplane getAirplane() {
return airplane;
}
public void setAirplane(Airplane airplane) {
this.airplane = airplane;
}
public String getSourceName() {
return sourceName;
}
public void setSourceName(String sourceName) {
this.sourceName = sourceName;
}
public String getDestinationName() {
return destinationName;
}
public void setDestinationName(String destinationName) {
this.destinationName = destinationName;
}
//Override equals method
}
public class Airplane {
private long id;
private String make;
private String airlinesCompany;
public Airplane(long id, String make, String airlinesCompany) {
this.id = id;
this.make = make;
this.airlinesCompany = airlinesCompany;
}
public long getId() {
return id;
}
public String getMake() {
return make;
}
public String getAirlinesCompany() {
return airlinesCompany;
}
}
我希望这个设计可以清除你的想法。