Design and Create a Menu for your Database

    Examples:

 

Employee Database:

 

void main(){

                load();

                char choice='0';

                while (choice!='e'&&choice!='E'){

                cout<<endl<<"Payroll Database"<<endl;

   cout<<"1. Insert Employee Record and Compute Net Pay"<<endl;

                cout<<"2. Display All Records"<<endl;

                cout<<"3. Search Database"<<endl;

                cout<<"4. Update Record"<<endl;

                cout<<"5. Delete Record"<<endl;

                cout<<"6. Generate Report"<<endl;

                cout<<"7. Delete Database File"<<endl;

                cout<<"8. Back-up Database File"<<endl;

                cout<<endl<<" Type e to exit"<<endl<<endl;

                cout<<"Enter the number of your choice:";

                cin>>choice;         cout<<endl;

                switch (choice){

                 case '1': insert(); break;

                 case '2': display(); break;

                 case '3': char s[100]; int index;

                     cout<<"Enter the last name of the person you want: ";

                                cin>>s;

                                index=search(s);

                                if(index!=-1) {

     cout<<"Name: "<<e[index].fname<<" "<<e[index].lname<<endl;

                   cout<<"Hourly Rate: "<<e[index].hourlyrate<<endl;

               cout<<"Hrs Worked: "<<e[index].hoursworked<<endl;

              cout<<"Net Pay: "<<e[index].netpay<<endl<<endl;}//IF

                 else cout<<"EMPLOYEE NOT FOUND"<<endl;

                                break;

                 case '4': update(); break;

                 case '5': deleterec(); break;

                 case '6': report(); break;

                 case '7': deletefile(); break;

                 case '8': backupfile(); break;

                 case 'e': store(); break; //EXIT

                 default: cout<<"Invalid Choice"<<endl; break; }//SWITCH

}//WHILE

}//MAIN

 

Java example:

 

       public static void main( String args[] ) throws IOException{

        Database db = new Database();

        Cin read = new Cin();

        db.load();

        int choice = 0;

        do{System.out.println("\t1-Insert");

           System.out.println("\t2-Display");

           System.out.println("\t3-Search");

           System.out.println("\t4-Modify");

           System.out.println("\t5-Remove");

           System.out.println("\t6-Quit");

           choice = read.readInt();

           if( choice == 1 ) db.insert();

           else if( choice == 2 ) db.display();

           else if( choice == 3 ) db.search();

           else if( choice == 4 ) db.modify();

           else if ( choice == 5 ) db.remove();

           else if ( choice == 6 ) db.quit();

           else System.out.println("Enter correct number");

        } while(true);

    }//MAIN

 

 

 

 

    From Chapter 2:

 

    1.      #include <iostream.h>

    2.      

    3.      main() {

    4.           cout << "               Ebrahimi Bank of New York”<<endl;

    5.           cout << ”               Old Westbury, NY 11568”<<endl<<endl; 

    6.           cout << ”\t  1.  Deposit \t\t  2.  Withdraw”<<endl;

    7.           cout << ”\t  3.  Transfer \t\t  4.  Loan”<<endl;

    8.           cout << ”\t  5.  Balance \t\t  6.  Help”<<endl;

    9.           return 0;

10.      }//MAIN

 

Student Database:

 

Welcome to SUNY OW Database

        Student Menu (S)

        Course Menu (C)

        Exit (E)


Student Menu

        Add (A)

        Update (U)

        Search by Last Name (L)

        Search by ID (I)

        Save (S)


 

Course Menu

        Add (A)

        Update (U)

        Search by CRN (L)

        Save (S) 

 

#include<fstream>

#include<string>

#include<iostream>

#include<iomanip>

using namespace std;

 

class course{

          public:

                   string CRN;

                   string title;

                   string department;

                   string room;

}; //course

 

class student{

          public:

                   string fname, lname, major;

                   double gpa;

}; //student

 

