I'm trying to use a key field with my array with Java but I'm confused on where to begin. My program is supposed to be that the user enters a name which would be the unique identifier and all this information would show up relating to the name kind of a mini Google. I have two classes, one is my abstract class and my other would be my main method class. If anyone could help me that would be great and very much appericated.
Here is my class.
import java.util.HashMap;
public class DataBase {
private String name;
private int year;
private String status;
private String habitat;
private String lifespan;
public DataBase() {
}
HashMap list=new HashMap();
public DataBase(String name, int year, String status, String habitat, String lifespan) {
this.name = name;
this.year = year;
this.status = status;
this.habitat = habitat;
this.lifespan = lifespan;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getHabitat() {
return habitat;
}
public void setHabitat(String habitat) {
this.habitat = habitat;
}
public String getLifeSpan() {
return lifespan;
}
public void setLifeSpan(String lifespan) {
this.lifespan = lifespan;
}
public String toString()
{
String result = "";
result += "\nName: "+ getName() + "\nYear: "+getYear()+ "\nStatus: "+getStatus()+ "\nHabitat: "+getHabitat()+"\nLifeSpan: "+getLifeSpan();
return result;
}
}
Here is my main method class.
import java.util.HashMap;
import java.util.Scanner;
public class DB
{
public static void main(String[] args)
{
Scanner in = new Scanner (System.in);
String fName;
System.out.print("Enter Name:");
fName=in.next();
String name;
name=fName+" ";
System.out.println(name);
DataBase turtle1 = new DataBase("Diamondback terrapin", 1793, "Near Threatened","East Coast of US","8 years");
DataBase turtle2 = new DataBase("Galápagos tortoise", 1535, "Vulnerable"," Galápagos Islands","At least 100 years");
DataBase turtle3 = new DataBase("West African mud turtle", 1812, "Least Concern","West and Central Africa","25 years");
DataBase turtle4 = new DataBase("Black softshell turtle", 1875, "Extinct in the Wild","South Asia","Unknown");
DataBase turtle5 = new DataBase("Radiated tortoise", 1802, "Critically Endangered","Southern Madagascar","At least 150 years");
DataBase turtle6 = new DataBase("Painted terrapin", 1844, "Critically Endangered","East Asia","Unknown");
DataBase turtle7 = new DataBase("Philippine pond turtle", 1920, "Critically Endangered ","Philippines","Unknown");
DataBase turtle8 = new DataBase("Flattened musk turtle", 1955, "Critically Endangered","Southern US","20-50 years");
DataBase turtle9 = new DataBase("McCord's box turtle", 1988, "Critically Endangered","China","Around 50 years");
DataBase turtle10 = new DataBase("Leatherback sea turtle", 1761, "Vulnerable","Atlantic and Pacfic Oceans","Around 30 years");
System.out.println(turtle1);
HashMap<String,DataBase> turtles = new HashMap<>();
turtles.put("Diamondback terrapin", new DataBase("Diamondback terrapin", 1793, "Near Threatened","East Coast of US","8 years"));
turtles.put("Galapagos tortoise", new DataBase("Galapagos tortoise", 1535, "Vulnerable"," Galapagos Islands","At least 100 years"));
turtles.put("West African mud turtle", new DataBase("West African mud turtle", 1812, "Least Concern","West and Central Africa","25 years"));
turtles.put("Black softshell turtle", new DataBase("Black softshell turtle", 1875, "Extinct in the Wild","South Asia","Unknown"));
turtles.put("Radiated tortoise", new DataBase("Radiated tortoise", 1802, "Critically Endangered","Southern Madagascar","At least 150 years"));
turtles.put("Painted terrapin", new DataBase("Painted terrapin", 1844, "Critically Endangered","East Asia","Unknown"));
turtles.put("Philippine pond turtle", new DataBase("Philippine pond turtle", 1920, "Critically Endangered ","Philippines","Unknown"));
turtles.put("Flattened musk turtle", new DataBase("Flattened musk turtle", 1955, "Critically Endangered","Southern US","20-50 years"));
turtles.put("McCord's box turtle", new DataBase("McCord's box turtle", 1988, "Critically Endangered","China","Around 50 years"));
turtles.put("Leatherback sea turtle", new DataBase("Leatherback sea turtle", 1761, "Vulnerable","Atlantic and Pacfic Oceans","Around 30 years"));
DataBase database = turtles.get("Diamondback terrapin");
DataBase databas = turtles.get("Galapagos tortoise");
DataBase databse = turtles.get("West African mud turtle");
DataBase databa = turtles.get("Black softshell turtle");
DataBase dataase = turtles.get("Radiated tortoise");
DataBase datab = turtles.get("Painted terrapin");
DataBase datase = turtles.get("Philippine pond turtle");
DataBase datae = turtles.get("Flattened musk turtle");
DataBase dataae = turtles.get("McCord's box turtle");
DataBase datbase = turtles.get("Leatherback sea turtle");
System.out.println(turtles.get(name));
}
}
UPDATE:
I can print out both the name and the information but I don't know how to connect the two together.
java.util.Mapandjava.util.HashMapfor a hint.private String[] platformsand `ArrayList list = new ArrayList();``is not needed. Use HashMap<String, Database>, where key is name.