콘텐츠로 이동

Unit 3 Mini Project: Soccer Ticket Booth

Name: ________

Time: 20 minutes

Create a small Java program that tracks ticket sales at one soccer ticket booth. Complete the TicketBooth class below, then run Main to test it.

Requirements

  • Use the provided constructor to initialize both instance variables.
  • Complete the void method sellTickets so it adds the argument to ticketsSold.
  • Complete the three non-void methods with the correct return statements:
  • int getTicketsSold()
  • boolean reachedGoal()
  • String getSummary()
  • Do not change the method headers or the code in Main.

Skeleton Code

class TicketBooth {
    private String teamName;
    private int ticketGoal;
    private int ticketsSold;

    // Constructor: initialize teamName and ticketGoal.
    public TicketBooth(String teamName, int ticketGoal) {
        // TODO
    }

    // Mutator: add newly sold tickets to this booth.
    public void sellTickets(int numberOfTickets) {
        // TODO
    }

    // Accessor: return the total tickets sold.
    public int getTicketsSold() {
        // TODO
        return 0;
    }

    // Return true when the ticket goal has been reached.
    public boolean reachedGoal() {
        // TODO
        return false;
    }

    // Return one sentence describing this ticket booth.
    public String getSummary() {
        // TODO
        return "";
    }
}

public class Main {
    public static void main(String[] args) {
        TicketBooth booth = new TicketBooth("Lions FC", 30);

        booth.sellTickets(15);
        booth.sellTickets(20);

        System.out.println(booth.getTicketsSold());
        System.out.println(booth.reachedGoal());
        System.out.println(booth.getSummary());
    }
}

Expected Output

35
true
Lions FC: 35 / 30 tickets sold

Before You Submit

  • Your constructor assigns values using this.teamName and this.ticketGoal.
  • sellTickets changes the object's state and has no return value.
  • Each non-void method returns a value with the declared type.

Reference: Runestone Academy, CSAwesome2 Unit 3: Class Creation.