Can you kindly help me with the assignment (C++) attached below regarding java p
ID: 3932798 • Letter: C
Question
Can you kindly help me with the assignment(C++) attached below regarding java programming?
My task is to write a program that sells different types of tickets (e.g., student, child, adult, senior citizen) for a theater by using inheritance, abstract class and java interfaces. I have completed for the most part but am lost at "TicketService" class
I really appreciate your time and effort.
Write a program that sells different types of tickets (e.g., student, child, adult, senior citizen) for a theater. (utilize the concepts of : inheritance, abstract class and java interfaces. )
- Create an abstract class Ticket that has:
variable id: a unique identify for each ticket and has type int
variable price: price per one ticket, has type float
Constructor Ticket(int id, float price) for assigning values to 2 variables id and price
- Create 4 classes: SeniorTicket, AdultTicket, ChildTicket and StudentTicket which extends abstract class Ticket and implement constructor and toString method for each class.
- Prices: $20 for adult, $10 for senior and children and $15 for student ticket.
- Create class TheaterTicketService which implements TicketService interface ( file TicketService.java) and sellTicket method. sellTicket method should return an object that is instance of one of 4 class: SeniorTicket, AdultTicket, ChildTicket and StudentTicket depend on ticketType.
- Class TheaterTicketService should:
-Instance variable int capacity : total number of tickets
-Instance variable int soldTicket : total number of sold tickets
-Instance variable float revenue which is the total money of selling tickets; also implement a getter for this variable .
- constructor TheaterTicketService(int capacity)
For example:
TheaterTicketService tts = new TheaterTicketService(20);
Ticket adultTicket = tts.sellTicket(TicketService.Type.ADULT);
System.out.println("Sold " + adultTicket.toString());
Ticket childTicket = tts.sellTicket(TicketService.Type.CHILD);
System.out.println("Sold " + childTicket.toString());
System.out.println("Total tickets: " + tts.getCapacity() + " ;Ticket Sold: " + tts.getTicketLeft());
System.out.println("Revenue until now: " + tts.getRevenue());
should have output:
Sold Adult ticket, id: 1, price: $20.0
Sold Child ticket, id: 2, price: $10.0
Total tickets: 20 ;Ticket Sold: 18
Revenue until now: $30.0
- Please document your code internally with comments
so far this is what i have:
//class1
public class ChildTicket extends Ticket{
public ChildTicket(int id, float price) {
super(id, price);
price = 15;
}
public String toString(){
String s = "Sold child ticket, id: " + id + ", price: $" + price;
return s;
}
}
//class2
public class SeniorTicket extends Ticket{
public SeniorTicket(int id, float price) {
super(id, price);
price = 10;
}
public String toString(){
String s = "Sold senior ticket, id: " + id + ", price: $" + price;
return s;
}
}
//class3
public class StudentTicket extends Ticket{
public StudentTicket(int id, float price) {
super(id, price);
price = 15;
}
public String toString(){
String s = "Sold student ticket, id: " + id + ", price: $" + price;
return s;
}
}
//class4
public class TheaterTicketService implements TicketService{
int capacity, soldTicket; private float revenue;
public TheaterTicketService(int capacity){
this.capacity = capacity;
}
int ticketLeft = capacity - soldTicket;
public float getRevenue(){
return revenue;
}
public int getCapacity(){
return capacity;
}
public int getTicketLeft(){
return ticketLeft;
}
public float sellTicket(String s){
if(s == "Adult"){
}
return revenue; //this is wrong
}
}
//class5
abstract class Ticket{ // abstract class for general tickets
int id;
float price;
public Ticket(int id, float price){ // constructor for Ticket
this.id = id;
this.price = price;
}
}
//class6
public interface TicketService {
public float sellTicket();
}
Explanation / Answer
Please Check this code for refrence:
abstract class Ticket{ // abstract class for general tickets
static int id=0; //To auto-incremenet the id
float price;
public Ticket(float price){ // Since id is unique should not be passed by constructor
this.id++;
this.price = price;
}
}
public class StudentTicket extends Ticket{
public StudentTicket() {
super(15); //Hardcoding price value as 15 for Student Ticket
}
public String toString(){
String s = "Sold student ticket, id: " + id + ", price: $" + price;
return s;
}
}
public class ChildTicket extends Ticket{
public ChildTicket() {
super(15); //Setting price of ticket as 15
}
public String toString(){
String s = "Sold child ticket, id: " + id + ", price: $" + price;
return s;
}
}
public class SeniorTicket extends Ticket{
public SeniorTicket() {
super(10);
}
public String toString(){
String s = "Sold senior ticket, id: " + id + ", price: $" + price;
return s;
}
}
public class AdultTicket extends Ticket{
public AdultTicket() {
super(20);
}
public String toString(){
String s = "Sold adult ticket, id: " + id + ", price: $" + price;
return s;
}
}
public interface TicketService {
public Ticket sellTicket(String type); //It should return as type Ticket
}
public class TheaterTicketService implements TicketService{
int capacity, soldTicket; private float revenue;
public TheaterTicketService(int capacity){
this.capacity = capacity;
}
public float getRevenue(){
return revenue;
}
public int getCapacity(){
return capacity;
}
@Override
public Ticket sellTicket(String type) {
Ticket sTicket=null;
if(type.equals("ADULT")){
sTicket=new AdultTicket();
}else if(type.equals("CHILD")){
sTicket=new ChildTicket();
}else if(type.equals("STUDENT")){
sTicket=new StudentTicket();
}else if(type.equals("SENIOR")){
sTicket=new SeniorTicket();
}
if(sTicket!=null){
soldTicket++; // for each call to this method the sold tickets are incremented
revenue+=sTicket.price; //For each sold ticket, the revenue is incremented
}
return sTicket;
}
}
TESTING CLASS:
public class TestTicket {
public static void main(String[] args) {
TheaterTicketService tts=new TheaterTicketService(20);
Ticket adultTicket = tts.sellTicket("ADULT");
System.out.println("Sold " + adultTicket.toString());
Ticket childTicket = tts.sellTicket("CHILD");
System.out.println("Sold " + childTicket.toString());
System.out.println("Total tickets: " + tts.getCapacity() + " ;Ticket Sold: " + tts.soldTicket);
System.out.println("Revenue until now: " + tts.getRevenue());
}
}
OUTPUT:
Sold Sold adult ticket, id: 1, price: $20.0
Sold Sold child ticket, id: 2, price: $15.0
Total tickets: 20 ;Ticket Sold: 2
Revenue until now: 35.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.