Hi Everyone,
I created a GUI using Java with Buttons, Pictures and labels.
When one of the buttons is pressed it increases the price, which can be seen by the labels increasing the necessary amount.
What I would like to be able to do in my GUI is allow the user to record details of the sale and be able to display the sales of past customers to date, by this I mean when I click one of the buttons it will record the amount of that item into a file and when I press the another button, not the same button as the items, it will show the user the existing orders that have been taken in the past.
Here is my Code:
//Importing needed classes import java.awt.*; import javax.swing.*; import java.awt.event.*; //The Main Class public class CoffeeShop extends JFrame { //Initialising the Buttons and Labels private JLabel instruction1; private JLabel instruction2; private JLabel instruction3; private JLabel label; private JLabel label1; private JLabel label2; private JLabel label3; private JLabel label4; private JLabel label5; private JButton button; private JButton button1; private JButton button2; private JButton button3; private JButton button4; private JButton button5; //Initialising the Result Labels private JLabel resultLabel; private JLabel resultLabel1; private JLabel resultLabel2; private JLabel resultLabel3; private JLabel resultLabel4; private JLabel resultLabel5; //Initialising Items on sale private double coffee; private double hotchocolate; private double cupcakes; private double scones; private double tea; private double fanta; //Initialising Images of Items private ImageIcon coffee1; private ImageIcon hotchocolate1; private ImageIcon cupcakes1; private ImageIcon scones1; private ImageIcon greentea; private ImageIcon fanta1; //Initialising the Screen Width and Length private static final int FRAME_WIDTH = 780; private static final int FRAME_HEIGHT = 400; //Initialising the Initial cost and Interest Rates of the items on sale private static final double INITIAL_COST = 0; private static final double PRICE = 1.5; private static final double PRICE1 = 2.0; private static final double PRICE2 = 1.0; private static final double PRICE3 = 2.5; private static final double PRICE4 = 3.5; private static final double PRICE5 = 3.0; public CoffeeShop() { super( "Caroline's Cafe" ); createComponents(); setSize(FRAME_WIDTH, FRAME_HEIGHT); } //Creating the Click Listeners class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { coffee = coffee + PRICE; resultLabel.setText("Total cost of your Coffee(s): " + coffee); } } class ClickListener1 implements ActionListener { public void actionPerformed(ActionEvent event) { hotchocolate = hotchocolate + PRICE1; resultLabel1.setText("Total cost of your Hot Chocolate(s): " + hotchocolate); } } class ClickListener2 implements ActionListener { public void actionPerformed(ActionEvent event) { cupcakes = cupcakes + PRICE2; resultLabel2.setText("Total cost of your Cupcake(s): " + cupcakes); } } class ClickListener3 implements ActionListener { public void actionPerformed(ActionEvent event) { scones = scones + PRICE3; resultLabel3.setText("Total cost of your Scone(s) with Jam: " + scones); } } class ClickListener4 implements ActionListener { public void actionPerformed(ActionEvent event) { tea = tea + PRICE4; resultLabel4.setText("Total cost of your Green Tea(s): " + tea); } } class ClickListener5 implements ActionListener { public void actionPerformed(ActionEvent event) { fanta = fanta + PRICE5; resultLabel5.setText("Total cost of your Fanta(s): " + fanta); } } //Creating the Components private void createComponents() { coffee1 = new ImageIcon (getClass().getResource("coffee.jpg")); hotchocolate1 = new ImageIcon (getClass().getResource("HotChocolate.jpg")); cupcakes1 = new ImageIcon (getClass().getResource("cupcakes.jpg")); scones1 = new ImageIcon (getClass().getResource("scones.jpg")); fanta1 = new ImageIcon (getClass().getResource("fanta.jpg")); greentea = new ImageIcon (getClass().getResource("tea.jpg")); button = new JButton("Coffee", coffee1); ActionListener listener = new ClickListener(); button.addActionListener(listener); label = new JLabel("Price $1.50"); button1 = new JButton("Mint Hot Chocolate", hotchocolate1); ActionListener listener1 = new ClickListener1(); button1.addActionListener(listener1); label1 = new JLabel("Price:$2.00"); button2 = new JButton("Cupcakes", cupcakes1); ActionListener listener2 = new ClickListener2(); button2.addActionListener(listener2); label2 = new JLabel("Price:$1.00"); button3 = new JButton("Scones with Jam", scones1); ActionListener listener3 = new ClickListener3(); button3.addActionListener(listener3); label3 = new JLabel("Price:$2.50"); button4 = new JButton("Green Tea", greentea); ActionListener listener4 = new ClickListener4(); button4.addActionListener(listener4); label4 = new JLabel("Price:$3.50"); button5 = new JButton("Fanta", fanta1); ActionListener listener5 = new ClickListener5(); button5.addActionListener(listener5); label5 = new JLabel("Price:$3.00"); resultLabel = new JLabel("Total cost of your Coffee(s): " + coffee); resultLabel1 = new JLabel("Total cost of your Hot Chocolate(s): " + hotchocolate); resultLabel2 = new JLabel("Total cost of your Cupcake(s): " + cupcakes); resultLabel3 = new JLabel("Total cost of your Scone(s) with Jam: " + scones); resultLabel4 = new JLabel("Total cost of your Green Tea(s): " + tea); resultLabel5 = new JLabel("Total cost of your Fanta(s): " + fanta); instruction1 = new JLabel("Click the below buttons to order:"); instruction2 = new JLabel("Here is the total of each item you bought:"); instruction3 = new JLabel("The Prices of each item are here:"); //Creating the main panel JPanel panel = new JPanel(); //Setting the Page Layout panel.setLayout( new GridLayout(7,3)); //Creating buttons, resultLabels and Labels panel.add(instruction1); panel.add(instruction2); panel.add(instruction3); panel.add(button); panel.add(resultLabel); panel.add(label); panel.add(button1); panel.add(resultLabel1); panel.add(label1); panel.add(button2); panel.add(resultLabel2); panel.add(label2); panel.add(button3); panel.add(resultLabel3); panel.add(label3); panel.add(button4); panel.add(resultLabel4); panel.add(label4); panel.add(button5); panel.add(resultLabel5); panel.add(label5); add(panel); } } import javax.swing.JFrame; public class CoffeeShopViewer { public static void main(String[]args) { JFrame frame = new CoffeeShop(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
Can anyone please help me?