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 StartUp {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String regex = ">>(?<item>[A-Za-z]+)<<(?<price>\\d+(.?\\d+)?)!(?<quantity>\\d+)";
- double sum = 0;
- System.out.println("Bought furniture:");
- while (true) {
- String input = scanner.nextLine();
- if (input.equalsIgnoreCase("Purchase")) {
- break;
- }
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(input);
- if (matcher.find()) {
- String product = matcher.group("item");
- double price = Double.parseDouble(matcher.group("price"));
- int quantity = Integer.parseInt(matcher.group("quantity"));
- sum += price * quantity;
- System.out.printf("%s%n", product);
- }
- }
- System.out.printf("Total money spend: %.2f", sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment