Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Regex;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Furniture2 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String regex = ">{2}(?<furniture>[A-Z][A-Z]*[a-z]*)<<(?<price>\\d*\\.?(\\d*)?)\\!(?<quantity>\\d*)";
- String input = scanner.nextLine();
- System.out.println("Bought furniture:");
- double totalPrice = 0;
- while (!"Purchase".equals(input)) {
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(input);
- if (matcher.find()){
- String product = matcher.group("furniture");
- double price = Double.parseDouble(matcher.group("price"));
- int quantity = Integer.parseInt(matcher.group("quantity"));
- System.out.println(product);
- totalPrice += quantity * price;
- }
- input = scanner.nextLine();
- }
- System.out.printf("Total money spend: %.2f", totalPrice);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement