Didart

Furniture

Nov 22nd, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 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 Furniture {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String input = scanner.nextLine();
  12.  
  13.         List<String> furniture = new ArrayList<>();
  14.  
  15.         double totalSum = 0;
  16.  
  17.         String regex = ">>(?<furnitureName>\\w+)<<(?<price>\\d+.?\\d*)!(?<quantity>\\d+)";
  18.        
  19.         Pattern pattern = Pattern.compile(regex);
  20.  
  21.         while(!input.equals("Purchase")) {
  22.  
  23.             Matcher matcher = pattern.matcher(input);
  24.            
  25.             if (matcher.find()) {
  26.  
  27.                 String furnitureName = matcher.group("furnitureName");
  28.                 double price = Double.parseDouble(matcher.group("price"));
  29.                 int quantity = Integer.parseInt(matcher.group("quantity"));
  30.  
  31.                 furniture.add(furnitureName);
  32.                 totalSum += price * quantity;
  33.             }
  34.             input = scanner.nextLine();
  35.         }
  36.         System.out.println("Bought furniture:");
  37.         furniture.forEach(System.out::println);
  38.         System.out.printf("Total money spend: %.2f", totalSum);
  39.     }
  40. }
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment