Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. package com.company.Regex;
  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.  
  12. Scanner scanner = new Scanner(System.in);
  13.  
  14. String input = scanner.nextLine();
  15. String myRegex = "(^| )(>>)(?<item>[A-Za-z\\s]+)<<(?<price>\\d+(.\\d+)?)!(?<quantity>\\d+)";
  16. List<String> purcahsedItems = new ArrayList<>();
  17. double total = 0;
  18.  
  19. while (!"Purchase".equals(input)) {
  20.  
  21. Pattern pattern = Pattern.compile(myRegex);
  22. Matcher matcher = pattern.matcher(input);
  23. while (matcher.find()) {
  24. String item = matcher.group("item");
  25. double price = Double.parseDouble(matcher.group("price"));
  26. int quantity = Integer.parseInt(matcher.group("quantity"));
  27. double production = price * quantity;
  28. purcahsedItems.add(item);
  29. total += production;
  30.  
  31. }
  32. input = scanner.nextLine();
  33. }
  34. System.out.println("Bought furniture:");
  35. for (int i = 0; i < purcahsedItems.size(); i++) {
  36. System.out.println(purcahsedItems.get(i));
  37. }
  38. System.out.println(String.format("Total money spend: %.2f", total));
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement