콘텐츠로 이동

Unit 3 Mini Project Answer: Soccer Ticket Booth

Complete Program

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

    public TicketBooth(String teamName, int ticketGoal) {
        this.teamName = teamName;
        this.ticketGoal = ticketGoal;
        this.ticketsSold = 0;
    }

    public void sellTickets(int numberOfTickets) {
        ticketsSold += numberOfTickets;
    }

    public int getTicketsSold() {
        return ticketsSold;
    }

    public boolean reachedGoal() {
        return ticketsSold >= ticketGoal;
    }

    public String getSummary() {
        return teamName + ": " + ticketsSold + " / " + ticketGoal + " tickets sold";
    }
}

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());
    }
}

Output

35
true
Lions FC: 35 / 30 tickets sold

Key Points

  • The constructor has no return type and initializes the object. this.teamName and this.ticketGoal refer to instance variables.
  • sellTickets is a void method that changes state. The other methods return an int, boolean, and String value.