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.
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
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.
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?
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