1

i have an exercise which i must write a programm in which i manage the timetable of trains.I write all the programm put i can not write the main class, because i must make an Arraylist with 3 passengers and i dont know what i must put in the blank

Code:

public class Route {
private int id;
private int aeroplane;
private String departure;
private String arrival;
private ArrayList<Ticket> Tickets = new ArrayList<>() ;
public Route(){
    id = 0 ;
    aeroplane = 0  ;
    departure = " ";
    arrival = " ";
    Tickets = new ArrayList<>();
    
}

public Route(int ID, int aerop, String depar,String arriv,ArrayList<Ticket> tick ){
    id=ID;
    aeroplane=aerop;
    departure=depar;
    arrival=arriv;
    Tickets=tick;
}



public static void main(String[] args) {
   ArrayList <Train> train=new ArrayList<>();
   Route d1= new Route(0051,50,"Greece","Italy",);// what i have to write in the last blank?
   
}}
6
  • Where is the constructor for your Route class? Commented Jun 11, 2022 at 19:06
  • Where every possible you should only receive and keep interfaces to your variables and fields. In your case your Tickets should be of type List<Ticket> Commented Jun 11, 2022 at 19:08
  • List<Ticket> tickets = new ArrayList<>(); Commented Jun 11, 2022 at 19:10
  • instead of using slightly different names for your constructor arguments, you can use this.tickets to reference the field in the class and use tickets as an argument. The assignment looks like this: this.tickets = tickets. Commented Jun 11, 2022 at 19:11
  • 1
    I want to know what i have to write in "" Route d1= new Route(0051,50,"Greece","Italy",); "" //next to the last comma Commented Jun 11, 2022 at 19:13

2 Answers 2

2

I suppose you have those three classes:

  • Train
  • Ticket - which is valid for multiple trains
  • Route - which could contain multiple Tickets

Then you should model your Route like that:

public class Route {
    private final int id;
    private final int aeroplane;
    private final String departure;
    private final String arrival;
    private final List<Ticket> tickets;

    public Route(int id, int aeroplane, String departure, String arrival, List<Ticket> tickets) {
        this.id = id;
        this.aeroplane = aeroplane;
        this.departure = departure;
        this.arrival = arrival;
        this.tickets = tickets;
    }
}

Your route class does not have, and should not have a list of lists of trains. It is perfectly fine, that you have it has a list of tickets.

To the question of how to add the list of Trains to the Route instantiation, you should create a ticket first, or multiple if you like.

public static void main(String[] args) {
    // create trains
    Train train1 = new Train(1202, "Piraeus", "Athens");
    Train train2 = new Train(1302, "Athens", "Thessaloniki");
    Train train3 = new Train(1502, "Thessaloniki", "Rome");

    // create ticket(s)
    Ticket ticket = new Ticket(95, List.of(train1, train2, train3));

    // create route and pass tickets
    Route myRoute = new Route(0051, 50, "Greece", "Italy", List.of(ticket));
}

To create the lists here I used the factory method java.util.List.of (available since Java 9). Your route class is also a good candidate for a record (since Java 18). As an record it would look like:

public record RouteRecord(int id, int aeroplane, String departure, String arrival, List<Ticket> tickets) {}

Also, think about the concept of immutability.

Sign up to request clarification or add additional context in comments.

2 Comments

"Thanks for the feedback! You need at least 15 reputation to cast a vote, but your feedback has been recorded. " @ValerijDobler i just became member of Stuck overflow and i can't vote.
So i can only say "Thank you"
1

In your main method ( it's not a class ) :

List<Ticket> tickets = new ArrayList<>();
tickets.add( new Ticket());
Route d1 = new Route(0051,50,"Greece","Italy",tickets);

and as it was mentined before you need to define a constructor :

public Route(int id, int aeroplane, String departure, String arrival, List<Ticket> tickets ){
    this.id = id;
    this.aeroplane = aeroplane;
    this.departure = departure;
    this.arrival   = arrival;
    this.Tickets   = tickets;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.