wood burning stoves 2.0*
The moose likes Java in General and the fly likes RPG'er lost in the sea of Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "RPG Watch "RPG New topic
Author

RPG'er lost in the sea of Java

Mikealan Smith
Greenhorn

Joined: Aug 29, 2003
Posts: 15
HI,
I'm new to java and have just started my 2nd semester java class, however its been about a year since i had java 1. i'm a little stuck and confused and am hoping someone can help me out. i'm going to try and post my question without posting code,
i have several class's Insurance, Health Insurance, Life insurance, Auto Insurance, Insurance INfo, Track Insurance and Customer.
Track insurance is my main class and preloads some data into ArrayList as such
ArrayList ins = new ArrayList();
ArrayList cust = new ArrayList();
Customer c = new Customer("Duck", "Donald");
Customer c1 = new Customer("Mouse", "Minnie");
Customer c2 = new Customer("Mouse", "Mickey");
cust.add(c2);
cust.add(c1);
cust.add(c );

ins.add(new AutoInsurance(c,2));
ins.add(new AutoInsurance(c1,3));
ins.add(new HealthInsurance(c,5));
ins.add(new HealthInsurance(c2,1));
ins.add(new LifeInsurance(c,30000,65));
ins.add(new LifeInsurance(c1,400000,34));

The modifications i need to make are in the Insurance Info class.
I need to add an insurance policy for a particular customer.
I need to ask for the customer, which i've done, but then i need to determine if this customer is in my ArrayList(this is where i'm stuck)
before i add the insurance policy.
The only thing being passed to this method is the Insurance arraylist ins.
I can't figure out how to do this.
I hope this is enough for someone to point me in the right direction.
If you need more info, please ask.
Thanks
Mike
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi MIke,
Welcome to Java Ranch!
One simple solution, neither especially elegant nor especially complex:
I assume AutoInsurance, LifeInsurance, etc, all extend some Insurace base class. This base class ought to have a method "getCustomer()". Similarly, the Customer class presumably has a getName() method (or getLastName(), getFirstName(), or similar).
Then you just need to iterator over the ArrayList, something like (I'm sure there are details I'm leaving out but I hope you get the idea)

Now, one big improvement would be to give Customer an equals() method. Then you could create a Customer from firstName, lastName and just call equals() on "c" inside the loop.


[Jess in Action][AskingGoodQuestions]
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
To piggyback on what Ernest said, if you implement the equals() method, you could do something like this:

Assuming you have properly implemented equals(), this will tell you whether or not a customer by the name of "Donald Duck" is already in your List.
Of course if you implementequals() you also really should be implementing hashCode(). For details on implementing these methods I would refer you to the indespensible Effective Java by Joshua Bloch, or Vipan Singla's article on Implementing equals() and hashCode().
[ August 29, 2003: Message edited by: Jason Menard ]
Mikealan Smith
Greenhorn

Joined: Aug 29, 2003
Posts: 15
Ernest, Jason
Thanks for the replies.

a couple comments and a question?
First, the code you posted appears to work and is actually very nearly identical to code i'm using in another method for printing. I guess where i'm confused and the reason i didn't use it for my ADD Insurance method is, that I would think i should be able to check to see if the Customer exists, without involving the Insurance Arraylist. What am i missing?
I have a CompareTO Method in my Customer Class, is that probably the same thing but with a different name to the EQUALS method you both mentioned.
Thanks again for the help.
i'm an old RPG programmer and this OO just hurts my head LOL.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
First let me apologize for not correctly reading your first message. I see now that you aren't simply looking up a customer in the "customer" List, but in the insurance List.
I would think i should be able to check to see if the Customer exists, without involving the Insurance Arraylist. What am I missing?
I'm not sure I understand how you get your Customer that you are trying to determine whether or not they already have insurance. Is this "new" Customer already in the Customer List? If not, depending on the rest of your program, you might be able to make the assumption that any Customer in the Customer List has Insurance, and is therefore in the Insurance List. If that is the case and you have access to the Customer List, then you can simply see if the Customer List contains() your new customer.
Mikealan Smith
Greenhorn

Joined: Aug 29, 2003
Posts: 15
LOL,
I guess maybe i should have posted all the code to start with, but i didn't want you to say, put this here, this here, kinda of thing.
i'll back up with some premises.
First i have intial data loaded, no big deal there just loaded for testing
i have an menu option to add a customer. Using console class to ask for a customer first name and last.
what i'm trying to accomplish in this instance is to add an insurance policy for an existing customer.
so these i think should be my steps.
ask for customer name- First and Last
Does Customer exist
Yes- then add insurance
No - then notify Customer doesn't exist and ask if user wants to add Customer, then add Insurance.
My stub for this was
public void addPolicy(ArrayList ins) // your write
{}
and this is where it is now.
public void addPolicy(ArrayList ins) // your write
{
int number = Console.readInt("What is the customer number?");
boolean found = false;
for (int i=0;i<ins.size();i++)
{
Insurance insure = (Insurance) ins.get(i);
int num = insure.getCustomer().getId();
if (number==num)
{

found=true;
}


}
if (!found)
System.out.println("That customer ID does not exist. Try 8 from the menu to find value numbers");


}
so where i have this--int num = insure.getCustomer().getId();
why can't i just do int num = cust. getCustomer().getId();
or something like that.
i see where what was posted works, and maybe I should just accept that, but i'm just trying to figure this out so it makes sense to me.
Thanks again
Java- Not your fathers Procedural language.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Originally posted by Mikealan Smith:
i'm an old RPG programmer and this OO just hurts my head LOL.

You're obviously a masochist. Personally, I have stowed any memories of my brief but traumatic encounter with RPG in the deepest recesses of my brain.
compareTo, which should return an int, is not the same as equals, which should return a boolean. contains() calls equals() not compareTo().


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RPG'er lost in the sea of Java