Hotel Managment System in C++

//Header Files..
#include <iostream>
#include <windows.h>
#include <string.h>
using namespace std;
// Structure for customer info..
struct customer_info{
 int days, roomrent, idcard;
 char name[100], address[100], phnumber[100];
}info[5];
//Structure for expences..
struct allexpences{
 int ebil, gbil, staffpay, repairs;
}expnc;
int available = 0;
// Displays the customer info..
void displayCustomer(int index){
 cout << "\n\tCustomer Name :   " << info[index].name << endl;
 cout << "\tCustomer Address :   " << info[index].address << endl;
 cout << "\tCustomer Phone Number :   " << info[index].phnumber << endl;
 cout << "\tCustomer ID :   " << info[index].idcard << endl;
 cout << "\tCustomer Room Rent :   " << info[index].roomrent << endl;
 cout << "\tCustomer's Staying Days :   " << info[index].days << endl;
}
// Display MENU...
void displayMenu(){
 system("color F0");
 cout << "\t========================================" << endl;
 cout << "\tHotel Management System BY Bilal & Group" << endl;
 cout << "\t========================================" << endl;
 cout << "\t1.Reserve Room" << endl;
 cout << "\t2.Check Customer Data" << endl;
 cout << "\t3.Check Stats" << endl;
 cout << "\t4.Checkout Customer" << endl;
 cout << "\t5.Add Expences" << endl;
 cout << "\t0.EXIT" << endl;
 cout << "\tYour Choice : ";
}
//Display the Statistics..
void statistics(int size){
 int totalexpences = 0, totalincome = 0;
 totalexpences = expnc.ebil + expnc.gbil + expnc.repairs + expnc.staffpay;
 for (int i = 0; i < size; i++)
 {
  totalincome = totalincome + info[i].roomrent;
 }
 cout << "\tTotal Expences = " << totalexpences << endl;
 cout << "\tTotal Income = " << totalincome << endl;
 if (totalincome > totalexpences)
 {
  cout << "\tYou are going in Profit of Rs: " << totalincome - totalexpences << endl;
 }
 else if (totalincome < totalexpences)
 {
  cout << "\tYour goin in Loss of Rs: " << totalexpences - totalincome << endl;
 }
 else
 {
  cout << "\tNo Profit No Loss !" << endl;
 }
}
// Customer Data Search Function..
void searchcustomer(int id)
{
 bool found = false;
 int index = 0;
 for (int i = 0; i < 5; i++)
 {
  if (id == info[i].idcard)
  {
   found = true;
   index = i;
   break;
  }
 }
 if (found){
  displayCustomer(index);
 }
 else {
  system("CLS");
  cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\tCustomer not Found !" << endl;
 }
}
//Delete Customer..
void deletecustomers(int id){
 bool found = false;
 int index = 0;
 for (int i = 0; i < 5; i++)
 {
  if (id == info[i].idcard)
  {
   found = true;
   index = i;
   break;
  }
 }
 if (found){
  for (int i = index; i < available; i++)
  {
   info[i] = info[i + 1];
  }
  available--;
  cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\tCustomer Deleted !" << endl;
 }
 else {
  system("CLS");
  cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\tCustomer Doesnot Exist !" << endl;
 }
}
//Main..
int main(){
 char choice;
 displayMenu();
 cin >> choice;
 switch (choice){
 case '0':
  system("CLS");
  cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\tBYE HAVE A GOOD DAY !" << endl;
  Sleep(2000);
  exit(0);
  break;
 case '1':
  if (available<5){
   system("CLS");
   cout << "\t=================" << endl;
   cout << "\tReservation Menu" << endl;
   cout << "\t=================" << endl;
   cout << "\tName: ";
   cin.ignore();
   cin.getline(info[available].name, 100);
   cout << "\tAddress: ";
   cin.getline(info[available].address, 100);
   cout << "\tContact No: ";
   cin.getline(info[available].phnumber, 100);
   cout << "\tID Card No: ";
   cin >> info[available].idcard;
   cout << "\tRoom Rent: ";
   cin >> info[available].roomrent;
   cout << "\tDay Stay's: ";
   cin >> info[available].days;
   system("CLS");
   cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\tCustomer Added !" << endl;
   available++;
   Sleep(3000);
   system("CLS");
   main();
  }
  else
  {
   system("CLS");
   cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\tHotel is Full !" << endl;
   Sleep(3000);
   system("CLS");
   main();
  }
  break;
 case '2':
  int id;
  system("CLS");
  cout << "\t==================" << endl;
  cout << "\tCustomer Data Menu" << endl;
  cout << "\t==================" << endl;
  cout << "\tEnter Customer ID Card Number: ";
  cin >> id;
  searchcustomer(id);
  Sleep(10000);
  system("CLS");
  main();
  break;
 case '3':
  system("CLS");
  cout << "\t==========" << endl;
  cout << "\tStats Menu" << endl;
  cout << "\t==========" << endl;
  statistics(available);
  Sleep(7000);
  system("CLS");
  main();
  break;
 case '4':
  system("CLS");
  cout << "\t=================" << endl;
  cout << "\tCheckout Menu" << endl;
  cout << "\t=================" << endl;
  cout << "\tID Card Number: ";
  cin >> id;
  system("CLS");
  deletecustomers(id);
  Sleep(3000);
  system("CLS");
  main();
  break;
 case '5':
  system("CLS");
  cout << "\t==============" << endl;
  cout << "\tExpences Menu" << endl;
  cout << "\t==============" << endl;
  cout << "\tElectricity Bill: ";
  cin >> expnc.ebil;
  cout << "\tGas Bill: ";
  cin >> expnc.gbil;
  cout << "\tTotal Staff Pay: ";
  cin >> expnc.staffpay;
  cout << "\tExpences on Maintenance: ";
  cin >> expnc.repairs;
  system("CLS");
  cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\tExpences Added!" << endl;
  Sleep(3000);
  system("CLS");
  main();
  break;
 default:
  system("cls");
  system("color 40");
  cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\tInvalid Input!" << endl;
  Sleep(1000);
  system("color F0");
  system("cls");
  main();
 }
 return 0;
}

Deadlock in Operating Systems



Deadlock

 

                                In an operating system deadlock arrived when two or more process want to access the same resource (resource may include processors, memory, network modem, printers and files etc.)  To compete their task or action.
Or when two or more process waiting for each other to complete is called a deadlock situation.
For example:
Process P0 using resource R0 and it need resource R1 to complete its task but at the same time another process P1 is using resource R1 and it need the resource R0 which is currently acquired by P0 to complete its own action so both processes wait for each other to complete their task and they keep waiting for life time because no one can complete its action. So we can say that the both processes are in deadlock situation.
As you can see in the figure given below.



Conditions when deadlock occur:


·         At one time a single process can use the a resource (Mutual Exclusion)
·         A process holding a resource and also waiting for another resource which is currently in use of other process (Hold and Wait)
·         A process can release resource when it completes its execution (No Preemption)
·         When two or more processes waiting for one another to release the resource so they can complete their task.

Solutions:


                                There are three method to get rid of deadlocks

Deadlock prevention:


                                                                In this method it is ensured in the Operating system that system will never enter in deadlock situation.
Every process have access to the shared resource at the same time but not for the unshared resources,
Make sure when a process request for a resource it is not holding any other resources. Process must request for resource and be allocated before start its execution. Or allow the process to request for resource when it has no resource allocated.
If a process request for resources and currently holding some resources then cannot allocate the new resource until it release the currently holding resources.
Restart the process when it regain the old resource and the new one which was requested. Each process must request for resources in increasing order.
Preempted resource should be added in to the required resources list for which process are waiting.

Deadlock avoidance:


Ignore the deadlocks and pretend that deadlock never occurs in the system. Most of the operating system use this method including UNIX.
This method need some prior information available about the processes and the resources.
In this each process which is currently loaded declare the maximum number of resource which required by the process to complete its task. Algorithm for deadlock avoidance check for resources allocation to ensure that there is no process waiting for the resource which is currently in use or it ensure that there is no circular waiting of processes. Maintains a state of available resource and allocated resource, and also keep the record of resourced demand by the processes.

 

Deadlock detection:


                                                                Allow the system to go in the deadlock situation and then recover the system from deadlock using different algorithms.
First allow the system to enter in a deadlock state then define some detection algorithms to detect the deadlock in the system, and make a recovery scheme to recover the system from deadlock condition.
To recover the system from a deadlock state select a victim and reclaim the resource, or roll back the process which is most recently started.

Deadlock recovery:

                                                                If deadlock is occurred now what steps we should take.

Process termination:

