Advertisement
Edzhevit

Furniture

Nov 19th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 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-Za-z]+)<<(?<price>[-+]?[0-9]*\\.?[0-9]*)!(?<quantity>\\d)$";
  14.         Pattern pattern = Pattern.compile(regex);
  15.         double totalCost = 0;
  16.         System.out.println("Bought furniture:");
  17.         while (!line.equals("Purchase")){
  18.             Matcher matcher = pattern.matcher(line);
  19.  
  20.             if (matcher.find()){
  21.                 String furniture = matcher.group("furniture");
  22.                 double price = Double.parseDouble(matcher.group("price"));
  23.                 double quantity = Double.parseDouble(matcher.group("quantity"));
  24.                 System.out.println(furniture);
  25.                 totalCost += price * quantity;
  26.             }
  27.  
  28.             line = reader.readLine();
  29.         }
  30.         System.out.println("Total money spend: " + totalCost);
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement