Reputation: 11
i want to be able to correlate these two arrays so that when i start with user input i can access the name and the price
public class fastFood
{
public static void main (String[]args)
{
//array for prices and order
double[] prices= new double[]{"5.79", "6.79", "4.59", "5.39", "6.59", "7.29", "6.09", "5.69"};
String[] menu= new String[8];
menu[0]="Whopper Meal";
menu[1]="Double Whopper Meal";
menu[2]="Chipotle Whopper Meal";
menu[3]="Whopper Jr. Meal";
menu[4]="BK Double Stacker Meal";
menu[5]="Premium Chicken Sandwhich (Crispy or Grilled) Meal";
menu[6]="Chipotle Chicken Sandwhich (Crispy or Grilled) Meal";
menu[7]="OriginalChicken Sandwich Meal";
}
}
Upvotes: 1
Views: 475
Reputation: 213311
Few mistakes in your code:
You are storing strings in a double array? (I don't know why. That wouldn't even compile.)
Never ever store price as double or float (a blunder). float
and double
don't have enough precision to store all the floating point numbers accurately. For e.g., 0.1 can not be stored accurately in double
or float
. With monetory value, you can't take that fact lightly. You should use BigDecimal instead which is arbitrary-precision signed decimal number.
Instead of maintaining two different arrays, you should maintain a Map<String, BigDecimal>
mapping from items to their prices.
Then to further refactor your code, you should create a class Item
storing all those attributes, like this:
class Item {
private String name;
private BigDecimal price;
// constructors, getters, setters
}
and then, maintain a list of items:
List<Item> items = new ArrayList<Item>();
See also:
Upvotes: 17
Reputation: 2653
While I agree that this is a poor way of implementing this, you could easily correlate these by accessing them with the same index. As long as they were entered in the correct order, this will work.
double price = prices[2];
String menuItem = menu[2];
System.out.println(String.format("%s costs $%.2d",menuItem, price);
This should print "Chipotle Whopper Meal costs $4.59".
Depending on what you are doing this for, you really should look into creating objects to represent these menu items. Example:
public class MenuItem
{
public double price;
public String name;
public MenuItem(String name, double price)
{
this.name = name;
this.price = price;
}
@Override
public String toString()
{
return String.format("name: %s cost: $%.2d",name,cost);
}
}
Now in your code, you could do the following:
List<MenuItem> menuItems = Arrays.asList(
new MenuItem("Big Mac",4.23);
new MenuItem("Quater Pounder",3.23);
new MenuItem("Chicken Sandwich",5.63);
);
for(MenuItem m : menuItems)
{
System.out.println(m.toString());
}
This will print out every item in menuItems. The name and price are automatically correlated since they are contained together as attributes in each instance of a MenuItem object.
Upvotes: 1
Reputation: 62864
You can use java.util.HashMap
(or some other java.util.Map
implementation).
Map<String, Double> menu = new HashMap<String, Double>();
menu.put("Whopper Meal", 5.79d);
//..and so on.
Upvotes: 3