Advertisement
jwrbg

asd

Apr 11th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. 11. *SoftUni Bar Income
  2. 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.
  3. Each valid order should have a customer, product, count and a price:
  4. • Valid customer's name should be surrounded by '%' and must start with a capital letter, followed by lower-case letters
  5. • Valid product contains any word character and must be surrounded by '<' and '>'
  6. • Valid count is an integer, surrounded by '|'
  7. • Valid price is any real number followed by '$'
  8. The parts of a valid order should appear in the order given: customer, product, count and a price.
  9. Between each part there can be other symbols, except ('|', '$', '%' and '.')
  10. For each valid line print on the console: "{customerName}: {product} - {totalPrice}"
  11. 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}".
  12. Input / Constraints
  13. • Strings that you have to process until you receive text "end of shift".
  14. Output
  15. • Print all of the valid lines in the format "{customerName}: {product} - {totalPrice}"
  16. • 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}"
  17. • Allowed working time / memory: 100ms / 16MB.
  18. Examples
  19. Input   Output  Comment
  20. %George%<Croissant>|2|10.3$
  21. %Peter%<Gum>|1|1.3$
  22. %Maria%<Cola>|1|2.4$
  23. end of shift    George: Croissant - 20.60
  24. Peter: Gum - 1.30
  25. Maria: Cola - 2.40
  26. Total income: 24.30 Each line is valid, so we print each order, calculating the total price of the product bought.
  27. At the end we print the total income for the day
  28.  
  29. %InvalidName%<Croissant>|2|10.3$
  30. %Peter%<Gum>1.3$
  31. %Maria%<Cola>|1|2.4
  32. %Valid%<Valid>valid|10|valid20$
  33. end of shift    Valid: Valid - 200.00
  34. Total income: 200.00    On the first line, the customer name isn`t valid, so we skip that line.
  35. The second line is missing product count.
  36. The third line don`t have a valid price.
  37. And only the forth line is valid
  38. ---------------------------------------------------------------------------------------------------------------------------------------
  39.  
  40.  
  41.  
  42. package TechModule;
  43.  
  44. import java.util.Scanner;
  45. import java.util.regex.Matcher;
  46. import java.util.regex.Pattern;
  47.  
  48. public class p07_RegexSoftUniBarIncome {
  49.    public static void main(String[] args) {
  50.        Scanner scanner = new Scanner(System.in);
  51.        String input=scanner.nextLine();
  52.  
  53.        String regex="^%(?<name>[A-Z][a-z]*)%[\\|\\$\\%\\.]*<(?<product>[A-Za-z]+)>[\\|\\$\\%\\.]*\\|(?<count>[0-9]+)\\|[\\|\\$\\%\\.]*(?<price>[0-9]+\\.*[0-9]*)[$]$";
  54.  
  55.        Pattern patter=Pattern.compile(regex);
  56.        double sum=0;
  57.  
  58.        while(!input.equals("end of shift")){
  59.  
  60.       Matcher matcher=patter.matcher(input);
  61.       while(matcher.find()){
  62.           double a =Double.parseDouble(matcher.group("count"));
  63.           double b= Double.parseDouble(matcher.group("price"));
  64.           System.out.println(String.format("%s: %s - %.2f",matcher.group("name"),matcher.group("product"),a*b));
  65.           sum+=a*b;
  66.       }
  67.  
  68.  
  69.            input=scanner.nextLine();
  70.  
  71.  
  72.        }
  73.        System.out.println(String.format("Total income: %.2f",sum));
  74.    }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement