Two Class but not using extends...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    Two Class but not using extends...

    Hello everyone:
    I have one last project that is due today and I have a question. Class assignment the Airliner...
    In this project that I'm working on it has several class in which each class has some data type it needs to use. It also need to two programs one to capture the required info. the other will create the user interface and db connections

    I think I need at least three class.
    Ticket
    Buyer
    The third one to conatin the main() to do the testing.

    Question is I don't think I can use extends from what I have being read. So the question is how do I get the info from TicketClass to the BuyerClass.
    The BuyerClass need info from the TicketClass in order to process a ticket and print out the in.

    any help would be great.

    nomad
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Ticket and Buyer would be fine classes but you need a 'TicketDispense r' that
    can give a Ticket (sold out is sold out so no ticket then). If you follow that little
    scenario, a Ticket is being instantiated given a Buyer. A ticket is associated
    with a Buyer and the Buyer should be updated (he bought another ticket again).
    Think 'physical' here: there are a number of tickets, the dispenser sells them to
    a Buyer in which case the Buyer has (another?) ticket again and the dispenser
    has one ticket less (because he sold it).

    A Dispenser could be anything: an Airline company a Theatre etc. etc. All it
    has to know how many tickets it has (and possibly the price for different type
    of tickets, but that's just an 'implementation detail').

    kind regards,

    Jos

    Comment

    • nomad
      Recognized Expert Contributor
      • Mar 2007
      • 664

      #3
      Originally posted by JosAH
      Ticket and Buyer would be fine classes but you need a 'TicketDispense r' that
      can give a Ticket (sold out is sold out so no ticket then). If you follow that little
      scenario, a Ticket is being instantiated given a Buyer. A ticket is associated
      with a Buyer and the Buyer should be updated (he bought another ticket again).
      Think 'physical' here: there are a number of tickets, the dispenser sells them to
      a Buyer in which case the Buyer has (another?) ticket again and the dispenser
      has one ticket less (because he sold it).

      A Dispenser could be anything: an Airline company a Theatre etc. etc. All it
      has to know how many tickets it has (and possibly the price for different type
      of tickets, but that's just an 'implementation detail').

      kind regards,

      Jos
      I understand that part..


      So is this right?
      TicketDispenerC lass hold the information from ticket and buyer and return info
      TicketClass - ticket information
      BuyerClass --name and address plus info from TicketClass

      But I don't get the data type to talk to each other from one class to the other class.
      That where I'm stuck at.

      Nomad
      If I fig this out I just might write another article on this one.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Maybe the TicketDispenser keeps a lists of Tickets sold to Buyers? IOW the
        TicketDispenser knows all? And maybe a TransAction object should be added:[code=java]
        public class TransAction {
        private Ticket ticket;
        private Buyer buyer;
        public Transaction(Tic ket ticket, Buyer buyer) {
        this.ticket= ticket;
        this.buyer= buyer;
        }
        public Ticket getTicket() { return ticket; }
        public Buyer getBuyer() { return buyer; }
        }[/code]

        kind regards,

        Jos

        Comment

        • nomad
          Recognized Expert Contributor
          • Mar 2007
          • 664

          #5
          Originally posted by JosAH
          Maybe the TicketDispenser keeps a lists of Tickets sold to Buyers? IOW the
          TicketDispenser knows all? And maybe a TransAction object should be added:[code=java]
          public class TransAction {
          private Ticket ticket;
          private Buyer buyer;
          public Transaction(Tic ket ticket, Buyer buyer) {
          this.ticket= ticket;
          this.buyer= buyer;
          }
          public Ticket getTicket() { return ticket; }
          public Buyer getBuyer() { return buyer; }
          }[/code]

          kind regards,

          Jos
          Thanks Jos..
          There was example from the Instructor on this..
          But his example did not work 100 percent so I could not figure it out.
          thanks
          nomad
          Howfull I can not finish this project.

          Comment

          • nomad
            Recognized Expert Contributor
            • Mar 2007
            • 664

            #6
            Originally posted by JosAH
            Maybe the TicketDispenser keeps a lists of Tickets sold to Buyers? IOW the
            TicketDispenser knows all? And maybe a TransAction object should be added:[code=java]
            public class TransAction {
            private Ticket ticket;
            private Buyer buyer;
            public Transaction(Tic ket ticket, Buyer buyer) {
            this.ticket= ticket;
            this.buyer= buyer;
            }
            public Ticket getTicket() { return ticket; }
            public Buyer getBuyer() { return buyer; }
            }[/code]

            kind regards,

            Jos
            One last question...RIGH TTT
            Not to sure about this one. Should I use One arraylist or two.
            Question you will ask why two.
            1. to hold the ticket info.
            the other two hold the buyer info.

            or just want to hold everything.

            nomad

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by nomad
              One last question...RIGH TTT
              Not to sure about this one. Should I use One arraylist or two.
              Question you will ask why two.
              1. to hold the ticket info.
              the other two hold the buyer info.

              or just want to hold everything.

              nomad
              The Transaction class (see above) would be ideal for that. The TIcketDispenser
              should just hold one list of Transactions. A single Transaction tells you who
              (which Buyer) bought what (Ticket). Never be afraid to create a new class if you
              think that thing represents an entitiy/class and never ever use two arrays or
              lists that both carry parts of another entity; build a new class instead.

              kind regards,

              Jos

              Comment

              • nomad
                Recognized Expert Contributor
                • Mar 2007
                • 664

                #8
                OK
                I'm very close in solving this project.

                but I have two question to ask and I hope you can help me:

                1. how do I do this
                System.out.prin tln("Event info is " .getTicket().ge tBuyer());

                I know there is value that needs to go before the .getTicket().ge tBuuer())

                2. I have a error in where buyerTracker temp = makeBuyer();
                I get this error code The method makeBuyer() is undefined for the type TicketSales


                Code:
                if (choice == 2) { // if 2 is select go to find
                			boolean endData2 = false;
                
                			while (!endData2) {
                				[B]buyerTracker temp = makeBuyer();[/B]
                				arlist.addAll((Collection<? extends EventClass>) temp);
                				System.out.println("Add More Events (Y/N)-->");
                
                				String ans = kbd.next();
                
                				if (ans.equalsIgnoreCase("N")) {
                					System.out.println(temp);
                					endData2 = true;
                				}
                			}// close while loop
                		}// close choice 2
                Thanks
                nomad

                Comment

                • emekadavid
                  New Member
                  • Mar 2007
                  • 46

                  #9
                  you don't need to use extends at all. you already know what you need. see, every buyer needs a ticket. so, every buyer has a ticket. that's the catch. has a relationship was what you were thinking of or what is called composition in object oriented lingo.

                  you simply declare a ticket object in the buyer classes and that does the work. composition. beginner programmers from a book by bruce eckel i was reading should think of composition before inheritance even though inheritance seems a much more powerful concept.

                  lol

                  Comment

                  Working...