sidrs

OPP Ass 2 (Rewrite) - Student Database

Aug 16th, 2024 (edited)
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. // TODO: Phone Number Fetching
  2.  
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7.  
  8. class Student {
  9.     string name, address, irn, grade, dob;
  10.     int roll;
  11.     char *blood, *div;
  12.     static string university;
  13. public:
  14.     long long phone;
  15.     friend class Contact;
  16.     friend class Database;
  17.  
  18.     Student(){
  19.         name = "NULL";
  20.         address = "NULL";
  21.         irn = "NULL";
  22.         grade = "NULL";
  23.         roll = 00;
  24.         phone = 0000000000;
  25.         blood = new char[1];
  26.         div = new char[1];
  27.     }
  28.    
  29.     void setData(){
  30.         cin.ignore();
  31.         blood = new char[4];
  32.         div = new char[4];
  33.         cout << "\nENTER DETAILS ----- ----- ----- ----- ----- -----";
  34.         cout << "\n\nEnter Name: "; getline(cin, name);
  35.         cout << "\nEnter Class (Year and Branch): "; getline(cin, grade);
  36.         cout << "\nEnter Roll: "; cin >> roll;
  37.         cout << "\nEnter Division: "; cin >> div;
  38.         cout << "\nEnter Date of Birth (DD/MM/YYYY): "; cin >> dob;
  39.         cout << "\nEnter Blood Group (A+, A-, B+, B-, AB+, AB-, O+, O-): "; cin >> blood;
  40.         cout << "\nEnter IRN: "; cin >> irn;
  41.         cin.ignore();
  42.         cout << "\nEnter Address: "; getline(cin, address);
  43.     }
  44.    
  45.     ~Student() {
  46.         delete[] blood;
  47.     }  
  48. };
  49.  
  50.  
  51. class Contact {
  52.     long long phone;
  53. public:
  54.     void setPhone() {
  55.         try {
  56.             cout << "\nEnter Phone No.: "; cin >> phone;
  57.             if ((phone < 1000000000) or (phone > 9999999999)) {
  58.                 cout << "\nERROR! Invalid Phone Number";
  59.                 throw (phone);
  60.             }
  61.         } catch (long long phone) {
  62.             cout << "\nEnter a Valid Phone No.: "; cin >> phone;
  63.         }
  64.     }
  65. };
  66.  
  67.  
  68. class Database {
  69. public:
  70.     void fetchData(Student &obj){
  71.         cout << "\n\n----- ----- ----- ----- ----- ----- ----- -----";
  72.         cout << "\n\nDETAILS FOR STUDENT: " << obj.name;
  73.         cout << "\nName: " << obj.name;
  74.         cout << "\nClass: " << obj.grade;
  75.         cout << "\nRoll No.: " << obj.roll;
  76.         cout << "\nBlood Group: " << obj.blood;
  77.         cout << "\nPhone No.: " << obj.phone;
  78.         cout << "\nIRN: " << obj.irn;
  79.         cout << "\nAddress: " << obj.address;
  80.         cout << "\nCollege Name: " << Student::university;
  81.         cout << "\n\n----- ----- ----- ----- ----- ----- ----- -----";
  82.     }
  83.    
  84.     static void fetchCollege() {
  85.         cout << "\nCollege Name: " << Student::university;
  86.     }
  87.    
  88.     inline int fetchRoll(Student &obj) {
  89.         return obj.roll;
  90.     }
  91. };
  92.  
  93. string Student::university = "I2IT, Pune";
  94.    
  95. int main() {
  96.  
  97.     int n;
  98.     cout << "\nEnter the number of students: "; cin >> n;
  99.     Student students[n];
  100.     Contact contacts[n];
  101.     Database db;
  102.    
  103.  
  104.    
  105.     for (int i = 0; i < n; i++){
  106.         students[i].setData();
  107.         contacts[i].setPhone();
  108.     }
  109.    
  110.    
  111.     cout << "\n ----- ----- ----- ----- ----- ----- ----- -----";
  112.    
  113.    
  114.     while (true) {
  115.         int choice;
  116.         cout << "\n\n----- MENU ----- ----- ----- ----- ----- -----";
  117.         cout << "\n1: Show All Data\n2: Search by Roll No\n3: Exit";
  118.         cout << "\nEnter choice number: ";
  119.         cin >> choice;
  120.        
  121.         if (choice == 1) {
  122.             cout << "\n----- SHOWING ALL DATA ----- ----- ----- ----- -----";
  123.             for (int i = 0; i < n; i++) {
  124.                 db.fetchData(students[i]);
  125.             }
  126.         }
  127.        
  128.         else if (choice == 2) {
  129.             while (true) {
  130.                 int ROLL, breaker;
  131.                 int flag = 0;
  132.                 cout << "\nEnter the Roll No. you want to look up (Enter -1 to Exit): "; cin >> ROLL;
  133.                 if (ROLL == -1) break;
  134.                 else {
  135.                     for (int i = 0; i < n; i++){
  136.                         if (ROLL == db.fetchRoll(students[i])) {
  137.                             db.fetchData(students[i]);
  138.                             flag = 1;
  139.                             break;
  140.                         }
  141.                     }
  142.                     if (!flag) {
  143.                         cout << "\nERROR! Record Not Found";
  144.                         cout << "\nDo you want to Continue? (Enter 1 for Yes, 0 for No): "; cin >> breaker;
  145.                         if (breaker != 1) {
  146.                             break;
  147.                         }
  148.                    
  149.                     }
  150.                 }
  151.             }
  152.         }
  153.        
  154.         else if (choice == 3) {
  155.             cout << "\nTERMINATING PROGRAM\n";
  156.             break; 
  157.         }
  158.     }
  159.  
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment