Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- String regex = ">>(?<furniture>[A-Z]*[a-z]*)<<(?<price>\\d+[.]{0,1}[\\d]*)!(?<quantity>\\d{1,9})+";//може и без имена, само по
- групи
- // String regex = ">>(?<furniture>[A-Za-z]+)<<(?<price>[0-9]+\\.?[0-9]+)!(?<quantity>\\d+)";//други верни варианти
- // String regex = ">>(?<furniture>\W+)<<(?<price>\d+\\.?\d+)!(?<quantity>\\d+)";
- Pattern pattern = Pattern.compile(regex);
- List<String> furnitureName = new ArrayList<>();
- double totalSpentMoney = 0;
- while (!input.equals("Purchase")) {
- Matcher matcher = pattern.matcher(input);
- while (matcher.find()) {
- String furniture = matcher.group("furniture");//може и без да именуваме само с номер на група да печатаме
- double price = Double.parseDouble(matcher.group("price"));
- int quantity = Integer.parseInt(matcher.group("quantity"));
- double spentMoney = price * quantity;
- totalSpentMoney += spentMoney;
- furnitureName.add(furniture);
- }
- input = scanner.nextLine();
- }
- //System.out.printf("Bought furniture:%n"); ГРЕШНО
- // System.out.println(String.join("\n", furnitureName)); ГРЕШНО
- System.out.println("Bought furniture:");
- for (String name : furnitureName) {
- System.out.println(name);
- }
- System.out.printf("Total money spend: %.2f", totalSpentMoney);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement