Advertisement
Guest User

Custom Errors

a guest
Jul 20th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. package lesson13;
  2.  
  3. import java.util.Random;
  4.  
  5. public class SoccerTournament {
  6.    
  7.     Random rand = new Random();
  8.  
  9.     private int games = 0;
  10.     private String name;
  11.     private int wins = 0;
  12.    
  13.     ArithmeticException notgames = new ArithmeticException("You need at least 3 games to win.");
  14.    
  15.     SoccerTournament(String name){
  16.         this.name = name;  
  17.     }
  18.    
  19.     public void playGame(SoccerTournament a) {
  20.         while (a.wins <= 3 || this.wins <= 3) {
  21.             if (rand.nextBoolean()) {
  22.                 a.wins ++;
  23.                 a.games ++;
  24.             } else {
  25.                 this.wins ++;
  26.                 this.games ++;
  27.             }
  28.         }
  29.     }
  30.    
  31.     public void getWinner(SoccerTournament a) {
  32.         if (a.wins > this.wins) {
  33.             if (a.games > 1) System.out.println(a.name + " is the winner.");
  34.             else throw notgames;
  35.         } else {
  36.             if (this.games > 1) System.out.println(this.name + " is the winner.");
  37.             else throw notgames;
  38.         }
  39.     }
  40.    
  41.     public static void main(String args[]) {
  42.         SoccerTournament TorontoFC = new SoccerTournament("Toronto FC");
  43.         SoccerTournament MontrealFC = new SoccerTournament("Montreal FC");
  44.        
  45.         MontrealFC.playGame(TorontoFC);
  46.         TorontoFC.getWinner(MontrealFC);
  47.     }
  48.    
  49.    
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement