Created
December 11, 2014 13:53
-
-
Save mrabaev48/6452a3ddb2cd6f9f2e27 to your computer and use it in GitHub Desktop.
Tickets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package tickets; | |
/** | |
* | |
* @author mikhail | |
*/ | |
public class Program { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
// TODO code application logic here | |
TicketMachine tm = new TicketMachine(); | |
TicketMachine tm2 = new TicketMachine(); | |
//tm.buyTicket("Sherlock"); | |
//tm2.buyTicket("HALK"); | |
Ticket t1 = tm.buyTicket("Sherlock"); | |
Ticket t3 = tm.buyTicket("Alala"); | |
Ticket t2 = tm2.buyTicket("HALK"); | |
Ticket t4 = tm2.buyTicket("Madagaskar"); | |
System.out.println(t1); | |
System.out.println(tm.isValid(t1)); | |
tm.useTicket(t1); | |
System.out.println(tm.isValid(t1)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package tickets; | |
import java.util.Objects; | |
/** | |
* | |
* @author mikhail | |
*/ | |
public class Ticket { | |
private static int nextNumber = 1001; | |
private String movie; | |
private int serialNumber; | |
public Ticket(String movie) { | |
this.movie = movie; | |
serialNumber = nextNumber++; | |
} | |
@Override | |
public String toString() { | |
return String.format("#%d %10s", serialNumber, movie); | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (obj == null) { | |
return false; | |
} | |
if (!(obj instanceof Ticket)) { | |
return false; | |
} | |
Ticket some = (Ticket) obj; | |
return movie.equals(some.movie) && serialNumber == some.serialNumber; | |
} | |
@Override | |
public int hashCode() { | |
int hash = 5; | |
hash = 47 * hash + Objects.hashCode(this.movie); | |
hash = 47 * hash + this.serialNumber; | |
return hash; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package tickets; | |
/** | |
* | |
* @author mikhail | |
*/ | |
import java.util.*; | |
public class TicketMachine { | |
private static ArrayList<Ticket> legalTickets = new ArrayList<Ticket>(); | |
public Ticket buyTicket(String movie) { | |
Ticket ticket = new Ticket(movie); | |
legalTickets.add(ticket); | |
return ticket; | |
} | |
public boolean isValid(Ticket ticket){ | |
return legalTickets.contains(ticket); | |
} | |
public boolean useTicket(Ticket ticket){ | |
if(!isValid(ticket)){ | |
return false; | |
} | |
legalTickets.remove(ticket); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment