2015-12-01 169 views
0

我已經創建了一個代碼,讓用戶輸入一個星球的名字,它會返回該星球的發現日期,否則它會返回無效的星球名稱。創建搜索數據庫

我的問題是,我怎麼能在代碼中添加更多細節,比如重力強度。

import java.util.HashMap; 
import java.util.Map; 
import java.util.Scanner; 

public class sky { 

    private static Map < String, Integer > planetMap = new HashMap < String, Integer >(); 

    public static void main(String args[]) { 
    populateDB(); 
    Scanner scanner = new Scanner(System.in); 

    String planetName = scanner.nextLine(); 

    if (planetMap.get(planetName) != null) { 
     System.out.println("The planet " + planetName + " was found in " + planetMap.get(planetName)); 
    } else { 
     System.out.println("Invalid Planet Name"); 
    } 

    } 

    public static void populateDB() { 

    planetMap.put("Earth", 1600); 
    planetMap.put("Mars", 1500); 
    planetMap.put("Jupiter", 1100); 
    planetMap.put("Saturn", 1900); 
    planetMap.put("Venus", 1300); 
    } 
} 

對phflack和bluelurker的信貸。

+1

對於每一個星球,而不是添加的日期,添加另一個哈希,其中鍵值對保存附加數據:'earthFacts.put(「found」,1600「); planetMap.put(」Earth「,earthFacts);'。 –

+0

@MichaWiedenmann對於這個地圖的地圖可能有點矯枉過正,當我想到一個地圖時,我想到的是關鍵/對象對,其中對象的值是相似的。行星被找到的日期,行星的名字以及行星的位置都是不是很相似 – phflack

+0

[早期問題](http://stackoverflow.com/questions/34025379/joining-both-hashsets-together) – phflack

回答

2

根據您的要求,您可以創建一個Planet類和/或使用Hashmap作爲另一個Hashmap的值。

至於階級來說,已經有介紹如何做到這一點的答案,反正會給你下面一個例子:

class Planet { 
    String name; 
    String gravityStrength; 
    String universe; 

    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getGravityStrength() { 
     return gravityStrength; 
    } 
    public void setGravityStrength(String gravityStrength) { 
     this.gravityStrength = gravityStrength; 
    } 
    public String getUniverse() { 
     return universe; 
    } 
    public void setUniverse(String universe) { 
     this.universe= universe; 
    } 
} 

然後你可以有你的Hashmap與鍵名跟隨地球(字符串)和值作爲地球的對象:

HashMap<String, Planet> planets = new HashMap<String, Planet>(); 

Planet planetEarth = new Planet(); 
planetEarth.setName("Earth"); 
planetEarth.setGravityStrength("X"); 
planetEarth.setUniverse("The Milky Way"); 

planets.put("earth", planetEarth); 

或者你可以有以下hasmap,其中關鍵是這個星球作爲字符串的名稱,而值是另一個HashMap中是需要的星球細節名作爲關鍵字和行星細節值作爲值(兩個字符串)。

HashMap<String, HashMap<String, String>> planets = new HashMap<String, HashMap<String, String>>(); 

HashMap<String, String> earth = new HashMap<String, String>(); 
earth.put("name", "Earth"); 
earth.put("gravityStrength": "X4"); 
earth.put("universe", "The Milky Way"); 

planets.put("earth", earth); 

以下是Planet類實現一個什麼樣的代碼做什麼。我已經重寫了Planet類中的toString()以允許Planet對象的漂亮打印。

import java.util.HashMap; 
import java.util.Map; 
import java.util.Scanner; 

public class Sky{ 
    private static Map<String, Planet> planetMap = new HashMap<String, Planet>(); 

    public static void main(String args[]) { 
     populateDB(); 

     Scanner scanner = new Scanner(System.in); 

     System.out.print("Please input planet name: "); 
     String planetName = scanner.nextLine(); 

     //converting to lowercase so user input Earth and earth and EARTH eArTH all match 
     if (planetMap.containsKey(planetName.toLowerCase())) { 
      //invokes the overrided toString() of Planet class 
      System.out.println(planetMap.get(planetName.toLowerCase()).toString()); 
     } else { 
      System.out.println("Invalid Planet Name"); 
     } 
    } 

    public static void populateDB() { 
     Planet earth = new Planet("Earth", "4x", 1600); 
     Planet mars = new Planet("Mars", "1x", 1500); 
     Planet jupiter = new Planet("Jupiter", "3x", 1100); 
     Planet saturn = new Planet("Saturn", "16x", 1900); 
     Planet venus = new Planet("Venus", "5x", 1300); 

     planetMap.put("earth", earth); 
     planetMap.put("mars", mars); 
     planetMap.put("jupiter", jupiter); 
     planetMap.put("saturn", saturn); 
     planetMap.put("venus", venus); 
    } 
} 

class Planet { 
    String name; 
    String gravityStrength; 
    int found; 

    //empty constructor 
    public Planet() {} 

    public Planet(String name, String gravityStrength, int found) { 
     this.name = name; 
     this.gravityStrength = gravityStrength; 
     this.found = found; 
    } 

    /* Getters and setters */ 

    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 

    public String getGravityStrength() { 
     return gravityStrength; 
    } 
    public void setGravityStrength(String gravityStrength) { 
     this.gravityStrength = gravityStrength; 
    } 

    public int getFound() { 
     return found; 
    } 
    public void setUniverse(int found) { 
     this.found = found; 
    } 

    //override toString methdo to pretty-print planet 
    public String toString() { 
     return "Planet " + name + " with gravity strenght " + gravityStrength + " was found in " + found; 
    } 
} 

如果你不喜歡Planet類實現,那麼你可以隨時使用下面的Hashmap裏面執行的Hashmap:

import java.util.HashMap; 
import java.util.Map; 
import java.util.Scanner; 

public class Sky { 
    private static Map<String, HashMap<String, String>> planetMap = new HashMap<String, HashMap<String, String>>(); 

    public static void main(String args[]) { 
     populateDB(); 
     Scanner scanner = new Scanner(System.in); 

     System.out.println("Enter planet name to search for: "); 
     String planetName = scanner.nextLine(); 

     if (planetMap.containsKey(planetName.toLowerCase())) { 
      HashMap<String, String> p = planetMap.get(planetName.toLowerCase()); 
      System.out.println("The planet " + planetName + " was found in " + p.get("name") 
        + " and has gravity strenght of " + p.get("gravityStrength")); 
     } else { 
      System.out.println("Invalid Planet Name"); 
     } 

    } 

    public static void populateDB() { 
     HashMap<String, String> earthDetails = new HashMap<String, String>(); 
     earthDetails.put("name", "Earth"); 
     earthDetails.put("gravityStrength", "4X"); 
     earthDetails.put("found", "1600"); 

     HashMap<String, String> marsDetails = new HashMap<String, String>(); 
     marsDetails.put("name", "Mars"); 
     marsDetails.put("gravityStrength", "31X"); 
     marsDetails.put("found", "1500"); 

     HashMap<String, String> jupiterDetails = new HashMap<String, String>(); 
     jupiterDetails.put("name", "Jupiter"); 
     jupiterDetails.put("gravityStrength", "1X"); 
     jupiterDetails.put("found", "1100"); 

     HashMap<String, String> saturnDetails = new HashMap<String, String>(); 
     saturnDetails.put("name", "Saturn"); 
     saturnDetails.put("gravityStrength", "100X"); 
     saturnDetails.put("found", "1900"); 

     HashMap<String, String> venusDetails = new HashMap<String, String>(); 
     venusDetails.put("name", "Venus"); 
     venusDetails.put("gravityStrength", "150X"); 
     venusDetails.put("found", "1300"); 

     planetMap.put("earth", earthDetails); 
     planetMap.put("mars", marsDetails); 
     planetMap.put("jupiter", jupiterDetails); 
     planetMap.put("saturn", saturnDetails); 
     planetMap.put("venus", venusDetails); 
    } 
} 
+0

可以將它寫入原始代碼嗎? –

+0

@AmrMixy查看答案,我已經爲最初回答中討論的方法提供了實現。 – Raf

4

如果您想了解更多的細節,你可以在你的Map<String, Integer>Map<String, PlanetDetails>,其中PlanetDetails是其定義像另一個類替換:

private class PlanetDetails 
{ 
    int date_found; 

    double gravity_strength; 

    double distance_from_sun; 
} 

,然後在填充,你應該做的:

planetMap.put("Earth", new PlanetDetails(1600, gravitystrenght, distancefromsun); 
+0

你可以在原始代碼中插入嗎? –

+0

只需將類PlanetDetails定義爲類天空中的私有類成員或單獨的獨立類(如果它是獨立的,則將其導入),然後修改populateDB以填充爲:planetMap.put(「Earth」,new PlanetDetails(1600,重力,距離));我希望它很清楚。 – SomeDude

+0

只需添加到Planet上可能會更容易,但PlanetDetails可能會更有意義,因爲名稱不是很需要 – phflack