Advertisement
desislava_topuzakova

01. Furniture

Mar 17th, 2023
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package 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_01 {
  10. public static void main(String[] args) {
  11. Scanner scanner = new Scanner(System.in);
  12. List<String> furniture = new ArrayList<>(); //списък със закупени мебели
  13. double totalSum = 0; //общо изхарчена сума за всички закупени мебели
  14.  
  15. String regex = ">>(?<furnitureName>[A-Za-z]+)<<(?<price>[0-9]+\\.?[0-9]*)!(?<quantity>[0-9]+)";
  16. Pattern pattern = Pattern.compile(regex);
  17.  
  18. String input = scanner.nextLine();
  19. //">>{furniture name}<<{price}!{quantity}"
  20. while(!input.equals("Purchase")) {
  21. //input = ">>Sofa<<312.23!3"
  22. Matcher matcher = pattern.matcher(input);
  23. //matcher = ">>(?<furnitureName>Sofa)<<(?<price>312.23)!(?<quantity>3)"
  24. //find
  25. //true -> input отговаря на regex
  26. //false -> input не отговаря на regex
  27. if (matcher.find()) {
  28. //input отговаря на regex -> купуваме мебели
  29. String furnitureName = matcher.group("furnitureName"); //мебел
  30. double price = Double.parseDouble(matcher.group("price")); //ед. цена
  31. int quantity = Integer.parseInt(matcher.group("quantity")); //количество
  32.  
  33. furniture.add(furnitureName);
  34. double currentFurniturePrice = quantity * price; //платено за текушата мебел
  35. totalSum += currentFurniturePrice;
  36. }
  37. input = scanner.nextLine();
  38. }
  39.  
  40. System.out.println("Bought furniture:");
  41. furniture.forEach(System.out::println);
  42.  
  43. System.out.printf("Total money spend: %.2f", totalSum);
  44. }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement