// BradDog.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


// CDog.cpp
// Authors: Brad   and Jon  
// Dog program in C++

#include <iostream>

using std::cout;
using std::cin;
using std::ios;
using std::cerr;
using std::endl;

#include <fstream>

using std::ofstream;
using std::ifstream;

#include <string>

using std::string;

#include "AllDogs.h"
#include "Dog.h"

Dog::Dog() {}

Dog::Dog(string n, double w) {
	name = n;
	weight = w;
}

string Dog::getName() {
	return name;
}

double Dog::getWeight() {
	return weight;
}

void Dog::setName(string n) {
	name = n;
}

void Dog::setWeight(double w) {
	weight = w;
}

AllDogs::AllDogs() {}

void AllDogs::load() {
	ifstream inClientFile("dogs.dat", ios::in);
	if (!inClientFile) {
		cerr << "File could not be opened" << endl;
	}
	else {
		for (int i = 0; i <= 9; ++i) {
			dogs[i].setName("");
			dogs[i].setWeight(0);
		}
		string n;
		double w;
		for (int j = 0; j <= 9; ++j) {
				inClientFile >> n >> w;
				dogs[j].setName(n);
				dogs[j].setWeight(w);
		}
		cout << "File loaded." << endl;
	}
}

void AllDogs::save() {
	ofstream outClientFile("dogs.dat", ios::out);
	if (!outClientFile) {
		cerr << "File could not be created" << endl;
	}
	else {
		for (int i = 0; i <= 9; ++i) {
			string d = dogs[i].getName();
			string s = "";
			int c = s.compare(d);
			if (c != 0) {
				outClientFile << dogs[i].getName() << ' '
					<< dogs[i].getWeight() << endl;
			}
		}
		cout << "File saved." << endl;
	}
}

void AllDogs::add() {
	string name;
	double w;
	cout << "Enter dogname: ";
	cin >> name;
	cout << "Enter weight: ";
	cin >> w;
	Dog dog;
	dog.setName(name);
	dog.setWeight(w);
	int i = 0;
	string s = "";
	string n = dogs[i].getName();
	int c = s.compare(n);
	while (c != 0 && i <= 9) {
		++i;
		n = dogs[i].getName();
		c = s.compare(n);
	}
	bool added = false;
	if (c == 0) {
		dogs[i] = dog;
		added = true;
	}
	if (added) {
		cout << "Dog added." << endl;
	}
	else { cout << "Addition failed." << endl; }
}

void AllDogs::remove() {
	string name;
	cout << "Enter dogname: ";
	cin >> name;
	int i = 0;
	bool found = false;
	int c;
	string n;
	while (!found && i <= 9) {
		n = dogs[i].getName();
		c = name.compare(n);
		if (c == 0) {
			found = true;
		}
		else { ++i; }
	}
	bool removed = false;
	if (found) {
		int j = i;
		while (j <= 9) {
			dogs[j] = dogs[j+1];
			++j;
		}
		removed = true;
	}
	if (removed) {
		cout << "Dog removed." << endl;
	}
	else { cout << "Dog not found." << endl; }
}

void AllDogs::find() {
	string name;
	cout << "Enter dogname: ";
	cin >> name;
	int i = 0;
	bool found = false;
	string n;
	int c;
	while (!found && i <= 9) {
		n = dogs[i].getName();
		c = name.compare(n);
		if (c == 0) {
			found = true;
		}
		else { ++i; }
	}
	if (found) {
		cout << "Weight: " << dogs[i].getWeight() << endl;
	}
	else { cout << "Dog not found!" << endl; }
}

int main()
{
	AllDogs all_dogs;
	cout << "Welcome to Brad and Jon's Dog Database!" << endl;
	char option;
	bool stop = false;
	while ( !stop ) {
		cout << "Enter option: (L)oad, (S)ave, (A)dd, (R)emove, (F)ind, (Q)uit." << endl;
		cin >> option;
		switch ( option ) {
			case 'L':
			case 'l':
				all_dogs.load();
				break;
			case 'S':
			case 's':
				all_dogs.save();
				break;
			case 'A':
			case 'a':
				all_dogs.add();
				break;
			case 'R':
			case 'r':
				all_dogs.remove();
				break;
			case 'F':
			case 'f':
				all_dogs.find();
				break;
			case 'Q':
			case 'q':
				stop = true;
				break;
			default:
				cerr << "Option not available." << endl;
				break;
		}
	}
	return 0;
}
