1

So my program, when instantiated asks the user how many "items" they want. My program then creates two arrays, one for the "item name" and one for the "item price". I use a loop to let the user input each item name and item price in their respective arrays, however I'm lost with the item price array. To use my loop, I need to utilize the "itemprice.length" element but I can't do that when I'm not working with Strings.

After the user inputs the "prices" of each item, I need to apply a multiplier to each array item and output it. So I want to, for example, have 3 items in the array: 1.20, 1.30, 1.40, and then I want the program to ask me for the "sales tax" of which I can enter 0.08 and it will then multiply 0.08 to each item and output a total.

Is there a way that I can make my program work so it allows the user to enter, let's say, 5 items and their prices and am I going about it the right way? Any way of doing this easier? Thanks!

public class Input
{
private Scanner keybd;
private String item;
private double cost;
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(){
    for(int index=0; index < itemArray.length; index++){ 
    System.out.println("Enter the item name: ");
    itemArray[index] = keybd.next();}
    for(int indexa=0; indexa < itemArray.length; indexa++){
        System.out.println(itemArray[indexa]);
    }
    for(int indexb=0; indexb < costArray.length; indexb++){ 
    System.out.println("Enter the item cost: ");
    costArray[indexb] = keybd.next();}
    for(int indexc=0; indexc < costArray.length; indexc++){
        System.out.println(costArray[indexc]);
    }
}
    /**
     * Accessor method to return the items cost with tax
     */
    public double getTax(){
        return costArray.length;
    }
1
  • You don't use indexa/b/c as name without special reason, just i and j, k, l if you need more of them, but here, they live in different scopes, so you can use i again for the second interation. Then, the code isn't OOP. If the itemArray.length has to be the same, for cost and item, you create a compound class Item with attributes (price, total), and create an Array over the items, and automatically you have one total per item and one netto price. And if possible (not homework to theme 'Array'), you're mostly better of using an ArrayList instead of arrays. Commented Apr 28, 2011 at 18:21

3 Answers 3

1

Use a Float[] and use Float.parseFloat(String str) to convert from a string to a float.

As an aside, when dealing with money, floating point is a bad idea, since there are always issues with precision. It is best to use ints/longs with the appropriate lowest currency unit (i.e cents in the US etc.)

Sign up to request clarification or add additional context in comments.

Comments

0

you can try as:

System.out.println("Enter the sales tax: ");
double salesTax = keybd.next();

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 * salesTax);
total = total + cost;
}

System.out.println("Total: " + (total-totalTax));

EDIT: TO calculate the total during inserting the cost.

9 Comments

So would I get rid of all my indexb stuff?
indexb stuff will be there to get the costs of all the item one bye one. after getting the cost for each item get the sales tax and apply it on all the items. And the same you can do while accepting the cost as well. I will get you the code in few secs.
I also need to find out how to add the total tax value to all the items added together? This gives me the total of the tax only. Thank you!
And you need to make keybd.next() keybd.nextDouble() ha.
After this, no need to calculate the tax on total. because the total is calculated after applying the tax on each item individually. So its the total cost with tax applied.
|
0

It's not clear to me what your question is. Are you having problems with the item cost data? You are just reading in a String. You should read in an array of doubles instead.

costArray = new double[anyAmountOfItems];
// Then when reading use the appropriate Scanner method
costArray[indexb] = keybd.nextDouble();

A couple style notes:

  1. You might want to consider using Lists instead of arrays. That way you don't have to worry about fixing the size of the arrays.
  2. There is no need to use new variable names in each of the for loops. It just adds confusion. Instead of indexa, indexb, etc just use i.
  3. Better yet, use the enhanced for loop for cases where you don't really need the index:

    for( String item : itemArray ) { System.out.println(item); }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.