Results 1 to 6 of 6
  1. #1
    JamieEdwards is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Making more than one version of the same class?

    Hi,

    This is my first thread on the forums so hi :)

    I've been looking for something like this all over the internet and cannot find anything so I've come here.

    Now I'm trying to make an sort of Movie Store database type thing where you're able to rent and return film's and such things, and even have a customer bit too.

    The part I'm stuck on is this, when I try to make a new customer, I have to put this for example:
    Java Code:
    Customer c1 = new Customer();
    
    // .. Get user input for customer 1..
    
    // Add that information to the Customer class variables:
    c1.addCustomer ( first, second, address );
    // etc...
    That's all well and good, but if I want to add new customer after "c1" I have to do:
    Java Code:
    Customer c2 = new Customer();
    My question is this, how would I make more than one "instance" of the Customer class? Say I add a new customer then it'd look like this:
    Java Code:
    first customer c[1];
    second customer c[2];
    Pretty much I want the number in the [] to increment up...


    Also as a side thing, if I wanted delete customer 2 ( c[2] ), how would I make it decrement and the next time I add a new customer, the new customer then becomes "c[2]"? This probably can't be done or is waaaay out of my scope but it'd be nice to know too :)

    Thanks.

    Jamie.

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    291
    Rep Power
    4

    Default Re: Making more than one version of the same class?

    My question is this, how would I make more than one "instance" of the Customer class?
    well, you did..
    You make Customer c1 = new Customer(); and Customer c2 = new Customer().
    c1 and c2 are both instances of one class..

  3. #3
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    291
    Rep Power
    4

    Default Re: Making more than one version of the same class?

    Have you thought of making list of customers, something like:
    Java Code:
    ArrayList <Customer> c = new ArrayList();
    then you can add or delete customers from list..

    Java Code:
    c.add(addNewCustomer());... // something like this
    Last edited by milovan; 10-12-2013 at 05:14 PM.

  4. #4
    JamieEdwards is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: Making more than one version of the same class?

    Quote Originally Posted by milovan View Post
    well, you did..
    You make Customer c1 = new Customer(); and Customer c2 = new Customer().
    c1 and c2 are both instances of one class..
    I want to be able to do it automatically...

  5. #5
    JamieEdwards is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: Making more than one version of the same class?

    Quote Originally Posted by milovan View Post
    Have you thought of making list of customers, something like:
    Java Code:
    ArrayList <Customer> c = new ArrayList();
    then you can add or delete customers from list..

    Java Code:
    c.add(addNewCustomer());... // something like this
    That doesn't seem to want to work :L Here's what I have at the moment...
    Java Code:
    ArrayList < Customer > c = new ArrayList();
    ... // Do while loop for this sections menu.
    
    ...
    if ( option == 1 ) { 
    				// Clear the buffer...
    				buf = buffer.nextLine();
    				
    				out.print ( "First name: " ); firstname = buffer.nextLine();
    				out.print ( "Last name: " ); lastname = buffer.nextLine();
    				out.print ( "Address: " ); address = buffer.nextLine();
    				
    				c.add ( addCustomer ( firstname, lastname, address ) );
    			}
    and with that I'm getting the following error:
    "The method addCustomer(String, String, String) is undefined for the type Menu"

    Despite having it and being able to reference it before?

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    291
    Rep Power
    4

    Default Re: Making more than one version of the same class?

    Well, I don't know how you want your code to work, I just suggested an approach..
    Idea is to have an array of Customers in your main class, where you can manipulate with it.
    Then for populating this array you can use a method which will do all work about creating Customer and will return an instance to the array, something like:

    Java Code:
    public Customer addNewCustomer(){
        Customer c = new Customer();
        ...
        // here you can enter all data about the customer in the way you prefer (I guess its Scanner in your case).
        ...
        return c;
    }
    For this you will need your Customer class that will hold all necessary data... This should not be a problem for you.

    Now in your main class you can add a new customer to the array:

    Java Code:
    ...
    // create an array to hold customers...
    ArrayList <Customer> c = new ArrayList();
    ...
    // add new element in array with described method
    c.add(addNewCustomer());
    I guess this is a little confusing if you want just to enter this piece in your existing code. It probably won't work. In this case you should rather start form beginning. Other ways you should provide more of your code for us so we could focus on problematic parts..

    "The method addCustomer(String, String, String) is undefined for the type Menu"
    This looks like you are trying to call this method but you didn't create one in the "Menu". Or at list, created method have not the exact signature (String, String, String).

    edit: I just saw that your method is placed in Customer class and you tried to call it for Menu class

    addCustomer ( firstname, lastname, address )
    instead of
    c.addCustomer ( firstname, lastname, address )
    Try to clean this code..
    Last edited by milovan; 10-12-2013 at 07:17 PM.

Similar Threads

  1. Replies: 1
    Last Post: 03-01-2013, 12:55 AM
  2. Bad version number in .class file
    By J-max04 in forum New To Java
    Replies: 6
    Last Post: 01-24-2013, 02:15 AM
  3. Unsupported class version.
    By wdh321 in forum New To Java
    Replies: 8
    Last Post: 10-02-2012, 04:58 PM
  4. Shall we skip Unsupported class version error?
    By makpandian in forum New To Java
    Replies: 1
    Last Post: 04-21-2011, 01:11 PM
  5. Class Version in Serialization
    By Gunas in forum New To Java
    Replies: 5
    Last Post: 02-04-2010, 04:01 AM

Posting Permissions

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