desislava_topuzakova

01. Furniture

Jul 31st, 2021
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 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_01 {
  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.         Pattern pattern = Pattern.compile(regex);
  12.  
  13.         String input = scanner.nextLine();
  14.         List<String> furnitureList = new ArrayList<>();
  15.         double totalMoney = 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.                 furnitureList.add(furniture);
  24.                 totalMoney += price * quantity;
  25.             }
  26.             input = scanner.nextLine();
  27.         }
  28.  
  29.         System.out.println("Bought furniture:");
  30.         furnitureList.forEach(furniture -> System.out.println(furniture));
  31.         System.out.printf("Total money spend: %.2f", totalMoney);
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment