import java.util.*;
import java.io.*;

public class SlotMachine {
	
	// fields
	protected String name;  // name of the machine!
	protected int winFrequency; 
	protected int jackpot;
	
	// constructor
	public SlotMachine (String na, int wf) {
		name = na;
		winFrequency = wf;
		jackpot = (int)(winFrequency/2);
	}
	
	protected int randomVal() {
		return (int)(Math.random()*100) % winFrequency;
	}		
	
	public int playSlotMachine() throws IOException {
		// This returns a random value between 0.0 and 1.0
		// The player wins if the random value can be evenly divided by winFrequency
		System.out.println("Playing " + name + " with a jackpot of " + jackpot + 
				" and a 1 in " + winFrequency + " chance of winning....");
		if ( randomVal() == 0 ) {
			System.out.println("You won!!!!!!");
			return jackpot;
		} else {	
			System.out.println("Sorry you lost, but try again!");
			return 0;
		} // if
	} // playSlotMachine
	
	// toString method so I can unit test this class
	public String toString() {
		return ("name = " + name + "; winfrequency = " 
				+ winFrequency + "; jackpot = " + jackpot);
	} // toString
	
	// main method so that I can test this class
	public static void main ( String[] args ) {
		SlotMachine sm = new SlotMachine("foo", 5);
		System.out.println(sm.toString());
	}
			
} // class