Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class ChristmasDecoration {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // 1. Read budget
- int budget = Integer.parseInt(scanner.nextLine());
- // 2. Read name of decoration until != stop
- String command = scanner.nextLine();
- while (!command.equals("Stop")) {
- // 2.1. Get word length
- int wordLength = command.length();
- // 2.2. Iterate through every character
- int currentDecorationPrice = 0;
- for (int charPos = 0; charPos < wordLength; charPos++) {
- char currentCharacter = command.charAt(charPos);
- // 2.3. Get ascii of current character
- int asciiValue = (int)currentCharacter;
- // 2.4. Sum to decoration price
- currentDecorationPrice += asciiValue;
- }
- // 2.5 Check if budget is enough
- if (currentDecorationPrice > budget) {
- System.out.println("Not enough money!");
- // 2.5.1. if not, break
- break;
- }
- // 2.6 Buy decoration
- budget -= currentDecorationPrice; // budget = budget - currentDecorationPrice
- // 2.7 Print
- System.out.println("Item successfully purchased!");
- command = scanner.nextLine();
- }
- // 3. Print money left
- if (command.equals("Stop")) {
- System.out.printf("Money left: %d", budget);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement