Counters and Accumulator
Lab4c

We saw counters in Unit 3. We counted the number of correct answers to the food questions and the number of games played and the number of wins when we were rolling dice in Lab3e.

In this Lab we are going to use accumulators and counters in the actionPerformed method

Copy, paste and run this - you will need the cherries and the cupcake gifs from Unit 3 before you start. You can copy all the gifs from Unit 3 to Unit 4 if you wish to at this time.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;

public class Lab4c1 extends JFrame implements ActionListener
{
   JButton b1, b2, b3, b4;
   JLabel L1, L2, L3, L4;
   JTextField t1, t2, t3, t4,t5,t6;
   JPanel p1, p2, p3;
   JLabel title, myorder;

    String s1="", s2="" ;
    int a1 = 0, a2 = 0;
    double d1 = 0, d2 = 0;
    double total;


   public Lab4c1 ()
   {
   super("Grocery Store");
   Container container = getContentPane();
   container.setLayout(new BorderLayout());
   setSize(300,150);
   container.setBackground(Color.yellow);

   title = new JLabel("Cafeteria", JLabel.CENTER);// how to center
   
   L1 = new JLabel("$0.79 each" );

  
   b1 = new JButton();
   b1.setIcon(new ImageIcon("cupcake.gif"));
   b1.addActionListener(this);
   
   t1 = new JTextField();

   L2 = new JLabel("$2.98 per lb" );

   b2 = new JButton();
   b2.setIcon(new ImageIcon("cherries.gif"));
   b2.addActionListener(this);

   t2 = new JTextField();

   p1 = new JPanel(new GridLayout (2, 3));
//row 1
     p1.add(L1);
     p1.add(b1);
     p1.add(t1);
// row 2
     p1.add(L2);
     p1.add(b2);
     p1.add(t2);

   myorder = new JLabel("your total" );
   t6 = new JTextField();

  p2 = new JPanel(new GridLayout (1, 2));
    p2.add(myorder);
    p2.add(t6);

   container.add(title,BorderLayout.NORTH );
   container.add(p1,BorderLayout.CENTER );
   container.add(p2,BorderLayout.SOUTH );

   setVisible(true);//Make Window Visible
   }

  public void actionPerformed(ActionEvent e)
  {
   


    }

    public static void main(String args[])
    {
     Lab4c1 prog = new Lab4c1 ();
     prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

When we click the cupcake JButton, we want it to tell us how many we are buying and how much it costs

How many times we click will require a counter ( an integer variable) where we add 1 each time
How much they cost will be an accumulater ( a double in this example ) where we add the costs

Later we will figure out the total

 

To make the "cherries JButton work

1. In the actionPerformed method add this ( it might be good to do this for b2 at this time )

if(e.getSource()==b1)
   {

   }// end of b1

2. Between the b1 braces add the counter     a1 = a1+1; //counter(add 1 each time b1 is clicked)

3. After the counter add the accumulator     d1 = d1 + 0.79; //accumulator for cupcakes

4. After the accumulator build the String for the setText and place in the JTextField     

s1 = a1+" for $"+d1;
t1.setText(s1);

Try the program now. It should work correctly for the cupcake.

Fixing flaws Unfortunately doubles are not as easy work work with. In Unit one, we learned how to force them to two decimal place. We should probably round decimals off to 2 places now.

5. Add this to the declaration of global variable section
    DecimalFormat df2 = new DecimalFormat("0.00");

6. Inside the actionPerformed method, inside the conditional for b1,
    add this before the s1= etc....
      String ans = df2.format(d1); // change d1 to a local String variable ans

7. Change the s1 line to this         s1 = a1 + " for $" + ans;

Now run the program and click b1 several times. It should always have 2 decimal places

8. Do similar things for JButton 2 .
Please use a2 as the integer and d2 as the double since my code in the next section uses these variables
        

Now we need to get a grand total for the cost of all the items you are buying

1. Inside actionPerformed method, after both JButtons add this -please read the comments

total = d1 + d2;                //add up the totals from each items
String ans = df2.format(total); // round off the total
s2="$ "+ ans;             // Make a String, add the $ to look like money
t6.setText(s2);   
       // set String s2 in the last JTextField

Your program should work correctly - please show me

Assignment

50 points -Finish Lab4c1

Colorize it, and use fonts - see GUI notes for help. Make up reasonable prices for food or use mine.

 

To make this GUI perfect, you really need to add a clear button for the next person to use the order form.

I could see a clear button in the SOUTH beside the JTextField or added to the top of p1 with dummy JLabels for spacing. If you add this, it is worth 5 points.

When you click the clear button, all counters and accumulators are set back to 0    i.e.   a1=0; etc. Try it.

continue on to part 2 of Lab4c