Inventory Program Part 4 - Gui

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

62 Replies - 11159 Views - Last Post: 21 February 2010 - 11:26 PM Rate Topic: -----

#1 alexlunar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 10-January 10

Inventory Program Part 4 - Gui

Posted 12 February 2010 - 08:56 PM

I am trying to modify my Inventory program so that the results are displayed in a GUI window.

The recomendations I saw online as well as my own common sense tell me I just need to change the printing choice. I don't think I am doing this right at all, can anyone tell me if I am even on the right track, and if not what may help.

Here are my original classes from Part 3...

Cars Class
// Week 6 Inventory Program Part 3
// Inventory program that stores data in class
// Uses Array to return data
// With added SUV subclass & added MPG

public class Cars 
{
   private static int itemNumber[] = new int[8];
   private static String name[] = new String[8];
   private static int units[] = new int[8];
   private static double price[] = new double[8];
   private static int i = 0;

      public Cars(){ }// 
    
      public Cars(int _itemNumber, String _name, int _units, double _price)//
      { 		
         itemNumber[i] = _itemNumber;
         name[i] = _name;
         units[i] = _units;
         price[i] = _price;
         i = i + 1;
      }
	
	
      public static double valueOfInventory(double p,int u)
      {
         return p*u;
   
   }


   
   public static void showInventory()

   
   {		

   
   
   for(int j=0;j<i;j++){

   
   
   System.out.println("\nItem Number: 1090" + itemNumber[j]);

   
   
   System.out.println("Car Name: " + name[j]);

   
   
   System.out.println("MPG: " + Suv.mpg[j]);

   
   
   System.out.println("Number In Stock: " + units[j]);

   
   
   System.out.println("Unit Price : " + price[j]);

   
   
   System.out.println("Value of inventory: " + valueOfInventory(price[j], units[j]));

   
   
   System.out.println("Value with restocking fee: " + Suv.valueOfInventory(price[j], units[j]));

   
   }
}


   
   public static void showValueofInventory()

   
   {

   
   
   double totalVal = 0.00;

   
   
   for (int j = 0; j < i; j++)

   
   
   {

   
   
   
   totalVal = totalVal + valueOfInventory(price[j], units[j]);

   
   
   }

   
   
   
   

   
   
   System.out.println("\n**Total Value of In Stock Cars is = " + totalVal);
	

   
   }
		
}



Products Class
//Week 6 Product Array Class


   import java.io.InputStreamReader;
   import java.io.BufferedReader;
   import java.io.IOException;

public class Products 
{
   public static void main( String[] args)
   {
     Cars p = new Suv(1, "Ford Ranger", "20", 5, 14000);
     Cars q = new Suv(2, "Ford Focus", "24", 2, 13000);
     Cars r = new Suv(3, "Ford F150", "18", 12, 18000);
     Cars s = new Suv(4, "Ford Probe", "22", 1, 7500);
     Cars t = new Suv(5, "Ford Escort", "28", 4, 4500);        
     Cars u = new Suv(6, "Ford Aspire", "30", 1, 10000);
     Cars v = new Suv(7, "Ford Taurus", "21", 8, 9750);
     Cars w = new Suv(8, "Ford Explorer SUV", "16", 6, 24000);

        ///show inventory
        Cars.showInventory();

        ///show total value of inventory
	Cars.showValueofInventory();
			 
   }
}



Suv Class
//Week 6 Added Subclass with MPG & Restocking Fee


public class Suv extends Cars

{

 static String mpg[] = new String[8];//New product feature itemCode added
 private static int i = 0;

     //Constructor
     public Suv(int _itemNumber, String _name, String _mpg, int _units, double _price) 
     {
      	 super( _itemNumber, _name, _units, _price);
    	 mpg[i] = _mpg; 
 		i++;   	
     }
   
     public static double valueOfInventory(double p,int u)
	{
	   return 1.05*Cars.valueOfInventory(p, u);
	}
    
}


I made changes to my Cars class, since thats where I designated how the results should print. Here is my modified class...

// Week 7 Inventory Program Part 4
// Inventory program that stores data in class
// Uses GUI to return data
// With added SUV subclass & added MPG


import java.awt.FlowLayout; // specifies how components are arranged
import javax.swing.JFrame; // provides basic window features
import javax.swing.JLabel; // displays text and images
import javax.swing.SwingConstants; // common constants used with Swing


public class Cars extends JFrame
{
   private static int itemNumber[] = new int[8];
   private static String name[] = new String[8];
   private static int units[] = new int[8];
   private static double price[] = new double[8];
   private static int i = 0;

