SHARE
TWEET

Untitled

shniaga Mar 5th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DemoMidExam;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class BreadFactory {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String[] input = (scanner.nextLine().split("\\|+"));
  11.         int initialEnergy = 100;
  12.         int initialCoins = 100;
  13.         boolean isClosed = false;
  14.  
  15.         for (int i = 0; i < input.length; i++) {
  16.             String[] command = input[i].split("-");
  17.             String eventOrIngredient = command[0];
  18.             int number = Integer.parseInt(command[1]);
  19.  
  20.             if (eventOrIngredient.equals("rest")) {
  21.                 if (number <= 0 || initialEnergy == 100) {
  22.                     System.out.println("You gained 0 energy.");
  23.                     System.out.println("Current energy: " + initialEnergy + ".");
  24.                 } else if (initialEnergy + number >= 100) {
  25.                     System.out.println(String.format("You gained %d energy.",100-initialEnergy));
  26.                     initialEnergy = 100;
  27.                     System.out.println("Current energy: 100.");
  28.                 } else {
  29.                     System.out.println(String.format("You gained %d energy.",number));
  30.                     initialEnergy += number;
  31.                     System.out.println("Current energy: " + initialEnergy + ".");
  32.                 }
  33.             } else if (eventOrIngredient.equals("order")) {
  34.                 if (initialEnergy - 30 > 0) {
  35.                     initialEnergy -= 30;
  36.                     initialCoins += number;
  37.                     System.out.println(String.format("You earned %d coins.", number));
  38.                 } else {
  39.                     System.out.println("You had to rest!");
  40.                     initialEnergy += 50;
  41.                 }
  42.  
  43.             } else {
  44.                 if (initialCoins - number > 0) {
  45.                     System.out.println(String.format("You bought %s.", eventOrIngredient));
  46.                     initialCoins -= number;
  47.                 } else {
  48.                     System.out.println(String.format("Closed! Cannot afford %s.", eventOrIngredient));
  49.                     isClosed = true;
  50.                     break;
  51.  
  52.  
  53.                 }
  54.             }
  55.         }
  56.         if (!isClosed) {
  57.             System.out.println("Day completed!");
  58.             System.out.println("Coins: " + initialCoins);
  59.             System.out.println("Energy: " + initialEnergy);
  60.         }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.     }
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top