Regex Tutorial

 avatar
unknown
java
a year ago
949 B
9
Indexable
public class Test {
    public static void main(String[] args) {
        String paragraph = "A core i 7 laptop price is 85000 tk " +
                "and a gaming mouse price is 2500 tk. If I buy the " +
                "laptop and 1 piece mouse, what will be my total cost " +
                "after giving 15% discount?";

        paragraph = paragraph.replaceAll("[^\\d]", " ").trim();
        paragraph = paragraph.replaceAll(" +", " ");

        String[] words = paragraph.split(" ");

        int laptopPrice = Integer.parseInt(words[1]);
        int mousePrice = Integer.parseInt(words[2]);
        double discountRate = Integer.parseInt(words[4]) / 100.0;

        //calculation
        int totalPrice = laptopPrice + mousePrice;
        double discount = totalPrice * discountRate;
        double finalPrice = totalPrice - discount;

        System.out.println("Price after discount : " + finalPrice + " Tk");
    }
}
Editor is loading...
Leave a Comment