이 코드를 어떻게 두 개의 클래스로 나눌 수 있습니까? Input 클래스가 순수 입력을 처리하고 Tax 클래스가 세금 및 결과 추가를 처리하기를 원합니다. 이것이 가능한가? 따라서 기본적으로 Input 클래스가 아닌 TaxClass를 통해 세금 합계 등을 인쇄하려고합니다. 여기 내 코드는 다음과 같습니다.Java - Breaking up Code
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));
}
}
당신은 의미있는 방식으로 배열을 사용하지 않는다는 것을 알고 있습니까? 이것이 정확히 무엇입니까? –
배열에는 모든 항목의 가격이 저장됩니다. 그런 다음 각 배열 항목에 승수를 곱하여 출력합니다. – tekman22
프로그램 흐름을 더 잘 이해할 수 있도록 기본 메소드를 추가하고 클래스를 좀 더 자세히 설명하는 것이 좋습니다. 또한 숙제 태그가 좋을 것입니다. – Riggy