Jump to content

hi,

 

I'm trying to write a code that will store the price of an item and the tax in an array based on user input. User will enter both price than taxes. I am trying to get a 3rd array to use the values in the other two and add the values to produce a total to include price+tax. 

 

here is the code, but it obviously doesn't work. I could combine price*tax and get the same result, but I'm trying to figure out how to avoid doing that by using the already assigned values in those two arrays than have a 3rd array use them to add them up.

 

public static void main(String[] args){
   Scanner input= new Scanner(System.in);
   final int NUM = 2;  
   
   int[] tax = new int[NUM];
   int[] price = new int[NUM];
   int[] total = new int[NUM];
   
   for (int index = 0; index < tax.length; index++){
   System.out.println("Enter tax " + (index+1) + ": ");
   tax[index] = input.nextInt();
   System.out.println("Enter price " + (index+1) + ": ");
   price[index] = input.nextInt();
   }
   System.out.println();
   System.out.println("total\t\tprice\t\ttax");
   System.out.println("-----\t\t-----\t\t------");
   for ( int i = 0; i < total.length; i++){
   System.out.print("" + total); // would like to do something like total = price + tax than just use total in the for loop. Anyone know how to do something like this?
   System.out.print("\t\t" + price);
   System.out.print("\t\t\t" + tax);
   System.out.println();
   }
 

Link to comment
https://linustechtips.com/topic/620120-need-help-with-array-for-loop-in-java/
Share on other sites

Link to post
Share on other sites

no need for this:

int[] total = new int[NUM];

Do this instead:

public int TotalPrice(int[] price, int[] tax, int itemIndex)
{
 int addMeIn = price[itemIndex] * tax[itemIndex];
 int totalPrice = price[itemIndex] + addMeIn;
}

// Or alternatively, do this

class Prices
{
 private int[] Price { get; set ; }
 private int[] Tax { get; set; }
  
 public Prices(int[] userPrice, int[] userTax)
 {
  this.Price = userPrice;
  this.Tax = userTax;
 }
  
 public int GetTotal(int itemIndex)
 {
  int addMeIn = this.Price[itemIndex] * this.Tax[itemIndex];
  return this.Price[itemIndex] + addMeIn;
 }
}

Or better yet, figure out how to group together the items into groups of item types that all share the same tax percentage, and write a class for each of those types with a static tax field and a non static item price field. That way you get even more flexibility. The reason that you don't want to do it your way is because your way has to calculate the totals for each item before you can use any item. If you do it this way, you only have to calculate the total when you actually need it. With large data-sets not pre-calculating the values will save lots of time by avoiding calculating the totals that may not be needed.

Regardless, here's how you do it your way:

public int[] FindAllTotals(int[] price, int[] tax)
{
 int stopMe = price.length;
 int[] total = new int[stopMe];
 if (tax.length != stopMe)
   // Handle your argument error here.
   
 for (int i = 0; i < stopMe; i++)
 {
   int addMeIn = price[i] * tax[i];
   total[i]=price[i] + addMeIn;
 }
 return total;
}

 

ENCRYPTION IS NOT A CRIME

Link to post
Share on other sites

Why are you storing the price of a single item and the tax as a unit of data?

 

Shouldn't the item prices be grouped as an invoice or transaction, and the tax be calculated dynamically based on the sales tax rate for the whole purchase? Why does the user need to input the tax at all? Shouldn't that be built into the system?

 

This is a prime example of where DESIGN is needed before a single line of code is written. It seems like the goal of this program is rather murky, a fact which is reflected in both the OP and the answer above.

 

16 hours ago, straight_stewie said:

 


class Prices
{
 private int[] Price { get; set ; }
 private int[] Tax { get; set; }
  
 public static void SetData(int[] userPrice, int[] userTax)
 {
  this.Price = userPrice;
  this.Tax = userTax;
 }
  
 public int GetTotal(int itemIndex)
 {
  int addMeIn = this.Price[itemIndex] * this.Tax[itemIndex];
  return this.Price[itemIndex] + addMeIn;
 }
}

 

 

This is ugly. There is implicit temporal coupling of the GetTotal to the SetData methods. As a client of this code, I have to know and remember that I must call SetData before I can call GetTotal. If I am simply given a Prices object, I have no way of knowing whether SetData has been called yet or not. I can call it again, but I may overwrite data that was previously set!

 

That's all moot, though, because you cannot modify non-static members from a static method. This code won't even compile. Also, you provide no way to instantiate an instance of the Prices class.

Link to post
Share on other sites

1 hour ago, straight_stewie said:

Set data is the constructor... Sorry for writing that at 2AM my time and accidentally not naming the constructor the same as the class. If you were so great you would have noticed that...

a42.png\

 

This is hysterical. A constructor that isn't named the same thing as the class isn't a constructor. Java also doesn't support static constructors, so even if the naming was right the code would still be wrong and fail to compile. Constructors don't have a return type, either, not even void.

 

As for being 2AM; I regularly go to bed at 2AM and operate on 5 hours of sleep. I don't buy the tired excuse from people.

Link to post
Share on other sites

21 hours ago, SSL said:

Why are you storing the price of a single item and the tax as a unit of data?

 

Shouldn't the item prices be grouped as an invoice or transaction, and the tax be calculated dynamically based on the sales tax rate for the whole purchase? Why does the user need to input the tax at all? Shouldn't that be built into the system?

 

This is a prime example of where DESIGN is needed before a single line of code is written. It seems like the goal of this program is rather murky, a fact which is reflected in both the OP and the answer above.

 

 

This is ugly. There is implicit temporal coupling of the GetTotal to the SetData methods. As a client of this code, I have to know and remember that I must call SetData before I can call GetTotal. If I am simply given a Prices object, I have no way of knowing whether SetData has been called yet or not. I can call it again, but I may overwrite data that was previously set!

 

That's all moot, though, because you cannot modify non-static members from a static method. This code won't even compile. Also, you provide no way to instantiate an instance of the Prices class.

just for a hwk assignment :)

