Results 1 to 7 of 7
- 04-10-2011, 12:19 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Problem with subclass reading info from main class and vice versa
I have a code I have been working on for a class project, and I can not get the classes to communicate. Could someone look at my code and give me a hint as to where I am going wrong? The veriables from the main class will not transfer to the Maker class for me to calculate the total inventory or the grand total. The variables that are declared in the Maker class such as Manuzero and ManuOne are recognized in the main class but I get the error "Non-static variable can not be referenced from a static context. How can I fix these errrors? Thank You for your help.
Java Code:package inventory; // This program displays inventory /** * * @author Julie Rader */ public class inventory { public static void main(String[] args) { { // get Product names String ProductName[] = {"CD","Shirt","Pants","Hat","Shoes"}; //Get product Number int[] ProductNumber = {2525,2526,2527,2528,2529}; //Get product quantity int ProductQuantity [] = {4,6,15,4,3}; //Get product price double ProductPrice[] = {14.99,12.58,22.58,10.72,52.58}; //Print product one System.out.println("Product Name is: " + ProductName[0]); System.out.printf("The manufacturer of this product is:%s\n " + Maker.ManuZero ); System.out.println("Product Number is: " + ProductNumber[0]); System.out.println("Product Quantity is: " + ProductQuantity[0]); System.out.printf("Product Price is: $ %.2f\n" , ProductPrice[0]); System.out.printf("Product Total is: $ &.2f\n" , (ProductQuantity[0] * ProductPrice[0])); System.out.printf("Restocking fee for this product is: $ %.2f\n" , (ProductPrice[0] * 0.05)); System.out.println(); //Print Product two System.out.println("Product Name is: " + ProductName[1]); System.out.printf("The manufacturer of this product is:%s\n " + Maker.ManuOne ); System.out.println("Product Number is: " + ProductNumber[1]); System.out.println("Product Quantity is: " + ProductQuantity[1]); System.out.printf("Product Price is: $ %.2f\n" , ProductPrice[1]); System.out.printf("Product Total is: $ %.2f\n" , (ProductQuantity[1] * ProductPrice[1])); System.out.printf("Restocking fee for this product is: $ %.2f\n" , (ProductPrice[1] * 0.05)); System.out.println(); //print product three System.out.println("Product Name is: " + ProductName[2]); System.out.f("The manufacturer of this product is: %s\n " , Maker.ManuTwo ); System.out.println("Product Number is: " + ProductNumber[2]); System.out.println("Product Quantity is: " + ProductQuantity[2]); System.out.printf("Product Price is: $ %.2f\n" , ProductPrice[2]); System.out.printf("Product Total is: $ %.2f\n" , (ProductQuantity[2] * ProductPrice[2])); System.out.printf("Restocking fee for this product is: $ %.2f\n" , (ProductPrice[2] * 0.05)); System.out.println(); //Print product four System.out.println("Product Name is: " + ProductName[3]); System.out.printf("The manufacturer of this product is:%s\n " + Maker.ManuThree ); System.out.println("Product Number is: " + ProductNumber[3]); System.out.println("Product Quantity is: " + ProductQuantity[3]); System.out.printf("Product Price is: $ %.2f\n" , ProductPrice[3]); System.out.printf("Product Total is: $ %.2f\n" , (ProductQuantity[3] * ProductPrice[3])); System.out.printf("Restocking fee for this product is: $ %.2f\n" , (ProductPrice[3] * 0.05)); System.out.println(); //Print product five System.out.println("Product Name is: " + ProductName[4]); System.out.printf("The manufacturer of this product is:%s\n " Maker.ManuFour ); System.out.println("Product Number is: " + ProductNumber[4]); System.out.println("Product Quantity is: " + ProductQuantity[4]); System.out.printf("Product Price is: $ %.2f\n" , ProductPrice[4]); System.out.printf("Product Total is: $ %.2f\n" , (ProductQuantity[4] * ProductPrice[4])); System.out.printf("Restocking fee for this product is: $ %.2f\n" , (ProductPrice[4] * 0.05)); System.out.println(); // print totals System.out.println("The total amount in inventory is: ", Maker.getInventoryTotal()); System.out.println(); System.out.println("Grand total in inventory plus all restocking fees is: ", Maker.getGrandTotal()); System.out.println() } }// end main }//end class inventory class Maker { String Manu0; String Manu1; String Manu2; String Manu3; String Manu4; double GrandTotal; double InventoryTotal; public Maker (String Sony, String Hanes, String Riders, String EverFashion, String KSwiss) { Manu0 = Sony; Manu1 = Hanes; Manu2 = Riders; Manu3 = EverFashion; Manu4 = KSwiss; computeGrandTotal(); computeInventoryTotal(); } //set manu 0 public void setManu0( String Sony) { Manu0 = Sony; } //return manu 0 public String getManu0() { return Manu0; } //set manu 1 public void setManu1 (String Hanes) { Manu1 = Hanes; } //return manu1 public String getManu1() { return Manu1; } //get manu 2 public void setManu2 (String Riders) { Manu2 = Riders; } //return menu 2 public String getManu2() { return Manu2; } //set manu 3 public void setManu3 (String EverFashion) { Manu3 = EverFashion; } //return manu 3 public String getManu3() { return Manu3; } //set manu4 public void setManu4 ( String KSwiss ) { Manu4 = KSwiss; } //return Manu 4 public String getManu4() { return Manu4; } public void computeGrandTotal() // compute grand total(Total inventory plus 5% restocking fee { GrandTotal = (ProductQuantity[1] * ProductPrice[1] +ProductQuantity[2] * ProductPrice[2] + ProductQuantity[3] * ProductPrice[3] + ProductQuantity[4] * ProductPrice[4] * 0.05) } //get grand total public double getGrandTotal() { return GrandTotal; } public void computeInventoryTotal() //compute inventory total { InventoryTotal = (ProductQuantity[1] * ProductPrice[1] +ProductQuantity[2] * ProductPrice[2] + ProductQuantity[3] * ProductPrice[3] + ProductQuantity[4] * ProductPrice[4]) } //Get inventory total public double getInventoryTotal() { return InventoryTotal; } }//end Maker
-
There are several significant issues at play here, but I'm not sure just where to start. Can you post your complete assignment?
- 04-10-2011, 02:53 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Assignment up to what is due tonight.
The code I posted was the entire code that I have for this project. The syllabus for the project is below.
2. CheckPoint: Inventory Program Part 1
• Resource: Ch. 7 & 13 of Java: How to Program
• Choose a product that lends itself to an inventory (for example, products at your workplace, office supplies, music CDs, DVD movies, or software).
• Create a product class that holds the item number, the name of the product, the number of units in stock, and the price of each unit.
• Create a Java application that displays the product number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory (the number of units in stock multiplied by the price of each unit). Pay attention to the good programming practices in the text to ensure your source code is readable and well documented.
• Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output 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 output should display the value of the entire inventory.
• Create a method to calculate the value of the entire inventory.
• Create another method to sort the array items by the name of the product.
• 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.
-
I hate to say this, but I think that your code is completely off the mark and that the best option for you is to scrap this code and restart your project from the beginning. Do each instruction one at a time, compile your code frequently, perhaps after adding each line of code, and not to add any new lines of code until the current code base compiles without errors.
First of all, review the first two instructions:
• Choose a product that lends itself to an inventory (for example, products at your workplace, office supplies, music CDs, DVD movies, or software).
• Create a product class that holds the item number, the name of the product, the number of units in stock, and the price of each unit.
and then you should create a class called Product and give it an int variable called itemNumber, a String variable called name, an int variable called unitsInStock and a double variable called price. Give it a constructor that will accept values for these fields and that initializes the fields.
Then when this is done, move on to the next steps in the instructions, one step at a time.
- 04-10-2011, 03:00 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Just scanning the requirements you are missing a huge piece of information. Bullet number 3, asks you to create a separate class for products. Your code doesn't do this, instead it does something fairly clumsy in he invetory class, parallel arrays. If you can avoid parallel arrays, do so.
Try doing the third bullet, create a class called product which encapsulates the pertinent information and has methods which allow you to manipulate this class. Once you have done this you should focus on the next step, but this bullet is fairly important. It will make the inventory class much neater.
edit: too slow, sadface.
Doing as fubar suggested and scrapping it and starting fresh is a good idea, be sure to compile and test frequently and do not jump ahead. One bullet at a time until you are comfortable with the results. If you get too stuck, ask for help.Last edited by sunde887; 04-10-2011 at 03:03 AM.
- 04-10-2011, 03:50 AM #6
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
code I had for this assignment
This is the code I had at first which ran perfectly, then the teacher asked us to make arrays, and I could not figure out how to make the arrays and get the information to print out the right way and I was very confused that is how the code got to where it is. So, from the code I have below which compiles and runs how do I get it to do what is in the syllabus?
Java Code:package product; import java.util.Scanner;// program uses class Scanner public class product { // main method begins execution of Java application public static void main(String[] args) { // Multiplication program to calculate payroll // create Scanner to obtain input from command window Scanner input = new Scanner(System.in); boolean enterMoreData = true; String proName; while(enterMoreData){ //get product name or stop program System.out.print("Enter product name or stop to quit: " ); proName = input.nextLine(); if (proName.equalsIgnoreCase("stop")) { System.out.println(" Good bye! Thank You for using inventory organizer."); System.exit(0);} // get product number int proNumber; do { System.out.println(" Please enter the product identification number for inventory: "); proNumber = input.nextInt(); if (proNumber < 0) { System.out.println("Number must be positive, please try again. "); } } while (proNumber < 0); int proQuant; // get product quantity in inventory do { System.out.println(" Please enter the quantity of product in inventory: "); proQuant = input.nextInt(); if (proQuant < 0) { System.out.println("Number must be positive, please try again. "); } } while ( proQuant < 0); double proPrice; // get product do { System.out.println("Please enter product price: "); proPrice = input.nextDouble(); if (proPrice < 0) { System.out.println("Number must be positive, please try again."); } } while ( proPrice < 0); Item item = new Item (proName, proNumber, proQuant, proPrice); System.out.printf("Product name is: %s\n Product number is : %.2f\n " + "Product quantity in stock is: %.2f\n Product price is: %.2f\n Total of products" + " in inventory is: $ %.2f\n ", item.ProductName, item.ProductNumber, item.ProductQuantity, item.ProductPrice, item.getProductTotal()); proName = input.nextLine(); }// end while } //end method main } //end class product class Item //creat new class Product { //input variables String ProductName; int ProductNumber; int ProductQuantity; double ProductPrice; double ProductTotal; double TotalInventory; // Get input public Item(String name, int number, int quant, double price) { this.ProductName = name; this.ProductNumber = number; this.ProductQuantity = quant; this.ProductPrice = price; computeProductTotal(); } // method to compute proTotal private void computeProductTotal() { ProductTotal = ProductQuantity * ProductPrice; } // method to get Total public double getProductTotal() { return ProductTotal; } }// end class Product
- 04-10-2011, 04:40 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
One of the biggest things is to start small. Instead of all the stuff in main, do the following to test your item class.
Do that and compile and run it and verify it produces correct results, once you do that you are ready for the next step.Java Code:create an instance of item Call all your methods on the item object
Also, this isn't really a rule but it's the typical style convention. Variable names should have the first letter of the first word lowercase and the rest of the words should have capital letters. Class names should have the first letter of each word capitalized.
Finally, you create an array for this class the same way you would create any other object array. A string array is an object array, the form of all arrays should beJava Code:int aVariable; String anotherVariable class AClass class AnotherClass
Java Code:Type[] typeArray = new Type[length]
Last edited by sunde887; 04-10-2011 at 04:47 AM.
Similar Threads
-
Retrieving all the keys corresponding to a value and vice versa
By Ms.Ranjan in forum New To JavaReplies: 5Last Post: 04-16-2009, 06:30 PM -
Postfix into prefix and vice versa
By sfe23 in forum New To JavaReplies: 9Last Post: 02-19-2009, 10:37 PM -
[SOLVED] passing array between main and method,vice-versa
By blueyan in forum New To JavaReplies: 5Last Post: 10-04-2008, 11:13 AM -
Converting to ASCII and vice-versa
By pheonix in forum New To JavaReplies: 2Last Post: 09-09-2008, 04:43 AM -
Document conversion PDF to MS doc and vice versa
By abintoms in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks