Advertisement
veronikaaa86

03. Vacation

Feb 6th, 2022
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. package whileLoop;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P03Vacation {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. double neededMoney = Double.parseDouble(scanner.nextLine());
  10. double availableMoney = Double.parseDouble(scanner.nextLine());
  11.  
  12. double finalSum = availableMoney;
  13.  
  14. int spendingCountDays = 0;
  15. int totalDays = 0;
  16. boolean isFailed = false;
  17. while (finalSum < neededMoney) {
  18. if (spendingCountDays >= 5) {
  19. isFailed = true;
  20. break;
  21. }
  22. String action = scanner.nextLine();
  23. double amount = Double.parseDouble(scanner.nextLine());
  24.  
  25. totalDays++;
  26.  
  27. if (action.equals("spend")) {
  28. spendingCountDays++;
  29. finalSum = finalSum - amount;
  30. if (finalSum < 0) {
  31. finalSum = 0;
  32. }
  33. } else if (action.equals("save")) {
  34. spendingCountDays = 0;
  35. finalSum = finalSum + amount;
  36. }
  37. }
  38.  
  39. if (isFailed) {
  40. System.out.println("You can't save the money.");
  41. System.out.println(totalDays);
  42. } else {
  43. System.out.printf("You saved the money for %d days.", totalDays);
  44. }
  45. }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement