desislava_topuzakova

01. Furniture

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