Advertisement
Edzhevit

Furniture

Nov 21st, 2018
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. package RegularExpressions;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class Furniture {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.         String line = reader.readLine();
  13.         String regex = "^>+([?<furniture>A-Z{0,}a-z]+)+<<(?<price>\\d+\\.?\\d+)+!([?<quantity>\\d+]+)\\b";
  14.         double totalCost = 0;
  15.         System.out.println("Bought furniture:");
  16.         while (!line.equals("Purchase")){
  17.             Pattern pattern = Pattern.compile(regex);
  18.             Matcher matcher = pattern.matcher(line);
  19.  
  20.             if (matcher.find()){
  21.                 String furniture = matcher.group(1);
  22.                 double price = Double.parseDouble(matcher.group(2));
  23.                 double quantity = Double.parseDouble(matcher.group(3));
  24.                 System.out.println(furniture);
  25.                 totalCost += price * quantity;
  26.             }
  27.  
  28.             line = reader.readLine();
  29.         }
  30.         System.out.printf("Total money spend: %.2f", totalCost);
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement