VasilKotsev

funiture

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