2017-12-21 6 views
0

私は宣言された配列のリストを持っています。国ごとにどのように値を取得し、結果を1つずつ表示するのですか?arraylistから値を取得する方法と、選択した国の選択に従って表示する方法はありますか?

例:国の検索の場合、オプション1:米国を選択する場合。それは私のarraylistからアメリカのすべての結果を一つ一つ取り出すでしょう。それ、どうやったら出来るの ?

public class Assignment { 

     //Declare variables 
     public static String[] country; 
     public static String[] description; 
     public static String[] travelmonth; 
     public static double[] price; 
     public static String[] travelpackage; 
     public static String[] newls; 

    public static void main(String[] args) { 

      country = new String[8]; 
      country[0] = "Australia"; 
      country[1] = "Australia"; 
      country[2] = "Australia"; 
      country[3] = "China"; 
      country[4] = "China"; 
      country[5] = "USA"; 
      country[6] = "USA"; 
      country[7] = "USA"; 

      travelmonth = new String[8]; 
      travelmonth[0] = "June"; 
      travelmonth[1] = "June"; 
      travelmonth[2] = "July"; 
      travelmonth[3] = "August"; 
      travelmonth[4] = "September"; 
      travelmonth[5] = "October"; 
      travelmonth[6] = "October"; 
      travelmonth[7] = "December"; 

      description = new String[8]; 
      description[0] = "6D5N Gold Coast Adventure"; 
      description[1] = "5D4N Melbourne Tour"; 
      description[2] = "6D5N Sydney City and Wine Tour"; 
      description[3] = "4D3N Beijing City and Great Wall Adventure"; 
      description[4] = "5D4N Hangzhou Scenic Tour"; 
      description[5] = "8D7N California Adventure"; 
      description[6] = "6D5N New York Shopping Trip"; 
      description[7] = "8D7N Los Angeles, Las Vegas Winter Wonderland"; 

      price = new double[8]; 
      price[0] = 1600; 
      price[1] = 1405.5; 
      price[2] = 1700; 
      price[3] = 880; 
      price[4] = 990; 
      price[5] = 2500; 
      price[6] = 2106.6; 
      price[7] = 2650.1; 

      travelpackage = new String[8]; 
      travelpackage[0] = "1 of 8"; 
      travelpackage[1] = "2 of 8"; 
      travelpackage[2] = "3 of 8"; 
      travelpackage[3] = "4 of 8"; 
      travelpackage[4] = "5 of 8"; 
      travelpackage[5] = "6 of 8"; 
      travelpackage[6] = "7 of 8"; 
      travelpackage[7] = "8 of 8"; 

      int choice = 0; 

      do { 

       String msg ="Main Menu\n"; 
       msg += "===========\n"; 
       msg+= "1. Display all Travel Packages\n"; 
       msg+= "2. Random tour selection\n"; 
       msg+= "3. Search by country\n"; 
       msg+= "0. Quit"; 

       //choice is an integer, therefore you need to convert 
       choice = Integer.parseInt(JOptionPane.showInputDialog(msg)); 

       if(choice == 1) { 

        displayAllPackages(); 

       } 

       if(choice == 2) { 

        JOptionPane.showMessageDialog(null, "Are you ready for your next holiday?"); 

        generateRandomPackage(); 

       } 

       if(choice == 3) { 

        searchByCountry(); 

       } 

      } 

      while(choice!=0); 

      JOptionPane.showMessageDialog(null, "Thank You for using the system"); 

     } 

    public static void searchByCountry() { 

      List<String> listString = new ArrayList<String>(); 

       listString.add("Australia"); 
       listString.add("Australia"); 
       listString.add("Australia"); 
       listString.add("China"); 
       listString.add("China"); 
       listString.add("USA"); 
       listString.add("USA"); 
       listString.add("USA"); 

      Set<String> setString = new HashSet<String>(listString); 

      listString.clear(); 

      listString.addAll(setString); 

      String msgs = ""; 

      int countryno = 0; 

         msgs = ""; //reset 
         msgs+= "Enter country number\n"; 
         msgs+= "1. " +listString.get(0) + "\n"; 
         msgs+= "2. " +listString.get(1) + "\n"; 
         msgs+= "3. " +listString.get(2) + "\n"; 

      countryno = Integer.parseInt(JOptionPane.showInputDialog(msgs));    

       if(countryno == 1) { 

        showAllUSA1(); 

       } 

    } 

    public static void showAllUSA1() { 

      String msg = ""; 
      int index = 0; 

      String inChoice =""; 

       msg = ""; //reset 
       msg+= "Search result : 1 \n"; 
       msg+= "==============\n"; 
       msg+= "Country : " +country + "\n"; 
       msg+= "Month : " +travelmonth [index] + "\n"; 
       msg+= "Description : " +description [index] + "\n"; 
       msg+= "Price : " +price [index] + "\n"; 
       msg+= "===============\n"; 
       msg+= "Enter M to return to main menu\n"; 

       for(int i=0; i< country.length; i++) 

       System.out.println(i + ":" +country[i]); 
       //inChoice = JOptionPane.showInputDialog(msg); 


      } 

} 
+2

List<TravelPlan> getListByCountry(String searchCountry) { List<TravelPlan> retValue = new ArrayList<>(); for (TravelPlan s: travelPlans) { String tempCountry = s.getCountry(); if (tempCountry != null) { if (tempCountry.equals(searchCountry)) { retValue.add(s); } } } return(retValue) } 

はでそれを呼び出します。 –

+1

一般的なアイデアは、ハッシュマップ –

+0

でPOJOクラスを作成して比較することでインデックスを見つけることです。コードを追加しようと思います。@older coderしかしエラーがあります。私はコードをどこに置くべきか分かりません – Cherlyn

答えて

1

インデックスに関連付けられた配列の束があります。それらを配列に入れるのではなく、それぞれのオブジェクトを作成してみましょう。今、あなたは、配列やリストにそれらを置くことができる

TravelPlan one = new TravelPlan ("Australia", 
           "June", 
           "6D5N Gold Coast Adventure", 
           1600, 
           "1 of 8"); 

public class TravelPlan { 
    String country; 
    String description; 
    String travelmonth; 
    double price; 
    String travelpackage; 
    String newls; 

    public TravelPlan() { 
    } 

    public TravelPlan(String country, 
         String description, 
         String travelmonth, 
         double price, 
         String travelpackage) { 
      this.country = country; 
      this.description = description; 
      this.travelmonth = travelmonth; 
      this.price = price; 
      this.travelpackage = travelpackage; 
    } 
    // add setters/getters here for example 
    String getCountry() { 
     return(country); 
    } 
    void setCountry(String country) { 
     this.country = country; 
    } 
    double getPrice() { 
    return(price); 
    } 
    void setPrice(double price) { 
    this.price = price; 
    } 
    // continue with rest of class variables here 
} 

次に、あなたがでそれをインスタンス化することができます。

java.util.List<TravelPlan> travelPlans = new java.util.ArrayList<>(); 
    travelPlans.add(one); 
    // add rest here 

このようにすると、すべての配列がオブジェクトになり、検索が容易になります。

for (TravelPlan s : travelPlans) { 
    if ("USA".equals(s.getCountry()) { 
      //we have a match for USA, do whatever ... 
    } 
} 

travelPlansは、すべての可能なTravelPlansのリストであり、次の方法で国で検索できます。あなたはこれらのフィールドを持つオブジェクトを使用する場合があります

List matches = getListByCountry("USA"); 
+0

setters/gettersの例を追加し、newlsをコンストラクタから削除しました。私はその変数が何であるか分からなかった。 –

+0

@cherlyn setter/getterのサンプルを追加し、検索の質問に一番下に答えました。 –

関連する問題