Large diffs are not rendered by default.

@@ -55,22 +55,24 @@ void FileReader<T>::internalRead(List<T> *&list)
read >> classType;
if (classType == "Friend") {
read >> homeNumber;
read >> name;
read.ignore();
std::getline(read, name);
read >> phoneNumber;
read >> email;
read >> birthYear;
read >> title;
p = new Friend(name, phoneNumber, email, birthYear, title, homeNumber);
} else if (classType == "Administraton") {
read >> name;
read.ignore();
std::getline(read, name);
read >> phoneNumber;
read >> email;
read >> birthYear;
read >> title;
read >> workTitle;
p = new Administration(name, phoneNumber, email, birthYear, title, workTitle);
} else {
throw "Read error: Classtype: " + classType + " not supported";
throw "Read error: Classtype: \"" + classType + "\" not supported";
}
list->insert(p);
}
@@ -134,13 +136,12 @@ void FileReader<T>::read(List<T> *&list)

// Saves data to storage
template <typename T>
// Should this be a reference or not?
bool FileReader<T>::save(List<T> *&data) const
{
if (!this->getFileName().empty()) {
std::ofstream write(this->getFileName());
write << data->size() << std::endl;
write << data->toString();
write << data->toString(false);
write.close();
return true;
}
@@ -65,3 +65,11 @@ std::string Friend::toStringSpec() const
ss << "Homenumber: " << this->homeNumber << std::endl;
return ss.str();
}

std::string Friend::toFileStringSpecc() const
{
std::stringstream ss;
ss << this->getClassName() << std::endl;
ss << this->homeNumber << std::endl;
return ss.str();
}
@@ -22,6 +22,7 @@ class Friend : public Person
bool operator==(const Friend& f) const;
bool operator!=(const Friend& f) const;
std::ostream& operator<<(std::ostream& os);
std::string toFileStringSpecc() const;
std::string toStringSpec() const;
std::string getClassName() const;

@@ -8,7 +8,7 @@ FriendEditDialog::FriendEditDialog(QWidget *parent) :
ui(new Ui::FriendEditDialog)
{
ui->setupUi(this);

this->setWindowTitle("Friend Dialog");
}

FriendEditDialog::~FriendEditDialog()
@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>400</width>
<height>527</height>
<height>473</height>
</rect>
</property>
<property name="windowTitle">
@@ -34,7 +34,7 @@ class LinkedList : public List<T>
T* remove(T *element);
unsigned int size() const;
bool isEmpty() const;
std::string toString() const;
std::string toString(bool pretty) const;
T*& elementAt(const unsigned int) const;
};

@@ -167,12 +167,16 @@ unsigned int LinkedList<T>::size() const
}

template <typename T>
std::string LinkedList<T>::toString() const
std::string LinkedList<T>::toString(bool pretty) const
{
std::stringstream ss;
Node *walker = this->first;
while (walker != nullptr) {
ss << walker->element->toString();
if (pretty) {
ss << walker->element->toString();
} else {
ss << walker->element->toFileString();
}
walker = walker->next;
}
return ss.str();
2 List.h
@@ -16,7 +16,7 @@ class List
virtual T* remove(T *element) = 0;
virtual unsigned int size() const = 0;
virtual bool isEmpty() const = 0;
virtual std::string toString() const = 0;
virtual std::string toString(bool pretty) const = 0;
};

#endif
@@ -50,6 +50,18 @@ std::ostream& Person::operator<<(std::ostream &os)
return os;
}

std::string Person::toFileString() const
{
std::stringstream ss;
ss << this->toFileStringSpecc();
ss << this->name << std::endl;
ss << this->phoneNumber << std::endl;
ss << this->email << std::endl;
ss << this->birthYear << std::endl;
ss << this->title << std::endl;
return ss.str();
}

