Newbie programmer here in need of help??? 10 points!!! pleeeaaassse?
-
I created a console application called FastFood in visual studio 2005. My goal here is to create burgers and consume burgers in a randomized way. It will run like this for 20 times. Here's my code: using System; using System.Collections.Generic; using System.Text; namespace FastFood { interface Actor { void act();} } class TooManyPeopleException : Exception { public TooManyPeopleException(string message) : base (message) { } } class BurgerConsumingException : Exception { BurgerConsumingException(string warning) : base(warning) { } class Person : Actor { public string name; public Person(string name) { this.name = name; } public String getName() { return this.name; } public void act() { Console.WriteLine("This should not appear in the program."); } } class Worker : Person { McDonalds job; public Worker(string name, McDonalds job): base(name) { this.name = name; this.job = job; } new public void act() { job.Create(); } class Consumer : Person { McDonalds restaurant; public Consumer(string name, McDonalds restaurant): base(name) { this.name = name; this.restaurant = restaurant; } new public void act() { try { restaurant.Consume(); } catch(BurgerConsumingException) { Console.WriteLine("no more burgers left."); } } } class McDonalds { private int numBurgers = 35; public List<Person> Customers; private const int maxSize = 20; private static Random randomGenerator = new Random(); public McDonalds() { Customers = new List<Person>(); Customers.ToString(); } public void add(Person p) { if (Customers.Count <= McDonalds.maxSize) { Customers.Add(p); } else { try { throw new TooManyPeopleException("too many people"); } catch (TooManyPeopleException v) { Console.WriteLine("Too many people in Mcdonalds"); } } } public void remove(Person p) { } public void Consume() { numBurgers--; } public void Create() { numBurgers++; } public Person getRandomPerson() { int i = randomGenerator.Next(Customers.Count+1); Console.WriteLine("Number generated: " + i); Person p = (Person) this.Customers[i]; Console.WriteLine(p.getName()); return p; } } } class Program { static void Main(string[] args) { Program prog = new Program(); prog.DoThis(); } public void DoThis() { McDonalds mcdo = new McDonalds(); Worker emp1 = new Worker("emp1", mcdo); Worker emp2 = new Worker("emp1", mcdo); Worker emp3 = new Worker("emp3", mcdo); Worker emp4 = new Worker("emp4", mcdo); Worker emp5 = new Worker("emp5", mcdo); Consumer buyer = new Consumer("buyer1", mcdo); Consumer buyer1 = new Consumer("buyer2", mcdo); Consumer buyer2 = new Consumer("buyer3", mcdo); Consumer buyer3 = new Consumer("buyer4", mcdo); Consumer buyer4 = new Consumer("buyer5", mcdo); Consumer buyer5 = new Consumer("buyer6", mcdo); mcdo.add(emp1); mcdo.add(emp2); mcdo.add(emp3); mcdo.add(emp4); mcdo.add(emp5); mcdo.add(buyer); mcdo.add(buyer1); mcdo.add(buyer2); mcdo.add(buyer3); mcdo.add(buyer4); mcdo.add9buyer5); //runs simulation 20 times for (int a = 1; a <= 20; a++) { Program program = new Program(); program.simulation(); } } public void simulation() { McDonalds mcdo = new McDonalds(); //Get a random person from Mcdo //And execute act //And see how many times you can catch the excpetion when there are no more burgers mcdo.getRandomPerson(); mcdo.Consume() mcdo.Create(); Console.ReadKey(); } } now my problem is this: whenever I try to run my program, it wont debug and the line "Person p = (Person)this.Cusotmers[i];" will be highlighted in yellow with the warning "ArgumentOutOfRangeException was unhandled." How can I run it the way its supposed to with putting an exception? I want to see how long it will run the simulation. I'm just a newbie programmer(only three months in programming). Please help!
-
Answer:
I'm not fully sure what you are trying to do, but I can tell you the reason for this error. In the DoThis procedure, you are creating mcdo and assigning it a list of employees. Then you are creating a new Program() and calling that program's simulation(). Inside simulation() you are creating a new mcdo, that has not had any people assigned to it. So when call GetRandomPerson, no one is in the list to get. The list length is 0, and you are trying to get something that does not exist. One easy way to fix this would be to take the Worker emp1 = new Worker("emp1", mcdo); ... mcdo.add(buyer5); and place it inside the simulation function. That way as you loop though and keep calling it, you will be populating the data for each. There are other ways to handle it as well, but this is the quickest based upon your existing code. BTW: The next issue you will run into is in the getRandomPerson int i = randomGenerator.Next(Customers.Count+1); it should probably be int i = randomGenerator.Next(Customers.Count); since the Customer[] array will start with 0, making the first item in the list [0] and the 11th item [10] Hope this helps.
Michiko at Yahoo! Answers Visit the source
Related Q & A:
- 10 Points!! Can You Please Help?Best solution by Yahoo! Answers
- Altering my business card, What should I change to make it better? (In exchange for 10 points B/A Thank you?
- Can you find this Austrian castle? 10 points to whoever finds it?Best solution by Yahoo! Answers
- Teen halloween party ideas?!? 10 points?Best solution by Yahoo! Answers
- Is it okay to apply online for multiple positions with the same company? Easy 10 points?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.