Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class FareCalc {
- public static void main(String[] args) {
- double fare = 0;
- double x = 0;
- double y = 0;
- double totalDistance = 0;
- boolean exitVar = false;
- double userDistance =0;
- String userDir ="";
- while(exitVar == false){
- userDir = JOptionPane.showInputDialog(null, "Please enter the direction you went. e.g. west\nor type 'exit' to exit. ");
- if (userDir.equals("exit")){
- exitVar = true;
- }
- else if ((!userDir.equals("south")) && (!userDir.equals("north")) && (!userDir.equals("east")) && (!userDir.equals("west"))){
- JOptionPane.showMessageDialog(null, "Error: please try again.");
- }
- else{
- userDistance = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter your distance. e.g. 0.5\nor type 'exit' to exit. "));
- }
- if (userDir.equals("north")){
- y = y+(userDistance);
- totalDistance = (totalDistance) + (userDistance);
- }
- else if (userDir.equals("south")){
- y = y-(userDistance);
- totalDistance = (totalDistance) + (userDistance);
- }
- else if (userDir.equals("east")){
- x = x+(userDistance);
- totalDistance = (totalDistance) + (userDistance);
- }
- else if (userDir.equals("west")){
- x = x-(userDistance);
- totalDistance = (totalDistance) + (userDistance);
- }
- }
- fare = 5+(8*totalDistance);
- JOptionPane.showMessageDialog(null, "You travelled a total of: " + totalDistance +" miles."+ "\nYou owe a total fare of: " + fare +" ."+ "\nYour final coordinates are: "+ "(" + x + "," + y+ ")"+".");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement