my code is posted below the program is to be very simple just just a button a choice box a text box and a canvas the user is to be able to select what item they want and then the price appears in the label....then the user is able to put in a quantity and when they hit order the program should calculate the price and display it nice and neat in the canvas....I'm new to Java and ...if this were C++ I could do it in an hour I just don't understand why it's not working...please someone help
thank you
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class grocerystore extends Applet{
private Button order = new Button("Order");
private Label price = new Label("Your price will appear here");
private Choice food = new Choice();
private TextField quantity = new TextField();
double quantitySelected;
double eggs = 1.90;
double milk = 1.47;
double bread = 2.12;
double total;
display output = new display();
public void init() {
add(order);
add(price);
add(quantity);
add(food);
food.add("Select your food");
food.add("Eggs");
food.add("Milk");
food.add("Bread");
order.addActionListener(this);
food.addItemListener(this);
add(output);
output.setSize(250,100);
order.addActionListener(this)
food.addActionListener(this)
}
class display extends Canvas
implements ActionListener, ItemListener {
int itemSelected = food.getSelectedIndex();
public void actionPerformed(ActionEvent action) {
quantitySelected = new Double(quantity.getText()).doubleValue();
if (itemSelected==1)
total = quantitySelected * eggs;
if (itemSelected==2)
total = quantitySelected * milk;
if (itemSelected==3)
total = quantitySelected * bread;
}
public void itemStateChanged(ItemEvent event) {
if (itemSelected==1)
price.setText("The price of a dozen eggs is " + eggs);
if (itemSelected==2)
price.setText("The price of a quart of milk is " + milk);
if (itemSelected==3)
price.setText("The price of a loaf of bread is " + bread);
}
public void paint(Graphics g){
if (itemSelected==1){
g.drawString("You purchased " + quantitySelected + "dozen eggs",20,20);
g.drawString("for a total of " + total,20,30);
}
}
}
}