473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

arraylist

I'm a beginner in programming world...
Currently, I coding a simple food ordering simulation.
It is something like this:-

Qty:1 Foodname:Fried rice
Qty:1 Foodname:Noodle
Qty:1 Foodname:Fried rice

Just wanna ask, is there anyway to make the order list become:-

Qty:2 Foodname:Fried rice
Qty:1 Foodname:Noodle

Which mean everything I key in a new food it will trace the list. If the food is exist, then the quantity will increase by 1.
Aug 3 '10 #1

✓ answered by Christian Binder

You won't be able to do this with a Dictionary.
You have to use another data-structure than Dictionary.

Maybe take a List of FoodOrders.
So you create a class FoodOrder which consists of a string-variable for the foodname and an integer for the count of orders.

Every time you have to insert a food order, you go through you whole list and search for a FoodOrder-item which has the same name as the food you want to insert and which has available space for another order, e.g. the qty is less than three.
Give it a try :-)

3 1532
Christian Binder
218 Expert 100+
I would suggest, using a Dictionary.
In a Dictionary you can store keys (unique) and values.

E.g. you have a Dictionary where the Key is the foodname (type string) and the value is quantity (type int)

Expand|Select|Wrap|Line Numbers
  1. Dictionary<string, int> _foodOrders = new Dictionary<string, int>();
  2.  
After you've declared and defined it, you can start to add values (_foodOrders.Add(key, value)) to your dictionary.
But if you add a key more than once, you'll get an Exception, because it's not allowed to store two same keys.

So you've got to check out, if the key is already existing, and if it is, you only increase it's quantity.

There is a dictionary's method ContainsKey() to check if the key is already existing. You can access the dictionary's values with _foodOrders[key] like _foodOrders["Noodle"].
Aug 3 '10 #2
Is it possible that it create another same food name if the quantity limit is reach.

Eg. Limit is set to 3 units only.

Qty:3 Foodname:Fried rice
Qty:1 Foodname:Noodle

If user enter Fried rice again, the order list will become:-

Qty:3 Foodname:Fried rice
Qty:1 Foodname:Noodle
Qty:1 Foodname:Fried rice
Aug 3 '10 #3
Christian Binder
218 Expert 100+
You won't be able to do this with a Dictionary.
You have to use another data-structure than Dictionary.

Maybe take a List of FoodOrders.
So you create a class FoodOrder which consists of a string-variable for the foodname and an integer for the count of orders.

Every time you have to insert a food order, you go through you whole list and search for a FoodOrder-item which has the same name as the food you want to insert and which has available space for another order, e.g. the qty is less than three.
Give it a try :-)
Aug 3 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a...
3
by: george r smith | last post by:
I am trying to create an arrayList that contains multiple arrayLists. My code attempt is below. The question I have is how can I get away from creating another pAttribute list than can be added to...
3
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
6
by: gane kol | last post by:
Hi, I have a code that creates a datatable from an arraylist, but i am getting an error in casting in for (int intRow = 0; intRow < alLCPlist.Count; intRow++) { DataRow drow =...
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello My pgm1 (User Interface Level) passes an empty ArrayList to pgm2 (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate the ArrayList. Question1: When pgm2 gets...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.