hello everyone! I need help....a prof asked for our help to create a simple program for her assignment but i am having a hard time doing it for I am still not an expert in java.....here is the scenario.....

You are about to go to the Super market and would like to buy groceries for your house that would last for a week.

List down items that you normally buy and the cost for each of the item.

I want you to write a simple program that will be used by the supermarket to compute for the total sales of all the items you will be buying and probably other customers too.

I want your program to compute the total sales from all the customers and list down the summary of the items bought by the customers in a form of a report for the owner or manager to see and probably decide if they need to replenish their stocks.

PROBLEMS

  • The clear doesn't work
  • How can I declare the textboxs under the products string?
  • How to create a report that would display the items sold?

here is the code that I finished....I was just able to make the total sales to add up......

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class grocery extends JFrame implements ActionListener
{

	private static int width=600;
	private static int height=300;
	private JLabel[] labelJL = new JLabel[3];
	private JTextField[] textJT = new JTextField[21];
	private JButton[] AddClearExit = new JButton[3];
	public grocery()
	{

		Container pane = getContentPane();
		pane.setBackground(Color.orange);
		setTitle("USING BUTTON EVENT");
		setSize(width,height);
		setLayout(null);


		labelJL[0] = new JLabel("Products",0);
		labelJL[0].setLocation(380,190);
		labelJL[0].setSize(100,20);
		pane.add(labelJL[0]);

		textJT[0] = new JTextField();
		textJT[0].setLocation(330,230);
		textJT[0].setSize(200,20);
		pane.add(textJT[0]);

		textJT[3] = new JTextField();
		textJT[3].setLocation(330,270);
		textJT[3].setSize(200,20);
		pane.add(textJT[3]);

		textJT[4] = new JTextField();
		textJT[4].setLocation(330,310);
		textJT[4].setSize(200,20);
		pane.add(textJT[4]);

		textJT[5] = new JTextField();
		textJT[5].setLocation(330,350);
		textJT[5].setSize(200,20);
		pane.add(textJT[5]);

		textJT[6] = new JTextField();
		textJT[6].setLocation(330,390);
		textJT[6].setSize(200,20);
		pane.add(textJT[6]);

		textJT[7] = new JTextField();
		textJT[7].setLocation(330,430);
		textJT[7].setSize(200,20);
		pane.add(textJT[7]);

		textJT[8] = new JTextField();
		textJT[8].setLocation(330,470);
		textJT[8].setSize(200,20);
		pane.add(textJT[8]);

		textJT[9] = new JTextField();
		textJT[9].setLocation(330,510);
		textJT[9].setSize(200,20);
		pane.add(textJT[9]);

		textJT[10] = new JTextField();
		textJT[10].setLocation(330,550);
		textJT[10].setSize(200,20);
		pane.add(textJT[10]);

		labelJL[1] = new JLabel("Price",0);
		labelJL[1].setLocation(710,190);
		labelJL[1].setSize(100,20);
		pane.add(labelJL[1]);

		textJT[11] = new JTextField();
		textJT[11].setLocation(710,230);
		textJT[11].setSize(200,20);
		pane.add(textJT[11]);

		textJT[12] = new JTextField();
		textJT[12].setLocation(710,270);
		textJT[12].setSize(200,20);
		pane.add(textJT[12]);

		textJT[13] = new JTextField();
		textJT[13].setLocation(710,310);
		textJT[13].setSize(200,20);
		pane.add(textJT[13]);

		textJT[14] = new JTextField();
		textJT[14].setLocation(710,350);
		textJT[14].setSize(200,20);
		pane.add(textJT[14]);

		textJT[15] = new JTextField();
		textJT[15].setLocation(710,390);
		textJT[15].setSize(200,20);
		pane.add(textJT[15]);

		textJT[16] = new JTextField();
		textJT[16].setLocation(710,430);
		textJT[16].setSize(200,20);
		pane.add(textJT[16]);

		textJT[17] = new JTextField();
		textJT[17].setLocation(710,470);
		textJT[17].setSize(200,20);
		pane.add(textJT[17]);

		textJT[18] = new JTextField();
		textJT[18].setLocation(710,510);
		textJT[18].setSize(200,20);
		pane.add(textJT[18]);

		textJT[19] = new JTextField();
		textJT[19].setLocation(710,550);
		textJT[19].setSize(200,20);
		pane.add(textJT[19]);



		labelJL[2] = new JLabel("Total sales",0);
		labelJL[2].setLocation(550,590);
		labelJL[2].setSize(100,20);
		pane.add(labelJL[2]);

		textJT[2] = new JTextField();
		textJT[2].setLocation(710,590);
		textJT[2].setSize(200,20);
		textJT[2].setEditable(false);
		pane.add(textJT[2]);



		AddClearExit[0] = new JButton("Total");
		AddClearExit[0].setLocation(270,680);
		AddClearExit[0].setSize(150,20);
		AddClearExit[0].addActionListener(this);
		pane.add(AddClearExit[0]);



		AddClearExit[1] = new JButton("Clear");
		AddClearExit[1].setLocation(450,680);
		AddClearExit[1].setSize(150,20);
		AddClearExit[1].addActionListener(this);
		pane.add(AddClearExit[1]);

		AddClearExit[2] = new JButton("Exit");
		AddClearExit[2].setLocation(629,680);
		AddClearExit[2].setSize(150,20);
		AddClearExit[2].addActionListener(this);
		pane.add(AddClearExit[2]);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);


	}
		public void actionPerformed(ActionEvent ae)
		{
			String temp = "";
			long a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;



	try
	{
		a=Long.parseLong(textJT[11].getText());
		b=Long.parseLong(textJT[12].getText());
		c=Long.parseLong(textJT[13].getText());
		d=Long.parseLong(textJT[14].getText());
		e=Long.parseLong(textJT[15].getText());
		f=Long.parseLong(textJT[16].getText());
		g=Long.parseLong(textJT[17].getText());
		h=Long.parseLong(textJT[18].getText());
		i=Long.parseLong(textJT[19].getText());
	}
		catch (Exception ne)
		{
		for(int x=0;x<21;textJT[x].setText(temp),x++);

		}


		if(ae.getActionCommand().equals("Total"))
			{temp+=a+b+c+d+e+f+g+h+i;
					textJT[2].setText(temp);}

			 else if(ae.getActionCommand().equals("Clear"))

				for(int x=0;x<21;textJT[x].setText(temp),x++);
				if(ae.getActionCommand().equals("Exit"))
					dispose();
		}



	public long total(long x,long y)
	{
		return x+y;

	}

	public static void main (String args[])
	{
		new Button();

		}
}

thank you so much for the help.....

Recommended Answers

All 2 Replies

What does the Button class do?

You need to design what the code in the actionPerformed method is supposed to do.
Add a paragraph of comments before that method describing what you want it to do.
It looks like a mess of half thought ideas.

Also There are coding problems with hidden brackets } and { that make it hard to read.
There is an else clause that should have the following code enclosed in {}s.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.