Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Hero{
- private String name;
- private int health;
- private int max_items;
- private ArrayList<String> items;
- /**
- * Constructor used to set values.
- *
- * @param name for the hero's name.
- * @param max_items for the maximum number of items that the hero can carry.
- */
- public Hero (String name, int max_items) {
- // set the hero's name to provided name
- this.name = name;
- try {
- // If the max_items is less than zero or more than 10, throw IllegalArgumentException
- if(max_items < 0 || max_items > 10)
- throw new IllegalArgumentException("The maximum number of items that you can carry is from 1 to 10");
- // set the max_items to the the second argument provided.
- this.max_items = max_items;
- } catch(IllegalArgumentException e) {
- System.out.println("You cannot carry that many items! " + e.getMessage());
- }
- // set hero's health to 100
- this.health = 100;
- // initialize the items list as empty
- this.items = new ArrayList<>();
- }
- /**
- * Default constructor
- */
- public Hero () {
- // set the hero's name to "Anonymous"
- this.name = "Anonymous";
- // set the max_items to 2
- this.max_items = 2;
- // set hero's health to 100
- this.health = 100;
- // initialize the items list as empty
- this.items = new ArrayList<>();
- }
- /**
- * Inventory method which stores the items the hero's items.
- *
- * @return a string using the .toString method
- */
- public String inventory() {
- String sb = "";
- // If the hero is not carrying anything, print a suitable message
- if (items.size() == 0) {
- sb += ("Your hero is unburdened by wordly possessions.");
- }
- // prints the list of items carried by hero
- else {
- sb += ("Displaying Inventory : ");
- sb += ("[");
- // Loop1: To print all the items
- for (int i = 0; i < items.size(); i++) {
- sb += (items.get(i) + ", ");
- }
- sb += (" ]");
- }
- return sb.toString();
- }
- /**
- * Take method for adding items to hero's inventory/.
- *
- * @param item is used for the hero to take/ carry an item
- */
- public void take(String item) {
- System.out.println("Handling Sir " + getName() + " " + item);
- // If the inventory is full, print an appropriate message
- if(items.size() == max_items) {
- System.out.println("Your hero is overburdened and cannot carry more...");
- } else {
- //add the item to the inventory
- items.add(item);
- }
- }
- /**
- * Drop method used for hero to drop items from the hero's inventory
- *
- * @param item which the hero may/may not drop. Depends on either hard-coded hero
- * or the final user input.
- */
- public void drop(String item) {
- // If the hero is not carrying the item, print an appropriate message
- if(!items.contains(item)) {
- System.out.println("Your hero is not carrying that item - " + item);
- } else {
- //drop the item from the inventory
- System.out.println("Brave sir " + getName() + " drops his " + item);
- items.remove(item);
- }
- }
- /**
- * Take damage method for the hero to take damage and lose health
- *
- * @param amount is used to keep track of how much damage the hero can take.
- */
- public void takeDamage(int amount) {
- health -= amount;
- //if hero's health is less than 0, make it 0
- if(health < 0) {
- health = 0;
- System.out.println("Remember, a hero can't have negative health nor health > 100");
- }
- }
- /**
- * Heal method is used to heal the hero
- *
- * @param amount is used to heal the hero.
- */
- public void heal(int amount) {
- health += amount;
- //if hero's health is more than 100, make it 100
- if(health > 100) {
- health = 100;
- System.out.println("Remember, a hero can't have negative health nor health > 100");
- }
- }
- @Override
- /**
- * toString method used for printing necessary information.
- */
- public String toString() {
- return "Displaying hero: Hero: Brave Sir " +this.name+ ", "+
- "health: " + this.health + ", " + "can carry: " + this.max_items+ ", "
- + "current inventory: " + inventory();
- }
- /**
- * getName method to get the hero's name.
- *
- * @return hero's name
- */
- public String getName() {
- return this.name;
- }
- /**
- * setName method to set the hero's name.
- *
- * @param name is hero's name.
- */
- public void setName(String name) {
- this.name = name;
- }
- }
- import java.util.*;
- import java.io.*;
- public class HeroTester {
- public static void main(String[] args) {
- FileWriter writer = null;
- // Option 1 will decide if user chooses hard coded heroes
- // or to create a user inputted hero
- int opt1;
- RandomAccessFile randomAccessFile=null;
- // getting the Path of the File ,
- // You can specify the path of your choice .
- try {
- writer = new FileWriter("C:\Users\Ayugma\Desktop\test.txt", true);
- randomAccessFile = new RandomAccessFile("C:\Users\Ayugma\Desktop\test.txt", "r");
- do {
- //Go to last line of the file
- randomAccessFile.seek(randomAccessFile.length()-1L);
- //Get the input
- opt1 = Integer.parseInt(randomAccessFile.readLine());
- writer.write("n");
- if (opt1 != 1 && opt1 != 2) {
- writer.write("Please enter a valid choice!");
- }
- } while (opt1 != 1 && opt1 != 2);
- if (opt1 == 1) {
- // Hard code the hero values
- writer.write("n----------Creating Heroes----------");
- writer.write("Creating Hero with no name.");
- // create a new hero using default constructor
- Hero hero1 = new Hero();
- // print hero1 state
- writer.write(hero1.toString());
- writer.write("Creating Hero with a name.");
- // create a new hero using argumented constructor
- Hero hero2 = new Hero("Robin", 3);
- // print hero2 state
- writer.write(hero2.toString());
- // Add an item to inventory
- hero2.take("sword");
- writer.write(hero2.inventory());
- // Add an item to inventory
- hero2.take("spoon");
- writer.write(hero2.inventory());
- // Add an item to inventory
- hero2.take("cape of good fortune");
- writer.write(hero2.inventory());
- // Add an item to inventory
- hero2.take("tomato");
- writer.write(hero2.inventory());
- // Drop an item from inventory
- hero2.drop("spoon");
- writer.write(hero2.inventory());
- // Drop an item from inventory
- hero2.drop("watch");
- writer.write(hero2.inventory());
- // Drop an item from inventory
- hero2.drop("sword");
- writer.write(hero2.inventory());
- // Drop an item from inventory
- hero2.drop("cape of good fortune");
- writer.write(hero2.inventory());
- // Drop an item from inventory
- hero2.take("pointy hat of success");
- writer.write(hero2.inventory());
- // Reduce health
- writer.write("Brave sir " + hero2.getName() + " is so busy worrying about his inventory"
- + "he doesn't notice the danger and takes 50" + " damage from a shrubery!");
- hero2.takeDamage(50);
- writer.write(hero2.toString());
- // Increase health
- writer.write("Brave sir " + hero2.getName() + " takes a deep breath" + " and recovers 5 health");
- hero2.heal(5);
- writer.write(hero2.toString());
- // Reduce health
- writer.write("Brave sir " + hero2.getName() + " is so busy worrying about his inventory"
- + " he doesn't notice the danger and takes 119" + " damage from a shrubery!");
- hero2.takeDamage(119);
- writer.write(hero2.toString());
- // Increase health
- writer.write(
- "Brave sir " + hero2.getName() + " enters the Avatar state" + " and heals for 128 points.");
- hero2.heal(128);
- writer.write(hero2.toString());
- // print the heros' final state
- writer.write("n----------Final State----------");
- writer.write(hero1.toString());
- writer.write(hero2.toString());
- } else if (opt1 == 2) {
- // User wants to enter values
- Hero hero;
- int opt2;
- do {
- writer.write("1. Create an Anonymous Hero. n2. Create a hero with a name.");
- writer.write("nEnter your choice: ");
- //Go to last line of the file
- randomAccessFile.seek(randomAccessFile.length()-1L);
- //Get the input
- opt2 = Integer.parseInt(randomAccessFile.readLine());
- if (opt2 != 1 && opt2 != 2) {
- writer.write("Please enter a valid choice! ");
- }
- } while (opt2 != 1 && opt2 != 2);
- writer.write("n");
- if (opt2 == 1) {
- writer.write("Creating Hero with no name.");
- // create a new hero using default constructor
- hero = new Hero();
- // print hero state
- writer.write(hero.toString());
- } else {
- writer.write("Creating Hero with a name.");
- writer.write("Enter hero's name: ");
- //Go to last line of the file
- randomAccessFile.seek(randomAccessFile.length()-1L);
- String name =randomAccessFile.readLine();
- writer.write("nEnter max items that the hero can carry: ");
- //Go to last line of the file
- randomAccessFile.seek(randomAccessFile.length()-1L);
- int max =Integer.parseInt(randomAccessFile.readLine());
- // create a new hero using argumented constructor
- hero = new Hero(name, max);
- // print hero state
- writer.write(hero.toString());
- }
- while (true) {
- // Menu options
- writer.write("n1. Add item to inventory n2. Drop item from inventory"
- + "n3. Cause damage to the hero n4. Heal the hero"
- + "n5. Display State of the hero n6. Exitn");
- writer.write("nEnter your choice: ");
- int opt3;
- //Go to last line of the file
- randomAccessFile.seek(randomAccessFile.length()-1L);
- //Get the input
- opt3 = Integer.parseInt(randomAccessFile.readLine());
- writer.write("n");
- switch (opt3) {
- case 1:
- // add item to hero's inventory
- writer.write("Enter item's name to be added: ");
- //Go to last line of the file
- randomAccessFile.seek(randomAccessFile.length()-1L);
- String take =randomAccessFile.readLine();
- hero.take(take);
- writer.write(hero.toString());
- break;
- case 2:
- // drop item from hero's inventory
- writer.write("Enter item's name to be dropped: ");
- //Go to last line of the file
- randomAccessFile.seek(randomAccessFile.length()-1L);
- String drop =randomAccessFile.readLine();
- hero.drop(drop);
- writer.write(hero.toString());
- break;
- case 3:
- // Cause damage to the hero
- writer.write("Enter the health damage amount: ");
- //Go to last line of the file
- randomAccessFile.seek(randomAccessFile.length()-1L);
- int damage =Integer.parseInt(randomAccessFile.readLine());
- hero.takeDamage(damage);
- writer.write(hero.toString());
- break;
- case 4:
- // Heal the hero
- writer.write("Enter the healing amount: ");
- //Go to last line of the file
- randomAccessFile.seek(randomAccessFile.length()-1L);
- int heal =Integer.parseInt(randomAccessFile.readLine());
- hero.heal(heal);
- writer.write(hero.toString());
- break;
- case 5:
- // Display the hero's state
- writer.write(hero.toString());
- break;
- case 6:
- writer.write("Exiting!!");
- return;
- default:
- // In case user enters an invalid option.
- writer.write("nEnter a valid option!!");
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if(writer!=null)
- {
- writer.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment