Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Parking Lot Program help!

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Parking Lot Program help!

    Hi guys,

    In my Java Programming II class, we have to create a program that displays cars entering and exiting a parking lot. I created 50 JButtons (I know there is a more efficient way to write my code, but how?) and I'm trying to get the Buttons to display a .GIF image when pressed.

    This is the error I'm getting:

    F:\Parking.java:326: cannot find symbol
    symbol : method add(javax.swing.ImageIcon)
    location: class javax.swing.JButton
    button01.add(splat);
    There are 50 errors (button01, button 02 etc...) for each button. I just didn't feel like pasting all of them.

    This is my code:
    /*Parking.java
     */
     
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
     
     
    public class Parking extends JFrame implements ActionListener
    {
    	//NUMERICAL DECLARATIONS
    	double hourlyRate;
    	double tow;
    	double dailyRate;
    	int timeIN = 0;
    	int timeOUT = 0;
    	int counter = 0;
    	int numCars = 0;
     
    	ImageIcon splat = new ImageIcon("images/middle.gif");
     
    	private JMenuBar mainBar  	= new JMenuBar();
     	private JMenu menu1 	  	= new JMenu("File");
     	private JMenu menu2 	  	= new JMenu("Parking");
     
     	private JMenuItem exit 		= new JMenuItem("Exit");
     	private JMenuItem profReport = new JMenuItem("Profit Report");
     
     	private JButton profitReport = new JButton("Profit Report");
     	private JButton towTruck = new JButton("Call Tow Truck");
     	private JTextField carNums = new JTextField(15);
     
    	private JButton button01 = new JButton("");
     	private JButton button02 = new JButton("");
     	private JButton button03 = new JButton("");
     	private JButton button04 = new JButton("");
     	private JButton button05 = new JButton("");
     	private JButton button06 = new JButton("");
     	private JButton button07 = new JButton("");
     	private JButton button08 = new JButton("");
     	private JButton button09 = new JButton("");
     	private JButton button10 = new JButton("");
     
     	private JButton button11 = new JButton("");
     	private JButton button12 = new JButton("");
     	private JButton button13 = new JButton("");
     	private JButton button14 = new JButton("");
     	private JButton button15 = new JButton("");
     	private JButton button16 = new JButton("");
     	private JButton button17 = new JButton("");
     	private JButton button18 = new JButton("");
     	private JButton button19 = new JButton("");
     	private JButton button20 = new JButton("");
     
     	private JButton button21 = new JButton("");
     	private JButton button22 = new JButton("");
     	private JButton button23 = new JButton("");
     	private JButton button24 = new JButton("");
     	private JButton button25 = new JButton("");
     	private JButton button26 = new JButton("");
     	private JButton button27 = new JButton("");
     	private JButton button28 = new JButton("");
     	private JButton button29 = new JButton("");
     	private JButton button30 = new JButton("");
     
     	private JButton button31 = new JButton("");
     	private JButton button32 = new JButton("");
     	private JButton button33 = new JButton("");
     	private JButton button34 = new JButton("");
     	private JButton button35 = new JButton("");
     	private JButton button36 = new JButton("");
     	private JButton button37 = new JButton("");
     	private JButton button38 = new JButton("");
     	private JButton button39 = new JButton("");
     	private JButton button40 = new JButton("");
     
     	private JButton button41 = new JButton("");
     	private JButton button42 = new JButton("");
     	private JButton button43 = new JButton("");
     	private JButton button44 = new JButton("");
     	private JButton button45 = new JButton("");
     	private JButton button46 = new JButton("");
     	private JButton button47 = new JButton("");
     	private JButton button48 = new JButton("");
     	private JButton button49 = new JButton("");
     	private JButton button50 = new JButton("");
     
     	private JPanel panel01 = new JPanel(new GridLayout(5, 10, 1, 3));
     	private JPanel panel02 = new JPanel(new FlowLayout());
     
     	public void composeMenus()
    	{
    		setJMenuBar(mainBar);
    		mainBar.add(menu1);
    		mainBar.add(menu2);
    		menu1.add(exit);
     
    		//PANEL01
    		panel01.add(button01);
    		panel01.add(button02);
    		panel01.add(button03);
    		panel01.add(button04);
    		panel01.add(button05);
    		panel01.add(button06);
    		panel01.add(button07);
    		panel01.add(button08);
    		panel01.add(button09);
    		panel01.add(button10);
     
    		panel01.add(button11);
    		panel01.add(button12);
    		panel01.add(button13);
    		panel01.add(button14);
    		panel01.add(button15);
    		panel01.add(button16);
    		panel01.add(button17);
    		panel01.add(button18);
    		panel01.add(button19);
    		panel01.add(button20);
     
    		panel01.add(button21);
    		panel01.add(button22);
    		panel01.add(button23);
    		panel01.add(button24);
    		panel01.add(button25);
    		panel01.add(button26);
    		panel01.add(button27);
    		panel01.add(button28);
    		panel01.add(button29);
    		panel01.add(button30);
     
    		panel01.add(button31);
    		panel01.add(button32);
    		panel01.add(button33);
    		panel01.add(button34);
    		panel01.add(button35);
    		panel01.add(button36);
    		panel01.add(button37);
    		panel01.add(button38);
    		panel01.add(button39);
    		panel01.add(button40);
     
    		panel01.add(button41);
    		panel01.add(button42);
    		panel01.add(button43);
    		panel01.add(button44);
    		panel01.add(button45);
    		panel01.add(button46);
    		panel01.add(button47);
    		panel01.add(button48);
    		panel01.add(button49);
    		panel01.add(button50);
     
    		//PANEL02
    		panel02.add(profitReport);
    		panel02.add(carNums);
    		panel02.add(towTruck);
     
    	}
     
    	public void setColor()
    	{
    		exit.addActionListener(this);
    		button01.setBackground(Color.DARK_GRAY);
    		button02.setBackground(Color.DARK_GRAY);
    		button03.setBackground(Color.DARK_GRAY);
    		button04.setBackground(Color.DARK_GRAY);
    		button05.setBackground(Color.DARK_GRAY);
    		button06.setBackground(Color.DARK_GRAY);
    		button07.setBackground(Color.DARK_GRAY);
    		button08.setBackground(Color.DARK_GRAY);
    		button09.setBackground(Color.DARK_GRAY);
    		button10.setBackground(Color.DARK_GRAY);
     
    		button11.setBackground(Color.DARK_GRAY);
    		button12.setBackground(Color.DARK_GRAY);
    		button13.setBackground(Color.DARK_GRAY);
    		button14.setBackground(Color.DARK_GRAY);
    		button15.setBackground(Color.DARK_GRAY);
    		button16.setBackground(Color.DARK_GRAY);
    		button17.setBackground(Color.DARK_GRAY);
    		button18.setBackground(Color.DARK_GRAY);
    		button19.setBackground(Color.DARK_GRAY);
    		button20.setBackground(Color.DARK_GRAY);
     
    		button21.setBackground(Color.DARK_GRAY);
    		button22.setBackground(Color.DARK_GRAY);
    		button23.setBackground(Color.DARK_GRAY);
    		button24.setBackground(Color.DARK_GRAY);
    		button25.setBackground(Color.DARK_GRAY);
    		button26.setBackground(Color.DARK_GRAY);
    		button27.setBackground(Color.DARK_GRAY);
    		button28.setBackground(Color.DARK_GRAY);
    		button29.setBackground(Color.DARK_GRAY);
    		button30.setBackground(Color.DARK_GRAY);
     
    		button31.setBackground(Color.DARK_GRAY);
    		button32.setBackground(Color.DARK_GRAY);
    		button33.setBackground(Color.DARK_GRAY);
    		button34.setBackground(Color.DARK_GRAY);
    		button35.setBackground(Color.DARK_GRAY);
    		button36.setBackground(Color.DARK_GRAY);
    		button37.setBackground(Color.DARK_GRAY);
    		button38.setBackground(Color.DARK_GRAY);
    		button39.setBackground(Color.DARK_GRAY);
    		button40.setBackground(Color.DARK_GRAY);
     
    		button41.setBackground(Color.DARK_GRAY);
    		button42.setBackground(Color.DARK_GRAY);
    		button43.setBackground(Color.DARK_GRAY);
    		button44.setBackground(Color.DARK_GRAY);
    		button45.setBackground(Color.DARK_GRAY);
    		button46.setBackground(Color.DARK_GRAY);
    		button47.setBackground(Color.DARK_GRAY);
    		button48.setBackground(Color.DARK_GRAY);
    		button49.setBackground(Color.DARK_GRAY);
    		button50.setBackground(Color.DARK_GRAY);
    		panel01.setBackground(Color.YELLOW);
    	}
     
    	public Parking()
    	{
    		composeMenus();
    		setColor();
    		addActionListeners();
    		setLayouts();
     
     
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
     
    		setSize(540, 540);
    		setVisible(true);
    	}	
     
    	public void setLayouts()
    	{
    		setLayout(new BorderLayout());
    		add(panel01, BorderLayout.CENTER);
    		add(panel02, BorderLayout.SOUTH);
    	}
     
    	public void addActionListeners()
    	{
    		profitReport.addActionListener(this);
    		exit.addActionListener(this);
    		button01.addActionListener(this);
    		button02.addActionListener(this);
    		button03.addActionListener(this);
    		button04.addActionListener(this);
    		button05.addActionListener(this);
    		button06.addActionListener(this);
    		button07.addActionListener(this);
    		button08.addActionListener(this);
    		button09.addActionListener(this);
    		button10.addActionListener(this);
     
    		button11.addActionListener(this);
    		button12.addActionListener(this);
    		button13.addActionListener(this);
    		button14.addActionListener(this);
    		button15.addActionListener(this);
    		button16.addActionListener(this);
    		button17.addActionListener(this);
    		button18.addActionListener(this);
    		button19.addActionListener(this);
    		button20.addActionListener(this);
     
    		button21.addActionListener(this);
    		button22.addActionListener(this);
    		button23.addActionListener(this);
    		button24.addActionListener(this);
    		button25.addActionListener(this);
    		button26.addActionListener(this);
    		button27.addActionListener(this);
    		button28.addActionListener(this);
    		button29.addActionListener(this);
    		button30.addActionListener(this);
     
    		button31.addActionListener(this);
    		button32.addActionListener(this);
    		button33.addActionListener(this);
    		button34.addActionListener(this);
    		button35.addActionListener(this);
    		button36.addActionListener(this);
    		button37.addActionListener(this);
    		button38.addActionListener(this);
    		button39.addActionListener(this);
    		button40.addActionListener(this);
     
    		button41.addActionListener(this);
    		button42.addActionListener(this);
    		button43.addActionListener(this);
    		button44.addActionListener(this);
    		button45.addActionListener(this);
    		button46.addActionListener(this);
    		button47.addActionListener(this);
    		button48.addActionListener(this);
    		button49.addActionListener(this);
    		button50.addActionListener(this);
     
    	}
     
    	public void profitFrame()
    	{
    		ProfitFrame profitFrame = new ProfitFrame();
    		profitFrame.setSize(200, 200);
    		profitFrame.setVisible(true);
    		profitFrame.setLayout(new FlowLayout());
     
    	}
     
    	public void carCounter()
    	{
    		counter++;
    		numCars++;
     
    		if(counter == 2)
    		{
    			counter = 0;
    		}
     
    		else if(counter == 1)
    		{
    			button01.add(splat);
    			button02.add(splat);
    			button03.add(splat);
    			button04.add(splat);
    			button05.add(splat);
    			button06.add(splat);
    			button07.add(splat);
    			button08.add(splat);
    			button09.add(splat);
    			button10.add(splat);
     
    			button11.add(splat);
    			button12.add(splat);
    			button13.add(splat);
    			button14.add(splat);
    			button15.add(splat);
    			button16.add(splat);
    			button17.add(splat);
    			button18.add(splat);
    			button19.add(splat);
    			button20.add(splat);
     
    			button21.add(splat);
    			button22.add(splat);
    			button23.add(splat);
    			button24.add(splat);
    			button25.add(splat);
    			button26.add(splat);
    			button27.add(splat);
    			button28.add(splat);
    			button29.add(splat);
    			button30.add(splat);
     
    			button31.add(splat);
    			button32.add(splat);
    			button33.add(splat);
    			button34.add(splat);
    			button35.add(splat);
    			button36.add(splat);
    			button37.add(splat);
    			button38.add(splat);
    			button39.add(splat);
    			button40.add(splat);
     
    			button41.add(splat);
    			button42.add(splat);
    			button43.add(splat);
    			button44.add(splat);
    			button45.add(splat);
    			button46.add(splat);
    			button47.add(splat);
    			button48.add(splat);
    			button49.add(splat);
    			button50.add(splat);
    		}
    		else if(counter == 0)
    		{
    			//BUTTON SHOULD RESET - REMOVING THE .gif IMAGE
    			button01.validate();
    			button02.validate();
    			button03.validate();
    			button04.validate();
    			button05.validate();
    			button06.validate();
    			button07.validate();
    			button08.validate();
    			button09.validate();
    			button10.validate();
     
    			button11.validate();
    			button12.validate();
    			button13.validate();
    			button14.validate();
    			button15.validate();
    			button16.validate();
    			button17.validate();
    			button18.validate();
    			button19.validate();
    			button20.validate();
     
    			button21.validate();
    			button22.validate();
    			button23.validate();
    			button24.validate();
    			button25.validate();
    			button26.validate();
    			button27.validate();
    			button28.validate();
    			button29.validate();
    			button30.validate();
     
    			button31.validate();
    			button32.validate();
    			button33.validate();
    			button34.validate();
    			button35.validate();
    			button36.validate();
    			button37.validate();
    			button38.validate();
    			button39.validate();
    			button40.validate();
     
    			button41.validate();
    			button42.validate();
    			button43.validate();
    			button44.validate();
    			button45.validate();
    			button46.validate();
    			button47.validate();
    			button48.validate();
    			button49.validate();
    			button50.validate();
    		}
     
    	}
     
    	public void actionPerformed(ActionEvent e)
    	{
    		Object source = e.getSource();
    		Container con = getContentPane();
     
    		if(source == exit)
    		{
    			System.exit(0);
    		}
    		else if (source == profitReport)
    		{
    			profitFrame();
    		}
    	}
     
    	public static void main (String[] args)
    	{
    		Parking frame = new Parking();
    	}
     
     
    }

    As I said before, I'm still a beginner, but I know there has to be an easier way to write something like this; perhaps an array and forloops? We've learned all about them; I'm just not sure how I would use them to change individual JButtons, as opposed to all 50 of them.

    This is what we have to write:

    Write a program that keeps track of cars entering a parking lot:
    • Time in/Time Out
    • 50 Parking Spaces
    • $2 per hour
    • $12 per day option
    • No Overnights
    • Hours of Operation: 7AM to 11PM (24 hour clock maybe?)
    • End of Day Profit Report
    I also referenced the Profit Report class: here is that code.
     import javax.swing.*;
     
     public class ProfitFrame extends JFrame
     {
     	final int width=350;
     	final int height=350;
     
     	final int width2=500;
     	final int height2=500;
     
     
     
     	public ProfitFrame()
     	{
     		super("GMoney Parking Profit Report");
     		setSize(width,height);
     		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
    	}
     }

    ANY HELP WOULD BE MUCH APPRECIATED!!
    Attached Images Attached Images


  2. #2
    Junior Member
    Join Date
    Jan 2011
    Posts
    14
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default Re: Parking Lot Program help!

    First you said you needed to display the cars:

    Quote Originally Posted by soul.salem View Post
    In my Java Programming II class, we have to create a program that displays cars entering and exiting a parking lot.
    Then you said you didn't need to:

    Quote Originally Posted by soul.salem View Post
    This is what we have to write:
    Write a program that keeps track of cars entering a parking lot:
    Time in/Time Out
    50 Parking Spaces
    $2 per hour
    $12 per day option
    No Overnights
    Hours of Operation: 7AM to 11PM (24 hour clock maybe?)
    End of Day Profit Report
    My question is: Are you required to create a program that displays cars entering and exiting? Also, it's not clear what you're asking for here. Are you asking for a more efficient way to display the cars?

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,706
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Parking Lot Program help!

    First, yes, I would put it in an array of 50 JButtons. I'm not in the best state of mind to help you out code-wise atm, but it is the same basic concept as using any other array. You would want that simply on the purpose of not having to write out each operation 50 times.

    Second, you need to use the setIcon(Icon defaultIcon) method (AbstractButton (Java Platform SE 6)) instead of the add(...) method. I'm not sure why you thought you should use the add method.

    F:\Parking.java:326: cannot find symbol
    symbol : method add(javax.swing.ImageIcon)
    location: class javax.swing.JButton
    button01.add(splat);
    Read your error like this:
    Cannot find symbol -> But what symbol?
    method add(javax.swing.ImageIcon) -> ok, so I cannot find the method add() that accepts ImageIcon as a parameter. Perhaps I should take a look at the API. Now, what API should I look at?
    class javax.swing.JButton -> oh, the JButton API.

    It is fairly simple to understand the error messages if you learn how to read them correctly.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/