Alec Alec - 1 month ago 15
Java Question

Issues with Static Methods Java

I'm working on a project but can't seem to figure out how to deal with static methods. I've tried searching, but nothing seems to be very clear at this point for me.

The SimpleProduct Class inherits methods from the Product interface but I can't figure out how to call the methods in the main, any help is greatly appreciated!

public class SimpleProduct implements Product {

public String name, type;
public int quantity;
public double price;
public boolean inStock;
public Queue<SimpleProduct> inStockProducts = new LinkedList<SimpleProduct>();
public Stack<SimpleProduct> outOfStockProducts = new Stack<SimpleProduct>();
public DecimalFormat format = new DecimalFormat("###.##");

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter Database Filename: ");
String fileName = scan.nextLine();
File fileNe = new File(fileName);
Scanner file = null;
try {
file = new Scanner(fileNe);
Customer customer = Customer.getCustomer(file);
while(readNewProduct(file)) {

}

Queue<SimpleProduct> tmp = inStockProducts;

System.out.println();
printShippingOrders(inStockProducts, customer);
System.out.println();
printOutstanding(outOfStockProducts, customer);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

scan.close();
file.close();

}

public double calculateTax(double cost, double tax) {
return cost * tax;
}

public double calculateShipping(double cost) {
if(cost < 10) {
return cost * 0.15;
}
else if(cost >= 10 && cost < 25) {
return cost * 0.05;
} else {
return 0;
}
}

public void printOutstanding(Stack<SimpleProduct> outstanding, Customer customer) {
System.out.println("Orders Outstanding For:");
System.out.println(customer.toString());
System.out.println("------------------------------------------");
double outstandingBalance = 0;
for(int i = 0; i <= outstanding.size()-1; i++) {
SimpleProduct product = outstanding.pop();
outstandingBalance += product.getPrice();
System.out.format("%n %d x " + product.getName() + "\t\t(%s)\t%s", product.getQuantity(), product.getType(), product.getPrice()) ;
}
System.out.println("\n -------------------------------------- ");
System.out.format("Outstanding Balance:\t$%s", format.format(outstandingBalance));
System.out.println("\n------------------------------------------");
}

public void printShippingOrders(Queue<SimpleProduct> outstanding, Customer customer) {
System.out.println("Shipping To:");
System.out.println(customer.toString());
System.out.println("------------------------------------------");
double subtotal = 0;
for(int i = 0; i <= outstanding.size()-1; i++) {
SimpleProduct product = outstanding.remove();
subtotal += product.getPrice();
System.out.format("%n %d x " + product.getName() + "\t(%s)\t$%s", product.getQuantity(), product.getType(), product.getPrice()) ;
}
double salesTax = calculateTax(subtotal, customer.getTax());
double shippingCost = calculateShipping(subtotal + salesTax);
System.out.println("\n -------------------------------------- ");
System.out.format(" Subtotal:\t\t$%s", subtotal);
System.out.format("%n Sales Tax:\t\t$%s", salesTax);
System.out.format("%n Shipping Cost:\t$%s", shippingCost);
System.out.println("\n -------------------------------------- ");
double total = subtotal + salesTax + shippingCost;
System.out.format(" Total:\t\t$%s", total);
System.out.println("\n------------------------------------------");
}

public SimpleProduct(String name, String type, int quant, double price, boolean stocked) {
this.name = name;
this.type = type;
this.quantity = quant;
this.price = price;
this.inStock = stocked;
}

@Override
public void setName(String name) {
this.name = name;
}

@Override
public String getName() {
return this.name;
}

@Override
public void setType(String type) {
this.type = type;
}

@Override
public String getType() {
return this.type;
}

@Override
public void setPrice(double price) {
this.price = price;
}

@Override
public double getPrice() {
return this.price;
}

@Override
public void setQuantity(int quantity) {
this.quantity = quantity;
}

@Override
public int getQuantity() {
return this.quantity;
}

@Override
public void setInStock(boolean inStock) {
this.inStock = inStock;
}

@Override
public boolean getInStock() {
return this.inStock;
}

public Queue<SimpleProduct> getInStockList() {
return this.inStockProducts;
}

public Stack<SimpleProduct> getOutOfStockList() {
return this.outOfStockProducts;
}

public boolean readNextProduct(Scanner file) {
String tmpName, tmpType;
double tmpPrice;
int tmpQuantity;
boolean tmpinStock;
boolean j = false;
try {
file.nextLine();
tmpName = file.nextLine();
tmpType = file.nextLine();
tmpPrice = file.nextDouble();
tmpQuantity = file.nextInt();
tmpinStock = file.nextBoolean();
SimpleProduct tmp = new SimpleProduct(tmpName, tmpType, tmpQuantity, tmpPrice, tmpinStock);
if(tmpinStock) {
inStockProducts.add(tmp);
j = true;
} else if(!tmpinStock) {
outOfStockProducts.add(tmp);
j = true;
} else {
j = false;
}
} catch(NoSuchElementException e) {
}
return j;
}

public static boolean readNewProduct(Scanner file) {
String tmpName, tmpType;
double tmpPrice;
int tmpQuantity;
boolean tmpinStock;
boolean j = false;
try {

file.nextLine();
tmpName = file.nextLine();
tmpType = file.nextLine();
tmpPrice = file.nextDouble();
tmpQuantity = file.nextInt();
tmpinStock = file.nextBoolean();
SimpleProduct tmp = new SimpleProduct(tmpName, tmpType, tmpQuantity, tmpPrice, tmpinStock);
if(tmpinStock) {
inStockProducts.add(tmp);
j = true;
} else if(!tmpinStock) {
outOfStockProducts.add(tmp);
j = true;
} else {
j = false;
}
} catch(NoSuchElementException e) {
}
return j;
}


Product Interface:

public interface Product {

/*
* setName
* @param name - new name for the product
*/
public void setName(String name);

/*
* getName
* @return the name of the product
*/
public String getName();

/*
* setType
* @param type - the type of the product
*/
public void setType(String type);

/*
* getType
* @return - the product type
*/
public String getType();

/*
* setPrice
* @param price - the price of the product
*/
public void setPrice(double price);

/*
* getPrice
* @return the price of the product
*/
public double getPrice();

/*
* setQuantity
* @param quantity - the number of this product in inventory
*/
public void setQuantity(int quantity);

/*
* getQuantity
* @return the number of this product in inventory
*/
public int getQuantity();

/*
* setInStock
* @param inStock - true if this product is in stock
*/
public void setInStock(boolean inStock);

/*
* getQuantity
* @return true if this product is in stock
*/
public boolean getInStock();

/*
* readNextProduct
* @param inFile - a Scanner containing product entries
* @return false if the product cannot be completely read,
* true otherwise
*/
public boolean readNextProduct(Scanner inFile);


My issue is that Eclipse is telling me that I cannot make a static reference to inStockProducts and outOfStockProducts in the main of SimpleProducts

Answer

When you implement an interface, you're providing an archetype for instances of a class. Static methods are, by definition, class-level methods. They can't access instance-level methods and fields. Based on the code you've shown, you're conflating your Product instance class with your main program runner. I'd recommend creating a separate class, called Runner or something similar, which contains your static main method.

Inside that method, create a new instance of SimpleProduct, and then you'll be able to interact with its fields, etc.

As an aside, you may want to consider whether readNextProduct(Scanner inFile) is truly a responsibility of the Product interface, or if that should be handled somewhere else (likely inside your Runner class).