Results 1 to 2 of 2
  1. #1
    rune is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default Class object with pointers to other classobjects

    Hello

    Im just starting out to learn java. Im trying to make a simple program to learn some principles. It will have 3 classes. Two classes will contain objects.
    Now i want to store pointers to objects of one class to objects of another class, like a simple database i suppose. I posting some code Ive made could one of you look at the code and tell me if this is an ok way about it or not:

    Java Code:
    class TestBank{
    	         public static void main(String args[]){
    
    	         Customer[] customers = new Customer[5];
    	         customers[0] = new Customer();
    	         customers[0].name = "Rune";
    	         customers[0].age =50;
    
    
                     Bank s1 = new Bank();
    	         s1.customerName = customers[0];
    	         System.out.println(s1.customerName.name);
    		}
    	}
    
            class Customer{
    	          String name;
    	          int age;
    
    	          Customer (String n, int a){
    		  name = n;
    		  age = a;
    }
    
    	Customer(){
    		   String navn = "none";
    		    int age = 0;
    		}
    
    	void printout(){
    		    System.out.println("name: " + name + " age: " + age);
    			}
    }
    
             class Bank{
    	            Customer customerName;
    	             int customerId;
    }
    Im mainly asking about this part :
    Java Code:
    Bank s1 = new Bank();
    	         s1.customerName = customers[0];
    	         System.out.println(s1.customerName.name);
    is this an extremely clumsy way about it(at least it looks like it works to me), or is there another better way to do this(a simple way keep in mind please), Im also considering to use this method for a program with more object classes. Also Ive been googling quite a bit to find out about this without success, so if anyone can point me to a beginners guide that deals with this problem that would be great.
    Thanks for your time.

    Regards Rune

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    This is probably how I would do it:

    Java Code:
    class TestBank{
    	         public static void main(String args[]){
    
    	         Customer[] customers = new Customer[5];
    	         customers[0] = new Customer();
    	         customers[0].setName("Rune");
    	         customers[0].setAge(50);
    
    
                     Bank s1 = new Bank();
    	         s1.addCustomer(customer[0]);
    	         System.out.println(s1.getCustomerName());
    		}
    	}
    
            class Customer{
    	          private String name;
    	          private int age;
                      private static int classId=0;
                      private int id;
    
    	          Customer (String n, int a){
    		  name = n;
    		  age = a;
                      id=classId;
                      classId++;
    }
    
    	Customer(){
    		   name = "none";
                       id=classId;
                       classId++;  
    		}
    
    	void printout(){
    		    System.out.println("name: " + name + " age: " + age+" id "+id);
    			}
    
                    void setName(String name){
                         this.name=name;
                     }
                    void setAge(int age){
                          this.age=age;
                     } 
                    int getId(){
                        return id;
                     }
                   
                    String getName(){
                           return name;
                     }
                    
                    int getAge(){
                          return age;
                        }
    }
    
             class Bank{
                        // I would use a collection instead of an array here
    	            private Customer[] customers=new Customer[10];
    
                        private int index=0;
    
                         public void addCustomer(Customer cust){
                                 customers[index++]=cust;
                          }   
                         public String getCustomerName(int id){
                         for(int i=0;i<customers.length;i++){
                              if(customers[i].getId()==id){
                                   return customers[i].getName();
                              }
                           }
                            return null;
                         }
    }
    If you have trouble understanding visit these

    helpful tutorial Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

    collections Trail: Collections (The Java™ Tutorials)

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  2. object new and pointers error
    By hydride in forum New To Java
    Replies: 2
    Last Post: 04-06-2010, 07:15 PM
  3. Replies: 3
    Last Post: 03-15-2010, 09:44 PM
  4. Pointers
    By ravian in forum New To Java
    Replies: 5
    Last Post: 11-28-2007, 02:49 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •