Solved

Collection Examplw

Posted on 2004-10-27
193 Views
Last Modified: 2010-03-31
Hi,

Would like an example of a collection class

Cheers
0
Question by:Cyart
    7 Comments
     
    LVL 86

    Accepted Solution

    by:
    Here's one that (just for instance ;-)) models a Bank having a collection of Customer

    import java.util.*;

    public class Bank {
          private Map customers;
          // need other attributes pertaining to the bank itself
          
          public Bank() {
                customers = new HashMap();
          }
          
          public void addCustomer(Customer c) {
                customers.put(c.getId(), c);
          }
          
          public Customer getCustomer(Integer id) {
                return (Customer)customers.get(id);
          }
          
          // need a remove, getCustomers too
          
          public String toString() {
                return customers.toString();
          }

    }
    0
     

    Author Comment

    by:Cyart
    Have you got a basic main to test this with,just so I can see it working
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Well, first you need to create a Customer class then that's fairly trivial. Do that first. Clue: the relationship between Bank/Customer is the same as Customer/Account
    0
     
    LVL 8

    Assisted Solution

    by:kiranhk
    check out the collections examples
    http://javaalmanac.com/egs/java.util/pkg.html
    0
     
    LVL 92

    Assisted Solution

    by:objects
    0
     
    LVL 8

    Expert Comment

    by:kiranhk
    did you find any of the suggestions useful?? havent heard from you for a long time
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Prepare to Pass the CompTIA A+ 900 Series Exam

    CompTIA aims to adapt its A+ Certification to reflect the most current knowledge and skills needed by today's IT professionals--and this year's 2016 exam is harder than ever. This certification is one of the most highly-respected and sought after in IT.

    Suggested Solutions

    Title # Comments Views Activity
    haveThree challenge 22 81
    NotAlone Challenge 20 56
    bunnyEars2 challenge 6 27
    Updating Java 9 44
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now