Advertisement
N_Damyanov

02. Furniture

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