Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<vector>
- #include<string>
- #include<sstream>
- #include<fstream>
- #include<iostream>
- #include<bitset>
- #include<stdlib.h>
- using namespace std;
- class Personnel
- {
- protected:
- int ssn;
- int nameLen;
- int cityLen;
- int yob;
- string salary;
- char* name;
- char* city;
- public:
- Personnel() : nameLen(10), cityLen(10)
- {
- name = new char[nameLen + 1];
- city = new char[cityLen + 1];
- }
- Personnel(int nssn, int nyob, int nsalary, char* nname, char* ncity) : ssn(nssn), yob(nyob), salary(bitset<32>(nsalary).to_string()), nameLen(20), cityLen(20)
- {
- name = nname;
- city = ncity;
- }
- //for testing
- int getSsn() { return ssn; }
- int getYob() { return yob; }
- string getSalary() { return salary; }
- char* getName() { return name; }
- char* getCity() { return city; }
- //writes attributes of Personnel obj to file
- void writeToFile(fstream& out) const
- {
- out.write(name, nameLen);
- out.write(city, cityLen);
- out << ssn;
- stringstream salstream(salary);
- string temp;
- while (salstream.good())
- {
- bitset<8> bits;
- salstream >> bits;
- char c = char(bits.to_ulong());
- temp += c;
- }
- out << temp;
- out << yob;
- out << '\n';
- }
- //reads file and assigns stored values to Personnel obj
- void readFromFile(fstream& in)
- {
- char temp[5];
- in.read(name, nameLen);
- in.read(city, cityLen);
- char ssntemp[10];
- in.read(ssntemp, 9);
- ssn = atoi(ssntemp);
- in.read(temp, 4);
- salary = bitset<8>(temp[0]).to_string();
- salary = salary + bitset<8>(temp[1]).to_string();
- salary = salary + bitset<8>(temp[2]).to_string();
- salary = salary + bitset<8>(temp[3]).to_string();
- in.seekp(+1, ios::cur);
- char yobtemp[5];
- in.read(yobtemp, 4);
- yob = atoi(yobtemp);
- in.seekp(+1, ios::cur);
- }
- bool operator==(const Personnel &newP)
- {
- return this->ssn == newP.ssn;
- }
- int size()
- {
- return nameLen + cityLen + 4 + 9 + 4 + 2;
- }
- friend istream &operator >>(istream &in, Personnel &p)
- {
- int nsalary;
- cout << "Enter SSN:";
- in >> p.ssn;
- cout << "Enter year of birth: ";
- in >> p.yob;
- cout << "Enter Name: ";
- in.ignore(numeric_limits<streamsize>::max(), '\n');
- in.get(p.name, sizeof(p.name), '\n');
- cout << "Enter City: ";
- in.ignore(numeric_limits<streamsize>::max(), '\n');
- in.get(p.city, sizeof(p.city), '\n');
- cout << "Enter salary: ";
- in.ignore(numeric_limits<streamsize>::max(), '\n');
- in >> nsalary;
- p.salary = bitset<32>(nsalary).to_string();
- return in;
- }
- };
- class Student : public Personnel
- {
- private:
- char* major;//
- int majorLen;
- public:
- char* getMajor() { return major; }//
- Student() :majorLen(30)
- {
- major = new char[majorLen];
- }
- void writeToFile(fstream& out) const
- {
- out.write(name, nameLen);
- out.write(city, cityLen);
- out << ssn;
- stringstream salstream(salary);
- string temp;
- while (salstream.good())
- {
- bitset<8> bits;
- salstream >> bits;
- char c = char(bits.to_ulong());
- temp += c;
- }
- out << temp;
- out << yob;
- out.write(major, majorLen);
- out << '\n';
- }
- void readFromFile(fstream& in)
- {
- char temp[5];
- in.read(name, nameLen);
- in.read(city, cityLen);
- char ssntemp[10];
- in.read(ssntemp, 9);
- ssn = atoi(ssntemp);
- in.read(temp, 4);
- salary = bitset<8>(temp[0]).to_string();
- salary = salary + bitset<8>(temp[1]).to_string();
- salary = salary + bitset<8>(temp[2]).to_string();
- salary = salary + bitset<8>(temp[3]).to_string();
- in.seekp(+1, ios::cur);
- char yobtemp[5];
- in.read(yobtemp, 4);
- yob = atoi(yobtemp);
- in.read(major, majorLen);
- in.seekp(+1, ios::cur);
- }
- int size()
- {
- return Personnel::size() + majorLen;
- }
- friend istream &operator >>(istream &in, Student &p)
- {
- int nsalary;
- cout << "Enter SSN:";
- in >> p.ssn;
- cout << "Enter year of birth: ";
- in >> p.yob;
- cout << "Enter Name: ";
- in.ignore(numeric_limits<streamsize>::max(), '\n');
- in.get(p.name, sizeof(p.name), '\n');
- cout << "Enter City: ";
- in.ignore(numeric_limits<streamsize>::max(), '\n');
- in.get(p.city, sizeof(p.city), '\n');
- cout << "Enter salary: ";
- in.ignore(numeric_limits<streamsize>::max(), '\n');
- in >> nsalary;
- p.salary = bitset<32>(nsalary).to_string();
- cout << "Enter major: ";
- in.ignore(numeric_limits<streamsize>::max(), '\n');
- in.get(p.major, sizeof(p.major), '\n');
- in.ignore(numeric_limits<streamsize>::max(), '\n');
- return in;
- }
- };
- template<class T>
- class Database
- {
- private:
- fstream database;
- public:
- Database()
- {}
- Database(string filename)
- {
- database.open(filename, fstream::out | fstream::in);
- if (!database.is_open())
- {
- database.open(filename, fstream::out | fstream::in | fstream::trunc);
- }
- }
- void print()
- {
- database.clear();
- database.seekp(0, ios::beg);
- string s;
- Personnel obj;
- int num = 0;
- cout << " = DATA = " << endl;
- do
- {
- obj.readFromFile(database);
- if (obj.getSsn() != 0)
- {
- cout << "Name: " << obj.getName() << endl;
- cout << "YOB: " << obj.getYob() << endl;
- cout << "SSN: " << obj.getSsn() << endl;
- cout << "City: " << obj.getCity() << endl;
- for (int i = 0; i < 32; i++)
- {
- if (obj.getSalary()[i] == '1') {
- num += pow(2, 31 - i);
- }
- }
- cout << "Salary: " << num << endl << endl;
- num = 0;
- }
- } while (getline(database, s));
- database.seekp(0, ios::beg);
- }
- //Student print
- void printS()
- {
- database.clear();
- database.seekp(0, ios::beg);
- string s;
- Student obj;
- int num = 0;
- cout << " = DATA = " << endl;
- do
- {
- obj.readFromFile(database);
- if (obj.getSsn() != 0)
- {
- cout << "Name: " << obj.getName() << endl;
- cout << "YOB: " << obj.getYob() << endl;
- cout << "SSN: " << obj.getSsn() << endl;
- cout << "City: " << obj.getCity() << endl;
- for (int i = 0; i < 32; i++)
- {
- if (obj.getSalary()[i] == '1') {
- num += pow(2, 31 - i);
- }
- }
- cout << "Salary: " << num << endl;
- cout << "Major: "<< obj.getMajor()<<endl<<endl;
- num = 0;
- }
- } while (getline(database, s));
- database.seekp(0, ios::beg);
- }
- //add file to end of database
- void add(T file)
- {
- database.seekp(0, ios::beg);
- if (!(this->find(file)))
- {
- database.clear();
- database.seekg(0, ios::end);
- file.writeToFile(database);
- }
- database.seekp(0, ios::beg);
- }
- bool find(T file)
- {
- database.clear();
- database.seekp(0, ios::beg);
- T p;
- string s;
- do
- {
- p.readFromFile(database);
- if (p == file)
- {
- return true;
- }
- } while (getline(database, s));
- return false;
- }
- //not done
- void modify(T file)
- {
- database.clear();
- database.seekp(0, ios::beg);
- if (this->find(file))
- {
- T editP;
- cout << "Please enter new information" << endl;
- cin >> editP;
- if (file.getSsn() == editP.getSsn())
- {
- database.seekp(-file.size(), ios::cur);
- editP.writeToFile(database);
- }
- else
- {
- cout << "Error. SSN does not match." << endl;
- }
- }
- }
- };
- template<class T>
- void menu(string filename)
- {
- Database<T> database(filename);
- int choice = 0;
- T newPerson;
- cout << "--- " << filename << " ---";
- do {
- cout << endl << "Options" << endl;
- cout << "1. New entry" << endl;
- cout << "2. Print database" << endl;
- cout << "3. Modify entry" << endl;
- cout << "4. Exit" << endl;
- cout << "Select: ";
- cin >> choice;
- cout << endl;
- bool isStudent = is_same<T, Student>::value;
- switch (choice) {
- case 1:
- cin >> newPerson;
- database.add(newPerson);
- break;
- case 2:
- if (!isStudent) {
- database.print();
- break;
- }
- else if (isStudent) {
- database.printS();
- break;
- }
- case 3:
- cout << "Please enter information (at least SSN) for Personnel to modify. Enter 0 if you don't know." << endl;
- cin >> newPerson;
- database.modify(newPerson);
- break;
- default: break;
- }
- } while (choice != 4);
- }
- //need menu
- int main()
- {
- /* Database<Personnel> database("database.txt");
- char n[10] = { 'r','o','s','e' };
- char c[10] = { 'h','o' };
- char n2[10] = { 'q','w' };
- char c2[10] = { 's','d' };
- Personnel p(123456789, 1111, 50000, n, c);
- Personnel q(123456790, 1111, 50000, n2, c2);
- database.add(p);
- database.add(q);
- database.add(Personnel(111111111, 2222, 400, n, c));
- database.add(Personnel(333333333, 3333, 80, n2, c2));
- */
- int option;
- string filename;
- do
- {
- cout << "Options " << endl;
- cout << "1. Create/Open Student database" << endl;
- cout << "2. Create/Open Personnel database" << endl;
- cout << "3. Exit" << endl << "Select: ";
- cin >> option;
- if (option == 1)
- {
- cout << "Enter database filename: ";
- cin >> filename;
- menu<Student>(filename);
- }
- else if (option == 2)
- {
- cout << "Enter database filename: ";
- cin >> filename;
- menu<Personnel>(filename);
- }
- } while (option != 3);
- return 0;
- }
Add Comment
Please, Sign In to add comment