Hi there i am trying to write a java code that will essentially be like a gps but a lot simplier and i need to make a mini map about 4 by 3. where Dunada and Government is the first intersection and i have already done so by using multiple nested if statements, now i know that there is a much better and easier solution, i was wondering if you could help please!!
This is what i have done so far,
String[] streetNamesX={"Dundas","Younge","University"};
String [] streetNamesY = {"Government","Elm","Alder","Rockland"};
public void takeuser()
{
Moving move;;
move = new Moving();
String user = "";
String user2 = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter where you would like to go (X enter) (Y enter): ");
try {
user = reader.readLine();
user2 = reader.readLine();}
catch (IOException e) {
e.printStackTrace(); }
System.out.print("You entered : " + user + " and " +user2);
String OrignalchoiceX = user;
for (String j: streetNamesY) {
for (String i: streetNamesX){
if (user.equalsIgnoreCase(i) && user2.equalsIgnoreCase(j)){
System.out.println(" and your destination is set to "+i + " and "+j);
}
} }
if (user2.equals(streetNamesY[0]) ){
if (user.equals(streetNamesX[0])){
}
else if (OrignalchoiceX.equals(streetNamesX[1])){
move.moveDown();
System.out.println("To return from this location to your origin ");
move.moveUp();
}
else if (OrignalchoiceX.equals(streetNamesX[2])){
move.moveDown();
move.moveDown();
System.out.println("To return from this location to your origin ");
move.moveUp(); move.moveUp();
}
}
else if (user2.equals(streetNamesY[1]) ){
if (OrignalchoiceX.equals(streetNamesX[0])){
move.moveforward();
System.out.println("To return from this location to your origin");
move.moveback();
}
else if (OrignalchoiceX.equals(streetNamesX[1])){
move.moveforward();
move.moveDown();
System.out.println("To return from this location to your origin");
move.moveback();
move.moveUp();
}
else if (OrignalchoiceX.equals(streetNamesX[2])){
move.moveforward();
move.moveDown();
move.moveDown();
System.out.println("To return from this location to your origin");
move.moveback();
move.moveUp(); move.moveUp();
}
}
else if (user2.equals(streetNamesY[2])){
if (OrignalchoiceX.equals(streetNamesX[0])){
move.moveforward();
move.moveforward();
System.out.println("To return from this location to your origin");
move.moveback(); move.moveback();
}
else if (OrignalchoiceX.equals(streetNamesX[1])){ // Johnson
move.moveDown();
move.moveforward();
move.moveforward();
System.out.println("To return from this location to your origin");
move.moveback();move.moveback();
move.moveUp();
}
else if (OrignalchoiceX.equals(streetNamesX[2])){
move.moveDown();
move.moveDown();
move.moveforward();
move.moveforward();
System.out.println("To return from this location to your origin");
move.moveback();
move.moveback();
move.moveUp();move.moveUp();} }
else if (user2.equals(streetNamesY[3])){
if (OrignalchoiceX.equals(streetNamesX[0])){
move.moveforward();
move.moveforward();
move.moveforward();
System.out.println("To return from this location to your origin");
move.moveback();
move.moveback();move.moveback();
}
else if (OrignalchoiceX.equals(streetNamesX[1])){
move.moveDown();
move.moveforward();
move.moveforward();
move.moveforward();
System.out.println("To return from this location to your origin");
move.moveback();
move.moveback();move.moveback();
move.moveUp();
}
else if (OrignalchoiceX.equals(streetNamesX[2])){
move.moveDown();
move.moveDown();
move.moveforward();
move.moveforward();
move.moveforward();
System.out.println("To return from this location to your origin");
move.moveback();
move.moveback();
move.moveback();
move.moveUp();move.moveUp();} }
}
}
where the move.moveback() or move.forward() is just a print statement saying move in that direction. i would like to use hashsets, or anykind of suggestion on how to improve my code would help,
thanks