简体   繁体   English

我无法弄清楚 getTotalFlightTime()

[英]I cannot figure out getTotalFlightTime()

The Itinerary class stores the information about itinerary with the following members:行程 class 存储有关行程的信息,其中包含以下成员:

• A private ArrayList data field named flights that contains the flights for the itinerary in increasing order of departureTime. • 名为航班的私有ArrayList 数据字段,其中包含按出发时间升序排列的行程航班。 (Hint: You do not need to do the sorting.) (提示:您不需要进行排序。)

• A constructor that creates an itinerary with the specified flights in ArrayList type. • 使用 ArrayList 类型的指定航班创建行程的构造函数。

• A method named getTotalFlightTime() that returns the total flight time of the itinerary in minutes. • 名为getTotalFlightTime() 的方法,以分钟为单位返回行程的总飞行时间。 (Hint: Invoke the getFlightTime() method for each Flight object.) (提示:为每个 Flight object 调用 getFlightTime() 方法。)

• A method named getTotalTravelTime() that returns the total travel time in minutes from the departure time of the first flight to the arrival time of the last flight in the itinerary. • 一个名为getTotalTravelTime() 的方法,它返回行程中从第一个航班的出发时间到最后一个航班的到达时间的总旅行时间(以分钟为单位)。 Assume all the times are in the same time zone.假设所有时间都在同一个时区。

package que6;

/**
 *
 * @author vpi764
 */


import java.util.ArrayList;
import java.util.GregorianCalendar;
public class TestFlightItinerary {


    public static void main(String[] args) {

  lic static void main(String[] args) {
        ArrayList<Flight>flights = new ArrayList<>();
  Flight F1 =  new Flight("US230", new GregorianCalendar(2014, 5, 5, 5, 5, 0), new GregorianCalendar(2014, 5, 5, 6, 15, 0));
    Flight F2 =  new Flight("US235", new GregorianCalendar(2014, 5, 5, 6, 55, 0), new GregorianCalendar(2014, 5, 5, 7, 45, 0));
      Flight F3 =  new Flight("US237", new GregorianCalendar(2014, 5, 5, 9, 35, 0), new GregorianCalendar(2014, 5, 5, 12, 55, 0));

      flights.add(F1);
      flights.add(F2);
      flights.add(F3);

 }

}
class Flight{

        private String flightNo;
        private GregorianCalendar departureTime;
        private GregorianCalendar arrivalTime;


        //Constructor
        Flight(String SpecNo,GregorianCalendar SpecDtime ,GregorianCalendar SpecAtime ){
            this.flightNo = SpecNo;
            this.departureTime = SpecDtime;
            this.arrivalTime = SpecAtime;
        }
        //Getters
        public String GetFlightNo() {
            return flightNo;
        }
        public GregorianCalendar GetDtime() {
            return departureTime;
        }
        public GregorianCalendar GetAtime() {
            return arrivalTime;
        }

        //setters
        // If we don't use setter the could would still work because we are getting input in constructor with parameters
        public void SetFlightNo(String Number) {
            flightNo = Number;
        }
        public void SetDtime(GregorianCalendar Dtime) {
            departureTime = Dtime;
        }
        public void SetAtime(GregorianCalendar Atime) {
            arrivalTime = Atime;
        }

        public long getTimeinMillis() {
                return (arrivalTime.getTimeInMillis() - departureTime.getTimeInMillis());
                }
                // getFlightTime method
                public long getFlightTime() {
                return getTimeinMillis() / (60 * 1000);
                }
    }
class Itinerary {


    ArrayList<Flight> flights = new ArrayList<>();

   public Itinerary(){
           this.flights = flights;
    }
    public long getTotalFlightTime(ArrayList<Flight> flights){

    }
    public long getTotalTravelTime(){

    }


}

ArrayList maintain the order in which elements are added. ArrayList 保持添加元素的顺序。 May be thats the cue.可能就是这个提示。

You can use either Comparable or Comparator on class flight.您可以在 class 航班上使用 Comparable 或 Comparator。 For compareTo and compare function use departure time as deciding factor for sorting.对于 compareTo 和比较 function 使用出发时间作为排序的决定因素。

You will need to call Collections.sort in order to sort arraylist.您需要调用 Collections.sort 来对 arraylist 进行排序。 It does not get done by default.默认情况下不会完成。 On how to use, please read https://www.geeksforgeeks.org/comparator-interface-java/使用方法请阅读https://www.geeksforgeeks.org/comparator-interface-java/

For total flight time you need to find difference of list.get(list.size()-1).GetDtime() list.get(0).GetDtime()对于总飞行时间,您需要找到 list.get(list.size()-1).GetDtime() list.get(0).GetDtime() 的差异

For travel time.对于旅行时间。 You will need to loop through arraylist and do sum of getTimeinMillis() for each flight object.您将需要遍历 arraylist 并对每个航班 object 进行 getTimeinMillis() 的总和。

I hope this will help.我希望这将有所帮助。

If I am understanding the instructions correctly, you do not need to do the sorting of the ArrayList.如果我正确理解说明,则无需对 ArrayList 进行排序。 All you are required to do is define a constructor that accepts an ArrayList of flights.您需要做的就是定义一个接受 ArrayList 航班的构造函数。

Your Itinerary class should be:您的行程 class 应该是:

class Itinerary {
    ArrayList<Flight> flights; // no need to instantiate, flights passed in as arg

    public Itinerary(ArrayList<Flight> flights) {
        this.flights = flights;
    }

    public long getTotalFlightTime(){
        // Do stuff
    }
    public long getTotalTravelTime(){
        // Do Stuff
    }
}

In your main method you would then add the following:然后在您的主要方法中添加以下内容:

ArrayList<Flight> flights = new ArrayList<>();
flights.addAll(F1, F2, F3);
Itinerary myItinerary = new Itinerary(flights);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM