Advertisement
GabrielHr00

Vacation

Nov 27th, 2022
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. package S5_WhileLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Vacation {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. double tripPrice = Double.parseDouble(scanner.nextLine());
  9. double currentMoney = Double.parseDouble(scanner.nextLine());
  10.  
  11. // save days info
  12. int spendInRowCount = 0;
  13. int allDaysCount = 0;
  14.  
  15. // read first command
  16. String command = scanner.nextLine();
  17. while(true) {
  18. // read spend or save
  19. String operator = command;
  20. // read money for spending/saving
  21. double price = Double.parseDouble(scanner.nextLine());
  22. allDaysCount++;
  23.  
  24. // check if we should spend or save
  25. if(operator.equals("spend")) {
  26. spendInRowCount++;
  27. currentMoney = currentMoney - price;
  28.  
  29. // set current money to 0 when we spend more than we have
  30. if(currentMoney < 0) {
  31. currentMoney = 0;
  32. }
  33.  
  34. } else if(operator.equals("save")){
  35. currentMoney = currentMoney + price;
  36. spendInRowCount = 0;
  37. }
  38.  
  39. // if we saved enough money - break
  40. if(currentMoney >= tripPrice) {
  41. break;
  42. }
  43.  
  44. // if we spent too much days spending money - break
  45. if(spendInRowCount == 5){
  46. break;
  47. }
  48.  
  49. command = scanner.nextLine();
  50. }
  51.  
  52. // check if we have enough money for trip
  53. if (currentMoney >= tripPrice) {
  54. System.out.printf("You saved the money for %d days.", allDaysCount);
  55. } else {
  56. System.out.printf("You can't save the money.%n%d", allDaysCount);
  57. }
  58.  
  59. }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement