Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*SoftUni Bar Income
- //Let`s take a break and visit the game bar at SoftUni. It is about time for the people behind the bar to go home and
- //you are the person who has to draw the line and calculate the money from the products that were sold throughout
- //the day. Until you receive a line with text "end of shift" you will be given lines of input. But before processing
- //that line you have to do some validations first.
- //Each valid order should have a customer, product, count and a price:
- // Valid customer's name should be surrounded by '%' and must start with a capital letter, followed by lower-
- //case letters
- // Valid product contains any word character and must be surrounded by '<' and '>';
- // Valid count is an integer, surrounded by '|'
- // Valid price is any real number followed by '$';
- //The parts of a valid order should appear in the order given: customer, product, count and a price.
- //Between each part there can be other symbols, except ('|', '$', '%' and '.')
- //For each valid line print on the console: "{customerName}: {product} - {totalPrice}"
- //When you receive "end of shift" print the total amount of money for the day rounded to 2 decimal places in
- //the following format: "Total income: {income}".
- //Input / Constraints
- // Strings that you have to process until you receive text "end of shift".
- //Output
- // Print all the valid lines in the format "{customerName}: {product} - {totalPrice}";
- // After receiving "end of shift" print the total amount of money for the day rounded to 2 decimal places
- //in the following format: "Total income: {income}"
- // Allowed working time / memory: 100ms / 16MB.
- //Examples
- //Input
- //%George%<Croissant>|2|10.3$
- //%Peter%<Gum>|1|1.3$
- //%Maria%<Cola>|1|2.4$
- //end of shift
- //Output:
- //George: Croissant - 20.60
- //Peter: Gum - 1.30
- //Maria: Cola - 2.40
- //Total income: 24.30
- //Comment
- //Each line is valid, so we print each
- //order, calculating the total price of the
- //product bought.
- //At the end we print the total income
- //for the day
- //Input:
- //%InvalidName<Croissant>|2|10.3$
- //%Peter%<Gum>1.3$
- //%Maria%>Cola>|1|2.4
- //%Valid%<Valid>valid|10|valid20$
- //end of shift
- //Output:
- //Valid: Valid - 200.00
- //Total income: 200.00
- //Comments:
- //On the first line, the customer name
- //isn`t valid, so we skip that line.
- //The second line is missing product
- //count.
- //The third line don`t have a valid price.
- //And only the forth line is valid
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class SoftuniBarIncome {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- String regex = "%([A-z][a-z]+)%[^|$%.]*?<([\\w+]+)>[^|$%.]*?\\|([\\d+]+)\\|[^|$%.]*?([\\d+]+\\.?[\\d?])\\$";
- Pattern pattern = Pattern.compile(regex);
- double totalIncome=0;
- while (!input.equalsIgnoreCase("end of shift")) {
- Matcher matcher = pattern.matcher(input);
- while(matcher.find()){
- String name=matcher.group(1);
- String product=matcher.group(2);
- int qty=Integer.parseInt(matcher.group(3));
- double price=Double.parseDouble(matcher.group(4));
- double totalPrice=qty*price;
- totalIncome+=totalPrice;
- System.out.printf("%s: %s - %.2f%n", name,product,totalPrice);
- }
- input = scanner.nextLine();
- }
- System.out.printf("Total income: %.2f", totalIncome);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment