Advertisement
Guest User

Untitled

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