Inheritance: Proper initialization to store values

2019-07-24 04:08发布

问题:

I am working with separate programs, one will read in a .txt file and then that same program will store the read in values into another program containing a HashMap. The program that contains the HashMap, is named Hero.java and is an abstract class. I have a program that extends that program called Cop.java. With that in mind, I would like to know the proper way to transfer the files read in from my GameDriver.java over to the HashMap.

To my knowledge, one way of doing this is initializing Cop.java like so,

Cop N = new Cop();

and then performing a method call

N.rescue();

What is the proper way to add the read in values? Can I use a solution similar to that one? I am trying to get the String cn to be stored in the map name and int pts to be stored in points below. Thanks for all answers and please let me know if you need any clarification. I will post my GameDriver.java code as well as my Hero.java code Thanks!!

GameDriver.java

import java.io.*;
import java.util.*;
import java.awt.*;

public class GameDriver {

  public static void main(String[] args) throws FileNotFoundException {

         Cop N = new Cop();
         String s;
         File f = new File (args[0]);
         Scanner input = new Scanner(f);
         while (input.hasNext()) {
                s = input.next();
                if (s.equalsIgnoreCase("h")) { Character c1 = new Cop();
                                                 System.out.println(c1); }

                if (s.equalsIgnoreCase("r")) { String cn = input.next(); //HashMap name
                                               int pts = input.nextInt(); //HashMap points
                                               N.rescue();}

         }

  }
}

Hero.java

import java.util.*;
import java.io.*;
import java.awt.*;

public abstract class Hero extends Character
{
   private String heroname1;
   public Hero() {
          heroname1 = "Rick Grimes"; //the default player name
   }
   HashMap name = new HashMap<String, GameDriver>();
   HashMap points = new HashMap<Integer, GameDriver>();

   public Hero(String newhero) {
          if (newhero.length() > 0) {
               heroname1 = newhero;
          } else { heroname1 = "Rick Grimes"; } //defaulted as protagonist
   }

   public String getHeroName() {
          return heroname1; //return the name
   }

   public String rescue() { //class to rescue people or things
          return heroname1 + " rescued " + name + " for " + points + "!";
   }

   public String toString() { //print
          return heroname1;

   }
}

Overall Objective

I realize I haven't added in my overall objective: The purpose is reading in a file to create a new hero (usually only one) and having them save multiple people/things in this hypothetical game. For example, if the file says

c h Rick Grimes //create hero
r Carl 100 //rescue for x amount of points

I want that data to be stored in a map or something and be outputted like so:

Rick Grimes
Rick Grimes rescued Carl for 100 points!

also note this all must be done through inheritance