创建搜索数据库

时间:2015-12-01 18:55:12

标签: java database hashmap java.util.scanner

我创建的代码可以让用户输入行星名称,然后返回找到行星的日期,否则会返回无效的行星名称。

我的问题是,如何在代码中添加更多细节,例如重力强度。

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。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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如下所示,键作为行星名称(字符串),值为Planet对象:

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,其中key是行星的名称作为字符串,value是另一个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);
    }
}