Advertisement
Lyubohd

01. Furniture

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