·         Delete all the process which are in deadlock condition but is expensive.
·         Delete one by one process until the deadlock is broken.
·         Select a process which is recently start and terminate it.

Resource preemption:

·         Select a resource to preempt.
·         Roll back to a previous safe state.
·         Prevent the one process which is always selected as a victim.

System States:


·         Safe state
·         Unsafe state
·         Deadlock state
System can be in one of the states given above.

Safe state:


                                A system is in a safe state if all the process are in a sequence and resource required by a process are available at the time and not in use of any of the other process. When a process request for the resources system must check that after the allocation of these resource system can remain in safe state.
If a process need a resource which is currently in use so this process can wait until the process release the required resource.
If a system is in safe state that means there is no deadlocks.

Unsafe state:

                                A system is in unsafe state when there is a chance of the occurrence of deadlock or resources allocated which is currently in use of some other processes.

Deadlock state:


                                System in deadlock state when two or more process want the same resources or two or more process waiting for each other to complete their tasks.

Medical Store Managment System in C++

#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>
#include <string.h>
using namespace std;
int main();
class Medicine{
public:
    string name;
    string weight;
    string type;
    string expiryDate;
};
void DisplyMed(Medicine m){
    cout << endl;
    cout << "\tMedicine : " << m.name << endl;
    cout << "\tType : " << m.type << endl;
    cout << "\tWeight : " << m.weight << endl;
    cout << "\tExpiry Date : " << m.expiryDate << endl;
}
// Node for Linked list
class mNode{
public:
    Medicine m;
    mNode *next;
    mNode(){
        next = NULL;
    }
};
//Linked List class for Medicine
class mList{
    mNode *head;
public:
    mList(){
        head = NULL;
    }
    bool isEmpty();
    void addMedicine(Medicine newMedicine);
    void searchMedicine(string mName);
    void DisplayMed(string mType);
    void removeMedicine(string mName);
    void showM_List();
    void countMedicine();
}newList;
// to check list is empty or not
bool mList::isEmpty(){
    return(head == NULL);
}
// to add medicine at end
void mList::addMedicine(Medicine newMedicine){
    if (head == NULL){
        head = new mNode;
        head->m = newMedicine;
        head->next = NULL;
    }
    else if (head->next == NULL){
        head->next = new mNode;
        head->next->m = newMedicine;
        head->next->next = NULL;
    }
    else{
        mNode *temp = head;
        while (temp->next != NULL){
            temp = temp->next;
        }
        temp->next = new mNode;
        temp->next->m = newMedicine;
        temp->next->next = NULL;
    }
}
// searching medicine by name
void mList::searchMedicine(const string mName){
    if (isEmpty()){
        cout << "/tEmpty List!" << endl;
    }
    else{
        mNode *temp = head;
        bool found = false;
        while (temp != NULL && !found){
            if (temp->m.name.compare(mName) == 0){
                found = true;
            }
            else{
                temp = temp->next;
            }
        }
        if (found){
            DisplyMed(temp->m);
        }
        else{
            cout << "/tMedicine not Found!" << endl;
        }
        delete temp;
    }

}
// to display medicine of same type
void mList::DisplayMed(string mType){
    if (isEmpty()){
        cout << "\tEmpty List!" << endl;
    }
    else{
        mNode *temp = head; bool c = false;
        while (temp != NULL){
            if (temp->m.type.compare(mType) == 0){
                c = true;
                DisplyMed(temp->m);
                cout << endl;
            }
            temp = temp->next;
        }
        delete temp;
        if (!c){
            system("cls");;
            cout << "\tNo Medicines found of " << mType << " Type."
                << endl;
            Sleep(1000);
        }

    }
}
// to display all Medicines in the list
void mList::showM_List(){
    if (isEmpty()){
        cout << "Empty List!" << endl;
    }
    else{
        mNode *temp = head;
        while (temp != NULL){
            DisplyMed(temp->m);
            temp = temp->next;
        }
        delete temp;
    }

}
void mList::removeMedicine(string mName){
    if (isEmpty()){
        cout << "\tEmpty List!" << endl;
    }
    else if (head->next == NULL){
        head == NULL;
        cout << "\tMedicine Deleted!" << endl;
    }
    else if (head->m.name.compare(mName) == 0){
        head = head->next;
        cout << "\tMedicine Deleted!" << endl;
    }
    else{
        mNode *temp = head->next;
        mNode *prev = head;
        bool found = false;
        while (temp != NULL){
            if (temp->m.name.compare(mName) == 0){
                found = true;
                break;
            }
            prev = temp;
            temp = temp->next;
        }
        if (found){
            prev = temp->next;
            cout << "\tMedicine Deleted!" << endl;
        }
        else{
            cout << "Medicine not Found!" << endl;
        }
        delete temp;
        delete prev;
    }
}
void mList::countMedicine(){
    int count = 0;
    mNode *temp = head;
    while (temp != NULL){
        count++;
        temp = temp->next;
    }
    cout << "\tNumber of Medicine(s) in the "
        << "List is " << count << endl;
    delete temp;

}
void displayNote(){
    cout << "Note : You can only Add 3 types of Medicines." << endl;
    cout << "\t1.Liquid\n\t2.Tablet\n\t3.Capsules\n";
    cout << "\tYour Choice : ";
}
void displayMenu(){
    cout << "\t1.To ADD new Medicine." << endl;
    cout << "\t2.To REMOVE Medicine." << endl;
    cout << "\t3.To FIND Medicine by Name." << endl;
    cout << "\t4.To SHOW Medicines of same Type." << endl;
    cout << "\t5.To DISPLAY all Medicines." << endl;
    cout << "\t0. To EXIT. " << endl;
    cout << "\tYour Choice : ";
}
void case0(){
}
void case1(){
    system("cls");
    displayNote();
    Medicine nM;
    char c;
    cin >> c;
    if ((c < '1') || (c > '3')){
        case1();
    }
    system("cls");
    cout << "Enter Med's Name: ";
    cin >> nM.name;
    if (c == '1'){
        nM.type = "Liquid";
    }
    if (c == '2')
        nM.type = "Tablet";
    if (c == '3')
        nM.type = "Capsules";
    if (c == '1'){
        cout << "Enter Med's Quantity(ml) : ";
        cin >> nM.weight;
    }
    else{
        cout << "Enter Med's Weight(mg) : ";
        cin >> nM.weight;
    }
    cout << "Enter Expiry Date (dd/mm/yyyy) : ";
    cin >> nM.expiryDate;

    newList.addMedicine(nM);
    system("cls");
    cout << "\tMedicine Added!";
    Sleep(1000);
    system("cls");
    main();
}
void case2(){
    system("cls");
    if (newList.isEmpty()){
        cout << "Empty List! Nothing to Remove." << endl;
    }
    else{
        string name;
        cout << "\tEnter Med's Name to Delete : ";
        cin >> name;
        newList.removeMedicine(name);
    }
    Sleep(2000);
    system("cls");
    main();
}
void case3(){
    system("cls");
    string name;
    cout << "Enter Medicine Name to Find : ";
    cin >> name;
    newList.searchMedicine(name);
    system("pause");
    system("cls");
    main();
}
void case4(){
    system("cls");
    if (newList.isEmpty()){
        cout << "\tNo Medicines in the List!" << endl;
        Sleep(2000);
    }
    else{
        cout << "Note.Following Med's type could in the List\n";
        cout << "\t1.Liquid\n\t2.Tablet\n\t3.Capsules\n";
        cout << "\tYour Choice : "; char c;
        cin >> c;
        system("cls");
        if (c <'1' || c > '3'){
            case4();
        }
        if (c == '1')
            newList.DisplayMed("Liquid");
        if (c == '2')
            newList.DisplayMed("Tablet");
        if (c == '3')
            newList.DisplayMed("Capsules");
        system("pause");
    }
    system("cls");
    main();
}
void case5(){
    system("cls");
    if (!newList.isEmpty()){
        newList.countMedicine();
    }
    Sleep(2000);
    newList.showM_List();
    system("pause");
    system("cls");
    main();
}
void caseD(){
    system("cls");
    system("color 40");
    cout << "\t\tInvalid Input!" << endl;
    Sleep(1000);
    system("color F0");
    system("cls");
    main();
}
int main(){

    char choice;
    displayMenu();
    cin >> choice;
    switch (choice){
    case '0':
        exit(0);
        break;
    case '1':
        case1();
        break;
    case '2':
        case2();
        break;
    case '3':
        case3();
        break;
    case '4':
        case4();
        break;
    case '5':
        case5();
        break;
    default:
        caseD();
    }
    return 0;
}