Advertisement
meteor4o

JF-RegEx-Exec-01.Furniture

Jul 29th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class Furniture {
  10.     public static void main(String[] args) {
  11.         Scanner sc = new Scanner(System.in);
  12.  
  13.         String input = sc.nextLine();
  14.         List<String> furniture = new ArrayList<>();
  15.  
  16.         String regex = ">>([A-Za-z]+)<<([0-9]+\\.?[0-9]+)!(\\d+)";
  17.  
  18.         Pattern pattern = Pattern.compile(regex);
  19.         double moneySpent = 0.0;
  20.  
  21.         while (!input.equals("Purchase")) {
  22.             Matcher matcher = pattern.matcher(input);
  23.  
  24.             while (matcher.find()) {
  25.                 String name = matcher.group(1);
  26.                 double price = Double.parseDouble(matcher.group(2));
  27.                 int quantity = Integer.parseInt(matcher.group(3));
  28.  
  29.                 furniture.add(name);
  30.                 moneySpent += price * quantity;
  31.             }
  32.  
  33.             input = sc.nextLine();
  34.         }
  35.         System.out.println("Bought furniture:");
  36.         for (String s : furniture) {
  37.             System.out.println(s);
  38.         }
  39.  
  40.         System.out.printf("Total money spend: %.2f", moneySpent);
  41.  
  42.  
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement