1 16 package com.tctest.spring.bean; 17 18 import java.io.Serializable ; 20 import java.util.Random ; 21 22 31 public class HigherLowerGame implements Serializable { 33 public static final Random random = new Random (); 34 35 public static final String INVALID = "invalid"; 36 37 public static final String TOO_HIGH = "toohigh"; 38 39 public static final String TOO_LOW = "toolow"; 40 41 public static final String CORRECT = "corrent"; 42 43 private long start = System.currentTimeMillis(); 45 46 private int answer = random.nextInt(101); 47 48 private int guesses = 0; 49 50 private String result; 51 52 53 public int getAnswer() { 54 return answer; 55 } 56 57 public int getGuesses() { 58 return guesses; 59 } 60 61 public String getResult() { 62 return result; 63 } 64 65 public void setResult(String result) { 66 this.result = result; 67 } 68 69 public long getDuration() { 70 long now = System.currentTimeMillis(); 74 return now - start; 75 } 76 77 public String makeGuess(int guess) { 78 if (guess < 0 || guess > 100) { 79 setResult(INVALID); 80 } 81 else { 82 guesses++; 83 if (answer < guess) { 84 setResult(TOO_HIGH); 85 } 86 else if (answer > guess) { 87 setResult(TOO_LOW); 88 } 89 else { 90 setResult(CORRECT); 91 } 92 } 93 return getResult(); 94 } 95 96 } 97 98 | Popular Tags |