Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package P09RegularExpressions.Exersice;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class P03SoftUniBarIncome {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- String regex = "%(?<customer>[A-Z][a-z]*)%[^|$%.]*<(?<product>\\w+)>[^|$%.]*|(?<quantity>[0-9]+)|[^|$%.]*?(?<price>\\d+.?\\d*)$";
- Pattern pattern = Pattern.compile(regex);
- double totalIncome = 0;
- while(!input.equals("end of shift")){
- Matcher matcher = pattern.matcher(input);
- if (matcher.find()){
- String customer = matcher.group("customer");
- String product = matcher.group("product");
- int count = Integer.parseInt(matcher.group("quantity"));
- double price = Double.parseDouble(matcher.group("price"));
- double currentIncome = count * price;
- totalIncome += currentIncome;
- System.out.printf("%s: %s - %.2f",customer,product,currentIncome);
- }
- input = scanner.nextLine();
- }
- System.out.printf("Total income: %.2f",totalIncome);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement