Advertisement
damesova

Furniture [Mimi]

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