Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Furniture_01 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> furniture = new ArrayList<>(); //списък с мебели
- double totalSum = 0; //общо изхарчена сума
- //">>{furniture name}<<{price}!{quantity}"
- //String regex = ">>(?<furnitureName>[A-Za-z]+)<<(?<price>[0-9]+.?[0-9]*)!(?<quantity>[0-9]+)";
- String regex = ">>(?<furnitureName>\\w+)<<(?<price>\\d+.?\\d*)!(?<quantity>\\d+)";
- Pattern pattern = Pattern.compile(regex);
- String input = scanner.nextLine();
- while (!input.equals("Purchase")) {
- //input = ">>Sofa<<312.23!3";
- Matcher matcher = pattern.matcher(input);
- //matcher = ">>(?<furnitureName>Sofa)<<(?<price>312.23)!(?<quantity>3)";
- //find
- //true -> input отговаря на regex
- //false -> input е отговаря на regex
- if (matcher.find()) {
- String furnitureName = matcher.group("furnitureName");
- double price = Double.parseDouble(matcher.group("price"));
- int quantity = Integer.parseInt(matcher.group("quantity"));
- furniture.add(furnitureName);
- double currentFurniturePrice = price * quantity; //платено за текущата мебел
- totalSum += currentFurniturePrice;
- }
- input = scanner.nextLine();
- }
- System.out.println("Bought furniture:");
- furniture.forEach(System.out::println);
- System.out.printf("Total money spend: %.2f", totalSum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement