Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package h02;
- import java.util.ArrayList;
- import java.util.HashMap;
- /**
- * @author kolmas
- *
- */
- public class Lottery {
- /**
- * Holds info about lottery activity.
- *
- */
- private boolean active = true;
- /**
- * Hold all the tickets sold.
- *
- */
- private ArrayList<LotteryTicket> tickets = new ArrayList<LotteryTicket>();
- /**
- * Holds all the tickets that have actually won.
- *
- */
- @SuppressWarnings("unchecked")
- private HashMap winningTickets = new HashMap();
- /**
- * Name of the lottery.
- */
- private String lotteryName;
- private int ticketPrice;
- private int jackPotInc;
- private int winnersCash;
- /**
- * Hold the total sum that each winner gets.
- */
- private double totalWin;
- /**
- * Holds the jackpot value. Increments it according the 'jackPotInc'
- * variable when a new ticket is being bough.
- */
- private int jackpot;
- /**
- * Winning number 1.
- */
- private int nr1;
- /**
- * Winning number 2.
- */
- private int nr2;
- /**
- * Constructor for setting all the lottery data.
- *
- * @param name
- * - Lottery name.
- * @param ticketPrice
- * - Lotteries ticket price.
- * @param toJackPot
- * - Cash that goes into the jackpot pile from each ticket.
- * @param cashPayed
- * - Ammount that the winning ticket gets BEFORE adding cash from
- * jackpot. Sure win ammount.
- */
- public Lottery(String name, int ticketPrice, int toJackPot, int cashPayed) {
- this.lotteryName = name;
- this.ticketPrice = ticketPrice;
- this.jackPotInc = toJackPot;
- this.winnersCash = cashPayed;
- }
- /**
- * Number of tickets sold.
- *
- * @return The number of tickets sold.
- */
- public int getNumberOfTickets() {
- return this.tickets.size();
- }
- /**
- * Current jackpot.
- *
- * @return The current jackpot.
- */
- public int getJackpot() {
- return this.jackpot;
- }
- /**
- * Player buys a ticket.
- *
- * @param n1
- * - players ticket nr1.
- * @param n2
- * - players ticket nr2.
- */
- public void gamble(int n1, int n2) {
- if (this.active) {
- tickets.add(new LotteryTicket(n1, n2, tickets.size() + 1));
- this.jackpot += this.jackPotInc;
- }
- }
- /**
- * Calculates profit. That the lottery firm actually made off this lottery.
- *
- * @return
- */
- public void calculateProfit() {
- double sum = (this.tickets.size() * this.ticketPrice)
- - (winningTickets.size() * this.totalWin);
- System.out.println("Teenitud tulu: " + sum + " eurot");
- }
- /**
- * Checks if the ticket has won or not. Checks if there is a key in hashmap
- * that equals to given index.
- *
- * @param index
- * - tickets number
- */
- public void checkTicket(long index) {
- if (!this.active) {
- LotteryTicket tic = (LotteryTicket) this.winningTickets.get(index);
- if (tic != null && tic.isValid()) {
- System.out.println("Palju õnne! Teie pilet numbriga " + index
- + " on võitnud! Võitsite kokku " + this.totalWin
- + " eurot");
- tic.toggleValid();
- } else {
- System.out.println("Teie pilet ei võitnud kahjuks.");
- }
- } else {
- System.out.println("Loosimine pole veel lõppenud.");
- }
- }
- /**
- * Ends the lottery!
- */
- @SuppressWarnings("unchecked")
- public void raffleJackpot() {
- if (this.active) {
- this.active = !this.active;
- this.nr1 = randNr();
- this.nr2 = randNr();
- // Checks what tickets have won the lottery. Adds winning tickets to
- // hashmap.
- for (int i = 0; i < tickets.size(); i++) {
- if (tickets.get(i).didIwin(this.nr1, this.nr2)) {
- this.winningTickets.put(tickets.get(i).getTicketNr(),
- tickets.get(i));
- }
- }
- // Calculates how much was the total ammount won by players.
- if (this.winningTickets.size() > 0) {
- this.totalWin = Math.round(((double) this.winnersCash / this.winningTickets
- .size())
- + this.tickets.size());
- }
- } else {
- System.out.println("Loosimine on juba läbi. Siin on tulemused: ");
- }
- System.out.println("'" + this.lotteryName
- + "' loosis võidunumbrid:\n\tVõidunumbriteks olid " + this.nr1
- + " ja " + this.nr2 + ".\n\tJagamisele läks võitjate vahel "
- + getJackpot() + " eurot.\n\tVõiduga pileteid: "
- + this.winningTickets.size() + "\n\tKokku osteti pileteid "
- + this.tickets.size());
- }
- /**
- * Generates random number between 1 and 10.
- *
- * @return random number.
- */
- private int randNr() {
- return (int) Math.round(Math.random() * 9 + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement