Make this code better

Singleton Pattern Program

Write a Java program (non-GUI preferred) to demonstrate the Singleton pattern.
The key parts of the singleton pattern are:
A private static variable to store the single instance called the singleton
A public static method for callers to get a reference to the instance
A private constructor so no callers can instantiate the object directly
Using these key parts, write a Java program that will allow a user of the program to assign only one runner to each of the 8 lanes of running track in a field.

SingletonPatternProgram
* Ava Hunt * 28 October 2017 * University of Phoenix * Write a Java program (non-GUI preferred) to demonstrate the Singleton pattern. */ package singletonpatternprogram; import java.util.HashSet; import java.util.Set; /** * * @author Ava */ public class SingletonPatternProgram { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here } public class Runners { private Runners getLane(int lane) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private final Runners LANE = new Runners(); private final Set<Integer> availableLanes; public Runners getLane() { return LANE; } private Runners() { availableLanes = new HashSet<>(); availableLanes.add(1); availableLanes.add(2); availableLanes.add(3); availableLanes.add(4); availableLanes.add(5); availableLanes.add(6); availableLanes.add(7); availableLanes.add(8); } public boolean takeLane(int lane) { return availableLanes.remove(lane); } public void main(String[] args) { assignLane(1); assignLane(2); assignLane(3); assignLane(4); assignLane(5); assignLane(6); assignLane(7); assignLane(8); } private void assignLane(int lane) { Runners runners; runners = Runners.getLane(lane); System.out.println(runners.takeLane(lane)); } } }

Reactions 3 reactions

av1046 Ava Hunt commented on this code
0
There seems to be some sort of error on Line 58 and Line 59 and thats what i need help with. Other than that the code builds and runs successfully.
av1046 Ava Hunt commented on this code
0
There seems to be some sort of error on Line 58 and Line 59 and thats what i need help with. Other than that the code builds and runs successfully.
av1046 Ava Hunt commented on this code
0
There seems to be some sort of error on Line 58 and Line 59 and thats what i need help with. Other than that the code builds and runs successfully.

Comment on this code:

Please Login or Register to leave a comment.