比较飞行数据库中的字符串?

huangapple 未分类评论44阅读模式
英文:

Comparing Strings for flight database?

问题

以下是翻译好的代码部分:

import java.util.ArrayList;

public class FlightDatabase {
    ArrayList<Flight> flights = new ArrayList<Flight> ();

    public FlightDatabase() {
        this.flights.add(new Flight("Wroclaw", "Berlin"));
        this.flights.add(new Flight("Warsaw", "Poznan"));
        this.flights.add(new Flight("Krakow", "Vienna"));
        this.flights.add(new Flight("Wroclaw", "Rotterdam"));
        this.flights.add(new Flight("Warsaw", "Moscow"));
        this.flights.add(new Flight("Wroclaw", "Frankfurt"));
        this.flights.add(new Flight("Warsaw", "London"));
        this.flights.add(new Flight("Krakow", "Paris"));
    }

    public void checkIfFlightExists(String departure, String arrival) {
        for(int i = 0; i < flights.size(); i++) {
            Flight check = flights.get(i);
            if (departure.equals(check.getDeparture()) && arrival.equals(check.getArrival())) {
                System.out.println("存在这样的航班");
            } else {
                System.out.println("抱歉,没有这样的航班");
            }
        }
    }
}

请注意,我已经将代码中的中文内容进行了翻译,同时稍作调整以确保代码逻辑正确。如果您对这些更改有任何疑问,请随时提问。

英文:

My task was to create a list of flights using ArrayList. I've created a Main class, a Flight class and FlightDatabase class,and in FlightDatabase I've added a few new Flights with departure and array Strings in ().

I was supposed to create a method in this class which checks if a searched flight exists. The whole goal was to practise the usage of .equals method with Strings. The problem is, I don't know how to encode this all together, my idea is to create a method which takes the String variables from Flight class constructor and compare them with the Strings added in ArrayList of Flights in FlightDatabase class. No idea how to achieve that.

Here's what I did already. Doesn't work the way i wanted, i even know why, but i have no idea how to make it work.

import java.util.ArrayList;

public class FlightDatabase {
    ArrayList&lt;Flight&gt; flights = new ArrayList&lt;Flight&gt; ();

    public FlightDatabase ( ) {
        this.flights.add ( new Flight ( &quot;Wroclaw&quot;, &quot;Berlin&quot; ) );
        this.flights.add ( new Flight ( &quot;Warsaw&quot;, &quot;Poznan&quot; ) );
        this.flights.add ( new Flight ( &quot;Krakow&quot;, &quot;Vienna&quot; ) );
        this.flights.add ( new Flight ( &quot;Wroclaw&quot;, &quot;Rotterdam&quot; ) );
        this.flights.add ( new Flight ( &quot;Warsaw&quot;, &quot;Moscow&quot; ) );
        this.flights.add ( new Flight ( &quot;Wroclaw&quot;, &quot;Frankfurt&quot; ) );
        this.flights.add ( new Flight ( &quot;Warsaw&quot;, &quot;London&quot; ) );
        this.flights.add ( new Flight ( &quot;Krakow&quot;, &quot;Paris&quot; ) );
    }

    public void checkIfFlightExists (String departure, String arrival ) {
        for(int i = 0; i &lt; flights.size (); i++) {
            Flight check = flights.get ( i );
            if (departure.equals ( check ) &amp;&amp; arrival.equals ( check )){
                System.out.println (&quot;there is such flight&quot;);


            }else {
                System.out.println (&quot;No such flight sorry&quot;);
            }
        }


    }
}

huangapple
  • 本文由 发表于 2020年4月5日 09:24:11
  • 转载请务必保留本文链接:https://java.coder-hub.com/61036913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定