Inventory Program

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 11457 Views - Last Post: 18 August 2007 - 08:38 AM Rate Topic: -----

#1 mudpuppy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-August 07

Inventory Program

Post icon  Posted 13 August 2007 - 09:19 PM

I know that there is already codes for numerous different inventory programs. I have read them, and I am still somewhat confused. OK, this is what my code is supposed to be doing: Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
Modify the output to display this additional feature you have chosen and the restocking fee.

This is the errors I keep getting: /tmp/27473/Product.java:10: class, interface, or enum expected
{
^
/tmp/27473/Product.java:13: class, interface, or enum expected
product[1] = new Product(2, "Clown Trigger", 1, 74.99);
^
/tmp/27473/Product.java:14: class, interface, or enum expected
product[2] = new Product(3, "Blueline Trigger", 2, 59.99);
^
/tmp/27473/Product.java:15: class, interface, or enum expected
product[3] = new Product(4, "Queen Trigger", 1, 99.99);
^
/tmp/27473/Product.java:18: class, interface, or enum expected
total = (inventory[ 0 ].getValueOfStock() +
^
/tmp/27473/Product.java:23: class, interface, or enum expected
System.out.printf( "The total value of the inventory is: $%.2f\n\n", total );
^
/tmp/27473/Product.java:26: class, interface, or enum expected
public Product(int number, String name, int quantity, double price) {
^
/tmp/27473/Product.java:28: class, interface, or enum expected
this.name = name;
^
/tmp/27473/Product.java:29: class, interface, or enum expected
this.quantity = quantity;
^
/tmp/27473/Product.java:30: class, interface, or enum expected
this.price = price;
^
/tmp/27473/Product.java:31: class, interface, or enum expected
}
^
/tmp/27473/Product.java:33: class, interface, or enum expected
public String getName() { return name; }
^
/tmp/27473/Product.java:33: class, interface, or enum expected
public String getName() { return name; }
^
/tmp/27473/Product.java:34: class, interface, or enum expected
public int getNumber() { return number; }
^
/tmp/27473/Product.java:34: class, interface, or enum expected
public int getNumber() { return number; }
^
/tmp/27473/Product.java:35: class, interface, or enum expected
public double getPrice() { return price; }
^
/tmp/27473/Product.java:35: class, interface, or enum expected
public double getPrice() { return price; }
^
/tmp/27473/Product.java:36: class, interface, or enum expected
public int getQuantity() { return quantity; }
^
/tmp/27473/Product.java:36: class, interface, or enum expected
public int getQuantity() { return quantity; }
^
/tmp/27473/Inventory.java:26: illegal start of expression
public void getInventory() {
^
20 errors

Finally, here is my codes:
public class Inventory
{
   
   public static final int MAXIMUM_ITEMS = 4;
   Product product[] = new Product[MAXIMUM_ITEMS];

public Inventory(){
		  buildInventory();
		  getInventory();		
	}

	public void buildInventory() {

	
		product[0] = new Fish(0, "Fancy Tailed Guppy",		25, 4.99);
		product[1] = new Fish(1, "Huma Huma Picasso Trigger", 2, 35.99);
		product[2] = new Fish(2, "Black Durgeon Trigger",		1, 99.99);
		product[3] = new Fish(3, "Niger Trigger",		2, 45.99);
		product[4] = new Fish(4, "Jack Dempsey Cichlid", 4, 19.99);
		product[5] = new Fish(5, "Midas Cichlid",		1, 69.99);
		product[6] = new Fish(6, "Neon Tetra",		25, 2.99);
		product[7] = new Fish(7, "Convict Cichlid",		2, 15.99);
		product[8] = new Fish(8, "Oscar", 4, 37.99);
		product[9] = new Fish(9, "Albino Cory Cat",		1, 29.99);

		public void getInventory() {
		for(int i = 0; i < product.length; i++) {
			System.out.println("Number:	 " + product.getNumber());
			System.out.println("Name:	   " + product.getName());
			System.out.println("Quantity:   " + product.getQuantity());
			System.out.println("Price:	  " + product.getPrice());
			System.out.println("Item Value: " + 
				product.getQuantity() * product.getPrice());
			System.out.printIn("Item Value:"+product.getQuantity() * product.getPrice() * 0.05);
			System.out.println();

		 
	  }  
   }
}


public class Product {

	private String name = "";
	private int number = 0;
	private double price = 0;
	private int quantity = 0;
}

	   
{

		product[0] = new Product(1, "Bursa Trigger", 3, 39.99);
		product[1] = new Product(2, "Clown Trigger",		1, 74.99);
		product[2] = new Product(3, "Blueline Trigger",	   2, 59.99);
		product[3] = new Product(4, "Queen Trigger",	   1, 99.99);


			   total = (inventory[ 0 ].getValueOfStock() +
			   inventory[ 1 ].getValueOfStock() +
			   inventory[ 2 ].getValueOfStock() +
			   inventory[ 3 ].getValueOfStock() +
			   inventory[ 4 ].getValueOfStock());
	  System.out.printf( "The total value of the inventory is: $%.2f\n\n", total );
 
	
	public Product(int number, String name, int quantity, double price) {
		this.number = number;
		this.name = name;
		this.quantity = quantity;
		this.price = price;
	}

	public String getName() { return name; }
	public int getNumber() { return number; }
	public double getPrice() { return price; }
	public int getQuantity() { return quantity; }
}



public class Fish{ 
	
		public Fish() {}

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

}


I have been reading and rereading, but I just get more and more confused. When I think I do something right, I ask my teacher and he makes me feel like an idiot. Any help at all is greatly appreciated. Thanks!

Ok, I've been working on the code trying to get rid of some of the errors. I am down to one in my inventory.java file. This is the error: Inventory.java:26: illegal start of expression
public void getInventory() {
^
This is the errors for the other codes: /tmp/18746/Inventory.java:26: illegal start of expression
public void getInventory() {
^
/tmp/18746/Product.java:9: class, interface, or enum expected
product[0] = new Product(1, "Bursa Trigger", 3, 39.99);
^
/tmp/18746/Product.java:10: class, interface, or enum expected
product[1] = new Product(2, "Clown Trigger", 1, 74.99);
^
/tmp/18746/Product.java:11: class, interface, or enum expected
product[2] = new Product(3, "Blueline Trigger", 2, 59.99);
^
/tmp/18746/Product.java:12: class, interface, or enum expected
product[3] = new Product(4, "Queen Trigger", 1, 99.99);
^
/tmp/18746/Product.java:15: class, interface, or enum expected
(inventory[ 0 ].getValueOfStock() +
^
/tmp/18746/Product.java:20: class, interface, or enum expected
System.out.printf( "The total value of the inventory is: $%.2f\n\n", total );
^
/tmp/18746/Product.java:23: class, interface, or enum expected
public Product(int number, String name, int quantity, double price) {
^
/tmp/18746/Product.java:25: class, interface, or enum expected
this.name = name;
^
/tmp/18746/Product.java:26: class, interface, or enum expected
this.quantity = quantity;
^
/tmp/18746/Product.java:27: class, interface, or enum expected
this.price = price;
^
/tmp/18746/Product.java:28: class, interface, or enum expected
}
^
/tmp/18746/Product.java:30: class, interface, or enum expected
public String getName() { return name; }
^
/tmp/18746/Product.java:30: class, interface, or enum expected
public String getName() { return name; }
^
/tmp/18746/Product.java:31: class, interface, or enum expected
public int getNumber() { return number; }
^
/tmp/18746/Product.java:31: class, interface, or enum expected
public int getNumber() { return number; }
^
/tmp/18746/Product.java:32: class, interface, or enum expected
public double getPrice() { return price; }
^
/tmp/18746/Product.java:32: class, interface, or enum expected
public double getPrice() { return price; }
^
/tmp/18746/Product.java:33: class, interface, or enum expected
public int getQuantity() { return quantity; }
^
/tmp/18746/Product.java:33: class, interface, or enum expected
public int getQuantity() { return quantity; }
^
20 errors

Doesn't look like I've made much difference!

Here is the code once again:

public class Inventory
{
   
   public static final int MAXIMUM_ITEMS = 4;
   Product product[] = new Fish[MAXIMUM_ITEMS];

public Inventory(){
		  buildInventory();
		  getInventory();		
	}

	public void buildInventory() {

	
		product[0] = new Fish(0, "Fancy Tailed Guppy",		25, 4.99);
		product[1] = new Fish(1, "Huma Huma Picasso Trigger", 2, 35.99);
		product[2] = new Fish(2, "Black Durgeon Trigger",		1, 99.99);
		product[3] = new Fish(3, "Niger Trigger",		2, 45.99);
		product[4] = new Fish(4, "Jack Dempsey Cichlid", 4, 19.99);
		product[5] = new Fish(5, "Midas Cichlid",		1, 69.99);
		product[6] = new Fish(6, "Neon Tetra",		25, 2.99);
		product[7] = new Fish(7, "Convict Cichlid",		2, 15.99);
		product[8] = new Fish(8, "Oscar", 4, 37.99);
		product[9] = new Fish(9, "Albino Cory Cat",		1, 29.99);

		public void getInventory() {
		for(int i = 0; i < product.length; i++) {
			System.out.println("Number:	 " + Fish.getNumber());
			System.out.println("Name:	   " + Fish.getName());
			System.out.println("Quantity:   " + Fish.getQuantity());
			System.out.println("Price:	  " + Fish.getPrice());
			System.out.println("Item Value: " + 
				Fish.getQuantity() * Fish.getPrice());
			System.out.printIn("Item Value:"+product.getQuantity() * product.getPrice() * 0.05);
			System.out.println();

		 
	  }  
   }
}


public class Product {

	private String name = "";
	private int number = 0;
	private double price = 0;
	private int quantity = 0;
}

		product[0] = new Product(1, "Bursa Trigger", 3, 39.99);
		product[1] = new Product(2, "Clown Trigger",		1, 74.99);
		product[2] = new Product(3, "Blueline Trigger",	   2, 59.99);
		product[3] = new Product(4, "Queen Trigger",	   1, 99.99);


			  (inventory[ 0 ].getValueOfStock() +
			   inventory[ 1 ].getValueOfStock() +
			   inventory[ 2 ].getValueOfStock() +
			   inventory[ 3 ].getValueOfStock() +
			   inventory[ 4 ].getValueOfStock());
	  System.out.printf( "The total value of the inventory is: $%.2f\n\n", total );
 
	
	public Product(int number, String name, int quantity, double price) {
		this.number = number;
		this.name = name;
		this.quantity = quantity;
		this.price = price;
	}

	public String getName() { return name; }
	public int getNumber() { return number; }
	public double getPrice() { return price; }
	public int getQuantity() { return quantity; }
}

		
public class Fish{ 
	
		public Fish() {}

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

}


Again, thanks for any help!

This post has been edited by mudpuppy: 13 August 2007 - 10:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Inventory Program

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Inventory Program

Posted 14 August 2007 - 10:36 AM

Ok, you have quite a bit of things wrong with this. Most of your mistakes come from what appears to be a lack of understanding of how to develop a class, subclass it and use them in a program. So I will go through the parts and provide you some code that will work. You will of course need to do a bit of work yourself using the overriden function I have created for you.

So the first problem is that you haven't defined a good Product class. This class will serve as your base class which you will inherit from (extend). It is crucial that you write a good base class first. A class should only describe itself and provide methods that are relevant only to a Product. Nothing about inventory or Fish should appear in the class. It should only describe what a Product IS and what it can DO.

Product.java

// Product class, this is a base class which Fish will subclass from.
// You only define basic fields and methods specific to a product item. 
// Nothing from inventory or fish should be found here.

public class Product {

	private String name = "";
	private int number = 0;
	private double price = 0;
	private int quantity = 0;

	// Constructor
	public Product(int number, String name, int quantity, double price) {
		this.number = number;
		this.name = name;
		this.quantity = quantity;
		this.price = price;
	}

	// Public accessor methods
	public String getName() { return name; }
	public int getNumber() { return number; }
	public double getPrice() { return price; }
	public int getQuantity() { return quantity; }

	// Get value of product (will be overriden by Fish subclass)
	public double getValue() {
		 return (double) quantity * price;
	}
}



This product describes the ID number, its name, its quantity and its price related to a product. Lastly we have given it a method to retrieve its value (quantity * price) = value.

Now that we have a strong base class describing a product, we need to make a "more specific" product called Fish. This is a type of product which is important to realize. A product isn't necessarily a fish, but a fish IS A product. The assignment asked that you inherit from the product (make it a subclass) and then override one of its base methods... its value.

Fish.java

// This class defines a fish. A fish IS A product so we extend the Product base class.
// Fish is now a subclass of Product (a more specify type of product)

public class Fish extends Product {
	
		public Fish(int number, String fishname, int fishquantity, double fishprice) {
				super(number, fishname, fishquantity, fishprice);
		}

	// Lets override the Product's getValue() function
	// First we call the base class function
	// Then we tack on that 5% fee.
	// This function then returns the value of the product + stocking fee.

	public double getValue() {
		double inventoryvalue = super.getValue();
		return (inventoryvalue * 1.05);
	}	
}



Notice how we define a fish by first passing its parameters to the super class (our product base class) and then we define a method with the same name and return type as our base class. This overrides the base class method. In it we then get the value from our Product super class and tack on the 5% fee. Remember we use 1.05 to increase the value 5%. Using .05 would give us the actual restocking fee (as you will see in the inventory class main program).

One thing I also want to point out is the use of the word "extends" at the top of this class. This will make Fish a "subclass" of product. A fish product EXTENDS the functionality of a Product.

Lastly the inventory main program. Here you have all sorts of problems with curly braces in the wrong spots (this causes a lot of your errors), you lacked proper dimensions on your product array, and you were referencing your products the wrong way while looping.

Inventory.java

// Inventory Class (Main Program)

public class Inventory
{
  
		// We setup our inventory to hold 10 products
		public static final int MAXIMUM_ITEMS = 10;
		private static Product product[] = new Fish[MAXIMUM_ITEMS];

		public static void main(String args[]) {
				buildInventory();
				getInventory();		
		}

		// Build the inventory, storing 10 different products.
		public static void buildInventory() {

				// Store the more specify fish class into its superclass array.
				// Since a fish "IS A" product, this will work. We couldn't store a Product
				// in an array of fish because a Product is NOT a fish.

				product[0] = new Fish(0, "Fancy Tailed Guppy",25, 4.99);
				product[1] = new Fish(1, "Huma Huma Picasso Trigger",2, 35.99);
				product[2] = new Fish(2, "Black Durgeon Trigger",1, 99.99);
				product[3] = new Fish(3, "Niger Trigger",2, 45.99);
				product[4] = new Fish(4, "Jack Dempsey Cichlid",4, 19.99);
				product[5] = new Fish(5, "Midas Cichlid",1, 69.99);
				product[6] = new Fish(6, "Neon Tetra",25, 2.99);
				product[7] = new Fish(7, "Convict Cichlid",2, 15.99);
				product[8] = new Fish(8, "Oscar", 4, 37.99);
				product[9] = new Fish(9, "Albino Cory Cat",1, 29.99);
		}


		// Loop through the products and get the various details of each.
		public static void getInventory() {
				for(int i = 0; i < product.length; i++) {
					System.out.println("Number:	 " + product[i].getNumber());
					System.out.println("Name:	   " + product[i].getName());
					System.out.println("Quantity:   " + product[i].getQuantity());
					System.out.println("Price:	  " + product[i].getPrice());
					System.out.println("Item Value: " + product[i].getQuantity() * product[i].getPrice());
			
						// This will need changing on your part to get the products value!

					System.out.println("Restock Fee: " + (product[i].getQuantity() * product[i].getPrice()) * 0.05);
					System.out.println();

			  }  
   	}
}



Starting from the top you will notice that we made your product array private and static but more importantly a dimension of 10! Why? Because you were storing 10 items, not 4. Second thing is that we put in main here. main should only appear in your main program file. You don't define it in any class definitions. When you run the program it looks for main and only one main to start the program. You had main in your fish class which was the definition of a product type. Since your inventory uses products and fish, it should be where your program starts.

Your buildinventory function was good, but when you went to loop through them you had one syntax error (one of your println functions was printIn (using an I instead of a L). Lastly when you go to access one of the items, you were using the subclass name, you need to get a reference to the actual product element in the array. So you reference it using the index variable on the array. There I have left it up to you to get the value of the product.

Last thing I wanted you to notice is how we were able to define fish and store it in a product array. Since a fish is a type of product we can store the fish in the product array just fine. We could not have stored a product in a fish array since a product isn't necessarily a fish. When we go to reference it in the loop however, it will call the right overriden function based on the type, which it will identify as a fish. If it wasn't a fish, it would call the regular base class version of getValue().

Hope this helps and learn all you can from this example. It is crucial to you in going further with programming.

Enjoy!
Was This Post Helpful? 0
  • +
  • -

#3 mudpuppy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-August 07

Re: Inventory Program

Posted 14 August 2007 - 12:35 PM

Thank you sooooo very much, Martyr2! You have made it possible for me to understand this a little better! You are a God send! When I submitted my codes to my teacher and a different forum, I was treated like an idiot. You, on the other hand, did not do this and explained everything so well just so I could understand what it was I was doing wrong! Again, thank you so very much! Now I have to figure out how to add a GUI to this program. There are three more parts to this program that I have to do over the course of the next two weeks, so I am sure I will be back to this forum very soon!
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1868
  • View blog
  • Posts: 20,280
  • Joined: 17-March 01

Re: Inventory Program

Posted 14 August 2007 - 12:42 PM

Quote

When I submitted my codes to my teacher and a different forum, I was treated like an idiot. You, on the other hand, did not do this and explained everything so well just so I could understand what it was I was doing wrong!


Tell your friends :)

Thanks Martyr2 for helping out! As usual, you rock.
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Inventory Program

Posted 14 August 2007 - 12:52 PM

I am glad I could be of assistance mudpuppy. I, and the rest of the Dream.In.Code team, try to strive to be better than other boards when it comes to prompt and precise answering. I am sure that next time you visit you may not get me answering the question quickly, but you will certainly get another qualified person who won't treat you like dirt. Programming is meant to be fun! So if you know anyone else looking for help, give us a shout and I am sure they will enjoy the site too. Heck, maybe they can even provide an answer or two to someone else as a way to say thanks! :)
Was This Post Helpful? 0
  • +
  • -

#6 mudpuppy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-August 07

Re: Inventory Program

Posted 14 August 2007 - 01:36 PM

I've already emailed everyone in my class to tell them about this forum! By far, this one is the best one out there! :D

Quick question. This is what I haev to add to the code this week: Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee

Should this go into a .java file all by itself and be called by the inventory or fish.java file or should the GUI just go into the inventory.java file? In the reading it gives an example of a GUI to make a rainbow. (Still not real sure how to make this GUI incorporate it into the program as not being a rainbow.) I am working on that though! lol

This post has been edited by mudpuppy: 14 August 2007 - 01:41 PM

Was This Post Helpful? 0
  • +
  • -

#7 mudpuppy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-August 07

Re: Inventory Program

Posted 14 August 2007 - 08:33 PM

View Postmudpuppy, on 14 Aug, 2007 - 01:36 PM, said:

I've already emailed everyone in my class to tell them about this forum! By far, this one is the best one out there! :D

Quick question. This is what I haev to add to the code this week: Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee

Should this go into a .java file all by itself and be called by the inventory or fish.java file or should the GUI just go into the inventory.java file? In the reading it gives an example of a GUI to make a rainbow. (Still not real sure how to make this GUI incorporate it into the program as not being a rainbow.) I am working on that though! lol



Ok I submitted my code to my teacher. This is what he said: Your output is something like $14.329202, change the format to dollars and cents only two digits behind the decimal. So I made a change and resubmitted it. He said I made everything worse! This is what he said: Go back to what you had before and slowly make changes, I just executed and this became worse:

Number: 0
Name: Fancy Tailed Guppy
Quantity: 25
Price: 4.99
Item Value: 124.75
Restock Fee: 6.237500000000001

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier 's'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Inventory.getInventory(Inventory.java:46)
at Inventory.main(Inventory.java:13)
Again, here is my code :


public class Inventory
{
  
		
		public static final int MAXIMUM_ITEMS = 10;
		private static Product product[] = new Fish[MAXIMUM_ITEMS];

		public static void main(String args[]) {
				buildInventory();
				getInventory();		
		}

		
		public static void buildInventory() {

				

				product[0] = new Fish(0, "Fancy Tailed Guppy",25, 4.99);
				product[1] = new Fish(1, "Huma Huma Picasso Trigger",2, 35.99);
				product[2] = new Fish(2, "Black Durgeon Trigger",1, 99.99);
				product[3] = new Fish(3, "Niger Trigger",2, 45.99);
				product[4] = new Fish(4, "Jack Dempsey Cichlid",4, 19.99);
				product[5] = new Fish(5, "Midas Cichlid",1, 69.99);
				product[6] = new Fish(6, "Neon Tetra",25, 2.99);
				product[7] = new Fish(7, "Convict Cichlid",2, 15.99);
				product[8] = new Fish(8, "Oscar", 4, 37.99);
				product[9] = new Fish(9, "Albino Cory Cat",1, 29.99);
		}


		
		public static void getInventory() {
				for(int i = 0; i < product.length; i++) {
					System.out.println("Number:	 " + product[i].getNumber());
					System.out.println("Name:	   " + product[i].getName());
					System.out.println("Quantity:   " + product[i].getQuantity());
					System.out.println("Price:	  " + product[i].getPrice());
					System.out.println("Item Value: " + product[i].getQuantity() * product[i].getPrice());
			
						

					System.out.println("Restock Fee: " + (product[i].getQuantity() * product[i].getPrice()) * 0.05);
					System.out.printf("\n%s's Inventory is $%.2f\n");
					System.out.println();

			  }  
	   }
}




I feel about ready to bang my head against my computer desk!
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Inventory Program

Posted 14 August 2007 - 09:50 PM

Lookup the printf function's use. You are missing the second part of that function, where you list the variables you want placed into the first part's string.

String hello ="Hi there!";

// This prints out "I went to my friend and said Hi there!"
System.out.printf("I went to my friend and said %s",hello);



You are right about using the %.2f to format that dollar amount though. Be sure to test before submitting to your teacher so that you know it works as it should.
Was This Post Helpful? 0
  • +
  • -

#9 mudpuppy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-August 07

Re: Inventory Program

Posted 15 August 2007 - 04:37 AM

View PostMartyr2, on 14 Aug, 2007 - 09:50 PM, said:

Lookup the printf function's use. You are missing the second part of that function, where you list the variables you want placed into the first part's string.


I looked the printf function's up and I was forgetting to the the . in front of \n So it should have read $%.2f.\n Is this right? I think some of the errors my teacher keeps getting is because of minor errors on my part that produce major errors.

How do I test my code? All I go by is if it compiles or not. I have tried to run the .class files that the compiler produces, but my computer won't allow me to run them. Is there something I am missing?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#10 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Inventory Program

Posted 15 August 2007 - 10:44 AM

You can simply run your code by opening up your DOS command prompt, navigating to the directory where your file is, compile it using the javac command and then run it using java filenamewithnoextension.

Example...

c:\myjavaproject>javac Inventory.java

c:\myjavaproject>java Inventory



Notice how I didn't specify an extension and used the "java" command to run it. As for your formatter question, you just need to use %.2f\n if you want to format it to two decimal places (for money) and get a newline afterwards.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#11 mudpuppy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-August 07

Re: Inventory Program

Posted 15 August 2007 - 12:58 PM

View PostMartyr2, on 15 Aug, 2007 - 10:44 AM, said:

As for your formatter question, you just need to use %.2f\n if you want to format it to two decimal places (for money) and get a newline afterwards.

Hope this helps.


That is how I originally turned it in to my teacher. He said that was wrong. He said that it's giving me a readout of something like $12.34567890 (Just using those numbers as an example) He does not make sense to me though because that is the same printf function I used for my Employee Payroll Program a couple weeks ago. That one worked just fine without that long number and gave a correct readout as far as I know. If it was wrong then he didn't say anything about it. I agree with you though Martyr2, I know it is to use %.2f\n. I even went back and checked my code from the previous assignments.

This post has been edited by mudpuppy: 15 August 2007 - 01:02 PM

Was This Post Helpful? 0
  • +
  • -

#12 mudpuppy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-August 07

Re: Inventory Program

Posted 15 August 2007 - 09:53 PM

Quick question. This is what I haev to add to the code this week: Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee

Should this go into a .java file all by itself and be called by the inventory or fish.java file or should the GUI just go into the inventory.java file? In the reading it gives an example of a GUI to make a rainbow. (Still not real sure how to make this GUI incorporate it into the program as not being a rainbow.) I am working on that though! lol :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#13 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Inventory Program

Posted 15 August 2007 - 10:56 PM

Well you could build it onto the Inventory class since that is where you are displaying the contents of your products or you could build another file. If you go with the other file option, your inventory class will need some public methods for getting at its underlying products.

Some methods could be...

getProducts() which would return an array of products
addProduct()
removeProduct()

etc...


Essentially your Inventory class would become a "wrapper" class for managing the Products array.

You can implement it either way, it is up to you and what your instructor wants.
Was This Post Helpful? 0
  • +
  • -

#14 mudpuppy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-August 07

Re: Inventory Program

Posted 16 August 2007 - 08:50 PM

:blink: Ok, I am working on the GUI. I am getting this error: JFrame.java:3: expected
import.javax.swing.JFrame;
^
1 error

I am sure it is something stupid, but I am thoroughly confused.

import java.awt.FlowLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel;
import javax.swing.SwingConstants; 
import javax.swing.Icon; 
import javax.swing.ImageIcon; 

public class LabelFrame extends JFrame
 {
	  private JLabel label1; 
	  private JLabel label2; 
	  private JLabel label3;

		public LabelFrame()
		{
		super( "Inventory Of Fish" );
		setLayout( new FlowLayout() ); 

		   label1 = new JLabel( "Label" );
		   label1.setToolTipText( "This is How many fish that are in stock." );
		   add( label1 ); // add label1 to JFrame

		  Icon bug = new ImageIcon( getClass().getResource( "bug1.gif" ) );
		  label2 = new JLabel( "This is the amount of inventory for that stock", bug,
		  SwingConstants.LEFT );
		  label2.setToolTipText( "This is label2" );
		  add( label2 ); 

	label3 = new JLabel(); 
	label3.setText( "Amount of money in the stock" );
	label3.setIcon( bug ); 
	label3.setHorizontalTextPosition( SwingConstants.CENTER );
	label3.setVerticalTextPosition( SwingConstants.BOTTOM );
	label3.setToolTipText( "This is label3" );
	add( label3 ); 
} 
}



import.javax.swing.JFrame;

public class JFrame extends Inventory
{
	public static void main( String args[] )
	{
		GUI panel = new GUI();
		JFrame application = new JFrame();

		application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		application.add( panel );
		application.setSize( 400, 250 );
		application.setVisible( true );
		}
	}



public class Fish extends Product {
	
		public Fish(int number, String fishname, int fishquantity, double fishprice) {
				super(number, fishname, fishquantity, fishprice);
		}

   

	public double getValue() {
		double inventoryvalue = super.getValue();
		return (inventoryvalue * 1.05);
	}	
}




public class Inventory
{
  
		
		public static final int MAXIMUM_ITEMS = 10;
		private static Product product[] = new Fish[MAXIMUM_ITEMS];

		public static void main(String args[]) {
				buildInventory();
				getInventory();		
		}

		
		public static void buildInventory() {

				

				product[0] = new Fish(0, "Fancy Tailed Guppy",25, 4.99);
				product[1] = new Fish(1, "Huma Huma Picasso Trigger",2, 35.99);
				product[2] = new Fish(2, "Black Durgeon Trigger",1, 99.99);
				product[3] = new Fish(3, "Niger Trigger",2, 45.99);
				product[4] = new Fish(4, "Jack Dempsey Cichlid",4, 19.99);
				product[5] = new Fish(5, "Midas Cichlid",1, 69.99);
				product[6] = new Fish(6, "Neon Tetra",25, 2.99);
				product[7] = new Fish(7, "Convict Cichlid",2, 15.99);
				product[8] = new Fish(8, "Oscar", 4, 37.99);
				product[9] = new Fish(9, "Albino Cory Cat",1, 29.99);
		}


		
		public static void getInventory() {
				for(int i = 0; i < product.length; i++) {
					System.out.println("Number:	 " + product[i].getNumber());
					System.out.println("Name:	   " + product[i].getName());
					System.out.println("Quantity:   " + product[i].getQuantity());
					System.out.println("Price:	  " + product[i].getPrice());
					System.out.println("Item Value: " + product[i].getQuantity() * product[i].getPrice());
			

						

					System.out.printIn("Restock Fee: " + (product[i].getQuantity() * product[i].getPrice()) * 0.05);
					System.out.printf("\n%s's Inventory Total including Restocking Fee is: $%4.2f%n");
					System.out.printIn();

			  }  
	   }
}




public class Product {

	private String name = "";
	private int number = 0;
	private double price = 0;
	private int quantity = 0;

	
	public Product(int number, String name, int quantity, double price) {
		this.number = number;
		this.name = name;
		this.quantity = quantity;
		this.price = price;
	}

	
	public String getName() { return name; }
	public int getNumber() { return number; }
	public double getPrice() { return price; }
	public int getQuantity() { return quantity; }

	
	public double getValue() {
		 return (double) quantity * price;
	}
}



I wasn't sure if all the code was needed or not so I sent them all in. Ok, what the GUI is supposed to be doing is this:Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee.

As far as I know, Inventory program and my GUI does this. (I think) lol Can anyone steer me in the right direction, please?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#15 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Inventory Program

Posted 16 August 2007 - 10:37 PM

First of all, you wrote your import wrong at the top of your second code segment there....

// Your error is here, you have an extra period after import. 
import.javax.swing.JFrame;

// You have a second potential problem here.
// Your Inventory class extends the JFrame, not the other way around.

public class JFrame extends Inventory
{
	public static void main( String args[] )
	{
		GUI panel = new GUI();
		JFrame application = new JFrame();

		application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		application.add( panel );
		application.setSize( 400, 250 );
		application.setVisible( true );
	 }
}



Read the comments I put in the code segment above for your corrections.

Enjoy
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2