Pet.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import java.io.*;
  2. public class Pet implements java.io.Serializable {
  3. // serializable version number
  4. private static final long serialVersionUID = 1;
  5. // the pet's stats
  6. private int hunger;
  7. private int thirst;
  8. private int energy;
  9. private boolean life;
  10. // everything starts off good
  11. public Pet(){
  12. hunger = 0;
  13. thirst = 0;
  14. energy = 100;
  15. life = true;
  16. }
  17. // reset the pet's hunger
  18. public void feed(){
  19. hunger = 0;
  20. thirst += 5;
  21. energy -= 5;
  22. if(hunger > 100 || thirst > 100 || energy < 0){
  23. this.kill();
  24. }
  25. }
  26. // reset the pet's thirst
  27. public void water(){
  28. hunger += 5;
  29. thirst = 0;
  30. energy -= 5;
  31. if(hunger > 100 || thirst > 100 || energy < 0){
  32. this.kill();
  33. }
  34. }
  35. // reset the pet's energy
  36. public void rest(){
  37. hunger += 5;
  38. thirst += 5;
  39. energy = 100;
  40. if(hunger > 100 || thirst > 100 || energy < 0){
  41. this.kill();
  42. }
  43. }
  44. // kill the pet
  45. public void kill(){
  46. hunger = 0;
  47. thirst = 0;
  48. energy = 0;
  49. life = false;
  50. }
  51. // do nothing
  52. public void neglect(){
  53. hunger += 5;
  54. thirst += 5;
  55. energy -= 5;
  56. if(hunger > 100 || thirst > 100 || energy < 0){
  57. this.kill();
  58. }
  59. }
  60. // print the pet's stats
  61. public void printStatus(){
  62. if(life){
  63. System.out.println("\tHunger: " + hunger);
  64. System.out.println("\tThirst: " + thirst);
  65. System.out.println("\tEnergy: " + energy);
  66. } else{
  67. System.out.println("This pet died.");
  68. }
  69. }
  70. // helper methods
  71. public int getHunger(){
  72. return hunger;
  73. }
  74. public int getThirst(){
  75. return thirst;
  76. }
  77. public int getEnergy(){
  78. return energy;
  79. }
  80. public boolean isAlive(){
  81. return life;
  82. }
  83. // save the current pet to the disk
  84. public void saveToDisk(){
  85. try{
  86. FileOutputStream fos = new FileOutputStream("pet.pet");
  87. ObjectOutputStream oos = new ObjectOutputStream(fos);
  88. oos.writeObject(this);
  89. oos.close();
  90. fos.close();
  91. }catch(IOException ioe){
  92. ioe.printStackTrace();
  93. }
  94. }
  95. // read a pet from the disk
  96. public static Pet loadFromDisk(){
  97. Pet pet = null;
  98. try{
  99. FileInputStream fin = new FileInputStream("pet.pet");
  100. ObjectInputStream oin = new ObjectInputStream(fin);
  101. pet = (Pet) oin.readObject();
  102. oin.close();
  103. fin.close();
  104. }catch(IOException ioe){
  105. // ioe.printStackTrace();
  106. }catch(ClassNotFoundException cnfe){
  107. cnfe.printStackTrace();
  108. }
  109. return pet;
  110. }
  111. }