-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.

8
  • Use a List<List<SellAbleItems>>, or a Map<String, List<SellAbleItems>>. The advantage of the map is, that you can actually choose a name (which has to be unique) Commented Mar 27, 2018 at 8:53
  • But I don't know how many orders will be made. It could be 10 or 20. Commented Mar 27, 2018 at 8:55
  • Describe what you actually want. Avoid XY problem. Commented Mar 27, 2018 at 8:55
  • Map has a generic length. You can always add new elements Commented Mar 27, 2018 at 8:55
  • See this. (this is for Python, but tl;dr, use a data structur) Commented Mar 27, 2018 at 8:55

3 Answers 3

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));
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

4 Comments

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.
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?
@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.
@LewisJefferies yes, it is described here: stackoverflow.com/questions/1066589/iterate-through-a-hashmap

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.