Guest User

Untitled

a guest
Jan 17th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.01 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. public class Hero{
  4. private String name;
  5. private int health;
  6. private int max_items;
  7. private ArrayList<String> items;
  8. /**
  9. * Constructor used to set values.
  10. *
  11. * @param name for the hero's name.
  12. * @param max_items for the maximum number of items that the hero can carry.
  13. */
  14. public Hero (String name, int max_items) {
  15. // set the hero's name to provided name
  16. this.name = name;
  17. try {
  18. // If the max_items is less than zero or more than 10, throw IllegalArgumentException
  19. if(max_items < 0 || max_items > 10)
  20. throw new IllegalArgumentException("The maximum number of items that you can carry is from 1 to 10");
  21. // set the max_items to the the second argument provided.
  22. this.max_items = max_items;
  23. } catch(IllegalArgumentException e) {
  24. System.out.println("You cannot carry that many items! " + e.getMessage());
  25. }
  26. // set hero's health to 100
  27. this.health = 100;
  28. // initialize the items list as empty
  29. this.items = new ArrayList<>();
  30. }
  31. /**
  32. * Default constructor
  33. */
  34. public Hero () {
  35. // set the hero's name to "Anonymous"
  36. this.name = "Anonymous";
  37. // set the max_items to 2
  38. this.max_items = 2;
  39. // set hero's health to 100
  40. this.health = 100;
  41. // initialize the items list as empty
  42. this.items = new ArrayList<>();
  43. }
  44. /**
  45. * Inventory method which stores the items the hero's items.
  46. *
  47. * @return a string using the .toString method
  48. */
  49. public String inventory() {
  50. String sb = "";
  51. // If the hero is not carrying anything, print a suitable message
  52. if (items.size() == 0) {
  53. sb += ("Your hero is unburdened by wordly possessions.");
  54. }
  55. // prints the list of items carried by hero
  56. else {
  57. sb += ("Displaying Inventory : ");
  58. sb += ("[");
  59. // Loop1: To print all the items
  60. for (int i = 0; i < items.size(); i++) {
  61. sb += (items.get(i) + ", ");
  62. }
  63. sb += (" ]");
  64. }
  65. return sb.toString();
  66. }
  67. /**
  68. * Take method for adding items to hero's inventory/.
  69. *
  70. * @param item is used for the hero to take/ carry an item
  71. */
  72. public void take(String item) {
  73. System.out.println("Handling Sir " + getName() + " " + item);
  74. // If the inventory is full, print an appropriate message
  75. if(items.size() == max_items) {
  76. System.out.println("Your hero is overburdened and cannot carry more...");
  77. } else {
  78. //add the item to the inventory
  79. items.add(item);
  80. }
  81. }
  82. /**
  83. * Drop method used for hero to drop items from the hero's inventory
  84. *
  85. * @param item which the hero may/may not drop. Depends on either hard-coded hero
  86. * or the final user input.
  87. */
  88. public void drop(String item) {
  89. // If the hero is not carrying the item, print an appropriate message
  90. if(!items.contains(item)) {
  91. System.out.println("Your hero is not carrying that item - " + item);
  92. } else {
  93. //drop the item from the inventory
  94. System.out.println("Brave sir " + getName() + " drops his " + item);
  95. items.remove(item);
  96. }
  97. }
  98. /**
  99. * Take damage method for the hero to take damage and lose health
  100. *
  101. * @param amount is used to keep track of how much damage the hero can take.
  102. */
  103. public void takeDamage(int amount) {
  104. health -= amount;
  105. //if hero's health is less than 0, make it 0
  106. if(health < 0) {
  107. health = 0;
  108. System.out.println("Remember, a hero can't have negative health nor health > 100");
  109. }
  110. }
  111. /**
  112. * Heal method is used to heal the hero
  113. *
  114. * @param amount is used to heal the hero.
  115. */
  116. public void heal(int amount) {
  117. health += amount;
  118. //if hero's health is more than 100, make it 100
  119. if(health > 100) {
  120. health = 100;
  121. System.out.println("Remember, a hero can't have negative health nor health > 100");
  122. }
  123. }
  124. @Override
  125. /**
  126. * toString method used for printing necessary information.
  127. */
  128. public String toString() {
  129. return "Displaying hero: Hero: Brave Sir " +this.name+ ", "+
  130. "health: " + this.health + ", " + "can carry: " + this.max_items+ ", "
  131. + "current inventory: " + inventory();
  132. }
  133. /**
  134. * getName method to get the hero's name.
  135. *
  136. * @return hero's name
  137. */
  138. public String getName() {
  139. return this.name;
  140. }
  141. /**
  142. * setName method to set the hero's name.
  143. *
  144. * @param name is hero's name.
  145. */
  146. public void setName(String name) {
  147. this.name = name;
  148. }
  149. }
  150.  
  151. import java.util.*;
  152. import java.io.*;
  153. public class HeroTester {
  154.  
  155. public static void main(String[] args) {
  156. FileWriter writer = null;
  157. // Option 1 will decide if user chooses hard coded heroes
  158. // or to create a user inputted hero
  159. int opt1;
  160. RandomAccessFile randomAccessFile=null;
  161. // getting the Path of the File ,
  162. // You can specify the path of your choice .
  163. try {
  164. writer = new FileWriter("C:\Users\Ayugma\Desktop\test.txt", true);
  165. randomAccessFile = new RandomAccessFile("C:\Users\Ayugma\Desktop\test.txt", "r");
  166. do {
  167. //Go to last line of the file
  168. randomAccessFile.seek(randomAccessFile.length()-1L);
  169. //Get the input
  170. opt1 = Integer.parseInt(randomAccessFile.readLine());
  171. writer.write("n");
  172. if (opt1 != 1 && opt1 != 2) {
  173. writer.write("Please enter a valid choice!");
  174. }
  175. } while (opt1 != 1 && opt1 != 2);
  176.  
  177. if (opt1 == 1) {
  178. // Hard code the hero values
  179. writer.write("n----------Creating Heroes----------");
  180. writer.write("Creating Hero with no name.");
  181. // create a new hero using default constructor
  182. Hero hero1 = new Hero();
  183. // print hero1 state
  184.  
  185. writer.write(hero1.toString());
  186. writer.write("Creating Hero with a name.");
  187.  
  188. // create a new hero using argumented constructor
  189. Hero hero2 = new Hero("Robin", 3);
  190. // print hero2 state
  191. writer.write(hero2.toString());
  192. // Add an item to inventory
  193. hero2.take("sword");
  194. writer.write(hero2.inventory());
  195.  
  196. // Add an item to inventory
  197. hero2.take("spoon");
  198. writer.write(hero2.inventory());
  199.  
  200. // Add an item to inventory
  201. hero2.take("cape of good fortune");
  202. writer.write(hero2.inventory());
  203.  
  204. // Add an item to inventory
  205. hero2.take("tomato");
  206. writer.write(hero2.inventory());
  207.  
  208. // Drop an item from inventory
  209. hero2.drop("spoon");
  210. writer.write(hero2.inventory());
  211.  
  212. // Drop an item from inventory
  213. hero2.drop("watch");
  214. writer.write(hero2.inventory());
  215.  
  216. // Drop an item from inventory
  217. hero2.drop("sword");
  218. writer.write(hero2.inventory());
  219.  
  220. // Drop an item from inventory
  221. hero2.drop("cape of good fortune");
  222. writer.write(hero2.inventory());
  223.  
  224. // Drop an item from inventory
  225. hero2.take("pointy hat of success");
  226. writer.write(hero2.inventory());
  227.  
  228. // Reduce health
  229. writer.write("Brave sir " + hero2.getName() + " is so busy worrying about his inventory"
  230. + "he doesn't notice the danger and takes 50" + " damage from a shrubery!");
  231. hero2.takeDamage(50);
  232. writer.write(hero2.toString());
  233.  
  234. // Increase health
  235. writer.write("Brave sir " + hero2.getName() + " takes a deep breath" + " and recovers 5 health");
  236. hero2.heal(5);
  237. writer.write(hero2.toString());
  238.  
  239. // Reduce health
  240. writer.write("Brave sir " + hero2.getName() + " is so busy worrying about his inventory"
  241. + " he doesn't notice the danger and takes 119" + " damage from a shrubery!");
  242. hero2.takeDamage(119);
  243. writer.write(hero2.toString());
  244.  
  245. // Increase health
  246. writer.write(
  247. "Brave sir " + hero2.getName() + " enters the Avatar state" + " and heals for 128 points.");
  248. hero2.heal(128);
  249. writer.write(hero2.toString());
  250.  
  251. // print the heros' final state
  252. writer.write("n----------Final State----------");
  253. writer.write(hero1.toString());
  254. writer.write(hero2.toString());
  255. } else if (opt1 == 2) {
  256. // User wants to enter values
  257. Hero hero;
  258. int opt2;
  259. do {
  260. writer.write("1. Create an Anonymous Hero. n2. Create a hero with a name.");
  261. writer.write("nEnter your choice: ");
  262. //Go to last line of the file
  263. randomAccessFile.seek(randomAccessFile.length()-1L);
  264. //Get the input
  265. opt2 = Integer.parseInt(randomAccessFile.readLine());
  266. if (opt2 != 1 && opt2 != 2) {
  267. writer.write("Please enter a valid choice! ");
  268. }
  269. } while (opt2 != 1 && opt2 != 2);
  270. writer.write("n");
  271.  
  272. if (opt2 == 1) {
  273. writer.write("Creating Hero with no name.");
  274. // create a new hero using default constructor
  275. hero = new Hero();
  276. // print hero state
  277. writer.write(hero.toString());
  278. } else {
  279. writer.write("Creating Hero with a name.");
  280. writer.write("Enter hero's name: ");
  281. //Go to last line of the file
  282. randomAccessFile.seek(randomAccessFile.length()-1L);
  283. String name =randomAccessFile.readLine();
  284.  
  285. writer.write("nEnter max items that the hero can carry: ");
  286. //Go to last line of the file
  287. randomAccessFile.seek(randomAccessFile.length()-1L);
  288. int max =Integer.parseInt(randomAccessFile.readLine());
  289. // create a new hero using argumented constructor
  290. hero = new Hero(name, max);
  291.  
  292. // print hero state
  293. writer.write(hero.toString());
  294. }
  295. while (true) {
  296. // Menu options
  297. writer.write("n1. Add item to inventory n2. Drop item from inventory"
  298. + "n3. Cause damage to the hero n4. Heal the hero"
  299. + "n5. Display State of the hero n6. Exitn");
  300.  
  301. writer.write("nEnter your choice: ");
  302. int opt3;
  303. //Go to last line of the file
  304. randomAccessFile.seek(randomAccessFile.length()-1L);
  305. //Get the input
  306. opt3 = Integer.parseInt(randomAccessFile.readLine());
  307.  
  308.  
  309. writer.write("n");
  310. switch (opt3) {
  311. case 1:
  312. // add item to hero's inventory
  313. writer.write("Enter item's name to be added: ");
  314. //Go to last line of the file
  315. randomAccessFile.seek(randomAccessFile.length()-1L);
  316. String take =randomAccessFile.readLine();
  317. hero.take(take);
  318. writer.write(hero.toString());
  319. break;
  320. case 2:
  321. // drop item from hero's inventory
  322. writer.write("Enter item's name to be dropped: ");
  323. //Go to last line of the file
  324. randomAccessFile.seek(randomAccessFile.length()-1L);
  325. String drop =randomAccessFile.readLine();
  326. hero.drop(drop);
  327. writer.write(hero.toString());
  328. break;
  329. case 3:
  330. // Cause damage to the hero
  331. writer.write("Enter the health damage amount: ");
  332. //Go to last line of the file
  333. randomAccessFile.seek(randomAccessFile.length()-1L);
  334. int damage =Integer.parseInt(randomAccessFile.readLine());
  335. hero.takeDamage(damage);
  336. writer.write(hero.toString());
  337. break;
  338. case 4:
  339. // Heal the hero
  340. writer.write("Enter the healing amount: ");
  341. //Go to last line of the file
  342. randomAccessFile.seek(randomAccessFile.length()-1L);
  343. int heal =Integer.parseInt(randomAccessFile.readLine());
  344. hero.heal(heal);
  345. writer.write(hero.toString());
  346. break;
  347. case 5:
  348. // Display the hero's state
  349. writer.write(hero.toString());
  350. break;
  351. case 6:
  352. writer.write("Exiting!!");
  353. return;
  354. default:
  355. // In case user enters an invalid option.
  356. writer.write("nEnter a valid option!!");
  357. }
  358. }
  359.  
  360. }
  361.  
  362. } catch (IOException e) {
  363. e.printStackTrace();
  364. } finally {
  365. try {
  366. if(writer!=null)
  367. {
  368. writer.close();
  369. }
  370.  
  371. } catch (IOException e) {
  372. e.printStackTrace();
  373. }
  374. }
  375. }
  376. }
Add Comment
Please, Sign In to add comment