Skip to content

Instantly share code, notes, and snippets.

@sonAndrew
Created March 16, 2020 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonAndrew/4cd9aab78afb695fe2ebf704144804ad to your computer and use it in GitHub Desktop.
Save sonAndrew/4cd9aab78afb695fe2ebf704144804ad to your computer and use it in GitHub Desktop.
/** AUTHOR : [ Andrew Jackson ]
* COURSE : [ CPT 187 ]
* PURPOSE : [ To calculate and generate a receipt for your purchase at SodNotZod.]
* STARTDATE : [ 03/12/2020 ] **/
package edu.cpt187.jackson.project1;
import java.util.Scanner;
public class Metropolis {
//NEW Scanner OPEN
public static Scanner input = new Scanner(System.in);
public static final String[] MENU_OPTIONS = {"Create a Metropolitan Train simulation", "Run a Metropolitan Train simulation", "Quit"};
public static void main(String[] args) {
// LOCAL VARIABLES
String userName = "";
char menuSelection = ' ';
// INITIALIZE SUPPORT CLASS
SubwayTrain currentTrain = new SubwayTrain();
// WELCOME BANNER
displayWelcomeBanner();
// GET USER NAME
userName = getUserName(input);
// SIMULATION SELECTION MENU
menuSelection = validateMenuSelection(input);
// WHILE SELECTION IS NOT 'Q'/"QUIT"
while(menuSelection != 'Q')
{
if(menuSelection == 'A') {
currentTrain.setNumberOfStations(validateNumberOfStations(input));
currentTrain.setHomeStation(validateHomeStation(input));
currentTrain.setMaximumCapacity(validateMaximumCapacity(input));
currentTrain.setMaximumStops();
currentTrain.setStations();
}// menuSelection == 'A'
else {
if(currentTrain.getMaximumCapacity() <= 0)
{
displaySimulationError();
} // currentTrain.getMaximumCapacity() <= 0
else {
currentTrain.setLoadPassengers(validateNumberWaiting(input));
displayLoadingAnnouncement(currentTrain.getNumberLoaded());
displayDepartingAnnouncement(currentTrain.getCurrentStation(), currentTrain.getNumberOnBoard());
currentTrain.setMoveStation();
while(currentTrain.getStopCounter() < currentTrain.getMaximumStops())
{
displayArrivalAnnouncement(currentTrain.getCurrentStation());
currentTrain.setUnloadPassengers(validateNumberLeaving(input));
displayUnloadingAnnouncement(currentTrain.getNumberUnloaded(), currentTrain.getNumberOnBoard());
currentTrain.setLoadPassengers(validateNumberWaiting(input));
displayLoadingAnnouncement(currentTrain.getNumberLoaded());
displayDepartingAnnouncement(currentTrain.getCurrentStation(), currentTrain.getNumberOnBoard());
currentTrain.setMoveStation();
} // currentTrain.getStopCounter() < currentTrain.getMaxStops()
displayArrivalAnnouncement(currentTrain.getCurrentStation());
currentTrain.setUnloadAllPassengers();
displayUnloadingAnnouncement(currentTrain.getNumberUnloaded(), currentTrain.getNumberOnBoard());
displayEndOfSimReport(currentTrain.getMaximumCapacity(), currentTrain.getNumberOfStations(), currentTrain.getStations(), currentTrain.getStopCounter());
currentTrain.setResetSimulation();
} // END - else / currentTrain.getMaximumCapacity() <= 0
} // END - else / menuSelection == 'A'
menuSelection = validateMenuSelection(input);
} // menuSelection != 'Q'
if(currentTrain.getCreateCounter() > 0)
{
displayUsageReport(userName, currentTrain.getCreateCounter(), currentTrain.getRunCounter());
}
displayFarewellMessage();
input.close();
}
////////////////////
// DISPLAYS
////////////////////
public static void displayWelcomeBanner()
{
// WELCOME BANNER
System.out.println("Hello and welcome to your local home improvement store,");
System.out.println("SodNotZod. We look forward to addressing all your needs.");
} // END - displayWelcomeBanner
public static void displayFarewellMessage()
{
// FAREWELL MESSAGE
System.out.println("Thanks for using the Metropolis Subway Train,");
System.out.println("Simulation. Until next time, come again.");
} // END - displayFarewellMessage
public static void displaySimulationMenu()
{
// SIMULATION MENU
System.out.println("\nSIMULATION MENU");
System.out.println("_____________________");
System.out.printf("\n%-5s%-5s%-5s","[A]","for ",MENU_OPTIONS[0]);
System.out.printf("\n%-5s%-5s%-5s","[B]","for ",MENU_OPTIONS[1]);
System.out.printf("\n%-5s%-5s%-5s","[C]","for ",MENU_OPTIONS[2]);
} // displaySimulationMenu
public static void displaySimulationError()
{
// WARNING / NOTICE
System.out.println(" NOTICE");
System.out.println("_____________________");
System.out.print("A simulation must be created before a simulation can be run.");
} // displaySimulationError
public static void displayLoadingAnnouncement(int borrowedNumberLoaded)
{
// LOADING ANNOUNCEMENT
System.out.println("\nLoading Announcement");
System.out.println("_____________________");
System.out.printf("\n%-5d,%s", borrowedNumberLoaded, "Passengers");
} // END - displayEndOfSimReport
public static void displayDepartingAnnouncement(int borrowedCurrentStation, int borrowedNumberOnBoard)
{
// DEPARTING ANNOUNCEMENT
System.out.println("\nDeparting Announcement");
System.out.println("_____________________");
System.out.printf("\n%-5s,%s","Leaving", "Passengers");
System.out.printf("\n%-5s,%s","Station", "Onboard");
System.out.printf("\n%-5d,%-5d",borrowedCurrentStation, borrowedNumberOnBoard);
} // END - displayEndOfSimReport
public static void displayArrivalAnnouncement(int borrowedCurrentStation)
{
// ARRIVAL ANNOUNCEMENT
System.out.println("\nArival Announcement");
System.out.println("_____________________");
System.out.printf("\n%-5s,%s","Arriving at", "Passengers");
System.out.printf("\n%-5s,%s","Station", "Onboard");
System.out.printf("\n%-5d,%s",borrowedCurrentStation, "[\"###\"]");
} // END - displayEndOfSimReport
public static void displayUnloadingAnnouncement(int borrowedNumberUnloaded, int borrowedNumberOnBoard)
{
// UNLOADING ANNOUNCEMENT
System.out.println("\nUnloading Announcement");
System.out.println("_____________________");
System.out.printf("\n%-5s,%-5d%d","Now unloading: ", borrowedNumberOnBoard, borrowedNumberUnloaded);
} // END - displayEndOfSimReport
public static void displayEndOfSimReport(int borrowedMaximumCapacity, int borrowedNumberOfStations, int[] borrowedStations, int borrowedStopCounter)
{
// SIMULATION REPORT
System.out.println("\nUsage Report");
System.out.println("_____________________");
System.out.printf("\n%-5s,%s","Maximum capacity", borrowedMaximumCapacity);
System.out.printf("\n%-5s,%d","Number of stations", borrowedNumberOfStations);
System.out.printf("\n%-5s,%d","Stations", borrowedStations);
System.out.printf("\n%-5s,%d","Stop counter", borrowedStopCounter);
} // END - displayEndOfSimReport
public static void displayUsageReport(String borrowedUserName, int borrowedCreateCounter, int borrowedRunCounter)
{
// USAGE REPORT
System.out.println("\nUsage Report");
System.out.println("_____________________");
System.out.printf("\n%-5s,%s","User name: ", borrowedUserName);
System.out.printf("\n%-5s,%d","Create counter", borrowedCreateCounter);
System.out.printf("\n%-5s,%d","Run counter", borrowedRunCounter);
} // END - displayUsageReport
//////////////////////////
// GETTERS
//////////////////////////
public static String getUserName(Scanner borrowedInput)
{
// LOCAL VARIABLES
String localUserName = "";
// FIRST NAME
System.out.println("\nWhat is your first name?");
localUserName = borrowedInput.nextLine();
return localUserName;
} // END - getUserName
////////////////////
// VALIDATION
////////////////////
public static int validateNumberOfStations(Scanner borrowedInput) {
// LOCAL VARIABLES
int localNumberOfStations;
// GET NUMBER OF STATIONS
System.out.printf("\nWhat is the number of stations?");
localNumberOfStations = borrowedInput.next().toUpperCase().charAt(0);
while(localNumberOfStations <= 0) {
// ERROR MESSAGE
System.out.print("The number of stations can not be zero.");
// GET NUMBER OF STATIONS
System.out.printf("\nWhat is the number of stations?");
localNumberOfStations = borrowedInput.next().toUpperCase().charAt(0);
}
return localNumberOfStations;
} // END - validateNumberOfStations
public static int validateHomeStation(Scanner borrowedInput) {
// LOCAL VARIABLES
int localHomeStation;
// GET HOME STATION
System.out.printf("\nWhat is the home station number?");
localHomeStation = borrowedInput.next().toUpperCase().charAt(0);
while(localHomeStation <= 0) {
// ERROR MESSAGE
System.out.print("The home station can not be zero.");
// GET HOME STATION
System.out.printf("\nWhat is the home station number?");
localHomeStation = borrowedInput.next().toUpperCase().charAt(0);
}
return localHomeStation;
} // END - validateHomeStation
public static int validateMaximumCapacity(Scanner borrowedInput) {
// LOCAL VARIABLES
int localMaxCapacity;
// GET MAXIMUM CAPACITY
System.out.printf("\nWhat is the maximum capacity?");
localMaxCapacity = borrowedInput.next().toUpperCase().charAt(0);
while(localMaxCapacity <= 0) {
// ERROR MESSAGE
System.out.print("Maximum capacity can not be zero.");
// GET MAXIMUM CAPACITY
System.out.printf("\nWhat is the maximum capacity?");
localMaxCapacity = borrowedInput.next().toUpperCase().charAt(0);
}
return localMaxCapacity;
} // END - validateMaximumCapacity
public static int validateNumberWaiting(Scanner borrowedInput)
{
// LOCAL VARIABLES
int localNumberWaiting;
// GET NUMBER WAITING
System.out.printf("\nHow many people are waiting?");
localNumberWaiting = borrowedInput.next().toUpperCase().charAt(0);
while(localNumberWaiting <= 0) {
// ERROR MESSAGE
System.out.print("The number of people waiting can not be zero.");
// GET NUMBER WAITING
System.out.printf("\nHow many people are waiting?");
localNumberWaiting = borrowedInput.next().toUpperCase().charAt(0);
}
return localNumberWaiting;
} // END - validateNumberWaiting
public static int validateNumberLeaving(Scanner borrowedInput) {
// LOCAL VARIABLES
int localNumberLeaving;
// GET NUMBER WAITING
System.out.printf("\nHow many people are leaving?");
localNumberLeaving = borrowedInput.next().toUpperCase().charAt(0);
while(localNumberLeaving <= 0) {
// ERROR MESSAGE
System.out.print("Number of people leaving cannot be zero.");
// GET NUMBER WAITING
System.out.printf("\nHow many people are leaving?");
localNumberLeaving = borrowedInput.next().toUpperCase().charAt(0);
}
return localNumberLeaving;
} // END - validateNumberLeaving
public static char validateMenuSelection(Scanner borrowedInput)
{
// LOCAL VARIABLES
char simSelection = ' ';
// SIMULATION MENU
displaySimulationMenu();
// LOCAL SELECTION
System.out.printf("\nPlease make your selection here: ");
simSelection = borrowedInput.next().toUpperCase().charAt(0);
// WHILE localSelection
while(simSelection != 'A' && simSelection != 'B' && simSelection != 'C' && simSelection != 'Q')
{
// ERROR MESSAGE
displaySimulationError();
// SIMULATION MENU
displaySimulationMenu();
// LOCAL SELECTION
System.out.printf("\nPlease make your selection here: ");
simSelection = borrowedInput.next().toUpperCase().charAt(0);
} // END - simSelection
return simSelection;
} // END - validateMenuSelection
}
/** AUTHOR : [ Andrew Jackson ]
* COURSE : [ CPT 187 ]
* PURPOSE : [ Support class ]
* STARTDATE : [ 03/12/2020 ] **/
package edu.cpt187.jackson.project1;
public class SubwayTrain {
private final int ZERO = 0;
private final int TWO = 2;
private int numberOfStations;
private int maximumStops;
private int homeStation;
private int[] stations;
private int createCounter;
private int runCounter;
private int maximumCapacity;
private int stopCounter;
private int currentStationIndex;
private int numberOnBoard;
private int numberLoaded;
private int numberUnloaded;
public void SubwayTrian() {}
//////////////////////////
// SETTERS
//////////////////////////
public void setNumberOfStations(int borrowedNoOfStations)
{
numberOfStations = borrowedNoOfStations;
}; // setNumberOfStations
public void setMaximumStops()
{
maximumStops = (numberOfStations * TWO) - TWO;
}; // setMaximumStops
public void setHomeStation(int borrowedHomeStation)
{
homeStation = borrowedHomeStation;
}; // setHomeStation
public void setStations()
{
int localIndex = 0;
int[] stations = new int[numberOfStations];
while(localIndex < stations.length)
{
stations[localIndex] = homeStation + localIndex;
localIndex++;
}
}; // setStations
public void setMaximumCapacity(int borrowedMaxCapacity)
{
maximumCapacity = borrowedMaxCapacity;
}; // setMaximumCapacity
public void setMoveStation()
{
stopCounter++;
if(stopCounter < numberOfStations)
{
currentStationIndex = stopCounter;
}
else {
currentStationIndex = stopCounter - 1;
}
}; // setMoveStations
public void setLoadPassengers(int borrowedNumberWaiting)
{
if(borrowedNumberWaiting > numberLoaded - numberOnBoard)
{
numberLoaded = maximumCapacity - numberOnBoard;
}
else
{
numberLoaded = borrowedNumberWaiting;
}
numberOnBoard = numberOnBoard + numberLoaded;
}; // setLoadPassengers
public void setUnloadPassengers(int borrowedNumberLeaving)
{
if(borrowedNumberLeaving > numberOnBoard)
{
numberUnloaded = numberOnBoard;
}
else
{
numberUnloaded = borrowedNumberLeaving;
}
numberOnBoard = numberOnBoard - numberUnloaded;
}; // setUnloadPassengers
public void setUnloadAllPassengers()
{
numberLoaded = numberLoaded - numberLoaded;
}; // setUnloadAllPassengers
public void setResetSimulation()
{
numberOfStations = ZERO;
homeStation = ZERO;
maximumCapacity = ZERO;
}; // setResetSimulation
//////////////////////////
// GETTERS
//////////////////////////
public int getNumberOfStations()
{
return numberOfStations;
}; // getNumberOfStations
public int getMaximumStops()
{
return maximumStops;
}; // getMaximumStops
public int getHomeStation()
{
return homeStation;
}; // getHomeStation
public int[] getStations()
{
return stations;
}; // getStations
public int getCreateCounter()
{
return createCounter;
}; // getCreateCounter
public int getRunCounter()
{
return runCounter;
}; // getRunCounter
public int getMaximumCapacity()
{
return maximumCapacity;
}; // getMaximumCapacity
public int getStopCounter()
{
return stopCounter;
}; // getStopCounter
public int getCurrentStation()
{
return currentStationIndex;
}; // getCurrentStation
public int getNumberOnBoard()
{
return numberOnBoard;
}; // getNumberOnBoard
public int getNumberLoaded()
{
return numberLoaded;
}; // getNumberLoaded
public int getNumberUnloaded()
{
return numberUnloaded;
}; // getNumberUnloaded
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment