Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. public class FareCalc {
  4. public static void main(String[] args) {
  5.  
  6. double fare = 0;
  7. double x = 0;
  8. double y = 0;
  9. double totalDistance = 0;
  10. boolean exitVar = false;
  11. double userDistance =0;
  12. String userDir ="";
  13. while(exitVar == false){
  14. userDir = JOptionPane.showInputDialog(null, "Please enter the direction you went. e.g. west\nor type 'exit' to exit. ");
  15.  
  16. if (userDir.equals("exit")){
  17. exitVar = true;
  18. }
  19. else if ((!userDir.equals("south")) && (!userDir.equals("north")) && (!userDir.equals("east")) && (!userDir.equals("west"))){
  20. JOptionPane.showMessageDialog(null, "Error: please try again.");
  21. }
  22. else{
  23. userDistance = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter your distance. e.g. 0.5\nor type 'exit' to exit. "));
  24. }
  25. if (userDir.equals("north")){
  26. y = y+(userDistance);
  27. totalDistance = (totalDistance) + (userDistance);
  28. }
  29. else if (userDir.equals("south")){
  30. y = y-(userDistance);
  31. totalDistance = (totalDistance) + (userDistance);
  32. }
  33. else if (userDir.equals("east")){
  34. x = x+(userDistance);
  35. totalDistance = (totalDistance) + (userDistance);
  36. }
  37. else if (userDir.equals("west")){
  38. x = x-(userDistance);
  39. totalDistance = (totalDistance) + (userDistance);
  40. }
  41. }
  42. fare = 5+(8*totalDistance);
  43. JOptionPane.showMessageDialog(null, "You travelled a total of: " + totalDistance +" miles."+ "\nYou owe a total fare of: " + fare +" ."+ "\nYour final coordinates are: "+ "(" + x + "," + y+ ")"+".");
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement