Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class _09_Furniture {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String reg = ">>(?<product>[\\w\\s]+)<<(?<price>[\\d.]+)!(?<quantity>[\\d]+)";
- Pattern p = Pattern.compile(reg);
- double total = 0;
- String input = "";
- System.out.println("Bought furniture:");
- while (!"Purchase".equals(input = scanner.nextLine())) {
- Matcher m = p.matcher(input);
- if(m.find()) {
- String product = m.group("product");
- double price = Double.parseDouble(m.group("price"));
- int quantity = Integer.parseInt(m.group("quantity"));
- System.out.println(product);
- total += (price * quantity);
- }
- }
- System.out.printf("Total money spend: %.2f", total);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement