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("-");
- switch (event[0]) {
- case "rest":
- int newEnergy = Integer.parseInt(event[1]);
- if (energy + newEnergy >= 100) { //energy cannot exceed your initial energy (100).
- System.out.println("You gained 0 energy.");
- } else {
- energy += newEnergy;
- System.out.println("You gained " + newEnergy + " energy.");
- }
- System.out.println("Current energy: " + energy + ".");
- break;
- case "order":
- energy -= 30;
- if (energy >= 0) {
- 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!");
- }
- break;
- default: //In any other case you are having an ingredient, you have to buy
- 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;
- }
- break;
- }
- if (bankrupt){
- break;
- }
- }
- 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