Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8894187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:25:55+00:00 2026-06-14T23:25:55+00:00

I am trying to implement the state pattern but the problem is, that I

  • 0

I am trying to implement the state pattern but the problem is, that I am not changing the states and when I use a test class it is using the concrete class that has a relationship with the interface.

For example in the fire and move methods in the state classes are not working and returning the result of the Railgun.

Here is the code:

public interface RailgunState 
{

    public String fire(Point p, int rounds);
    public String move(Point p);
}

public class Railgun 
{
    /**
     * @param ammo repersent the current ammonitions.
     */
     int ammo = 0;
     /**
     * @param MAX_AMMO repersent the maximum ammonitions.
     */
      public static int MAX_AMMO = 10;
    int test = 10;
    /**
     *@param position represent the possition of the state 
     */
    Point position;
    Point point;
    //ask mark
    int x = 0;
    int y = 0;
    int result = 6;
    /**
     * @param damagedState represent an object of DamagedState
     */
    RailgunState damagedState ;
    RailgunState needAmmoState ;
    RailgunState normalState ;
     /**
     * @param state represent the state of the RailGun
     */
    RailgunState state; 

    Railgun()
    {
        damagedState = new DamagedState();
        needAmmoState = new NeedAmmoState();
        normalState = new NormalState();
        state = normalState;
    }
    /** fire().
     * @param p is the coordinates of the target
     * @param rounds number of rounds HQ has requested
     * @return success/ partial success/ failure
     */
    public String fire(Point p, int rounds)
    {
         return "Fire order: Success 6/6";
    }

    /**  move(). success or failure only the string
     * @param p is the destination
     * @return success or failure when a railgun can move to the destination
     */
    public String move(Point p)
    {
        position = p;
        return  "Move order: Success" ; 
    }

    /**getPosition().
     * @return the possstion of the point 
     */
    public Point getPosition()
    {             
        return position ;
    }

    void setPosition(Point position)
    {
        this.position = position;
    }

     public RailgunState getState()
    {
        return state;
    }

    void setAmmo(int ammo) 
    {
        this.ammo = ammo;
    }

    public Point getPoint() {
        return point;
    }

    public RailgunState getDamagedState() {
        return damagedState;
    }

    public RailgunState getNeedAmmoState() {
        return needAmmoState;
    }

    public RailgunState getNormalState() {
        return normalState;
    }

    int getAmmo()
    {
        return MAX_AMMO;
    }

    void setState(RailgunState state)
    {
         this.state = state;
    }
    void setState(NeedAmmoState needAmmoState)
    {
        this.state = needAmmoState;
    }

    void setState(NormalState normal)
    {
        this.state = normalState;
    }
        void setState(DamagedState damagedState)
    {
        this.state = damagedState;
    }
}


public class NormalState implements RailgunState {

    Railgun railgun;    
    static int MAX_AMMO = 10;
    int result = 6;

    NormalState()
    {
    }
    @Override
    public String fire(Point p, int rounds) 
    {
        MAX_AMMO -= rounds;
        if((MAX_AMMO >= 0) && (rounds == result))
        {
            NeedAmmoState needAmmo = new NeedAmmoState();
            System.out.println("Seccuees: " + MAX_AMMO);
            NormalState state = new NormalState();
            railgun.setState(state);
            return "Fire order: Success 6/6";
        }
        else if((MAX_AMMO < 0) && (MAX_AMMO != -result))
        {
              System.out.println("Partial: " + MAX_AMMO);
              Railgun.MAX_AMMO = 0;
              System.out.println("Partial: " + MAX_AMMO);
              NeedAmmoState needAmmo = new NeedAmmoState();
              railgun.setState(needAmmo);
              return "Fire order: Partial success 4/6";
        }

        else 
        {
             System.out.println("Fail: " + MAX_AMMO);
             DamagedState damaged = new DamagedState();
             railgun.setState(damaged);
             return "Fire order: Failure 0/6";
        }
    }

    @Override
    public String move(Point p)
    {
        return p + "";
    }

    /**getPosition().
     * @return the possstion of the point 
     */
    public Point getPosition()
    {             
        return railgun.getPosition();
    }
}


public class NeedAmmoState implements RailgunState
{
    Point position;
    Railgun railgun;
    NeedAmmoState()
    {
    }

    @Override
    public String fire(Point p, int rounds)
    {
          return "Fire order: Failed 0/4";
    }

    @Override
    public String move(Point p)
    {
        railgun = new Railgun();
         railgun.move(p);
        position = p;
        return  "Move order: Success" ; 
    }

    void setPosition(Point position)
    {
        this.position = position;
    }
    int getAmmo()
    {
        return Railgun.MAX_AMMO;
    }

    /**getPosition().
     * @return the possstion of the point 
     */
    public Point getPosition()
    {             
        int x = 320;
        int y = 43;
        Point p = new Point(x,y);
        return p;
    }
}


public class NormalStateTest {

    /**
     * Test of fire method, of class NormalState.
     */
    @Test
    public void testFire() {
        final Railgun railgun = new Railgun();
        railgun.setState(new NormalState());
        final int numRounds = 6;
        final int x = 100;
        final int y = 340;

        // This fire mission should be completely successful
        String actualResult = railgun.fire(new Point(x, y), numRounds);
        String expectedResult = "Fire order: Success 6/6";
        //System.out.println(actualResult);
        assertEquals(expectedResult, actualResult);

        // This fire mission should be partially successful
        actualResult = railgun.fire(new Point(x, y), numRounds);
        //System.out.println(actualResult);
        expectedResult = "Fire order: Partial success 4/6";
        assertEquals(expectedResult, actualResult);

        // This fire mission should fail
        actualResult = railgun.fire(new Point(x, y), numRounds);
        //System.out.println(actualResult);
        expectedResult = "Fire order: Failure 0/6";
        assertEquals(expectedResult, actualResult);

        // Check state change to NeedAmmo state
        assertEquals(railgun.getState().getClass(), NeedAmmoState.class); 
    }

    /**
     * Test of move method, of class NormalState. Should always succeed.
     */
    @Test
    public void testMove() {
        final Railgun railgun = new Railgun();
        final int x = 129;
        final int y = 444;
        final String actualResult = railgun.move(new Point(x, y));
        final String expectedResult = "Move order: Success";
        System.out.println(actualResult);
        assertEquals(new Point(x, y), railgun.getPosition());
        assertEquals(expectedResult, actualResult);
    }
}

So the problem is that when testing in NormalStateTest.java for example, the result of the classes like NormalState and NeedAmmoSate are returning the values from the Railgun in the methods fire and move.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:25:56+00:00Added an answer on June 14, 2026 at 11:25 pm

    If you want the RailGun.fire() method delegate to the current state fire() method, then do so. In your code, it returns a hard-coded string and doesn’t use the state at all.

    Change

    public String fire(Point p, int rounds) {
         return "Fire order: Success 6/6";
    }
    

    to

    public String fire(Point p, int rounds) {
         return state.fire(p, rounds);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement the APM pattern using Richter's AsyncEnumerator class. The goal is
I'm trying to implement a State Pattern in C++, but have problems with the
Im trying to apply the state pattern on a multi threaded application.The problem is
I'm trying to implement the command design pattern , but I'm stumbling accross a
Not sure if I'm doing something wrong ... but I'm trying to implement a
I am trying to implement a function to change state of the menu, but
I am trying to implement the command pattern to control a robot. I'm using
I'm trying to implement Finite-State Machine in C and need it to be very
Trying to implement 3-layer (not: tier, I just want to separate my project logically,
Come across a problem with the repository pattern combined with the use of abstract

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.