Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Audience {
- private Ticket ticket = Ticket.EMPTY;
- private Invitation invitation = Invitation.EMPTY;
- private Long amount;
- public Audience(Long amount){this.amount = amount;}
- public void buyTicket(TicketSeller seller){
- ticket = seller.getTicket(this);
- }
- public boolean hasAmount(Long amount){
- return this.amount >= amount;
- }
- public boolean minusAmount(Long amount){
- if(amount > this.amount) return false;
- this.amount -= amount;
- return true;
- }
- public Invitation getInvitation(){return invitation;}
- public void removeInvitation(){
- invitation = Invitation.EMPTY;
- }
- public Ticket getTicket(){return ticket;}
- public void setInvitation(Invitation invitation){
- this.invitation = invitation;
- }
- }
- public class Invitation {
- final static public Invitation EMPTY = new Invitation(null);
- final private Theater theater;
- public Invitation(Theater theater){
- this.theater = theater;
- }
- }
- public class Main {
- public static void main(String[] args) {
- Theater theater = new Theater(100L);
- Audience audience1 = new Audience(0L);
- Audience audience2 = new Audience(50L);
- TicketOffice ticketOffice = new TicketOffice(0L);
- TicketSeller seller = new TicketSeller();
- theater.setTicketOffices(ticketOffice);
- theater.setTicket(10L);
- theater.setInvitation(audience1);
- seller.setTicketOffice(ticketOffice);
- audience1.buyTicket(seller);
- audience2.buyTicket(seller);
- boolean isOk1 = theater.enter(audience1);
- boolean isOk2 = theater.enter(audience2);
- System.out.println(isOk1);//true
- System.out.println(isOk2);//false
- }
- }
- public class Theater{
- private TicketOffice ticketOffice = TicketOffice.EMPTY ;
- final private Long fee;
- public Theater( Long fee){
- this.fee = fee;
- }
- Long getFee(){
- return this.fee;
- }
- public void setTicketOffices(TicketOffice ticketOffice ) {
- this.ticketOffice = ticketOffice;
- }
- public void setTicket(Long num){
- if(this.ticketOffice == TicketOffice.EMPTY) return;
- while(num-- > 0) {
- this.ticketOffice.addTicket(new Ticket(this));
- }
- }
- public void setInvitation(Audience audience){
- audience.setInvitation(new Invitation(this));
- }
- public boolean enter(Audience audience){
- Ticket ticket = audience.getTicket();
- return ticket.isValid(this);
- }
- }
- public class Ticket {
- final static public Ticket EMPTY = new Ticket(null);
- final private Theater theater;
- private boolean isEntered = false;
- public Ticket(Theater theater){
- this.theater = theater;
- }
- public boolean isValid(Theater theater){
- if(isEntered || theater != this.theater || this == EMPTY){
- return false;
- }else{
- isEntered = true;
- return true;
- }
- }
- public Long getFee(){
- return theater.getFee();
- }
- }
- import java.util.ArrayList;
- import java.util.List;
- public class TicketOffice {
- final static public TicketOffice EMPTY = new TicketOffice(null);
- private Long amount;
- private List<Ticket> tickets = new ArrayList<>();
- public TicketOffice(Long amount){this.amount = amount;}
- public void addTicket(Ticket ticket){
- this.tickets.add(ticket);
- }
- public Ticket getTicketWithFee(){
- if(tickets.size() == 0) return Ticket.EMPTY;
- else{
- Ticket ticket = tickets.remove(0);
- amount += ticket.getFee();
- return ticket;
- }
- }
- public Ticket getTicketWithNoFee(){
- if(tickets.size() == 0) return Ticket.EMPTY;
- else return tickets.remove(0);
- }
- public Long getTicketPrice(){
- if(tickets.size() == 0) return 0L;
- else return tickets.get(0).getFee();
- }
- }
- public class TicketSeller {
- private TicketOffice ticketOffice;
- public void setTicketOffice(TicketOffice ticketOffice){
- this.ticketOffice = ticketOffice;
- }
- public Ticket getTicket(Audience audience){
- Ticket ticket = Ticket.EMPTY;
- if(audience.getInvitation() != Invitation.EMPTY){
- ticket = ticketOffice.getTicketWithNoFee();
- if(ticket != Ticket.EMPTY) audience.removeInvitation();
- }else{
- Long price = ticketOffice.getTicketPrice();
- if(price > 0 && audience.hasAmount(price)){
- ticket = ticketOffice.getTicketWithFee();
- if(ticket != Ticket.EMPTY) audience.minusAmount(price);
- }
- }
- return ticket;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement