Alright so I'm trying to have the variables "totalTax" and "total" hold the price of the item, and they work fine when I call them within the method they are being set at, but when I call them from another class, it returns 0.
public class Input
{
private Scanner keybd;
private String[] costArray;
private String[] itemArray;
private double totalTax;
private double total;
/**
* Constructor for objects of class Scanner
*
* @param anyAmountofItems the amount of items you are going to be buying
*/
public Input(int anyAmountofItems)
{
keybd = new Scanner(System.in);
costArray = new String[anyAmountofItems];
itemArray = new String[anyAmountofItems];
totalTax = 0.0;
total = 0.0;
}
/**
* Mutator method to set the item names and costs
*
* @param anyValue the tax rate percentage (ex. 0.08 for 8%)
*/
public void setArray(double anyValue){
//System.out.println("Enter the sales tax percentage: ");
//double salesTax = keybd.nextDouble();
//for(int index=0; index < itemArray.length; index++){
//System.out.println("Enter the item name: ");
//itemArray[index] = keybd.next();}
double totalTax=0.0;
double total=0.0;
for(int indexc=0; indexc < costArray.length; indexc++){
System.out.println("Enter the item name: ");
String anyName = keybd.next();
itemArray[indexc] = anyName;
System.out.println("Enter the item cost: ");
double cost = Double.valueOf(keybd.next()).doubleValue();
costArray[indexc] = " " + cost;
totalTax = totalTax + (cost * anyValue);
total = total + cost;
}
//for(int indexa=0; indexa < itemArray.length; indexa++){
//System.out.println(itemArray[indexa] + "-" + costArray[indexa]);}
//System.out.println("Sales Receipt");
//System.out.println("--------------");
//for(int i=0;i<costArray.length;i++){
// System.out.println(itemArray[i] + " - $" + costArray[i]);
//}
//returnCostArray();
//System.out.println("Total tax: $" + totalTax);
//System.out.println("Total cost pre-tax: $" + total);
//System.out.println("Total cost including tax: $" + (total+totalTax));
totalTax = totalTax;
total = total;
}
public String returnArray(int anyElement){
int index = anyElement - 1;
return itemArray[index];
}
public double returnTotal(){
return total;
}
public double returnTotalTax(){
return totalTax;
}
public void returnCostArray(){
for(int i=0;i<costArray.length;i++){
System.out.println(itemArray[i] + " - $" + costArray[i]);
}
}
}
Then, I'm running this class which uses the Input class and it is returning 0, yet when I do the System.out.println in the same class that I already have, it works and returns the total.
public class TaxClass
{
private Input newList;
/**
* Constructor for objects of class Tax
*
* @param anyAmount Enter the number of items
*/
public TaxClass(int anyAmount)
{
newList = new Input(anyAmount);
}
/**
* Mutator method to add items and their cost
*
*
* @param anyTax Enter the sales tax percentage
*/
public void addItems(double anyTax){
double salesTax = anyTax;
newList.setArray(salesTax);
System.out.println("Sales Receipt");
System.out.println("--------------");
newList.returnCostArray();
System.out.println("Total tax: $" + newList.returnTotalTax());
System.out.println("Total cost pre-tax: $" + newList.returnTotal());
System.out.println("Total cost including tax: $" + (newList.returnTotal()+newList.returnTotalTax()));
}
//public String returnArray(){
//newList.returnArray();
//}
}
EDIT: Local variable problem. I'm trying to use the "return array(int AnyElement)" method in the first class, but when I try to compile it is telling me that: returnArray(int) in Input cannot be applied to (). Any help? Thanks! Thanks!