Sepp1
Sepp1

Reputation: 45

how to change toString() value

How do I make it so I'm able to change the jar1.toString() value and put it in the player.toString() value.

my programs output:
"Jill[](1) [a#1](1) [b#2](2) [c#3](3) /7\(4)"
desired output when using turn() method:
"Jill[a#1](1) [](1) [b#2](2) [c#3](3) /7\(4)"

I've already made it so that when a player inputs the string "P" for pickup, it deletes the value in the 1st jar "[](1) [b#2](2) [c#3](3)", I did this by making stone null and doing an if statement under the jar class java if stone == null we return the empty brackets [] but it is yet to transfer it to the player.toString() jar "Jill[](1)"

i've tried implementing if statements for Class Player String toString() method,

   public String toString()  {
        return name + "[" + "]" + "(" + position + ")";
    }

something along the lines of return name + "[" + stone.toString() "]" + "(" + position + ")";


public class Ground
{
    public Jar jar1;
    private Jar jar2;
    private Jar jar3;
    private Player player;
    private Chest chest;
    private String move;

   public String toString(){
    return player.toString() + " " +
    jar1.toString() + " " + 
    jar2.toString() + " " +
    jar3.toString() + " " +
    "/" + chest.combination + "\\" + "(" + chest.getPosition() + ")";

   }

   public void turn(){
    System.out.print("Move (l/r/p/d): ");
    move = Global.keyboard.nextLine();

   if (move.equals("p") && player.getPosition() == jar1.getPosition()){
      jar1 = new Jar(1, null);
    }
public class Jar
{

    private int position;
    private Stone stone;
    private Player pos;

    public String toString() {
        if (stone == null) {
            return "[]" + "(" + getPosition() + ")";
        }
        return "[" + stone.toString() + "]" + "(" + getPosition() + ")";
    }

public class Player
{

    private String name;
    private int position;
    private static Jar jar;
    private Stone stone;
    private Ground ground;

    public String toString()  {
        return name + "[" + "]" + "(" + position + ")";
    }
public class Stone
{

    private String name;
    private int weight;


    public Stone()
    {
        System.out.print("Enter stone name: ");
        name = Global.keyboard.nextLine();

        System.out.print("Enter stone weight: ");
        weight = Global.keyboard.nextInt();
        Global.keyboard.nextLine();
    }

public String toString()
    {
      return name + "#" + weight;
    }

Upvotes: 0

Views: 287

Answers (1)

Ruzihm
Ruzihm

Reputation: 20269

Change Player.toString to include the player's stone. Also add a stone setter if you don't already have one:

public class Player {
    private String name;
    private int position;
    private static Jar jar;
    private Stone stone;
    private Ground ground;

    public String toString()  {
        return name + "[" + stone + "]"  + "(" + position + ")";
    }

    public void setStone(Stone s) {
        stone = s;
    }
}

Add a stone getter to the Jar if you don't already have one:

public class Jar
{

    private int position;
    private Stone stone;
    private Player pos;

    public String toString() {
        if (stone == null) {
            return "[]" + "(" + getPosition() + ")";
        }
        return "[" + stone.toString() + "]" + "(" + getPosition() + ")";
    }

    public Stone getStone() {
        return stone;
    }

And when the player takes the turn, have them pick up the stone:

public void turn() {
    System.out.print("Move (l/r/p/d): ");
    move = Global.keyboard.nextLine();

    if (move.equals("p") && player.getPosition() == jar1.getPosition()) {

        player.setStone(jar1.getStone());
        jar1 = new Jar(1, null);
    }
}

Upvotes: 1

Related Questions