class university{


private:

 

student e[100];

course c[100];

 

int n;

int z;

 

public:

void load(){

          n=0;

          z=0;

          ifstream studentFile("students.txt", ios::in);

          ifstream courseFile("courses.txt", ios::in);

          while(studentFile>>e[n].fname>>e[n].lname>>e[n].major>>e[n].gpa){

                   n++;}//WHILE

          while(courseFile>>c[z].CRN>>c[z].title>>c[z].department>>c[z].room){

        z++;}//WHILE

}//LOAD

 

void store(){

          ofstream studentFile("students.txt",ios::out);

          for(int i=0; i<n; i++){

                   if(e[i].fname!=" ")

                             studentFile<<e[i].fname<<" "<<e[i].lname<<" "<<e[i].major<<" "<<e[i].gpa<<endl;

          }//FOR

          ofstream courseFile("courses.txt", ios::out);

          for(int j=0; j<z; j++){

                   if(c[j].CRN!=" ")

                             courseFile<< c[j].CRN << " "<<c[j].title << " "<<c[j].department << " "<<c[j].room<<endl;

          }//FOR

}//STORE

 

void insert(){

          cout<<"Enter student's first name: "; cin>>e[n].fname;

          cout<<"Enter student's last name: "; cin>>e[n].lname;

          cout<<"Enter student's major: "; cin>>e[n].major;

          if(cin.fail()){

                   cout<<"Error\n"; exit(1);}

                   cout<<"Enter studen's gpa: ";

                   cin>>e[n].gpa;

                   n++;

}//INSERTS THE RECORD TO ARRAY

 

void addCourse( ){

          cout<<"Enter course CRN: ";

          cin>>c[z].CRN;

          cout<<"Enter course title: ";

          cin>>c[z].title;

          cout<<"Enter course room: ";

          cin>>c[z].room;

          cout<<"Enter course department: ";

          cin>>c[z].department;

          z++;

}//addCourse

 

int searchCourse(string crn){

          for(int i=0;i<z;i++) if(crn==c[i].CRN) return i;//FOR

                   return -1;

}//SEARCH COURSE BY CRN

 

 

void display(){

          cout<<setiosflags(ios::left)<<setw(15)<<"First Name"<<setw(15)<<"Last Name";

          cout<<setw(10)<<"Major"<<setw(10)<<"GPA";

          cout<<endl<<endl;

          cout<<setiosflags(ios::fixed|ios::showpoint|ios::left);

          for(int i=0;i<n;i++){

                   if(e[i].fname!=" "){

                             cout<<setiosflags(ios::left)<<setw(15)<<e[i].fname<<setw(15)<<e[i].lname;

                             cout<<setprecision(2)<<setiosflags(ios::left)<<setw(10)<<e[i].major;

                             cout<<setw(10)<<e[i].gpa;

                             cout<<resetiosflags(ios::left)<<endl;}//IF

          }//FOR

}//THIS FUNCTION DISPLAYS ALL RECORDS IN ARRAY

 

int search(string s){

          for(int i=0;i<n;i++) if(s==e[i].lname) return i;//FOR

                   return -1;

}//SEARCH BY NAME,-1 NOT FOUND

 

void update(){

          string searchn;

          cout<<"Enter the Last Name of the Student to be modified: "; cin>>searchn;

          int i=search(searchn);

          if(i==-1) cout<<"STUDENT NOT LISTED"<<endl;

          else{

                   cout<<endl<<"Please enter the updated information."<<endl;

                   cout<<"Enter student's first name: "; cin>>e[i].fname;

                   cout<<"Enter student's last name: "; cin>>e[i].lname;

                   cout<<"Enter major: "; cin>>e[i].major;

                   cout<<"Enter gpa: "; cin>>e[i].gpa;

          }//ELSE

}//UPDATE

 

 

void deletefile(){

          int pword;

          cout<<"Enter your administrative password: "; cin>>pword;

          if(pword==5438){

                   ofstream record("students.txt", ios::out);

                   record<<endl;

                   load();

                   record.close();}

          else cout<<"Wrong password entered."<<endl;

}//DELETE FILE

 

void backupfile(){

          system("copy students.txt backup.txt");

          cout<<endl<<"Back-up file has been made."<<endl;

}//BACKUPFILE use cp command for UNIX

 

 

void studentmenu(){

          char choice='z';  

          system("cls");

          cout<<"Student Menu"<<endl<<endl;

          cout<<"1. Insert Student Record"<<endl;

          cout<<"2. Display All Records"<<endl;

          cout<<"3. Search by Last Name"<<endl;

          cout<<"4. Search by Student ID"<<endl;

          cout<<"5. Update Record"<<endl;

          cout<<"8. Delete Database File"<<endl;

          cout<<"9. Back-up Database File"<<endl;

          cin>>choice;

 

          switch (choice){

                   case '1': insert(); break;

                   case '2': display(); break;

                   case '3':

                             char s[100]; int index;

                             cout<<"Enter the last name of the student you want: ";

                             cin>>s;

                             index=search(s);

                             if(index!=-1) {

                                      cout<<"Name: "<<e[index].fname<<" "<<e[index].lname<<endl;

                                      cout<<"Major: "<<e[index].major<<endl;

                                      cout<<"GPA: "<<e[index].gpa<<endl;

                             }//IF

 

                             else cout<<"STUDENT NOT FOUND"<<endl;

 

                             break;

 

                   case '4':

                             int id;

                             cout<<"Enter Student ID: " ;

                             cin>>id;

                             if(id < n){

                                      cout<<"Name: "<<e[id].fname<<" "<<e[id].lname<<endl;

                                      cout<<"Major: "<<e[id].major<<endl;

                                      cout<<"GPA: "<<e[id].gpa; }

                             else cout<<"Student not found"<<endl;

                             break;

 

                   case '5': update(); break;

 

                   case 'e': store(); break; //EXIT

 

                   default: cout<<"Invalid Choice"<<endl; break; }//SWITCH

         

}//studentmenu

 

 

void coursemenu(){

          char choice='z';

          system("cls");

          cout<<"Course Menu"<<endl;

          cout<<"Add Course (a)"<<endl;

          cout<<"Search for Course (s)"<<endl;

          cin>>choice;

          switch(choice){

                   case 'a': addCourse(); break;

                   case 's':

                             char crn[100]; int index;

                             cout<<"Enter the CRN of the course: ";

                             cin>>crn;

                             index=searchCourse(crn);

                             if(index!=-1) {

                                      cout<<"CRN: "<<c[index].CRN<<endl;

                                      cout<<"Title: "<<c[index].title<<endl;

                                      cout<<"Room: "<<c[index].room<<endl;

                                      cout<<"Department: "<<c[index].department<<endl;

                             }//IF

 

                             else cout<<"COURSE NOT FOUND"<<endl;

 

                             break;break;

                   default: cout<<"Invalid Choice"<<endl; break;

          }//SWITCH

 

}//coursemenu

};

void main(){

 

university OW;

 

OW.load();

char choice='z';

while (choice!='e'&&choice!='E'){

          cout<<endl<<"SUNY OW Database"<<endl<<endl;

          cout<<"Student Menu (s)"<<endl;

          cout<<"Course Menu (c)"<<endl;

          cout<<endl<<" Type e to exit"<<endl<<endl;

          cin>>choice;

 

          switch(choice){

                   case 's':

                                      OW.studentmenu();

                                      break;

 

                   case 'c':

                                      OW.coursemenu();

                                      break;

 

                   case 'e': OW.store(); break; //EXIT

 

                   default: cout<<"Invalid Choice"<<endl; break; }//SWITCH

 

}//WHILE

 

}//MAIN