Guest User

Untitled

a guest
Oct 31st, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class BakingRush {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int energy = 100;
  8.         int coins = 100;
  9.         boolean bankrupt = false;
  10.         String[] dayEvents = scanner.nextLine().trim().split("\\|");
  11.  
  12.         for (int i = 0; i < dayEvents.length; i++) {
  13.             String[] event = dayEvents[i].split("-");
  14.             if (event[0].equals("rest")) {
  15.                 int newEnergy = Integer.parseInt(event[1]);
  16.                 if (energy + newEnergy >= 100) {    //energy cannot exceed your initial energy (100). Print difference to 100.
  17.                     System.out.println("You gained " + (100 - energy) + " energy.");
  18.                     energy = 100;
  19.                 } else {
  20.                     energy += newEnergy;
  21.                     System.out.println("You gained " + newEnergy + " energy.");
  22.                 }
  23.                 System.out.println("Current energy: " + energy + ".");
  24.  
  25.             } else if (event[0].equals("order")) {
  26.                 if ((energy - 30) >= 0) {
  27.                     energy -= 30;
  28.                     System.out.println("You earned " + event[1] + " coins.");
  29.                     coins += Integer.parseInt(event[1]);
  30.  
  31.                 } else { //your energy drops below 0, you skip the order and gain 50 energy points
  32.                     energy += 50;
  33.                     System.out.println("You had to rest!");
  34.                 }
  35.  
  36.             } else {
  37.                 String ingredient = event[0];
  38.                 int cost = Integer.parseInt(event[1]);
  39.                 coins -= cost;
  40.                 if (coins > 0) {
  41.                     System.out.println("You bought " + ingredient + ".");
  42.                 } else {
  43.                     System.out.println("Closed! Cannot afford " + ingredient + ".");
  44.                     bankrupt = true;
  45.                 }
  46.  
  47.             }
  48.             if (bankrupt) {
  49.                 break;
  50.             }
  51.         }
  52.         //ending
  53.         if (!bankrupt) {
  54.             System.out.println("Day completed!");
  55.             System.out.println("Coins: " + coins);
  56.             System.out.println("Energy: " + energy);
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment