aspose file tools*
The moose likes Beginning Java and the fly likes Creating an Array list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating an Array list" Watch "Creating an Array list" New topic
Author

Creating an Array list

Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27
I'm working on a final project for my Java class in school and need some help creating an Array list. Here is my assignment:

The perfect party catering company hosts events for clients. Create an application that accepts an event number, the event host's last name and the month, day and year values representing the event date. The application should also accept the number of guests who will attend the event and a numeric meal code that represents the entree.

As each client's data is entered, verify the meal code for validity, if not valid, contiune to prompt the user until it is. The valid meal codes are shown below:

Code Entree Price per Person ($)
1 Rost Beef 24.50
2 Salmon 19.00
3 Linguine 16.50
4 Chicken 18.00


Design the logic for an application that outputs each event number, host name, date, meal code, entree name, number of guests, gross total price for the party and price for the party after discount. The gross total price for the party is the meal price per guest times the number of guests. The final price includes a discount based on the accompanying table.

number of guest discount
1-25 0
26-50 75
51-100 125
101-250 200
251 and over 300


Here is the code I've done so far, I know it's rough but I'm not sure quite if I got it 100% correct up to this point or what's next:


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
No, that looks nothing like 100% right. You have a List<Event> but you are not filling it with Events. You are filling it with Strings. The compiler won’t be happy about that. It won’t be happy about some other things.
Before you can create a List<Event>, you need an Event class. All the inputs like number of guests are used as attributes of the Event class, Create an Event class, with its fields, constructor and a few methods. You might do well to create a Menu class or similar for the entrées. Forget all about the List for the time being. Get one object of the Event class working first, then you can consider the List later.
Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27
Campbell Ritchie wrote:No, that looks nothing like 100% right. You have a List<Event> but you are not filling it with Events. You are filling it with Strings. The compiler won’t be happy about that. It won’t be happy about some other things.
Before you can create a List<Event>, you need an Event class. All the inputs like number of guests are used as attributes of the Event class, Create an Event class, with its fields, constructor and a few methods. You might do well to create a Menu class or similar for the entrées. Forget all about the List for the time being. Get one object of the Event class working first, then you can consider the List later.


I don't know what methods need to be done in the Event class but I've already created an Event class with instance variables set to private and a constructor. If anyone can help me at all create a loop (not sure what I'm doing wrong) and/or add methods in the Event class, that would be greatly appreciated.
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 75

Right now your adding Strings to the arraylist rather than Event. The ArrayList you created takes in Event objects.

So it'd be something like:


Also we can't comment on your event class without seeing what you have.


Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27
Aj Prieto wrote:Right now your adding Strings to the arraylist rather than Event. The ArrayList you created takes in Event objects.

So it'd be something like:


Also we can't comment on your event class without seeing what you have.


Ok I'm a bit lost... Here is my Event class:

Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 75

On what part are you lost?

First you have to create an instance/object with the constructor of the Event class. Then you add that object to your ArrayList.
It looks like you need accessor methods for your loop.

The current loop you have prints out the index of the loop rather than the month.
Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27
Aj Prieto wrote:On what part are you lost?

First you have to create an instance/object with the constructor of the Event class. Then you add that object to your ArrayList.


Didn't I do that??? It compiles and runs correctly. Oh and I need the mealCode under that loop not month, my original question mentioned that, though I typed in month instead of mealCode in my coding.
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 75

No, you have a constructor that can create the object. At least in the code you've posted, I do not see a line in there that creates an object.

Are you sure it compiles and runs correctly? I don't recall seeing an indexOf method in the ArrayList class that takes in more than one parameter.
If there is, please show me as I haven't seen it before.
Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27
Jackson Lawton wrote:
Aj Prieto wrote:On what part are you lost?

First you have to create an instance/object with the constructor of the Event class. Then you add that object to your ArrayList.


Didn't I do that??? It compiles and runs correctly. Oh and I need the mealCode under that loop not month, my original question mentioned that, though I typed in month instead of mealCode in my coding.


Hmm, I typed in the original CateringCompany coding wrong... Here is the correct coding minus the loop:



Don't know where I got that other coding.


Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27

I seem not to be getting anywhere... Here is what I have now in the CateringCompany class but I'm not sure how to end the while loop.


If anyone can help me with the while loop, that would be greatly appreciated. I need the loop to not only say invalid for meal codes greater than 5 but for also less than 1 and I also need to prompt the user to type in a valid meal code, which is anything 1-5.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1031
    
    5

I would like to suggest writing a static method on the Event class to validate the meal code:

I will leave you with the implementation of this method which you can then use in your while loop:
Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27
James Boswell wrote:I would like to suggest writing a static method on the Event class to validate the meal code:

I will leave you with the implementation of this method which you can then use in your while loop:


That's a big help... Thanks, now all I have to do is putting the proper coding under the public static boolean isMealCodeValid(int mealCode) in the Event class. I believe I can figure that part out. Thanks again.
Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27
Ok here is what I have so far...

CateringCompany class:

Event class:

I'm not sure how to code the discounts in or what else may need to be added to get the program running par the requirments, so if anyone could help me figure any part of the program out or if there is anything wrong with what I've coded so far please let me know.
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 75

Your do-while loop could be shortened to:


For discouts, it looks like you have to associate a price for each meal code and multiply that by the amount of people that order it. With that number, you subtract the discounted amount.
Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27
Aj Prieto wrote:Your do-while loop could be shortened to:


For discouts, it looks like you have to associate a price for each meal code and multiply that by the amount of people that order it. With that number, you subtract the discounted amount.

Thanks... I've created a new class called Meal where I put the meals and their prices in, so I probably have to set the discounts in there some how.

If anyone would be willing to check over my code and help me add the discounts in as well as lable things that would be greatly appreciated. Also I'm stuck on how to add this new class to the main class (CateringCompany) so it will print everything out correctly.
Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27
Look at my message above!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
No, that class bears very little resemblance to a Meal class, I am afraid. It ought to have fields for meal type (and the names of the meals are as near as you will get at present) and price.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating an Array list