1

How would I split this code up into two classes? I want the Input class to deal with purely input and the Tax class to deal with the addition of taxes and result. Is this possible? So basically I want to print the tax total, etc. through the first class, TaxClass, rather than the Input class. Here is my 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));
}
}
5
  • You realize that you're not using those arrays in any meaningful fashion, right? What exactly is it that this is supposed to do? Apr 28, 2011 at 19:56
  • The array holds all the items' prices. I then multiply each array item by a multiplier and output it.
    – tekman22
    Apr 28, 2011 at 19:59
  • I would suggest adding your main method so as to give you a better idea of the program flow and might help delineate your classes a bit. Also, the Homework tag would be good.
    – Riggy
    Apr 28, 2011 at 19:59
  • I added both classes, TaxClass and Input
    – tekman22
    Apr 28, 2011 at 20:02
  • What I meant by that was that you never access the arrays in this code beyond using one's length as the bound on your for loop. What @Riggy was trying to point out is that if this is all of your code, it won't run because there's not a main() method. Apr 28, 2011 at 20:44

2 Answers 2

0

I think what you want is a Model and a Controller. Your controller would have methods that would deal with input.

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

Your model would be an item with a cost and a tax.

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 ) { ... }
}

Then in your main method, you'll create an array of TaxableItem objects, one per input from the user. You can also create a Receipt class to do much of this for you, which would be better.

0

Your code is a mess - names of variables are confusing, there are pieces of code in comments, unnecessary unboxing in loop ...

If you want to take an array of Double values and multiply each value of the array with some constant, what about making some custom List class with Iterator doing the math in next() method for you ? You get the numbers multiplied when iterating through the collection and the original values are kept intact.

Your Input class will just collect the input in an input list, that you will use it to create your list and in Output class loop through it and print the results. You can also make Input and Output interfaces and implement them - more flexible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.