• Post Reply Bookmark Topic Watch Topic
  • New Topic

Making an abstract connect to a main  RSS feed

 
Kyle Grady
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be more specific, i am trying to create tickets in my program. This is what i have.

My ticket booth



A walk up ticket.



A Ticket constructor



Advance ticket



And a student ticket




Ok, so what is going on here is that i have abstract methods in order to build Tickets. I've been told that no actual objects of type Ticket will be created, even though the blue print shows that is it creating a ticket.
When this is done my TicketBooth should print out all the tickets to show the client what is going on. Let me show you the diagram of how this is looking in a map.

................................... Ticket
..................................----------
...................................number // this is in private
..................................----------
...............................Ticket(number) constructing a ticket here
...............................getPrice() getting price and string as abstraction methods
...............................toString()

................ /................................................\

........WalkupTicket................................Advance Ticket

...................................................................|

........................................................ StudentAdvanceTicket


I'm having everything in good form (if you can see the other classes of code), i'm just wonder how does one build the TicketBooth correctly when abstraction is going on, because i'm getting an error: cannot instantiate the type Ticket.
And i need to pass in a number for the tickets so that the number can relate to what price the client would get in return. Since i am doing an I/O behavior of asking the client to type in the date at which they enterd the event it would return the price. but the error is that i cannot create a new ticket like before - needing some help with this and guide.

Please and thank you. : )
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ticket extends TicketBooth? That looks incorrect. You cannot say that a ticket is a kind of ticket booth.
No, if Ticket is an abstract class, you cannot instantiate it directly. Only as a concrete subclass. You probably don't want anonymous subclasses and you probably don't know what anonymous classes are, so you will have to do something like this:-
Ticket t = new AdvanceTickets(1234567);
I am not sure I understand the whole of your question, but try applying that to your booth and see what happens.

Your ticket booth cannot be turned into a ticket, but it can maintain a list of tickets sold, and supply the next number for sale, etc.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a hierarchy, maybe you should say that every ticket has a price. In which case, shouldn't price be a private field of the ticket class, and shouldn't the get price method be implemented fully? Same for number.
 
Kyle Grady
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Ticket extends TicketBooth? That looks incorrect. You cannot say that a ticket is a kind of ticket booth.
No, if Ticket is an abstract class, you cannot instantiate it directly. Only as a concrete subclass. You probably don't want anonymous subclasses and you probably don't know what anonymous classes are, so you will have to do something like this:-
Ticket t = new AdvanceTickets(1234567);
I am not sure I understand the whole of your question, but try applying that to your booth and see what happens.

Your ticket booth cannot be turned into a ticket, but it can maintain a list of tickets sold, and supply the next number for sale, etc.


Ok i took off the extension of the Ticket class to just be on it's own as it's constructor.


And i have this


But i do get an underline on my AdvanceTicket which is "Cannot instantiate the type AdvanceTicket"
 
Kyle Grady
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you want a hierarchy, maybe you should say that every ticket has a price. In which case, shouldn't price be a private field of the ticket class, and shouldn't the get price method be implemented fully? Same for number.


How would that look like?

because i have private in the Ticket class for the number.

I took off the extend to the TicketBooth to be connected to Ticket since needs to be its own abstract method and i'm calling a few things like the String and the price after getting the number from the client which will determine the price they will get for the ticket.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the engine class you can see that every subclass has a fully implemented version of the getCc method, which they cannot change. Not only that method is final, but also the cc field, so there is no setCc method. Note that
System.out.println(myEngine);
will display something like
A 1800cc DieselEngine

This is just something I knocked up in a few minutes. I shall leave you to convert it to tickets and polish it a bit.
 
Kyle Grady
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you I will look into it and make sure if any questions I needed up on my behalf I will address them to this board.

Next question is how would it look like if it were in the ticket booth and when constructing every new ticket ?
 
Kyle Grady
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . .
This is just something I knocked up in a few minutes. I shall leave you to convert it to tickets and polish it a bit.


Ok, the ticket conversion is complete (never knew that format for a string) now it's just the TickeBooth class where it is telling me an error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot instantiate the type Ticket

at TicketBooth.main(TicketBooth.java:15)

Here is my code






Tried think what is wrong but i am a little burned from thinking too hard about it, i am taking the info of the user as selling to print out a result like this.

"Number: 17, Price: 15.0 (ID required) " < This is what students print out would be, as an example.
 
Kyle Grady
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Grady wrote: . . .



Update, i got it straight.

Thanks for your help, really appreciate it.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

Ticket t = new StudentTicket(123);
For the toString method have you seen this idiom?Don't quote the whole of the preceding post; that simply makes the posts too long to read easily.
 
Kyle Grady
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You're welcome

Ticket t = new StudentTicket(123);
For the toString method have you seen this idiom?Don't quote the whole of the preceding post; that simply makes the posts too long to read easily.



No i have not, but as of now i did polish up the program to be much more easier to read now. Thanks. ^^
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!