Someone try my code. And help me with the txtQty TextField.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.text.*;
import javax.swing.*;
public class Cashier extends Applet implements ActionListener, KeyListener {
private final double applePrice = 15.75,
orangePrice = 13.50,
pomeloPrice = 95.25;
Panel topPanel,
eastPanel,
westPanel;
Label lblTitle,
lblQty,
lblTotal;
JRadioButton radApple,
radOrange,
radPomelo;
TextField txtQty;
JTextArea txtScreen;
Button btnDone,
btnPrint,
btnCancel,
btnExit,
btnStock;
ButtonGroup bg;
static double appleprice=0, orangeprice=0, pomeloprice=0;
static double totalprice=0;
static String strApples="", strOranges = "", strPomelos = "";
public void init(){
setSize(600,490);
setVisible(true);
setLayout(null);
add(lblTitle=new Label("Tricia's Grocery Store"));
lblTitle.setFont(new Font("Broadway", Font.PLAIN, 35));
lblTitle.setBounds(50,0,600,80);
add(radApple=new JRadioButton("Apples ---------- P15.75"));
add(radOrange=new JRadioButton("Oranges -------- P12.50"));
add(radPomelo=new JRadioButton("Pomelos -------- P95.25"));
radApple.setBounds(70,100,150,50);
radOrange.setBounds(70,130,155,50);
radPomelo.setBounds(70,160,155,50);
bg = new ButtonGroup();
bg.add(radApple);
bg.add(radOrange);
bg.add(radPomelo);
add(lblQty=new Label("Quantity:"));
lblQty.setBounds(70,200,150,50);
add(txtQty=new TextField("1",2));
txtQty.setBounds(150,210,70,30);
add(lblTotal=new Label("P0.00"));
lblTotal.setBounds(150,500,500,500);
add(btnDone=new Button("Done"));
btnDone.setBounds(70,270,80,20);
add(btnPrint=new Button("Print Receipt"));
btnPrint.setBounds(150,270,80,20);
add(btnCancel=new Button("Reset Values"));
btnCancel.setBounds(70,290,80,20);
add(btnExit=new Button("Close"));
btnExit.setBounds(150,290,80,20);
add(txtScreen=new JTextArea("----------------------------------"+"\n-------------WELCOME--------------"
+"\n----------------------------------"+ "\nProduct | Quantity | Price"));
txtScreen.setFont(new Font("Courier",Font.PLAIN,15));
txtScreen.setBackground(new Color(64,64,64));
txtScreen.setForeground(new Color(0,255,255));
txtScreen.setBounds(250,100,300,320);
txtScreen.setEditable(false);
radApple.addActionListener(this);
radOrange.addActionListener(this);
radPomelo.addActionListener(this);
txtQty.addKeyListener(this);
btnPrint.addActionListener(this);
btnDone.addActionListener(this);
}
public void start(){
txtQty.selectAll();
}
public void keyPressed(KeyEvent arg0) {}
public void keyTyped(KeyEvent arg0) {}
public void keyReleased(KeyEvent e) {
// Trap all non-valid numbers
try {
Integer.parseInt(txtQty.getText());
}
catch (NumberFormatException fe) {
txtQty.setText("0");
}
refreshPrice();
}
public void actionPerformed(ActionEvent e) {
String strReceipt = txtScreen.getText();
if(e.getSource()== btnPrint){
JOptionPane.showMessageDialog(null, strReceipt,"Invoice",JOptionPane.INFORMATION_MESSAGE);
}
if(e.getSource()==btnDone){
txtScreen.append("\n" + "\n" + "\n");
txtScreen.append(" Total Price: " + lblTotal.getText());
bg.clearSelection();
}
refreshPrice();
}
private void refreshPrice() {
int qty = Integer.parseInt(txtQty.getText());
double totalprice;
if (radApple.isSelected()){
appleprice += 15.75 * qty;
txtScreen.append("\n");
txtScreen.append(" Apples ----- x" + txtQty.getText()+ " ----- P" + appleprice);
}
if (radOrange.isSelected()){
orangeprice += 12.50 * qty;
txtScreen.append("\n");
txtScreen.append(" Oranges ----- x" + txtQty.getText()+ " ----- P" + orangeprice);
}
if (radPomelo.isSelected()){
pomeloprice += 95.25 * qty;
txtScreen.append("\n");
txtScreen.append(" Pomelos ----- x" + txtQty.getText()+ " ----- P" + pomeloprice);
}
totalprice=appleprice+orangeprice+pomeloprice;
lblTotal.setText("$"+(totalprice));
}
public static void main(String[]args){
Cashier wew = new Cashier();
}
}