Home » Java » system – How to compute all the total amount of the items? Netbeans Java-Exceptionshub

system – How to compute all the total amount of the items? Netbeans Java-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

enter image description here
so here’s the thing i am going to put some items, quantity and price and I can’t identify how to compute all of the items and price that i input.

enter image description here
this is what it looks like

enter image description here
then this is the output but i want to compute all the total price of the items that i input how can i do it?

this are the codes that i used to get the values from the textfield like item name, quantity and price

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        txtbill.setText(
        txtbill.getText()+txtname.getText()+"-"+txtquentity.getText()+"\t\t"+txtprice.getText()+"\n"

        );

        txtname.setText("");
        txtquentity.setText("");
        txtprice.setText("");

    }                                        

then this is the code that i used to print all the items that i input

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        try {

            txtbill.setText(txtbill.getText()+"============================"+"\n"
        +"Done Printing your Bill!");

            txtbill.print();
        } catch (Exception e) {
        }


    }                     
How to&Answers:

You can store the added itens in some sort of collection to keep the idea of “cart” along with its quantity. You can do that in many ways.
For instance, you can represent your “cart” as a Map where the integer is item count, when you finish/confirm your buying list you can iterate through the map and multiply each product with its price and sum all together.

class Product{
    String name;
    double price;
}

Map<Product, Integer>  cart = new HashMap<>();

void addProduct(Product p, int quant){
    cart.put(p, quant);

    StringBuilder sb = new StringBuilder();
    double sum = 0;
    for(Entry<Product, Integer> entry : cart.entrySet()){
        sum += entry.getKey().price * entry.getValue();
        sb.append(entry.getKey().name).append(": ").append(entry.getValue());
    }
    sb.append("\nTotal price:").append(sum);
    //PRINT YOUR CART FROM THE MAP, NOT DIRECTLY FROM THE FIELDS HERE
    txtBill.setText(sb.toString());

}

That just a untested example to give you and possible ideia. Printing from a dedicated objected to hold your stuff might give you some nice features, as remove from list, change quantity, etc.

Answer:

Do it as follows:

String regExp = "[0-9,.]+(\$)";
Pattern p = Pattern.compile(regExp);
Matcher matcher = p.matcher(txtbill.getText());
double sum = 0.0;
while (matcher.find()) {            
    sum += Double.parseDouble(matcher.group().replaceAll("\$", ""));
}
txtbill.setText(txtbill.getText() + "\nTotal bill: " + sum);

Demo:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String txtBillValue = "====================\nSample1-2      100$\nSample2-2     200$\nSample3-1     50$\n====================\nDone Printing your bill";
        String regExp = "[0-9,.]+(\$)";
        Pattern p = Pattern.compile(regExp);
        Matcher matcher = p.matcher(txtBillValue);
        double sum = 0.0;
        while (matcher.find()) {
            sum += Double.parseDouble(matcher.group().replaceAll("\$", ""));
        }
        txtBillValue += "\nTotal bill: " + sum;
        System.out.println(txtBillValue);
    }
}

Output:

====================
Sample1-2   100$
Sample2-2   200$
Sample3-1   50$
====================
Done Printing your bill
Total bill: 350.0