0

I am learning Java and I am working in Java class Cycles, which mimics the London Boris bicycles. To create a interface that counts the number of time a bycicle station is used. The output I want is something like this;

  • Please enter docking No. for Cycle 1 - 0
  • Please enter docking No. for Cycle 2 - 1
  • Please enter docking No. for Cycle 3 - 2
  • Cycle with ID: 101 is in use
  • Cycle with ID: 102 is at docking station 1
  • Cycle with ID: 103 is at docking station 2

Number of cycles currently in the systems Cycle@355da254

What I want to achieve instead is

  • Please enter docking No. for Cycle 1 0
  • Please enter docking No. for Cycle 2 1
  • Please enter docking No. for Cycle 3 2
  • Cycle with ID: 101 is in use
  • Cycle with ID: 102 is at docking station 1
  • Cycle with ID: 103 is at docking station 2

Number of cycles currently in the systems 3

Here is the source code Cycle Class;

/**
* Created by devops05 on 28/02/16.
*/

public class Cycle {
private int ID;
private int dockingStationID;
private static int lastAssignedNumber = 100;
//private int count;

public Cycle()

{
    lastAssignedNumber ++;
    ID = lastAssignedNumber;
    dockingStationID = 0;

}

public int getID()
{
    return ID;
}

public boolean pickup()

{
    if (dockingStationID == 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

public boolean park(int dockSID)
{
    if (dockingStationID == 0)
    {
        dockingStationID = dockSID;
        return true;
    }
    else
    {
        return false;
    }

}

public String getDockingStationNo()
{
    if(dockingStationID == 0)
    {
        return " is in use ";
    }
    else
    {
        return " is at docking station "+ dockingStationID;
    }

}
public int getNumberOfCycles()
{
    int cycle1 = 1;
    int cycle2 = 1;
    int cycle3 = 1;
    int cycleTotal = cycle1 + cycle2 + cycle3;
    //for(int count = 0; count<3; count++)
    return cycleTotal;
}
}

The second Java surce Class CycleTest;

/**
* Created by devops05 on 28/02/16.
*/
import java.util.Scanner;


public class CycleTest {
public static void main(String[] args)
{
    Cycle cycle1 = new Cycle();
    //int limit = 3;
    //int count = 0;
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter docking No. for Cycle 1");
    int dockSID = in.nextInt();
    cycle1.park(dockSID);
    //for(int i=0; i < dockSID; i++)
    {
        //count++;


        if (cycle1.park(dockSID))
        {
            System.out.println("Cycle with ID: " + cycle1.getID() + "    is in use");
        }
        else
        {
            System.out.println("Cycle with ID: " + cycle1.getID() + cycle1.getDockingStationNo());

        }

        Cycle cycle2 = new Cycle();
        System.out.println("Please enter docking No. for Cycle 2");
        dockSID = in.nextInt();
        cycle2.park(dockSID);

        if (cycle2.park(dockSID))
        {
            System.out.println("Cycle with ID: " + cycle2.getID() + " is in use");
        }
        else
        {
            System.out.println("Cycle with ID: " + cycle2.getID() + cycle2.getDockingStationNo());
        }

        Cycle cycle3 = new Cycle();
        System.out.println("Please enter docking No. for Cycle 3");
        dockSID = in.nextInt();
        cycle3.park(dockSID);
        if (cycle3.park(dockSID))
        {
            System.out.println("Cycle with ID: " + cycle3.getID() + " is in use");
        }
        else
        {
            System.out.println("Cycle with ID: " + cycle3.getID() + cycle3.getDockingStationNo());

        }
        //Cycle cycleTotal = new Cycle();
        //System.out.print("Number of cycles currently in the systems " + cycleTotal);

        Cycle numberOfCycles = new Cycle();
        System.out.print("Number of cycles currently in the systems " + numberOfCycles);

    }

    System.out.println();

    //System.out.println("Number of cycles currently in the system is " + cycle.getNumberOfCycles());
    //System.out.println("Number of cycles currently in the system is " + count);
}
}

2 Answers 2

1

Just to be clear, your question seem to be how to print the total number of cycles. It seems it's currently hardcoded to be three so you can probably just print out "3"!

However I'm assuming that you want a deeper comment about your design. The key problem is that you are using separate variables for each cycle rather than storing them in a collection. Ideally your code should look more like:

class System {
    private final List<Cycle> cycles = new ArrayList<>();

    public void addCycle(Cycle cycle) {
        cycles.add(cycle);
    }

    public int getCycleCount() {
        return cycles.size();
    }
}

Then your main method could be clearer:

System system = new System();
for (int i = 0; i < 3; i++) {
    Cycle cycle = new Cycle(i);
    system.add(cycle);
    System.out.println("Please enter docking number for cycle " + i);
    int dock = in.nextInt();
    if (dock > 0)
        cycle.park(dock);
}
System.out.println("The number of cycles in the system is" + system.getCycleCount());

You could have the list or count of cycles as a static variable in the Cycle class but many designers would consider that as bad design. There are several reasons to avoid these types of global state but the most obvious is that you might want to create cycles that aren't in the system.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @sprinter thank you so very much for your insight. The piece of source code its part of a college course work, one of the pre-requisites for this specific work, is that we are not allowed to use Arrays, at least at this stage. As we have the same idea previously.
0

Create an auxiliary counter where you store the amount of cycles created so far:

public class Cycle {
...
    public static int numberOfCycles;
...
}

public class CycleTest {

    public static void main(String[] args) {
        Cycle cycle1 = new Cycle();
        Cycle.numberOfCycles++;

        ...

        Cycle cycle2 = new Cycle();
        Cycle.numberOfCycles++;

        ...

        Cycle cycle3 = new Cycle();
        Cycle.numberOfCycles++;

        ...

        System.out.print("Number of cycles currently in the systems " + Cycle.numberOfCycles);
    }
}

1 Comment

Hi @aribeiro, I have just implemented your suggested solution and the code just work we get expected result .. Thank you so very much for your help... I am so very glad, as you guys also helped me learn a little bit more about Java thanks ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.