      public Cars(){ }// 
    
      public Cars(int _itemNumber, String _name, int _units, double 

_price)//
      { 		
         itemNumber[i] = _itemNumber;
         name[i] = _name;
         units[i] = _units;
         price[i] = _price;
         i = i + 1;
      }
	
	
      public static double valueOfInventory(double p,int u)
      {
         return p*u;
   
   }



   
   public static void showInventory()

   // LabelFrame constructor adds JLabels to JFrame
   {
   super( "Testing JLabel" );
   setLayout( new FlowLayout() ); // set frame layout

   
   for(int j=0;j<i;j++){

 
     // JLabel constructor with a string argument
   label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: 

" + name[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + 

"Unit Price : " + price[j] + "Value of inventory: " + valueOfInventory

(price[j], units[j] + "Value with restocking fee: " + 

Suv.valueOfInventory(price[j], units[j]" );
   label1.setToolTipText( "This is label1" );
   
   {		

    
   
 
   
   
  

   
   
 

   
   }
}


   
   public static void showValueofInventory()

   
   {

   
   
   double totalVal = 0.00;

   
   
   for (int j = 0; j < i; j++)

   
   
   {

   
   
   
   totalVal = totalVal + valueOfInventory(price[j], units[j]);

   
   
   }

   
   
   
   

   
   
   System.out.println("\n**Total Value of In Stock Cars is = " + 

totalVal);

add( label1 ); // add label1 to JFrame

	

   
   }
		
}




These are all the errors I received


c:\gui>javac cars.java
cars.java:54: ')' expected
label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + nam
e[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " +
price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value
with restocking fee: " + Suv.valueOfInventory(price[j], units[j]" );

^
cars.java:54: not a statement
label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + nam
e[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " +
price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value
with restocking fee: " + Suv.valueOfInventory(price[j], units[j]" );

^
cars.java:54: ';' expected
label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + nam
e[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " +
price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value
with restocking fee: " + Suv.valueOfInventory(price[j], units[j]" );

^
cars.java:54: ';' expected
label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + nam
e[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " +
price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value
with restocking fee: " + Suv.valueOfInventory(price[j], units[j]" );

^
cars.java:54: not a statement
label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + nam
e[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " +
price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value
with restocking fee: " + Suv.valueOfInventory(price[j], units[j]" );

^
cars.java:54: ';' expected
label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + nam
e[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " +
price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value
with restocking fee: " + Suv.valueOfInventory(price[j], units[j]" );

^
cars.java:54: ';' expected
label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + nam
e[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " +
price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value
with restocking fee: " + Suv.valueOfInventory(price[j], units[j]" );

^
cars.java:54: ';' expected
label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + nam
e[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " +
price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value
with restocking fee: " + Suv.valueOfInventory(price[j], units[j]" );


^
cars.java:54: not a statement
label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + nam
e[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " +
price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value
with restocking fee: " + Suv.valueOfInventory(price[j], units[j]" );


^
cars.java:54: ';' expected
label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + nam
e[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " +
price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value
with restocking fee: " + Suv.valueOfInventory(price[j], units[j]" );


^
cars.java:54: ';' expected
label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + nam
e[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " +
price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value
with restocking fee: " + Suv.valueOfInventory(price[j], units[j]" );



^
cars.java:54: ';' expected
label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + nam
e[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " +
price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value
with restocking fee: " + Suv.valueOfInventory(price[j], units[j]" );



^
cars.java:76: illegal start of expression
public static void showValueofInventory()
^
cars.java:76: illegal start of expression
public static void showValueofInventory()
^
cars.java:76: ';' expected
public static void showValueofInventory()
^
cars.java:76: ';' expected
public static void showValueofInventory()
^
16 errors

Is This A Good Question/Topic? 0
  • +

Replies To: Inventory Program Part 4 - Gui

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10558
  • View blog
  • Posts: 39,065
  • Joined: 27-December 08

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 09:11 PM

Work on your indentations for your code. It is a nightmare to read.

As for your label1 errors, if you look at the highlighting, you'll notice that the double quotes don't exactly match up, which is probably causing your error.
Was This Post Helpful? 1
  • +
  • -

#3 alexlunar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 10-January 10

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 09:20 PM

I found that I had 1 too man " , but when I remove it, I still get the exact same errors.
Was This Post Helpful? 0
  • +
  • -

#4 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 09:25 PM

I tried to take a quick scan through this, but it is very hard to read, not only because of the indentation, but also because of the HUGE white spaces between lines. Sorry. Be happy to help if you post it legibly though.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10558
  • View blog
  • Posts: 39,065
  • Joined: 27-December 08

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 09:29 PM

Take a look here. You need a quote before MPG: as well as to look at the number of opening and closing parentheses. Also, the params you have in the valueOfInventory() invocation do not match up the definition. You have a (double, String) when it was defined to accept a (double, int).
label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + name[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " +
price[j] + "Value of inventory: " + 

valueOfInventory(price[j], units[j] + "Value with restocking fee: " + Suv.valueOfInventory(price[j], units[j]" ); 


Was This Post Helpful? 1
  • +
  • -

#6 alexlunar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 10-January 10

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 09:36 PM

Posted without all the white space...
Original Cars class
// Week 6 Inventory Program Part 3
// Inventory program that stores data in class
// Uses Array to return data
// With added SUV subclass & added MPG

public class Cars 
{
   private static int itemNumber[] = new int[8];
   private static String name[] = new String[8];
   private static int units[] = new int[8];
   private static double price[] = new double[8];
   private static int i = 0;

      public Cars(){ }// 
    
      public Cars(int _itemNumber, String _name, int _units, double _price)//
      { 		
         itemNumber[i] = _itemNumber;
         name[i] = _name;
         units[i] = _units;
         price[i] = _price;
         i = i + 1;
      }
	
      public static double valueOfInventory(double p,int u)
      {
         return p*u;
   }

   public static void showInventory()
   {		

   for(int j=0;j<i;j++){
   System.out.println("\nItem Number: 1090" + itemNumber[j]);
   System.out.println("Car Name: " + name[j]);
   System.out.println("MPG: " + Suv.mpg[j]);
   System.out.println("Number In Stock: " + units[j]);
   System.out.println("Unit Price : " + price[j]);
   System.out.println("Value of inventory: " + valueOfInventory(price[j], units[j]));
   System.out.println("Value with restocking fee: " + Suv.valueOfInventory(price[j], units[j]));
   }
}
   public static void showValueofInventory()
   {
   double totalVal = 0.00;
   for (int j = 0; j < i; j++)
   {
   totalVal = totalVal + valueOfInventory(price[j], units[j]);
   }
   System.out.println("\n**Total Value of In Stock Cars is = " + totalVal);
   }
}


Original Products Class
//Week 6 Product Array Class

   import java.io.InputStreamReader;
   import java.io.BufferedReader;
   import java.io.IOException;

public class Products 
{
   public static void main( String[] args)
   {
     Cars p = new Suv(1, "Ford Ranger", "20", 5, 14000);
     Cars q = new Suv(2, "Ford Focus", "24", 2, 13000);
     Cars r = new Suv(3, "Ford F150", "18", 12, 18000);
     Cars s = new Suv(4, "Ford Probe", "22", 1, 7500);
     Cars t = new Suv(5, "Ford Escort", "28", 4, 4500);        
     Cars u = new Suv(6, "Ford Aspire", "30", 1, 10000);
     Cars v = new Suv(7, "Ford Taurus", "21", 8, 9750);
     Cars w = new Suv(8, "Ford Explorer SUV", "16", 6, 24000);

        ///show inventory
        Cars.showInventory();

        ///show total value of inventory
	Cars.showValueofInventory();
   }
}

Original Suv Class
//Week 6 Added Subclass with MPG & Restocking Fee
public class Suv extends Cars
{
 static String mpg[] = new String[8];//New product feature itemCode added
 private static int i = 0;
     //Constructor
     public Suv(int _itemNumber, String _name, String _mpg, int _units, double _price) 
     {
      	 super( _itemNumber, _name, _units, _price);
    	 mpg[i] = _mpg; 
 		i++;   	     }
     public static double valueOfInventory(double p,int u)
	{
	   return 1.05*Cars.valueOfInventory(p, u);
	}
}


Modified Cars Class
import java.awt.FlowLayout; // specifies how components are arranged
import javax.swing.JFrame; // provides basic window features
import javax.swing.JLabel; // displays text and images
import javax.swing.SwingConstants; // common constants used with Swing

public class Cars extends JFrame
{
   private static int itemNumber[] = new int[8];
   private static String name[] = new String[8];
   private static int units[] = new int[8];
   private static double price[] = new double[8];
   private static int i = 0;

      public Cars(){ }// 
      public Cars(int _itemNumber, String _name, int _units, double _price)//
      { 		
         itemNumber[i] = _itemNumber;
         name[i] = _name;
         units[i] = _units;
         price[i] = _price;
         i = i + 1;
      }
	
      public static double valueOfInventory(double p,int u)
      {  return p*u;
      }
   
      public static void showInventory()

   // LabelFrame constructor adds JLabels to JFrame
   {
   super( "Testing JLabel" );
   setLayout( new FlowLayout() ); // set frame layout
 
   for(int j=0;j<i;j++){
   // JLabel constructor with a string argument
   label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + name[j] + MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " + price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value with restocking fee: " + Suv.valueOfInventory(price[j], units[j] );
   label1.setToolTipText( "This is label1" );
  
   {		
   }
}
   public static void showValueofInventory()
   {
   double totalVal = 0.00;
   for (int j = 0; j < i; j++)
   {
   totalVal = totalVal + valueOfInventory(price[j], units[j]);
   }
   System.out.println("\n**Total Value of In Stock Cars is = " + totalVal);
add( label1 ); // add label1 to JFrame
}
   
   }
}


Was This Post Helpful? 0
  • +
  • -

#8 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 09:45 PM

Ok, the first thing I noticed is that your code for the GUI is NOT in the constructor. It needs to be. It should look something like this:
import java.awt.*;
import javax.swing.*;

public class Cars extends JFrame {

  //This is a constructor.
  public Cars() {  
    super("This is the GUI");  //This MUST be the first line of the constructor. This sets the title.

    /*This sets the size and placement.  The first two numbers 
    are the x,y position.  The second two are the x,y size. */
    setBounds(100,100,500,500);  

    /*This tells the program what to do if someone closes the window.  Here it is to kill the program. */
    setDefaultCloseOperation(EXIT_ON_CLOSE);  

    /*This makes it so that you can see the window.  It starts out invisible.*/
    setVisible(true);
  }
}

//This creates a window for you to work with.

If you understand this, we'll move to putting things IN the window. If not, ask. Happy to explain further.
Was This Post Helpful? 1
  • +
  • -

#9 alexlunar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 10-January 10

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 10:01 PM

Thank you, this was the first reply that was helpful.

I have updated my Cars Class to ...

import java.awt.FlowLayout; // specifies how components are arranged
import javax.swing.JFrame; // provides basic window features
import javax.swing.JLabel; // displays text and images
import javax.swing.SwingConstants; // common constants used with Swing

public class Cars extends JFrame
{
   public Cars()
   {
      super("Car Inventory");
      setBounds(100,100,500,500);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setVisible(true);
   }
   private static int itemNumber[] = new int[8];
   private static String name[] = new String[8];
   private static int units[] = new int[8];
   private static double price[] = new double[8];
   private static int i = 0;
      public Cars(){ }// 
      public Cars(int _itemNumber, String _name, int _units, double _price)//
      { 		
         itemNumber[i] = _itemNumber;
         name[i] = _name;
         units[i] = _units;
         price[i] = _price;
         i = i + 1;
      }
      public static double valueOfInventory(double p,int u)
      {
         return p*u;
      }
}

   public static void showInventory()
   // LabelFrame constructor adds JLabels to JFrame
   {
   setLayout( new FlowLayout() ); // set frame layout
   for(int j=0;j<i;j++){
     // JLabel constructor with a string argument
   label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + name[j] + "MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " + price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value with restocking fee: " + Suv.valueOfInventory(price[j], units[j] );
   label1.setToolTipText( "This is label1" );
   }
   public static void showValueofInventory()
   {
   double totalVal = 0.00;
   for (int j = 0; j < i; j++)
   {
   totalVal = totalVal + valueOfInventory(price[j], units[j]);
   }
   System.out.println("\n**Total Value of In Stock Cars is = " + totalVal);
add( label1 ); // add label1 to JFrame
         }
}


I am now down to 13 errors

c:\gui>javac Cars.java
Cars.java:51: class, interface, or enum expected
public static void showInventory()
^
Cars.java:58: class, interface, or enum expected
for(int j=0;j<i;j++){
^
Cars.java:58: class, interface, or enum expected
for(int j=0;j<i;j++){
^
Cars.java:58: class, interface, or enum expected
for(int j=0;j<i;j++){
^
Cars.java:63: class, interface, or enum expected
label1.setToolTipText( "This is label1" );
^
Cars.java:65: class, interface, or enum expected
}
^
Cars.java:69: class, interface, or enum expected
public static void showValueofInventory()
^
Cars.java:80: class, interface, or enum expected
for (int j = 0; j < i; j++)
^
Cars.java:80: class, interface, or enum expected
for (int j = 0; j < i; j++)
^
Cars.java:80: class, interface, or enum expected
for (int j = 0; j < i; j++)
^
Cars.java:93: class, interface, or enum expected
}
^
Cars.java:104: class, interface, or enum expected
add( label1 ); // add label1 to JFrame
^
Cars.java:108: class, interface, or enum expected
}
^
13 errors
Was This Post Helpful? 0
  • +
  • -

#10 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 10:09 PM

Look at that, I killed three errors with one post. I feel good. :P

Ok, so it looks like you understand how to create the window, yeah? So now to adding things to it.
import javax.swing.*;
import java.awt.*;

/*You need to create something to put the stuff in before 
you put it in the window.  JPanels are great for this.*/
JPanel pane = new JPanel();  

/*You also need to create whatever it is you want to put in there.
I think you just need to display text right?  In which case a 
JLabel is just fine.*/
JLabel label = new JLabel("This is a label.");

//Notice these were declared as instance variables. That's important.


public class Cars extends JFrame
{
   public Cars()
   {
      super("Car Inventory");
      setBounds(100,100,500,500);
      setDefaultCloseOperation(EXIT_ON_CLOSE);

      /*Now, BEFORE setVisible(), we add everything
      to the JPanel*/
      pane.add(label);

      //Now, add it to the frame.
      add(pane);


      setVisible(true);
   }



Edit: How we doing? Following along? Please ask questions if not.

This post has been edited by xor-logic: 12 February 2010 - 10:10 PM

Was This Post Helpful? 1
  • +
  • -

#11 alexlunar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 10-January 10

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 10:09 PM

I just had another thought, should I be making these changed to my Products class... not my Cars class?
Was This Post Helpful? 0
  • +
  • -

#12 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 10:15 PM

View Postalexlunar, on 12 February 2010 - 09:09 PM, said:

I just had another thought, should I be making these changed to my Products class... not my Cars class?


Where you put it is up to you.

Actually, what I've done in my programs is just to separate the GUI out into an entirely separate class. I would really recommend doing that. Makes it easier to distinguish things.

This post has been edited by xor-logic: 12 February 2010 - 10:16 PM

Was This Post Helpful? 1
  • +
  • -

#13 alexlunar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 10-January 10

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 10:25 PM

I added the information you recommended, but now I have more errors than before...

// Week 7 Inventory Program Part 4
// Inventory program that stores data in class
// Uses GUI to return data
// With added SUV subclass & added MPG


import java.awt.FlowLayout; // specifies how components are arranged
import javax.swing.JFrame; // provides basic window features
import javax.swing.JLabel; // displays text and images
import javax.swing.SwingConstants; // common constants used with Swing

JPanel pane = new JPanel();
JLabel label = new JLabel("This is a label")

public class Cars extends JFrame
{
   public Cars()
   {
      super("Car Inventory");
      setBounds(100,100,500,500);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pane.add(label);
      add(pane);
      setVisible(true);
   }

   private static int itemNumber[] = new int[8];
   private static String name[] = new String[8];
   private static int units[] = new int[8];
   private static double price[] = new double[8];
   private static int i = 0;
      public Cars(){ }// 
      public Cars(int _itemNumber, String _name, int _units, double _price)//
      { 		
         itemNumber[i] = _itemNumber;
         name[i] = _name;
         units[i] = _units;
         price[i] = _price;
         i = i + 1;
      }
	
      public static double valueOfInventory(double p,int u)
      {
         return p*u;
      }
}
   public static void showInventory()

   // LabelFrame constructor adds JLabels to JFrame
   {
   setLayout( new FlowLayout() ); // set frame layout

   for(int j=0;j<i;j++){

   // JLabel constructor with a string argument
   label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + name[j] + "MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " + price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value with restocking fee: " + Suv.valueOfInventory(price[j], units[j] );
   label1.setToolTipText( "This is label1" );
  }
   public static void showValueofInventory()
   {
   double totalVal = 0.00;
   for (int j = 0; j < i; j++)
   {
   totalVal = totalVal + valueOfInventory(price[j], units[j]);
   }
   System.out.println("\n**Total Value of In Stock Cars is = " + totalVal);
add( label1 ); // add label1 to JFrame
         }
}



c:\gui>javac Cars.java
Cars.java:12: class, interface, or enum expected
JPanel pane = new JPanel();
^
Cars.java:13: class, interface, or enum expected
JLabel label = new JLabel("This is a label")
^
Cars.java:55: class, interface, or enum expected
public static void showInventory()
^
Cars.java:62: class, interface, or enum expected
for(int j=0;j<i;j++){
^
Cars.java:62: class, interface, or enum expected
for(int j=0;j<i;j++){
^
Cars.java:62: class, interface, or enum expected
for(int j=0;j<i;j++){
^
Cars.java:67: class, interface, or enum expected
label1.setToolTipText( "This is label1" );
^
Cars.java:69: class, interface, or enum expected
}
^
Cars.java:73: class, interface, or enum expected
public static void showValueofInventory()
^
Cars.java:84: class, interface, or enum expected
for (int j = 0; j < i; j++)
^
Cars.java:84: class, interface, or enum expected
for (int j = 0; j < i; j++)
^
Cars.java:84: class, interface, or enum expected
for (int j = 0; j < i; j++)
^
Cars.java:97: class, interface, or enum expected
}
^
Cars.java:108: class, interface, or enum expected
add( label1 ); // add label1 to JFrame
^
Cars.java:112: class, interface, or enum expected
}
^
15 errors
Was This Post Helpful? 0
  • +
  • -

#14 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 10:29 PM

Sorry, my bad. Take the declarations of the JPanel and the JLabel, and drop them down between the class declaration and the beginning of the constructor.
My brain is fried, sorry, but I'm trying.
Was This Post Helpful? 1
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10558
  • View blog
  • Posts: 39,065
  • Joined: 27-December 08

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 10:30 PM

Look at your curly braces. The brace ending the class occurs after you finish defining the valueOfInventory() method, leaving the rest of your code without a class.

By the way, your indentations are looking a lot better. :) You've still got a little more work to do in that area though. Try to organize your code aesthetically so it looks like an outline; it will help both you and us follow your logic much easier.

This post has been edited by macosxnerd101: 12 February 2010 - 10:32 PM

Was This Post Helpful? 1
  • +
  • -

#16 alexlunar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 10-January 10

Re: Inventory Program Part 4 - Gui

Posted 12 February 2010 - 10:41 PM

Thank you both, it looks like we are getting there. After fixing the } and relocating that declaration, we are down to 9 errors.

// Week 7 Inventory Program Part 4
// Inventory program that stores data in class
// Uses GUI to return data
// With added SUV subclass & added MPG

import java.awt.FlowLayout; // specifies how components are arranged
import javax.swing.JFrame; // provides basic window features
import javax.swing.JLabel; // displays text and images
import javax.swing.SwingConstants; // common constants used with Swing

public class Cars extends JFrame
{
   JPanel pane = new JPanel();
   JLabel label = new JLabel("This is a label")
     public Cars()
     {
      super("Car Inventory");
      setBounds(100,100,500,500);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pane.add(label);
      add(pane);
      setVisible(true);
   }
   private static int itemNumber[] = new int[8];
   private static String name[] = new String[8];
   private static int units[] = new int[8];
   private static double price[] = new double[8];
   private static int i = 0;
      public Cars(){ }// 
      public Cars(int _itemNumber, String _name, int _units, double _price)
      { 		
         itemNumber[i] = _itemNumber;
         name[i] = _name;
         units[i] = _units;
         price[i] = _price;
         i = i + 1;
      }
      public static double valueOfInventory(double p,int u)
      {
         return p*u;
   }
   public static void showInventory()
   // LabelFrame constructor adds JLabels to JFrame
   {
   setLayout( new FlowLayout() ); // set frame layout
   for(int j=0;j<i;j++){
     // JLabel constructor with a string argument
   label1 = new JLabel( "Item Number: 1090" + itemNumber[j] + "Car Name: " + name[j] + "MPG: " + Suv.mpg[j] + "Number In Stock: " + units[j] + "Unit Price : " + price[j] + "Value of inventory: " + valueOfInventory(price[j], units[j] + "Value with restocking fee: " + Suv.valueOfInventory(price[j], units[j] );
   label1.setToolTipText( "This is label1" );
  }
   public static void showValueofInventory()
   {
   double totalVal = 0.00;
   for (int j = 0; j < i; j++)
   {
   totalVal = totalVal + valueOfInventory(price[j], units[j]);
   }
   System.out.println("\n**Total Value of In Stock Cars is = " + totalVal);

add( label1 ); // add label1 to JFrame

         }
    }
}

Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »