如何將此代碼分成兩個類?我希望Input類處理純粹的輸入和Tax類來處理添加稅和結果。這可能嗎?所以基本上我想通過第一類TaxClass而不是Input類來打印稅額等。這裏是我的代碼:Java - 分解代碼
public class TaxClass
{
private Input newList;
/**
* Constructor for objects of class Tax
* Enter the number of items
*/
public TaxClass(int anyAmount)
{
newList = new Input(anyAmount);
}
/**
* Mutator method to add items and their cost
* Enter the sales tax percentage
*/
public void addItems(double anyTax){
double salesTax = anyTax;
newList.setArray(salesTax);
}
}
public class Input
{
private Scanner keybd;
private String[] costArray;
private String[] itemArray;
/**
* Constructor for objects of class Scanner
*/
public Input(int anyAmountofItems)
{
keybd = new Scanner(System.in);
costArray = new String[anyAmountofItems];
itemArray = new String[anyAmountofItems];
}
/**
* Mutator method to set the item names and costs
*/
public void setArray(double anyValue){
//System.out.println("Enter the sales tax percentage: ");
//double salesTax = keybd.nextDouble();
double totalTax=0.0;
double total=0.0;
for(int indexc=0; indexc < costArray.length; indexc++){
System.out.println("Enter the item cost: ");
double cost = Double.valueOf(keybd.next()).doubleValue();
totalTax = totalTax + (cost * anyValue);
total = total + cost;
}
System.out.println("Total tax: " + totalTax);
System.out.println("Total cost pre-tax: " + total);
System.out.println("Total cost including tax: " + (total+totalTax));
}
}
你意識到你沒有以任何有意義的方式使用這些數組,對吧?這到底是什麼意思? – 2011-04-28 19:56:33
數組包含所有商品的價格。然後我乘以每個數組項並輸出它。 – tekman22 2011-04-28 19:59:30
我建議添加你的主要方法,以便給你一個更好的程序流程概念,並可能有助於描述你的類。另外,家庭作業標籤將會很好。 – Riggy 2011-04-28 19:59:57