Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class BakingRush {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int energy = 100;
- int coins = 100;
- boolean bankrupt = false;
- String[] dayEvents = scanner.nextLine().trim().split("\\|");
- for (int i = 0; i < dayEvents.length; i++) {
- String[] event = dayEvents[i].split("-");
- if (event[0].equals("rest")) {
- int newEnergy = Integer.parseInt(event[1]);
- if (energy + newEnergy >= 100) { //energy cannot exceed your initial energy (100). Print difference to 100.
- System.out.println("You gained " + (100 - energy) + " energy.");
- energy = 100;
- } else {
- energy += newEnergy;
- System.out.println("You gained " + newEnergy + " energy.");
- }
- System.out.println("Current energy: " + energy + ".");
- } else if (event[0].equals("order")) {
- if ((energy - 30) >= 0) {
- energy -= 30;
- System.out.println("You earned " + event[1] + " coins.");
- coins += Integer.parseInt(event[1]);
- } else { //your energy drops below 0, you skip the order and gain 50 energy points
- energy += 50;
- System.out.println("You had to rest!");
- }
- } else {
- String ingredient = event[0];
- int cost = Integer.parseInt(event[1]);
- coins -= cost;
- if (coins > 0) {
- System.out.println("You bought " + ingredient + ".");
- } else {
- System.out.println("Closed! Cannot afford " + ingredient + ".");
- bankrupt = true;
- }
- }
- if (bankrupt) {
- break;
- }
- }
- //ending
- if (!bankrupt) {
- System.out.println("Day completed!");
- System.out.println("Coins: " + coins);
- System.out.println("Energy: " + energy);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment