View difference between Paste ID: Ltz1yhLH and Dq35vh5A
SHOW: | | - or go back to the newest paste.
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
        String regex = ">>(?<furniture>[A-Za-z]+)<<(?<price>[0-9]+.?[0-9]*)!(?<quantity>[0-9]+)";
11
        Pattern pattern = Pattern.compile(regex);
12
13
        String input = scanner.nextLine();
14
        List<String> furnitureList = new ArrayList<>();
15
        double totalMoney = 0;
16
        while (!input.equals("Purchase")) {
17
            //">>Sofa<<312.23!3"
18
            Matcher matcher = pattern.matcher(input);
19
            if (matcher.find()) {
20
                String furniture = matcher.group("furniture");
21
                double price = Double.parseDouble(matcher.group("price"));
22
                int quantity = Integer.parseInt(matcher.group("quantity"));
23
                furnitureList.add(furniture);
24
                totalMoney += price * quantity;
25
            }
26
            input = scanner.nextLine();
27
        }
28
29
        System.out.println("Bought furniture:");
30
        furnitureList.forEach(furniture -> System.out.println(furniture));
31
        System.out.printf("Total money spend: %.2f", totalMoney);
32
    }
33
}