Untitled

 avatar
unknown
java
a year ago
1.7 kB
8
Indexable
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex {
    public static void main(String[] args) {
        // Input paragraph
        String input = "A core i 7 laptop price is 85000 tk and a gaming mouse price is 2500 tk.";

        // Regular expressions to extract prices
        String laptopRegex = "\\b\\d+\\b(?=\\s+tk)";
        String mouseRegex = "(?<=mouse price is\\s)\\d+";

        // Pattern objects
        Pattern laptopPattern = Pattern.compile(laptopRegex);
        Pattern mousePattern = Pattern.compile(mouseRegex);

        // Matcher objects
        Matcher laptopMatcher = laptopPattern.matcher(input);
        Matcher mouseMatcher = mousePattern.matcher(input);

        // Variables to store prices
        int laptopPrice = 0;
        int mousePrice = 0;

        // Find laptop price
        if (laptopMatcher.find()) {
            laptopPrice = Integer.parseInt(laptopMatcher.group());
        }

        // Find mouse price
        if (mouseMatcher.find()) {
            mousePrice = Integer.parseInt(mouseMatcher.group());
        }

        // Calculate total price before discount
        int totalPriceBeforeDiscount = laptopPrice + mousePrice;

        // Calculate discount amount
        double discountRate = 0.15;
        double discountAmount = discountRate * totalPriceBeforeDiscount;

        // Calculate total cost after discount
        double totalPriceAfterDiscount = totalPriceBeforeDiscount - discountAmount;

        System.out.println("Total cost after giving a 15% discount: " + totalPriceAfterDiscount + " Tk");
    }
}
Editor is loading...
Leave a Comment