deiom

Untitled

Jul 25th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. //*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
  3. //you are the person who has to draw the line and calculate the money from the products that were sold throughout
  4. //the day. Until you receive a line with text "end of shift" you will be given lines of input. But before processing
  5. //that line you have to do some validations first.
  6. //Each valid order should have a customer, product, count and a price:
  7. // Valid customer's name should be surrounded by '%' and must start with a capital letter, followed by lower-
  8. //case letters
  9. // Valid product contains any word character and must be surrounded by '<' and '>';
  10. // Valid count is an integer, surrounded by '|'
  11. // Valid price is any real number followed by '$';
  12. //The parts of a valid order should appear in the order given: customer, product, count and a price.
  13. //Between each part there can be other symbols, except ('|', '$', '%' and '.')
  14. //For each valid line print on the console: "{customerName}: {product} - {totalPrice}"
  15. //When you receive &quot;end of shift&quot; print the total amount of money for the day rounded to 2 decimal places in
  16. //the following format: "Total income: {income}".
  17. //Input / Constraints
  18. // Strings that you have to process until you receive text "end of shift".
  19. //Output
  20. // Print all the valid lines in the format "{customerName}: {product} - {totalPrice}";
  21. // After receiving "end of shift" print the total amount of money for the day rounded to 2 decimal places
  22. //in the following format: "Total income: {income}"
  23. // Allowed working time / memory: 100ms / 16MB.
  24. //Examples
  25. //Input
  26. //%George%<Croissant>|2|10.3$
  27. //%Peter%<Gum>|1|1.3$
  28. //%Maria%<Cola>|1|2.4$
  29. //end of shift
  30. //Output:
  31. //George: Croissant - 20.60
  32. //Peter: Gum - 1.30
  33. //Maria: Cola - 2.40
  34. //Total income: 24.30
  35. //Comment
  36. //Each line is valid, so we print each
  37. //order, calculating the total price of the
  38. //product bought.
  39. //At the end we print the total income
  40. //for the day
  41. //Input:
  42. //%InvalidName<Croissant>|2|10.3$
  43. //%Peter%<Gum>1.3$
  44. //%Maria%>Cola>|1|2.4
  45. //%Valid%<Valid>valid|10|valid20$
  46. //end of shift
  47. //Output:
  48. //Valid: Valid - 200.00
  49. //Total income: 200.00
  50. //Comments:
  51. //On the first line, the customer name
  52. //isn`t valid, so we skip that line.
  53. //The second line is missing product
  54. //count.
  55. //The third line don`t have a valid price.
  56. //And only the forth line is valid
  57.  
  58. import java.util.ArrayList;
  59. import java.util.List;
  60. import java.util.Scanner;
  61. import java.util.regex.Matcher;
  62. import java.util.regex.Pattern;
  63.  
  64. public class SoftuniBarIncome {
  65. public static void main(String[] args) {
  66. Scanner scanner = new Scanner(System.in);
  67.  
  68. String input = scanner.nextLine();
  69. String regex = "%([A-z][a-z]+)%[^|$%.]*?<([\\w+]+)>[^|$%.]*?\\|([\\d+]+)\\|[^|$%.]*?([\\d+]+\\.?[\\d?])\\$";
  70. Pattern pattern = Pattern.compile(regex);
  71. double totalIncome=0;
  72. while (!input.equalsIgnoreCase("end of shift")) {
  73. Matcher matcher = pattern.matcher(input);
  74. while(matcher.find()){
  75. String name=matcher.group(1);
  76. String product=matcher.group(2);
  77. int qty=Integer.parseInt(matcher.group(3));
  78. double price=Double.parseDouble(matcher.group(4));
  79.  
  80. double totalPrice=qty*price;
  81. totalIncome+=totalPrice;
  82. System.out.printf("%s: %s - %.2f%n", name,product,totalPrice);
  83. }
  84.  
  85. input = scanner.nextLine();
  86. }
  87. System.out.printf("Total income: %.2f", totalIncome);
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment