Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1. package h02;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5.  
  6. /**
  7.  * @author kolmas
  8.  *
  9.  */
  10. public class Lottery {
  11.  
  12.     /**
  13.      * Holds info about lottery activity.
  14.      *
  15.      */
  16.     private boolean active = true;
  17.  
  18.     /**
  19.      * Hold all the tickets sold.
  20.      *
  21.      */
  22.     private ArrayList<LotteryTicket> tickets = new ArrayList<LotteryTicket>();
  23.     /**
  24.      * Holds all the tickets that have actually won.
  25.      *
  26.      */
  27.     @SuppressWarnings("unchecked")
  28.     private HashMap winningTickets = new HashMap();
  29.  
  30.     /**
  31.      * Name of the lottery.
  32.      */
  33.     private String lotteryName;
  34.     private int ticketPrice;
  35.     private int jackPotInc;
  36.     private int winnersCash;
  37.  
  38.     /**
  39.      * Hold the total sum that each winner gets.
  40.      */
  41.     private double totalWin;
  42.     /**
  43.      * Holds the jackpot value. Increments it according the 'jackPotInc'
  44.      * variable when a new ticket is being bough.
  45.      */
  46.     private int jackpot;
  47.  
  48.     /**
  49.      * Winning number 1.
  50.      */
  51.     private int nr1;
  52.     /**
  53.      * Winning number 2.
  54.      */
  55.     private int nr2;
  56.  
  57.     /**
  58.      * Constructor for setting all the lottery data.
  59.      *
  60.      * @param name
  61.      *            - Lottery name.
  62.      * @param ticketPrice
  63.      *            - Lotteries ticket price.
  64.      * @param toJackPot
  65.      *            - Cash that goes into the jackpot pile from each ticket.
  66.      * @param cashPayed
  67.      *            - Ammount that the winning ticket gets BEFORE adding cash from
  68.      *            jackpot. Sure win ammount.
  69.      */
  70.     public Lottery(String name, int ticketPrice, int toJackPot, int cashPayed) {
  71.         this.lotteryName = name;
  72.         this.ticketPrice = ticketPrice;
  73.         this.jackPotInc = toJackPot;
  74.         this.winnersCash = cashPayed;
  75.     }
  76.  
  77.     /**
  78.      * Number of tickets sold.
  79.      *
  80.      * @return The number of tickets sold.
  81.      */
  82.     public int getNumberOfTickets() {
  83.         return this.tickets.size();
  84.     }
  85.  
  86.     /**
  87.      * Current jackpot.
  88.      *
  89.      * @return The current jackpot.
  90.      */
  91.     public int getJackpot() {
  92.         return this.jackpot;
  93.     }
  94.  
  95.     /**
  96.      * Player buys a ticket.
  97.      *
  98.      * @param n1
  99.      *            - players ticket nr1.
  100.      * @param n2
  101.      *            - players ticket nr2.
  102.      */
  103.     public void gamble(int n1, int n2) {
  104.         if (this.active) {
  105.             tickets.add(new LotteryTicket(n1, n2, tickets.size() + 1));
  106.             this.jackpot += this.jackPotInc;
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * Calculates profit. That the lottery firm actually made off this lottery.
  112.      *
  113.      * @return
  114.      */
  115.     public void calculateProfit() {
  116.         double sum = (this.tickets.size() * this.ticketPrice)
  117.                 - (winningTickets.size() * this.totalWin);
  118.         System.out.println("Teenitud tulu: " + sum + " eurot");
  119.     }
  120.  
  121.     /**
  122.      * Checks if the ticket has won or not. Checks if there is a key in hashmap
  123.      * that equals to given index.
  124.      *
  125.      * @param index
  126.      *            - tickets number
  127.      */
  128.     public void checkTicket(long index) {
  129.         if (!this.active) {
  130.             LotteryTicket tic = (LotteryTicket) this.winningTickets.get(index);
  131.             if (tic != null && tic.isValid()) {
  132.                 System.out.println("Palju õnne! Teie pilet numbriga " + index
  133.                         + " on võitnud! Võitsite kokku " + this.totalWin
  134.                         + " eurot");
  135.                 tic.toggleValid();
  136.             } else {
  137.                 System.out.println("Teie pilet ei võitnud kahjuks.");
  138.             }
  139.         } else {
  140.             System.out.println("Loosimine pole veel lõppenud.");
  141.         }
  142.  
  143.     }
  144.  
  145.     /**
  146.      * Ends the lottery!
  147.      */
  148.     @SuppressWarnings("unchecked")
  149.     public void raffleJackpot() {
  150.         if (this.active) {
  151.             this.active = !this.active;
  152.             this.nr1 = randNr();
  153.             this.nr2 = randNr();
  154.  
  155.             // Checks what tickets have won the lottery. Adds winning tickets to
  156.             // hashmap.
  157.             for (int i = 0; i < tickets.size(); i++) {
  158.                 if (tickets.get(i).didIwin(this.nr1, this.nr2)) {
  159.                     this.winningTickets.put(tickets.get(i).getTicketNr(),
  160.                             tickets.get(i));
  161.                 }
  162.             }
  163.             // Calculates how much was the total ammount won by players.
  164.             if (this.winningTickets.size() > 0) {
  165.                 this.totalWin = Math.round(((double) this.winnersCash / this.winningTickets
  166.                         .size())
  167.                         + this.tickets.size());
  168.             }
  169.         } else {
  170.             System.out.println("Loosimine on juba läbi. Siin on tulemused: ");
  171.         }
  172.         System.out.println("'" + this.lotteryName
  173.                 + "' loosis võidunumbrid:\n\tVõidunumbriteks olid " + this.nr1
  174.                 + " ja " + this.nr2 + ".\n\tJagamisele läks võitjate vahel "
  175.                 + getJackpot() + " eurot.\n\tVõiduga pileteid: "
  176.                 + this.winningTickets.size() + "\n\tKokku osteti pileteid "
  177.                 + this.tickets.size());
  178.     }
  179.  
  180.     /**
  181.      * Generates random number between 1 and 10.
  182.      *
  183.      * @return random number.
  184.      */
  185.     private int randNr() {
  186.         return (int) Math.round(Math.random() * 9 + 1);
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement