Advertisement
Sim0o0na

ChristmasDecoration

Jul 4th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ChristmasDecoration {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         // 1. Read budget
  10.         int budget = Integer.parseInt(scanner.nextLine());
  11.         // 2. Read name of decoration until != stop
  12.         String command = scanner.nextLine();
  13.         while (!command.equals("Stop")) {
  14.             // 2.1. Get word length
  15.             int wordLength = command.length();
  16.             // 2.2. Iterate through every character
  17.             int currentDecorationPrice = 0;
  18.             for (int charPos = 0; charPos < wordLength; charPos++) {
  19.                 char currentCharacter = command.charAt(charPos);
  20.                 // 2.3. Get ascii of current character
  21.                 int asciiValue = (int)currentCharacter;
  22.                 // 2.4. Sum to decoration price
  23.                 currentDecorationPrice += asciiValue;
  24.             }
  25.  
  26.             // 2.5 Check if budget is enough
  27.             if (currentDecorationPrice > budget) {
  28.                 System.out.println("Not enough money!");
  29.                 // 2.5.1. if not, break
  30.                 break;
  31.             }
  32.             // 2.6 Buy decoration
  33.             budget -= currentDecorationPrice; // budget = budget - currentDecorationPrice
  34.             // 2.7 Print
  35.             System.out.println("Item successfully purchased!");
  36.             command = scanner.nextLine();
  37.         }
  38.         // 3. Print money left
  39.         if (command.equals("Stop")) {
  40.             System.out.printf("Money left: %d", budget);
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement