Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- //String regex = "%(?<name>[A-Z][a-z]+)%[^|$%.]*<(?<product>\\w+)>[^|$%.]*\\|(?<count>\\d+)\\|[^|$%.]*?(?<price>\\d+\\.?\\d+)\\$";
- String nameReGex = "%(?<name>[A-Z][a-z]+)%";
- String productReGex = "<(?<product>\\w+)>";
- String countRegex = "\\|(?<count>\\d+)\\|";
- String priceRegex = "(?<price>\\d+\\.?\\d+)\\$";
- Pattern pattern = Pattern.compile(nameReGex);
- Pattern pattern1 = Pattern.compile(productReGex);
- Pattern pattern2 = Pattern.compile(countRegex);
- Pattern pattern3 = Pattern.compile(priceRegex);
- String input = scan.nextLine();
- double income = 0.0;
- while (!"end of shift".equals(input)) {
- Matcher matcher = pattern.matcher(input);
- Matcher matcher1 = pattern1.matcher(input);
- Matcher matcher2 = pattern2.matcher(input);
- Matcher matcher3 = pattern3.matcher(input);
- if (matcher.find() && matcher1.find() && matcher2.find() && matcher3.find()) {
- String name = matcher.group("name");
- String product = matcher1.group("product");
- int count = Integer.parseInt(matcher2.group("count"));
- double price = Double.parseDouble(matcher3.group("price"));
- double totalPrice = count * price;
- income += totalPrice;
- System.out.println(String.format("%s: %s - %.2f", name, product, totalPrice));
- }
- input = scan.nextLine();
- }
- System.out.println(String.format("Total income: %.2f", income));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement