Skip to content

Instantly share code, notes, and snippets.

@edwin-std
Last active July 21, 2020 21:50
Show Gist options
  • Save edwin-std/ddf43f71977da661120d2a7d767f2b20 to your computer and use it in GitHub Desktop.
Save edwin-std/ddf43f71977da661120d2a7d767f2b20 to your computer and use it in GitHub Desktop.
All code-files for "Notebook" program
#pragma once
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<iterator>
#include<fstream>
using namespace std;
class Book
{
public:
char* type;
virtual char* Type()=0;
virtual void print()=0;
virtual void edit()=0;
virtual void add(string method) = 0;
//virtual void save(ofstream&F)=0;
//virtual void load(ifstream&F)=0;
};
#include"cont.h"
cont::cont(void)
{
type=new char[7];
type="contact";
Name=0;
Commentary=0;
};
cont::~cont(void)
{
if(Name!=0)
delete Name;
if(Commentary!=0)
delete Commentary;
Phone.clear();
delete &Phone;
};
void cont::setName()
{
char c[100];
cout<<"Enter the name"<<endl;
cin >> c;
Name=new char[strlen(c)+1];
strcpy(Name,c);
};
void cont::setCommentary()
{
char c[100];
cout<<"Enter the commentary if needed"<<endl;
cin >> c;
Commentary=new char[strlen(c)+1];
strcpy(Commentary,c);
};
void cont::add(string method)
{
if (method == "new")
{
cout << "Creating new contact" << endl;
setName();
setCommentary();
}
else if (method == "phone")
{
cout << "Creating new phone" << endl;
phone*A = new phone;
A->add("new");
Phone.push_back(A);
}
};
char* cont::Type()
{
return type;
};
void cont::print()
{
cout<<type<<endl;
int ind = 0;
if(Name!="\0")
cout << Name << endl;
else cout << "No name" << endl;
if(Phone.empty()!=true)
{
while(Phone[ind]!=Phone.back())
Phone[ind++]->print();
Phone[ind]->print();
}
if(Commentary!="\0")
cout << Commentary << endl;
else cout << "No commentary" << endl;
};
void cont::edit()
{
int i;
print();
while(1)
{
cout<<"1 - edit name\n2 - edit phones\n3 - edit commentary\n0 - exit"<<endl;
cin>>i;
switch(i)
{
case 1:
setName();
break;
case 2:
if(Phone.empty()==true)
{
cout<<"No phones avaible"<<endl;
return;
}
int j;
cout << "Number of phones: " << Phone.size() << endl;
cout << "Enter the phone id you want to edit" << endl;
cin >> j;
if(j < Phone.size())
Phone[j]->edit();
else cout << "This id doesn't exist" << endl;
break;
case 3:
setCommentary();
break;
case 0:
return;
default:
cout << "404" << endl;
break;
}
}
};
/*void cont::LegacySave(ofstream &F)
{
int i=0;
F<<"''"<<type<<"'': {"<<endl;
F<<"''Name'': "<<"''"<<Name<<"'',"<<endl;
F<<"''Commentary'': "<<"''"<<Commentary<<"'',"<<endl;
if(Phone.empty()!=true)
{
F<<"''Phones'': ["<<endl;
while(Phone[i]!=Phone.back())
{
Phone[i++]->save(F);
F<<","<<endl;
}
Phone[i]->save(F);
F<<endl<<"]"<<endl;
}
};*/
#include"phone.h"
class cont: public Book
{
public:
char* type;
char* Type();
cont();
~cont();
char* Name;
vector<phone*>Phone;
char*Commentary;
void print();
void edit();
void add(string method);
void setName();
void setPhone();
void setCommentary();
//void save(ofstream &F);
//void load(ifstream &F);
};
#include"Menu.h"
void main()
{
Menu*A=new Menu;
A->load();
A->printCont();
A->MainMenu();
}
#include"Menu.h"
Menu::Menu()
{
cout<<"WIP"<<endl;
};
Menu::~Menu()
{
Book.clear();
delete &Book;
}
void Menu::addCont()
{
cout << "Adding contact" << endl;
cont*Cont=new cont;
Cont->add();
Cont->print();
Book.push_back(Cont);
};
void Menu::addNote()
{
cout<<"Adding note" << endl;
note*Note=new note;
Note->add();
Note->print();
Book.push_back(Note);
};
void Menu::printAll()
{
int i=0;
if(Book.empty()==true)
{
cout << "The list is empty" << endl;
return;
}
while(Book[i]!=Book.back())
{
cout << i << ". ";
Book[i++]->print();
}
cout << i << ". ";
Book[i]->print();
};
void Menu::printCont()
{
int i=0;
if(Book.empty()==true)
{
cout<<"The list is empty"<<endl;
return;
}
while(Book[i]!=Book.back())
{
if(strcmp(Book[i]->Type(),"contact")==0)
{
cout<<i<<". ";
Book[i]->print();
}
i++;
}
if(strcmp(Book[i]->Type(),"contact")==0)
{
cout<<i<<". ";
Book[i]->print();
}
};
void Menu::del()
{
int i;
printAll();
cout << Book.size() << " elements" << endl;
cout << "Enter the id you want to delete" << endl;
cin>>i;
if(i<Book.size())
{
delete Book[i];
Book.erase(Book.begin()+i);
}
else cout << "404" << endl;
};
void Menu::addPhone()
{
int i, k;
while(1)
{
cout << "1 - add to the existing contact\n2 - add to the new contact\n"<<endl;
cin >> i;
switch(i)
{
case 1:
if(Book.empty()==true)
cout << "No contacts avaible, please add new one" << endl;
else
{
printCont();
while(1)
{
cout << "Enter the number of the contact" << endl;
cin >> k;
if(k < Book.size())
{
if(strcmp(Book[k]->Type(),"contact")==0)
{
Book[k]->add("phone");
cout << "Added" << endl;
}
else cout << "Not a contact, enter valid id" << endl;
break;
}
else cout << "404" << endl;
}
return;
}
case 2:
addCont();
Book.back()->add("phone");
cout << "Added" << endl;
return;
}
}
};
void Menu::addElem()
{
int a;
while(1)
{
cout << "Adding new page\n1 - add contact\n2 - add phone\n3 - add note" << endl;
cin >> a;
switch(a)
{
case 1:
addCont();
return;
case 2:
addPhone();
return;
case 3:
addNote();
return;
default:
cout << "404" << endl;
break;
}
}
}
void Menu::edit()
{
int i;
printAll();
cout << "Enter the id you want to edit" << endl;
cin >> i;
int d = Book.size();
if(i<d)
Book[i]->edit();
else cout << "404" << endl;
};
void Menu::MainMenu()
{
int a;
while(1)
{
cout << "Main menu\nEnter the type of operation\n1 - print\n2 - add\n3 - edit\n4 - delete\n5 - save\n0 - exit" << endl;
cin >> a;
switch(a)
{
case 1:
printAll();
break;
case 2:
addElem();
break;
case 3:
edit();
break;
case 4:
del();
break;
case 5:
save();
break;
case 0:
return;
default:
cout << "404" << endl;
break;
}
}
};
void Menu::save()
{}
/*
{
int i = 0;
ofstream F;
F.open("123.txt");
if (Book.empty() != true)
{
while (Book[i] != Book.back())
Book[i++]->save(F);
Book[i]->save(F);
}
F.close();
cout << "Saved" << endl;
};*/
void Menu::load()
{
char S[100];
ifstream F;
F.open("1234.txt");
while (!F.eof())
{
F.getline(S, 99);
if (strcmp(S, "contact") == 0)
{
F.getline(S, 99);
cont*A = new cont;
A->Name = new char[strlen(S) + 1];
strcpy(A->Name, S);
F.getline(S, 99);
A->Commentary = new char[strlen(S) + 1];
strcpy(A->Commentary, S);
F.getline(S, 99);
while (strcmp(S, "/contact") != 0)
{
phone*B = new phone;
B->type = new char[strlen(S) + 1];
strcpy(B->type, S);
F.getline(S, 99);
B->Phone = new char[strlen(S) + 1];
strcpy(B->Phone, S);
F.getline(S, 99);
B->Commentary = new char[strlen(S) + 1];
strcpy(B->Commentary, S);
A->Phone.push_back(B);
F.getline(S, 99);
}
Book.push_back(A);
}
}
};
/*void Menu::Legacysave()
{
int i=0;
ofstream F;
F.open("123.txt");
F<<"{"<<endl;
if(Book.empty()!=true)
{
while(Book[i]!=Book.back())
{
Book[i++]->save(F);
F<<","<<endl;
}
Book[i]->save(F);
}
F<<"}"<<endl;
F.close();
cout<<"Saved"<<endl;
};
void Menu::Legacyload()
{
string S;
ifstream F;
F.open("123.txt");
while(!F.eof())
{
getline(F,S);
int i=S.find("''",0);
if(i!=-1)
{
int j=S.find("''",i+1);
string Info=S.substr(i+2,j-i-2);
cout<<Info<<endl;
}
}
};*/
#include"phone.h"
class cont: public Book
{
public:
char* type;
char* Type();
cont();
~cont();
char* Name;
vector<phone*>Phone;
char*Commentary;
void print();
void edit();
void add(string method);
void setName();
void setPhone();
void setCommentary();
//void save(ofstream &F);
//void load(ifstream &F);
};
#include"note.h"
note::note(void)
{
type=new char[4];
type="note";
Body=0;
Header=0;
};
note::~note(void)
{
if (type != 0)
delete type;
if(Body!=0)
delete Body;
if(Header!=0)
delete Header;
};
char* note::Type()
{
return type;
};
void note::print()
{
cout<<type<<endl;
if (Header != "\0")
cout << Header << endl;
else
cout << "Header is empty" << endl;
if(Body!="\0")
cout << Body << endl;
else
cout << "Body is empty" << endl;
}
void note::initBody(int a)
{
if(Body!=0)
delete Body;
Body = new char[a];
};
void note::initHeader(int a)
{
if(Header !=0)
delete Header;
Header = new char[a];
};
void note::setBody()
{
char a[5000];
cout << "Enter the note" << endl;
cin >> a;
initBody(strlen(a)+1);
strcpy(Body,a);
};
void note::setHeader()
{
char a[5000];
cout << "Enter the header" << endl;
cin >> a;
initHeader(strlen(a)+1);
strcpy(Header,a);
};
void note::add(string method)
{
if (method != "new")
return;
cout << "Creating new note" << endl;
setHeader();
setBody();
};
void note::edit()
{
int i;
while(1)
{
cout << "1 - edit header\n2 - edit note\n0 - exit" << endl;
cin >> i;
switch(i)
{
case 1:
setHeader();
break;
case 2:
setBody();
break;
case 0:
return;
default:
cout << "404" << endl;
break;
}
}
};
/*void note::Legacysave(ofstream& F)
{
F<<"''"<<type<<"'': {"<<endl;
F<<"''Header'': "<<"''"<<Header<<"'',"<<endl;
F<<"''Body'': "<<"''"<<Body<<"''"<<endl;
F<<"}"<<endl;
};*/
/*void note::Legacyload(ifstream& F)
{
string S;
getline(F,S);
int i=S.find("''",0);
if(i!=-1)
{
int j=S.find("''",i+1);
string Info=S.substr(i+2,j-i-2);
if(Info=="Header")
{
string Info1=S.substr(j+6,S.length()-j-8);
initHeader(Info1.length()+1);
strcpy(Header,Info1.c_str());
}
if(Info=="Body")
{
string Info1=S.substr(j+6,S.length()-j-8);
initBody(Info1.length()+1);
strcpy(Body,Info1.c_str());
}
}
};*/
#include"Book.h"
class note: public Book
{
public:
note();
~note();
char* type;
char* Type();
char* Body;
char* Header;
void print();
void edit();
void add(string method);
void initBody(int a);
void initHeader(int a);
void setBody();
void setHeader();
//void save(ofstream &F);
//void load(ifstream &F);
};
#include"phone.h"
phone::phone(void)
{
Phone=0;
type=0;
Commentary=0;
};
phone::~phone(void)
{
if(Phone!=0)
delete Phone;
if(type!=0)
delete type;
if(Commentary!=0)
delete Commentary;
};
void phone::print()
{
cout << "Phone type: " << type << endl;
cout << "Phone number: " << Phone << endl;
cout << "Commetary: " << Commentary << endl;
};
void phone::initPhone(int a)
{
if (Phone != "\0")
delete Phone;
Phone = new char[a];
};
void phone::initType(int a)
{
if (type != "\0")
delete type;
type = new char[a];
};
void phone::initCommentary(int a)
{
if (Commentary != "\0")
delete Commentary;
Commentary = new char[a];
};
void phone::setPhone()
{
char P[100];
cout << "Enter the number" << endl;
cin >> P;
initPhone(strlen(P)+1);
strcpy(Phone,P);
};
void phone::setCommentary()
{
char P[100];
cout << "Enter the commentary" << endl;
cin >> P;
initCommentary(strlen(P)+1);
strcpy(Commentary,P);
};
void phone::setType()
{
char P[100];
cout << "Enter the type" << endl;
cin >> P;
initType(strlen(P) + 1);
strcpy(type, P);
};
void phone::add(string method)
{
if (method != "new")
return;
cout << "Creating new phone" << endl;
setPhone();
setType();
setCommentary();
};
char* phone::Type()
{
return type;
};
void phone::edit()
{
print();
int i;
while(1)
{
cout<<"1 - edit number\n2 - edit type\n3 - edit commentary\n0 - exit"<<endl;
cin>>i;
switch(i)
{
case 1:
setPhone();
break;
case 2:
setType();
break;
case 3:
setCommentary();
break;
case 0:
return;
default:
cout<<"404"<<endl;
break;
}
}
};
/*void phone::Legacysave(ofstream &F)
{
F<<"''"<<type<<"'': {"<<endl;
F<<"''Phone'': "<<"''"<<Phone<<"''"<<endl;
F<<"''Commentary'': "<<Commentary<<endl;
F<<"}";
};*/
#include"Book.h"
class phone: public Book
{
public:
phone();
~phone();
char* Type();
char*type;
char*Phone;
char*Commentary;
void print();
void edit();
void initPhone(int n);
void initType(int n);
void initCommentary(int n);
void setPhone();
void setType();
void setCommentary();
void add(string method);
//void save(ofstream &F);
//void load(ifstream &F);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment