开发者

Java applying multiply to each item in an array

Alright, so I'm trying to create a "sales tax program' where the user can input the items and it adds it to an array, called "costArray". I only know how to do it, almost, with a String (since I need costArray.length for the loop) but I'm kind of开发者_StackOverflow中文版 lost. Can anyone help point me in the right direction so I can: Take an array of numbers (doubles) and apply a multiplier to it (0.08 for sales tax percentage) and output a total number (double)? Here is what I have so far, can you tell me if I'm close? 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;
    }
    //         /**
    //          * Mutator method to calculate tax on each item
    //          */
    //         public void calculateTax(){
    //             for(int index=0; index < costArray.length; index++){
    //                 System.out.println(0.08 * costArray[index]);
    //             }
    //         }
    }


The number ist stored in a String and you have to "convert" it to a real number (a double value)

The way to do it is shown here:

 String s = "-1.234";
 double value = Double.parseDouble(s);


In Java 8:

import java.util.stream.DoubleStream;

double taxCoef = 0.08;
double[] prices = {10,20,30};
double total = DoubleStream.of(prices).map(p->p*(1+taxCoef)).sum();

System.out.println(total);

output: 64.80000000000001

(alternatively, can sum up first and then multiply)


Arrays are a bad idea, if you don't know before which size they will have. Why not use an ArrayList? If you don't know it right know: You'll really will need it often!

Names of indexes inside a loop are well with 'i', 'n', 'p, q, r' or 'x', and they only exist in their own loop, so you can define a new 'i' in the second loop - no need for indexa, indexb, ... .

For learning, a double might be sufficient, but in real world, you shouldn't use double for Money amounts, but BigDecimal. The bit-representation of fractions leads else to surprising effects.

And Scanner has already methods like scanner.nextDouble () to read a Number.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