Advertisement
desislava_topuzakova

01. Furniture

Jul 24th, 2022
1,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 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 Furniture_01 {
  8.     public static void main(String[] args) {
  9.        Scanner scanner = new Scanner(System.in);
  10.  
  11.        List<String> furniture = new ArrayList<>(); //списък с мебели
  12.        double totalSum = 0; //общо изхарчена сума
  13.  
  14.         //">>{furniture name}<<{price}!{quantity}"
  15.        //String regex = ">>(?<furnitureName>[A-Za-z]+)<<(?<price>[0-9]+.?[0-9]*)!(?<quantity>[0-9]+)";
  16.        String regex = ">>(?<furnitureName>\\w+)<<(?<price>\\d+.?\\d*)!(?<quantity>\\d+)";
  17.        Pattern pattern = Pattern.compile(regex);
  18.  
  19.        String input = scanner.nextLine();
  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.                 String furnitureName = matcher.group("furnitureName");
  29.                 double price = Double.parseDouble(matcher.group("price"));
  30.                 int quantity = Integer.parseInt(matcher.group("quantity"));
  31.  
  32.                furniture.add(furnitureName);
  33.                double currentFurniturePrice = price * quantity; //платено за текущата мебел
  34.                totalSum += currentFurniturePrice;
  35.  
  36.            }
  37.            input = scanner.nextLine();
  38.        }
  39.  
  40.         System.out.println("Bought furniture:");
  41.         furniture.forEach(System.out::println);
  42.         System.out.printf("Total money spend: %.2f", totalSum);
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement