Advertisement
Guest User

Furniture

a guest
Jul 20th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. package Regex;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class Furniture2 {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String regex = ">{2}(?<furniture>[A-Z][A-Z]*[a-z]*)<<(?<price>\\d*\\.?(\\d*)?)\\!(?<quantity>\\d*)";
  12.         String input = scanner.nextLine();
  13.  
  14.         System.out.println("Bought furniture:");
  15.  
  16.         double totalPrice = 0;
  17.  
  18.         while (!"Purchase".equals(input)) {
  19.             Pattern pattern = Pattern.compile(regex);
  20.             Matcher matcher = pattern.matcher(input);
  21.             if (matcher.find()){
  22.                 String product = matcher.group("furniture");
  23.                 double price = Double.parseDouble(matcher.group("price"));
  24.                 int quantity = Integer.parseInt(matcher.group("quantity"));
  25.                 System.out.println(product);
  26.                 totalPrice += quantity * price;
  27.             }
  28.  
  29.             input = scanner.nextLine();
  30.         }
  31.         System.out.printf("Total money spend: %.2f", totalPrice);
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement