Advertisement
stoyanoff

Vacation

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