Hello I am a fairly new to Java, below I have created a program for calculating the distance between two cites that is inputed by the user. For the second part of my program I need to have a menu system and the constant SPEED value to change according to the type of road that is selected by the user before hand in the menu system.
So I figure I need to the change the constant Speed value depending on what the user has selected before hand, I understand that I need to use some sort of 'switch' and methods to cut the code down (which I don't quite get), Also how would I get the program work again without restarting from the beginning, would this be done by a boolean value? Is there a book you recommend reading or a similar piece of code I could look through to get an idea where to start. It took me a while just to do this which I understantd is pretty basic so any help would be appreciated.
import java.math.*;
public class TravelPrograms {
public static final double SPEED = 50;
public static void main(String[] args) {
//local variables
String city1;
String city2;
int hours;
int mins;
int distance;
double timeInMins = 0;
int timeInHours;
// prints out user prompt
System.out.println ("Welcome to Isfandyar Ali Travel Calculator" + "\n");
System.out.println ("====================================================="+ "\n");
System.out.print ("Please enter the name of the first city: ");
city1 = UserInput.readString();
System.out.println(); // insert blank line
System.out.print ("Please enter the name of the second city: ");
city2 = UserInput.readString();
System.out.println(); // insert blank line
System.out.print ("please enter the distance in miles: ");
distance = UserInput.readInt();
System.out.println ("============================Result========================"+ "\n");
//Error message recieved if the distance is negative
if (distance < 1) {
System.out.println ("Error. distance must be a positive nmumber");
}
else {
timeInMins = (distance/SPEED*60);
timeInHours = (int)(timeInMins/60);
timeInMins = timeInMins -(timeInHours *60);
timeInMins = Math.round(timeInMins);
mins = (int) timeInMins;
hours = (int) timeInHours;
System.out.println("The time required to travel " + distance+" miles"+" between " + city1+ "and " + city2 +" is " + hours+" hours and "+mins+" minutes"+ "\n");
}
System.out.println ("===================================================="+ "\n");
System.out.println ("Please enter C to continue or any other character to quit: q"+ "\n");
System.out.print ("Thank you for using Travel program");
} //end of method
} // end of class