如何将此代码分成两个类?我希望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