diff --git a/Airport.cpp b/Airport.cpp new file mode 100644 index 0000000..e894105 --- /dev/null +++ b/Airport.cpp @@ -0,0 +1,334 @@ +/* + * John Sadiq + * CS 281 Fall 2019 + * Airport Project + * + * Airport.cpp + * + * + * + */ + +#include "Airport.h" + +using namespace std; + +// Contructor +Airport::Airport() { + + headqueItem = nullptr; + currentPlate = nullptr; + hasAirForce1 = false; + +}; + +//Destructor +Airport::~Airport() {}; + +//Creates the plane as by its status then fills in the rest such as name and fuel, pre adding to the que +void Airport::CreatePlane(string pname, int pfuel, int ptype) +{ + /* + The user can add planes while the simulation is running. + Each time the planes circle and the code reports status, present the menu to the user. + When the user selects Complete the Simulation, just report status at every iteration and don't prompt the user anymore. + */ + + //Prompts choice for the user to choose which plane they would like to create + switch (ptype) + { + case 1: + { planeType y = Private; + addAirplane(*(new Plane(pname, pfuel, y, 1, "QUEUED"))); + break; } + case 2: + { planeType x = Commmercial; + addAirplane(*(new Plane(pname, pfuel, x, 2, "QUEUED"))); + break; } + case 3: + { planeType z = Air_Force_1; + + if (hasAirForce1 == false) + { + addAirplane(*(new Plane(pname, pfuel, z, 3, "QUEUED"))); + + //only allows one air force 1 plane + hasAirForce1 = true; + } + break; } + } +} + +//Add airplane to que +void Airport::addAirplane(Plane newPlane) +{ + + //currentplanes que item + queItem *newqueItem = new queItem(); + newqueItem->plane = newPlane; + + // When adding a new node if the que is empty then add the new node as the head node + if (headqueItem == nullptr) + { + headqueItem = newqueItem; + return; + } + // When the new node has a priority larger then the head node then becomes the head node + else if (newqueItem->plane.getpriority() > headqueItem->plane.getpriority()) { + + headqueItem->prvqueItem = newqueItem; + newqueItem->nxtqueItem = headqueItem; + headqueItem = newqueItem; + return; + } + // any other situation, it will sive through and find its place + else { + + currentqueItem = headqueItem->nxtqueItem; + queItem* lastqueItem = headqueItem; + + //While loop for scanning through and finding its place within the que + while (true) + { + + //If the current node its compairing to is a null + //then the new node will become that place in the que + //using a lastqueItem and currentqueItem to keep correct nodes linked + if (currentqueItem == nullptr) + { + lastqueItem->nxtqueItem = newqueItem; + newqueItem->prvqueItem = lastqueItem; + return; + + } + //If it comes that the newNode has a value that is between 2 nodes + //that already have a connection + else if (newqueItem->plane.getpriority() > currentqueItem->plane.getpriority()) + { + lastqueItem->nxtqueItem = newqueItem; + currentqueItem->prvqueItem = newqueItem; + newqueItem->nxtqueItem = currentqueItem; + newqueItem->prvqueItem = lastqueItem; + return; + } + //moves currentqueItem and LastqueItem over by one until + //it finds its place within the que + else + { + lastqueItem = currentqueItem; + currentqueItem = lastqueItem->nxtqueItem; + } + } + } +} + +//Dequed Plane by taking it out of the list for other use. +Plane Airport::dequeAirplane(struct queItem* currentqueItem) { + + //currentplanes que item + Plane returnPlane = currentqueItem->plane; + + //If one plane is the only plane in the list + if (currentqueItem->prvqueItem == nullptr && currentqueItem->nxtqueItem == nullptr) + { + //sets the only plane to nullptr + headqueItem = nullptr; + } + //If dequeing a node at the front of the list + else if (currentqueItem->prvqueItem == nullptr) + { + //sets the head pointer to the new head node + headqueItem = currentqueItem->nxtqueItem; + //sets the head node to nullptr + currentqueItem->nxtqueItem->prvqueItem = nullptr; + } + //If dequeing a node at the end of the list + else if (currentqueItem->nxtqueItem == nullptr) + { + //sets the tail node to nullptr + currentqueItem->prvqueItem->nxtqueItem = nullptr; + } + //If dequeing a node in the middle of the list, between the first and last node + else + { + //sets the current next pointer to the current previous node + currentqueItem->nxtqueItem->prvqueItem = currentqueItem->prvqueItem; + //sets the current previous pointer to the current next node + currentqueItem->prvqueItem->nxtqueItem = currentqueItem->nxtqueItem; + } + + currentqueItem = nullptr; + delete currentqueItem; + return returnPlane; +} + + +//show status, clasifies plane by its class; QUEUED, CIRCLING, LANDING_NEXT, LANDED, and CRASHED. via Couting vector +void Airport::showStatus() { + + //sets currentqueItem to point to the same location as headqueItem + queItem* currentqueItem = headqueItem; + + + //sives through the list until its end of list + while (currentqueItem != nullptr) + { + //COUT all info for the plane which is from getInfo method from Plane.cpp + //Will COUT Name, Fuel, Priority, Type, and Status + cout << currentqueItem->plane.getInfo() << endl; + currentqueItem = currentqueItem->nxtqueItem; + } +} + +//simulation loop, each time system is looped, an interation goes through and subtract 5% from all flying planes, and has high priority plane land +//aswell as reorganizing priority, if plane goes in to emergency state then we remove it from the vector and insert it to the front giving it a higher priority. +void Airport::simLoop() { + + //sets currentqueItem to point to the same location as headqueItem + queItem* currentqueItem = headqueItem; + + + //FOR LANDED PLANES + //sives through the list until its end of list + if (currentqueItem != nullptr) + { + + //Creates a stack called stackplane for landed planes + Plane stackplane = dequeAirplane(currentqueItem); + stackplane.setStatus("LANDED"); + stackPush(stackplane); + + //If crashed plane is a air force 1 then set to false, allowing a user to make a new air force 1 plane + if (stackplane.getplanetype() == Air_Force_1) + { + hasAirForce1 = false; + } + } + + currentqueItem = headqueItem; + + //during loop, this function lowers the fuel of all planes in the air by 5% + while (currentqueItem != nullptr) + { + currentqueItem->plane.changeFuel(- 5); + currentqueItem->plane.setStatus("CIRCLING"); + currentqueItem = currentqueItem->nxtqueItem; + } + + currentqueItem = headqueItem; + + //this function determines which plane needs to be at the front or is crashed + while (currentqueItem != nullptr) + { + + + //If plane crashed then give lable as crashed and push it to the crashed stack + if (currentqueItem->plane.getFuel() <= 0) + { + currentqueItem->plane.setStatus("CRASHED"); + stackedPushCrashed(currentqueItem->plane); + dequeAirplane(currentqueItem); + currentqueItem = currentqueItem->nxtqueItem; + + //If crashed plane is a air force 1 then set to false, allowing a user to make a new air force 1 plane + if (currentqueItem->plane.getplanetype() == Air_Force_1) + { + hasAirForce1 = false; + } + + } + //If fuel is less then 10% then reprioritize and set the plane to emergency mode + //give new priority as higher then non emergency planes + else if (currentqueItem->plane.getFuel() < 10 ) + { + Plane dequePlane = dequeAirplane(currentqueItem); + currentqueItem = currentqueItem->nxtqueItem; + dequePlane.setPriority(100 - dequePlane.getFuel()); + addAirplane(dequePlane); + + } + //else interate through the list + else + { + currentqueItem = currentqueItem->nxtqueItem; + + } + } + + + + //If plane has the highest priority and is next in line to land, give status as "LANDING_NEXT" + if (headqueItem != nullptr) { + headqueItem->plane.setStatus("LANDING_NEXT"); + } + +} + +//functions the same as simLoop, except goes through the list untill all planes are either crashed or landed +void Airport::fullsimLoop() { + + while (headqueItem != nullptr) + { + simLoop(); + showStatus(); + cout << "---------------------------------------------------------------------------------------------------- \n"; + + //adds delay + system("timeout /t 5 /nobreak>nul"); + } +} + + + + +//Stack Pusher +void Airport::stackPush(Plane airplane) { + + //creating plate for the landing airplane + plate* Plate = new plate(); + Plate->plane = airplane; + + //pushing it onto the stack + Plate->nxtPlate = currentPlate; + currentPlate = Plate; + +} + +//Stack Pusher for crashed planes +void Airport::stackedPushCrashed(Plane airplane) { + + //creating plate for the landing airplane + plate* Plate = new plate(); + Plate->plane = airplane; + + //pushing it onto the stack + Plate->nxtPlate = currentCrash; + currentCrash = Plate; + +} + +//Display the stack for landed planes +void Airport::landedstackShow() { + + //sets landedpate to point to the same location as currentPlate + plate* landedplate = currentPlate; + + while (landedplate != nullptr) + { + cout << landedplate->plane.getInfo() << endl; + landedplate = landedplate->nxtPlate; + } +} + +//Display the stack for crashed planes +void Airport::crashedstackShow() { + + //sets crashedplate to point to the same location as currentCrash + plate* crashedplate = currentCrash; + + while (crashedplate != nullptr) + { + cout << crashedplate->plane.getInfo() << endl; + crashedplate = crashedplate->nxtPlate; + } +} \ No newline at end of file diff --git a/Airport.h b/Airport.h new file mode 100644 index 0000000..66f15b0 --- /dev/null +++ b/Airport.h @@ -0,0 +1,84 @@ +/* + * John Sadiq + * CS 281 Fall 2019 + * Airport Project + * + * Plane.h + * + * + * + */ +#include "Plane.h" +#pragma once + +class Airport { +private: + + //que for planes in sky. + struct queItem { + Plane plane; + queItem* nxtqueItem; + queItem* prvqueItem; + + }; + + // stack for show landed. + struct plate { + Plane plane; + plate* nxtPlate; + }; + + //current landed plane in stack + plate* currentPlate; + + //current crashed plane in stack + plate* currentCrash; + + //traversal que node + queItem* currentqueItem; + + //front of que + queItem* headqueItem; + + //Set Bool, so that there can only be one Air Force plane flying + bool hasAirForce1; + +public: + + // Contructor + Airport(); + + //Destructor + ~Airport(); + + //Pusher + void stackPush(Plane airplane); + + //Pusher for crashed planes + void stackedPushCrashed(Plane airplane); + + //Creates the plane as by its status then fills in the rest such as name and fuel, pre adding to the que + void CreatePlane(std::string pname, int pfuel, int ptype); + + //adding airplane to a vector, within the vector fuction values with sort in numeral order + //aswell as code using a for loop so once value gets to the front of the vector to insert and push the list over that its not operating with a null pointer by mistake + void addAirplane(Plane newPlane); + + //show status, clasifies plane by its class; QUEUED, CIRCLING, LANDING_NEXT, LANDED, and CRASHED. via Couting vector + void showStatus(); + + //standard stack operations. + void landedstackShow(); + void crashedstackShow(); + + //simulation loop, each time space button is hit, an interation goes through and subtract 5% from all flying planes, and has high priority plane land + //aswell as reorganizing priority, if plane goes in to emergency state then we remove it from the vector and insert it to the front giving it a higher priority. + void simLoop(); + + //functions the same as simLoop, except goes through the list untill all planes are either crashed or landed + void fullsimLoop(); + + //remove head node. + Plane dequeAirplane(struct queItem* currentqueItem); + +}; \ No newline at end of file diff --git a/Plane.cpp b/Plane.cpp new file mode 100644 index 0000000..03c06bd --- /dev/null +++ b/Plane.cpp @@ -0,0 +1,113 @@ +/* + * John Sadiq + * CS 281 Fall 2019 + * Airport Project + * + * Plane.cpp + * + * + * + */ + +#include "Plane.h" + +using namespace std; + + + + // Contructor for Plane + Plane:: Plane() {} + + //Constructor sets Plane: name, fuel, and type. Using a switch to pick which plane is needed at the time. + Plane::Plane(string pname, int pfuel, planeType ptype, int ppriority, string pstatus) { + name = pname; + fuel = pfuel; + type = ptype; + priority = ppriority; + status = pstatus; + + } + + //Destructor for Plane + Plane:: ~Plane() {} + + + + //Getters + + //get Plane Name + string Plane::getName() + { + return name; + } + + //get Plane Fuel + int Plane::getFuel() + { + return fuel; + } + + //get Plane Priority + int Plane::getpriority() + { + return priority; + } + + //get Plane Status + string Plane::getStatus() + { + return status; + } + + //get Plane Type + planeType Plane::getplanetype() + { + return type; + } + + //get info for display, This is for when the user asks for the status of all the planes + //grabs info per plane and displays when user prompts and displays them appropiately and with even space + string Plane::getInfo() + { + string converstionTypes[3] = { "Commmercial", "Private", "Air_Force_1" }; + + ostringstream outputStream; + outputStream <<"Name: " << std::setw(10) << name << " | fuel: " << std::setw(4) << fuel << + "%" << " | Priority: " << std::setw(3) << priority << " | Plane type: " << std::setw(15) << + converstionTypes[type] << " | Status: " << std::setw(10) << status; + + return outputStream.str(); + } + + //Setters + + //set Plane Name + void Plane::setName(string planeName) + { + name = planeName; + } + + //set Fuel, fuel for a new plane will be a random number between 10% - 90% + void Plane::changeFuel(int pfuel) + { + fuel += pfuel; + + } + + //set Plane Priority + void Plane::setPriority(int ppriority) + { + priority = ppriority; + } + + //Set Plane Status + void Plane::setStatus(string pstatus) + { + status = pstatus; + } + + //Set Plane Type + void Plane::setPlanetype(planeType pplaneType) + { + type = pplaneType; + } \ No newline at end of file diff --git a/Plane.h b/Plane.h new file mode 100644 index 0000000..53516df --- /dev/null +++ b/Plane.h @@ -0,0 +1,77 @@ +/* + * John Sadiq + * CS 281 Fall 2019 + * Airport Project + * + * Airport.h + * + * + * + */ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include + +enum planeType { Commmercial, Private, Air_Force_1 }; + + +class Plane { +private: + + //Plane type + //enum type to discribe the type of plane, can be called upon as a user assigned type + //Now whenever a function is looking for a plane type it will have to fall between Comm, priv, or AF1 plane + + //name of plane + std::string name; + + //fuel, new plane is random between 10%-90% + int fuel; + + //priority, Priority goes 1 for comm planes, 2 for priv planes, then 3 for AF1 plane + //The plane with the highest priority will land in the que, + //but once a plane goes into emergency mode they will skip to the top of the priority + int priority; + + //status of plane, the status a plane can have are either, QUEUED, CIRCLING, LANDING_NEXT, LANDED, or CRASHED. + //Plane will get their status as situation is appropiate, each plane can only hold one status and will change as conditions change + std::string status; + + //plane type, calling from enum planeType making type be a planeType type. + planeType type; + + +public: + + // Contructor + Plane(); + + //Constructor sets name, fuel, and priority + Plane(std::string pname, int pfuel, planeType ptype, int ppriority, std::string pstatus); + + //Destructor + ~Plane(); + + //Getters, getters will get name, fuel levels, priority number, Status, Plane Type + std::string getName(); + int getFuel(); + int getpriority(); + std::string getStatus(); + planeType getplanetype(); + std::string getInfo(); + + + //Setters, setters will set name, fuel levels, priority number, Status, Plane Type + void setName(std::string planeName); + void changeFuel(int pfuel); + void setPriority(int priority); + void setStatus(std::string status); + void setPlanetype(planeType pplaneType); + +}; \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..6a510ff --- /dev/null +++ b/main.cpp @@ -0,0 +1,140 @@ +/* + * John Sadiq + * CS 281 Fall 2019 + * Airport Project + * + * Plane.cpp + * + * + * + */ + +#include "Airport.h" + +using namespace std; + +int main() +{ + //sets time to be 0 so when the time of the programmed is run and a new plane is created, It will be random + srand(unsigned int (time(0))); + + Airport* Aport = new Airport(); + + //Introduction + cout << "Welcome to the San Diego Airlines! \n" << + "Enter l to have simulation go one cycle \n" << + "Then enter a number for the given options \n" << + "1) Create a new plane \n" << + "2) Check status on all planes \n" << + "3) Check list of landed planes \n" << + "4) Check list of crashed planes \n" << + "5) Loops all planes until all planes are on the ground \n" << + "Enter q to quit \n" << + "----------------------------------------- \n"; + + + // sets a variable for input, for the do while loop + //allowing a quit option for user + string input; + + //loops until user decides to quit + do + { + //Variables + string name; + int fuel; + int type; + + //Adds >> to show where user will enter their imput + cout << "\n >>"; + cin >> input; + + //switch giving the user options to choose + switch (input.at(0)) + { + + case 'l': // Simulate loop + + //loops once through the list + Aport->simLoop(); + //displays the status of planes + Aport->showStatus(); + + + /* + one cycle goes through with every space buttom pressed, + plane does a lap and 5% of fuel is lost, one plane lands per cycle + */ + + break; + + case '1': //New Plane + + // user enter values for name and type + cout << "Enter Plane Name: "; + cin >> name; + cout << "Would you like plane type to be \n 1) Private 2) Commercial 3) Air Force 1 :"; + cin >> type; + + + //Random fuel for new plane, fuel can only be between 10% - 90% + fuel = rand() % 90 + 10; + + //Sends values the user inputed to the Airport.cpp to go through algorithm + Aport->CreatePlane(name, fuel, type); + + /* + creates new plane, sets a random number between 10-90 for fuel percent, + set priority depending on plane type, user inputs name for plane, set plane status + */ + break; + + case '2': //check status + + //COUT msg + cout << "current status of all planes \n"; + cout << "---------------------------- \n"; + + //Show Status, background work is in Airport.cpp + Aport->showStatus(); + break; + + case '3': // Show stack of LANDED planes + + //COUT msg + cout << "List of landed planes \n"; + cout << "--------------------- \n"; + + //Show Stack of landed planes, background work is in Airport.cpp + Aport->landedstackShow(); + + break; + + case '4': // Show stack of CRASHED planes + + //COUT msg + cout << "List of crashed planes \n"; + cout << "---------------------- \n"; + + //Show Stack of crashed planes, background work is in Airport.cpp + Aport->crashedstackShow(); + + break; + + case '5': //loops until all planes are either landed or crashed + + //COUT msg + cout << "Looped through all planes \n"; + cout << "------------------------- \n"; + + //completes simloop until empty + Aport->fullsimLoop(); + + break; + } + + //When user enters 'q' the program quits + } while (input != "q"); + + return 0; +} \ No newline at end of file