std::string Person::toString() const
{
std::stringstream ss;
@@ -25,8 +25,10 @@ class Person
virtual ~Person() {}
Person& operator=(const Person& p);
std::string toString() const;
std::string toFileString() const;
virtual std::string toStringSpec() const = 0;
virtual std::string getClassName() const = 0;
virtual std::string toFileStringSpecc() const = 0;

std::ostream& operator<<(std::ostream &os);
bool operator !=(const Person &p) const;
@@ -16,7 +16,9 @@ SOURCES += main.cpp\
Administration.cpp \
Phonebook.cpp \
viewDialog.cpp \
FriendEditDialog.cpp
FriendEditDialog.cpp \
ColleagueDialog.cpp \
Teacher.cpp

HEADERS += mainwindow.h \
List.h \
@@ -29,10 +31,13 @@ HEADERS += mainwindow.h \
LinkedList.h \
Phonebook.h \
viewDialog.h \
FriendEditDialog.h
FriendEditDialog.h \
ColleagueDialog.h \
Teacher.h

FORMS += mainwindow.ui \
viewDialog.ui \
FriendEditDialog.ui
FriendEditDialog.ui \
ColleagueDialog.ui

QMAKE_CXXFLAGS += -std=c++11
@@ -0,0 +1,48 @@
#include "Teacher.h"

Teacher::Teacher() : Colleague()
{
this->grade = "";
}

Teacher::Teacher(
std::string name, std::string phoneNumber, std::string email,
int birthYear, std::string title, std::string workTitle, std::string grade
) : Colleague(name, phoneNumber, email, birthYear, title, workTitle)
{
this->grade = grade;
}

void Teacher::setGrade(std::string grade)
{
this->grade = grade;
}

std::string Teacher::getGrade() const
{
return this->grade;
}

std::string Teacher::toStringSpec() const
{
std::stringstream ss;
ss << "Group: " << this->getClassName() << std::endl;
ss << "Title: " << this->getWorkTitle() << std::endl;
ss << "Grade: " << this->getGrade() << std::endl;
return ss.str();
}

std::string Teacher::getClassName() const
{
std::string className = typeid(*this).name();
return className.erase(0, 1);
}

std::string Teacher::toFileStringSpecc() const
{
std::stringstream ss;
ss << this->getClassName() << std::endl;
ss << this->getWorkTitle() << std::endl;
ss << this->getGrade() << std::endl;
return ss.str();
}
@@ -0,0 +1,27 @@
#ifndef TEACHER_H
#define TEACHER_H

#include <string>
#include <sstream>

#include "Person.h"
#include "Colleague.h"

class Teacher : public Colleague
{
private:
std::string grade;
public:
Teacher();
Teacher(std::string name, std::string phoneNumber, std::string email,
int birthYear, std::string title, std::string workTitle, std::string grade);
virtual ~Teacher();
std::string getGrade() const;
void setGrade(std::string grade);

virtual std::string toStringSpec() const;
virtual std::string getClassName() const;
virtual std::string toFileStringSpecc() const;
};

#endif
@@ -30,15 +30,18 @@ MainWindow::MainWindow(QWidget *parent) :
ui->setupUi(this);
this->vd = new ViewDialog(this);
this->book = new Phonebook();
this->filename = "myContacts.txt";
try {
this->book->loadBook("myContact.txt");
this->book->loadBook(this->filename);
} catch (const char *e) {
QMessageBox messageBox;
messageBox.critical(this,"Error", "Failed to load file");
messageBox.setFixedSize(500,200);
std::cout << e << std::endl;
}
this->reloadListWidget();

this->setWindowTitle("Phonebook");
}

MainWindow::~MainWindow()
@@ -67,7 +70,6 @@ void MainWindow::on_pushButton_clicked()
messageBox.setFixedSize(500,200);
break;
}

} else {
QMessageBox messageBox;
messageBox.information(this,"Error","No contact selected!");
@@ -82,7 +84,7 @@ void MainWindow::on_actionSave_triggered()
if (filename != "") {
this->book->saveBook(filename.toStdString());
QMessageBox messageBox;
messageBox.information(this,"Error","Saved!");
messageBox.information(this,"Success","Saved!");
messageBox.setFixedSize(500,200);
}
}
@@ -116,6 +118,25 @@ void MainWindow::on_actionAddFriend_triggered()
messageBox.setFixedSize(500,200);
break;
}
}


void MainWindow::on_actionAdd_Colleague_triggered()
{
Person *p = new Administration();
ColleagueDialog d(this);
d.setPerson(p);
int ret = d.exec();
switch (ret) {
case 0:
break;
case 1:
this->book->addPerson(p);
this->reloadListWidget();
break;
default:
QMessageBox messageBox;
messageBox.critical(0,"Error", "Something terrible happend!");
messageBox.setFixedSize(500,200);
break;
}
}
@@ -7,6 +7,7 @@

#include "viewDialog.h"
#include "FriendEditDialog.h"
#include "ColleagueDialog.h"

#include "FileReader.h"
#include "Person.h"
@@ -27,6 +28,7 @@ class MainWindow : public QMainWindow
Phonebook *book;
Ui::MainWindow *ui;
ViewDialog *vd;
std::string filename;
void reloadListWidget();
public:
explicit MainWindow(QWidget *parent = 0);
@@ -36,6 +38,7 @@ private slots:
void on_actionSave_triggered();
void on_actionLoad_triggered();
void on_actionAddFriend_triggered();
void on_actionAdd_Colleague_triggered();
};

#endif
@@ -14,10 +14,10 @@
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
@@ -88,6 +88,7 @@
<string>Contact</string>
</property>
<addaction name="actionAddFriend"/>
<addaction name="actionAdd_Colleague"/>
</widget>
<addaction name="menuPhonebook"/>
<addaction name="menuContact"/>
@@ -104,7 +105,12 @@
</action>
<action name="actionAddFriend">
<property name="text">
<string>Add</string>
<string>Add Friend</string>
</property>
</action>
<action name="actionAdd_Colleague">
<property name="text">
<string>Add Colleague</string>
</property>
</action>
</widget>