2011-04-28 4 views
1

이 코드를 어떻게 두 개의 클래스로 나눌 수 있습니까? 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)); 
} 
} 
+0

당신은 의미있는 방식으로 배열을 사용하지 않는다는 것을 알고 있습니까? 이것이 정확히 무엇입니까? –

+0

배열에는 모든 항목의 가격이 저장됩니다. 그런 다음 각 배열 항목에 승수를 곱하여 출력합니다. – tekman22

+0

프로그램 흐름을 더 잘 이해할 수 있도록 기본 메소드를 추가하고 클래스를 좀 더 자세히 설명하는 것이 좋습니다. 또한 숙제 태그가 좋을 것입니다. – Riggy

답변

0

원하는 것은 모델과 컨트롤러입니다. 컨트롤러에는 입력을 처리 할 수있는 메소드가 있습니다.

public class InputController { 
    public int getCost() { ... } 
    public void promptUser() { ... } 
} 

귀하의 모델은 비용과 세금이있는 항목입니다.

public class TaxableItem { 
    private int costInCents; 
    private int taxInCents; 
    public int getTotal(); 
    public int getTaxInCents() { ... } 
    public void setTaxInCents(int cents) { ... } 
    public int getCostInCents() { ... } 
    public void setCostInCents(int cents) { ... } 
} 

그런 다음 기본 메소드에서 사용자 입력에 따라 하나씩 TaxableItem 객체의 배열을 만듭니다. 당신은 또한 당신을 위해 많은 것을하기 위해 영수증 클래스를 만들 수 있습니다.

0

코드는 엉망이다 - 당신이 두 값의 배열을 가지고 각각의 값을 곱하려면 변수의 이름은

... 루프에서 불필요한 언 박싱 주석의 코드 조각이있다, 혼란 어떤 상수와 배열, 어떤에 대해 사용자 정의 List 클래스는 다음에 수학을하고 있습니다() 방법을 당신을 위해? 컬렉션을 반복 할 때 곱해지는 숫자를 가져오고 원래 값은 그대로 유지됩니다.

입력 클래스는 입력 목록에서 입력을 수집하여 목록을 만들고 입력 클래스를 통해 반복하여 결과를 인쇄합니다. 입력 및 출력 인터페이스를 만들고 구현할 수도 있습니다.