Reputation: 331
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
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
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);
}
}
Upvotes: 0
Views: 101
Reputation: 27966
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.
Upvotes: 1
Reputation: 4202
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);
}
}
Upvotes: 0