-1

I want to generate a new name of each new array that are created.

I have a SellAbleItem class, with a constructor, which takes the following:

  SellAbleItems Vodka = new Brunch("Brunch", 199);
  SellAbleItems Brunch = new Brunch("Vodka", 99);

I have this array list

   ArrayList<SellAbleItems> order = new ArrayList<SellAbleItems>();

And I put these inside my order array list

   order.add(Vodka);
   order.add(Brunch);

So, my question goes on how to generate at new name for the order array list? I would like to have orders, to be called like order1, order2, order3 and so on. And at the end, able to print these names.

  • Use a `List>`, or a `Map>`. The advantage of the map is, that you can actually choose a name (which has to be unique) – XtremeBaumer Mar 27 '18 at 08:53
  • But I don't know how many orders will be made. It could be 10 or 20. – Peter Holdensgaard Mar 27 '18 at 08:55
  • Describe what you actually want. Avoid XY problem. – user202729 Mar 27 '18 at 08:55
  • `Map` has a generic length. You can always add new elements – XtremeBaumer Mar 27 '18 at 08:55
  • [See this](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables). (this is for Python, but tl;dr, use a data structur) – user202729 Mar 27 '18 at 08:55
  • Objects don't have names. What is your outer problem? – user207421 Mar 27 '18 at 08:57
  • Create a `Order` class that encapsulate the name and the list of items. In future, you can add more to this – Thiyagu Mar 27 '18 at 08:57
  • [Possible duplicate](https://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java). [Alternative](https://stackoverflow.com/questions/19336202/how-to-create-variables-dynamically-in-java). [Alternative](https://stackoverflow.com/questions/23038215/how-would-i-define-an-infinite-number-of-variables-java). – user202729 Mar 27 '18 at 09:01

3 Answers3

0

You don't need to know how many orders a user creates before you create your map but you will need to know when a user creates a new Order. Once you know that you can add a Map to hold your orders like this:

// Program initiates
Map<String, ArrayList<SellAbleItems>> orders = new HashMap<>();

// User places order 1
List<SellAbleItems> order1 = new ArrayList<>();
order1.put(Vodka);
order1.put(Brunch);
orders.put("order1", order1);

// Some time later user places order 2
List<SellAbleItems> order2 = new ArrayList<>();
order2.put(Foo);
order2.put(Bar);
orders.put("order2", order2);

You can then loop through the Map as described here: Iterate through a HashMap

A better abstraction would be to create an Order class that has a List to which you can Add your SellAbleItem and then you can keep a List<Order> instead but that would be a different question.

span
  • 5,405
  • 9
  • 57
  • 115
  • That could be. But when the user interacts with my system, i don't know how many orders that would be made. So I need some kind of for loop in order make unique names like order1, order2, order 3 and so on. – Peter Holdensgaard Mar 27 '18 at 08:57
  • If he want to iterate through all the order could do orders.keySet() get all the keys and iterate through all the names with a loop? – farmlandbee Mar 27 '18 at 09:00
  • @PeterHoldensgaard You don't need to know the amount of orders. Any time the user creates a new order you create a new List and add it to the map. You can use `orders.size()` to figure out how many orders you have and you can use that index to name your next entry. – span Mar 27 '18 at 09:03
  • @LewisJefferies yes, it is described here: https://stackoverflow.com/questions/1066589/iterate-through-a-hashmap – span Mar 27 '18 at 09:04
0

You don't have to give names, just add new Brunch directly

order.add(new Brunch("Brunch", 199));
order.add(new Brunch("Vodka", 99));
order.add(new Brunch("Whisky", 1199));
Guy
  • 46,488
  • 10
  • 44
  • 88
0

If you want go list all the keys, then this will work.

Map<String, ArrayList<SellAbleItems>> orders = new HashMap<>();
orders.put("orderA", order1);
orders.put("orderB", order2);

Set<String> keys = orders.keySet();
for(int i = 0; i < orders.size(); i++) {
  System.out.println(keys.toArray()[i]);
}

Prints:

orderA
orderB
farmlandbee
  • 345
  • 1
  • 3
  • 14