Link to post
Share on other sites

On 7/3/2016 at 0:51 AM, straight_stewie said:

no need for this:


int[] total = new int[NUM];

Do this instead:


public int TotalPrice(int[] price, int[] tax, int itemIndex)
{
 int addMeIn = price[itemIndex] * tax[itemIndex];
 int totalPrice = price[itemIndex] + addMeIn;
}

// Or alternatively, do this

class Prices
{
 private int[] Price { get; set ; }
 private int[] Tax { get; set; }
  
 public Prices(int[] userPrice, int[] userTax)
 {
  this.Price = userPrice;
  this.Tax = userTax;
 }
  
 public int GetTotal(int itemIndex)
 {
  int addMeIn = this.Price[itemIndex] * this.Tax[itemIndex];
  return this.Price[itemIndex] + addMeIn;
 }
}

Or better yet, figure out how to group together the items into groups of item types that all share the same tax percentage, and write a class for each of those types with a static tax field and a non static item price field. That way you get even more flexibility. The reason that you don't want to do it your way is because your way has to calculate the totals for each item before you can use any item. If you do it this way, you only have to calculate the total when you actually need it. With large data-sets not pre-calculating the values will save lots of time by avoiding calculating the totals that may not be needed.

Regardless, here's how you do it your way:


public int[] FindAllTotals(int[] price, int[] tax)
{
 int stopMe = price.length;
 int[] total = new int[stopMe];
 if (tax.length != stopMe)
   // Handle your argument error here.
   
 for (int i = 0; i < stopMe; i++)
 {
   int addMeIn = price[i] * tax[i];
   total[i]=price[i] + addMeIn;
 }
 return total;
}

 

thanks for your help, I ended up using a similar thing which did the trick:

 

public class ProjectArray{
   public static void main(String[] args){
   Scanner input = new Scanner(System.in);
   final int NUM = 2;  
   
   int[] tax = new int[NUM];
   int[] price = new int[NUM];
   int[] total = new int[NUM];
   
   for (int index = 0; index < tax.length; index++){
   System.out.println("Enter amount " + (index+1) + ": ");
   tax[index] = input.nextInt();
   System.out.println("Enter price " + (index+1) + ": ");
   price[index] = input.nextInt();
   total[index]=price[index]+tax[index]; /* this is what I was looking for :) */
   }
   System.out.println();
   System.out.println("total\t\tprice\t\tamount");
   System.out.println("-----\t\t-----\t\t------");
   for ( int i = 0; i < total.length; i++){
   System.out.print("" + total);
   System.out.print("\t\t" + price);
   System.out.print("\t\t\t" + tax);
   System.out.println();
   }
   }
}

Link to post
Share on other sites

6 minutes ago, SSL said:

 

That explains nothing about the specification of the program.

sorry, I didn't go into greater detail. I figured it out shortly after posting this, but I appreciate everyone's effort. It had to be done using only one dimensional arrays.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×