#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
class Character
{
private:
int _hp;
string _nazwa;
public:
void setHp(int hp)
{
_hp = hp;
}
int getHp()
{
return _hp;
}
void setName(string nazwa)
{
_nazwa = nazwa;
}
string getName()
{
return _nazwa;
}
string to_String()
{
stringstream s;
s << "Postac nazywa sie " << _nazwa << " i ma " << _hp << " punktow zycia" << endl;
return s.str();
}
};
class CharacterEditor
{
private:
vector<Character> characters;
public:
Character createCharacter() // tworzenie postaci
{
Character temp;
cout << "Podaj nazwe: ";
string nazwa;
cin >> nazwa;
cout << "Podaj ilosc zycia: ";
int hp;
cin >> hp;
temp.setName(nazwa);
temp.setHp(hp);
characters.push_back(temp);
return temp;
}
string to_String() // tworzy jeden spojny string
{
cout << "Nasze postacie:" << endl;
stringstream s;
int j = 0;
for (unsigned int i = 0; i < characters.size(); i++)
{
s << "Nr:" << j << " " << characters[i].to_String(); // numer j, żeby móc edytować
j++;
}
return s.str();
}
void saveToFile(string zapis) // zapisywanie postaci
{
fstream plik;
plik.open(zapis, ios::out);
for (unsigned int i = 0; i < characters.size(); i++)
{
plik << characters[i].getName();
plik << ":";
plik << characters[i].getHp() << ";" << endl; // rozdzielenie elementów obiektu dwukropek, koniec obiektu średnik;
}
plik.close();
}
void loadFromFile(string wczytaj) // wczytywanie postaci
{
characters.clear();
int y = 0;
string linia, tekst, tekst2;
fstream plik2;
plik2.open(wczytaj, ios::in);
if (plik2.good() == false)
{
cout << "Plik nie istnieje!" << endl;
}
while (getline(plik2, linia))
{
Character temp;
tekst = "";
tekst2 = "";
size_t dwukropek_pozycja = linia.find(':');
size_t srednik_pozycja = linia.find(';');
tekst.insert(0, linia, 0, dwukropek_pozycja); // kopiowanie nazwy
tekst2.insert(0, linia, dwukropek_pozycja + 1, srednik_pozycja - dwukropek_pozycja - 1); // okreslenie dlugosci znakow ile ma hp
y = atoi(tekst2.c_str());
temp.setName(tekst);
temp.setHp(y);
characters.push_back(temp);
}
plik2.close();
}
void editCharacter() // edycja postaci
{
int n = 0;
char c = '\0';
int zdrowie = 0;
string nazwa;
cout << "Postac pod ktorym numerem chcesz edytowac? " << endl;
cin >> n;
cout << "Wybrales edycje postaci nr: " << n << " o imieniu " + characters[n].getName() + " ktora ma " << characters[n].getHp() << " punktow zdrowia" << endl;
cout << "Chcesz edytowac imie(n) czy zdrowie(z)?" << endl;
cin >> c;
if (c == 'n') {
cout << "Podaj nowa nazwe" << endl;
cin >> nazwa;
characters[n].setName(nazwa);
}
else if (c == 'z') {
cout << "Podaj nowa liczbe punktow zdrowie" << endl;
cin >> zdrowie;
characters[n].setHp(zdrowie);
}
else {
cout << "Wpisales zla litere!" << endl;
}
}
};
int main()
{
CharacterEditor postacie;
int wybor = 9;
for (;;)
{
cout << endl;
cout << "MENU GLOWNE" << endl;
cout << "-----------------" << endl;
cout << "1. Dodaj postac" << endl;
cout << "2. Zapisz" << endl;
cout << "3. Wczytaj" << endl;
cout << "4. Wyswietl postacie" << endl;
cout << "5. Edycja postaci" << endl;
cout << "6. Koniec programu" << endl;
cout << endl;
wybor = getchar();
switch (wybor)
{
case '1':
postacie.createCharacter();
break;
case '2':
postacie.saveToFile("postacie.dat");
break;
case '3':
postacie.loadFromFile("postacie.dat");
break;
case '4':
cout << postacie.to_String();
cout << "Aby kontynuowac wcisnij enter";
cin.get();
break;
case '5':
cout << postacie.to_String() << endl;
postacie.editCharacter();
break;
case '6':
exit(0);
break;
default: cout << "Nie ma takiej opcji w menu!";
}
cin.get();
system("cls");
}
cin.get();
cin.get();
}