Untitled

 avatar
unknown
plain_text
4 years ago
7.1 kB
11
Indexable
GEBOT.H




#ifndef GEBOT_H
#define GEBOT_H

#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
using namespace std;

//Definition der Klasse Gebot
class Gebot{
  string name;
  int Betrag;
  
  public:
  Gebot(string name , int Betrag= 100);
  int get_Betrag() const;
  string get_name() const;
  bool selbe_bieterin(const Gebot&) const;
  bool operator==(const Gebot&) const;
  bool operator<(const Gebot&) const;
  bool operator>=(int) const;
  ostream&print(ostream& o) const;
};
ostream&operator<<(ostream&o , const Gebot& g);


#endif











GEBOT.CPP


#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include "gebot.h"
using namespace std;


//ganze Zahl >0 und <=10.000.000
Gebot::Gebot(string name , int Betrag): name{name} , Betrag{Betrag} {
  if (name.empty() || Betrag<=0 || Betrag>10000000)
    throw runtime_error("e1");

}

int Gebot::get_Betrag() const{
 return Betrag ;
  
}


string  Gebot::get_name() const{
 return name; 
}


bool Gebot::selbe_bieterin(const Gebot& g) const{
  if( name == g.name)
    return true;
  else return false;
  
}


bool Gebot::operator==(const Gebot& g) const{
  if (Betrag == g.Betrag)
    return true;
  else return false;
  
}


bool Gebot::operator<(const Gebot& g) const{
  if (Betrag < g.Betrag)
    return true;
  else return false;
   
}

 bool Gebot::operator>=(int Ayham) const{
   if(Betrag >= Ayham)
     return true;
   else return false;
   
 }
//[Name: Betrag Euro]
ostream&Gebot::print(ostream& o) const{
  o<<"[" <<name << ": " << Betrag<< " Euro]";
  return o;
  
  
}

ostream&operator<<(ostream&o , const Gebot& g) {
  return g.print(o);
  
}












LOT.H:



#ifndef LOT_H
#define LOT_H

#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
using namespace std;

enum class Kategorie {Schmuck, Moebel, Kunst, Sonstiges};

extern const vector<string> kategorie_namen;

//Definition der Klasse Lot
class Lot{
  string Bezeichnung;
  int Mindesterlos;
  Kategorie kategorie;
  vector<Gebot> liste;
  public:
  Lot(string Bezeichnung , int Mindesterlos , Kategorie kategorie = Kategorie::Sonstiges);
  bool ist_offen() const;
  bool bieten(const Gebot&);
  ostream&print(ostream&o) const;
  
};
ostream&operator<<(ostream&o, const Lot& l );


#endif












LOT.CPP:
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include "gebot.h"
#include "lot.h"
using namespace std;

const vector<string> kategorie_namen{"Schmuck", "Moebel", "Kunst", "Sonstiges"};
//ganze Zahl >0 und  <=10.000.000
Lot::Lot(string Bezeichnung , int Mindesterlos , Kategorie kategorie): Bezeichnung{Bezeichnung} , Mindesterlos{Mindesterlos} , kategorie{kategorie}{
  if (Bezeichnung.empty() || Mindesterlos <= 0 || Mindesterlos > 10000000)
    throw runtime_error("e2");
  
  
  
}
 

bool Lot::ist_offen() const{
  if(kategorie==Kategorie::Schmuck || kategorie==Kategorie::Kunst)
    return true;
  else return false;
  
}


bool Lot::bieten(const Gebot& g){
  if(ist_offen() && g.get_Betrag() < Mindesterlos)
    return false;
  for(auto& l : liste){
   if( l.selbe_bieterin(g) && !(g >= l.get_Betrag() ))
     throw runtime_error("e4");}
    
  liste.push_back(g);
  return true;
    
  }
  
//[Bezeichnung: Mindesterlös Euro, Kategorie {Liste der Gebote}]
ostream&Lot::print(ostream&o) const{
  o<<"[" <<Bezeichnung << ": " << Mindesterlos << " Euro, ";
  o<<kategorie_namen.at(static_cast<int>(kategorie)) << " {";
  bool help=true;
  for(auto& l : liste){
   if(help) {o<< l ; help=false;}
    else{o<<", " << l;}
    
  }
  o<<"}]";
  return o;
  
}

ostream&operator<<(ostream&o , const Lot& l){
 return l.print(o); 
  
}
  



















MAIN:
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include "gebot.h"
#include "lot.h"
using namespace std;

int main() {
  const vector<string> namen{"Susi","Franz","Marie","Paul","Jane","","John"};
  const vector<int> betraege{1,100,-3,50000,0,267,42,273,10000000,708,90,42,300000,7568,10000001,49,502};

  vector<Gebot> gebote;
  size_t i{0};
  for (const auto& betrag : betraege) {
    try {
      gebote.push_back(Gebot{namen.at(i%namen.size()),betrag});
    }
    catch (runtime_error&) {
      cout<<"Ungueltiges Gebot: "<< namen.at(i%namen.size()) << ", " << betrag <<'\n';;
    }
    ++i;
  }
  gebote.push_back(Gebot{"Susi",273});
  for (const auto& gebot : gebote)
    cout << gebot << '\n';
  cout << Gebot{"Lisa"} << '\n';
  cerr << Gebot{"Lisa"} << '\n';

  for (const auto& gebot : gebote) 
    cout << gebot << ", " << gebote.at(3) << ": " << gebot.selbe_bieterin(gebote.at(3)) << (gebot==gebote.at(3))
         << (gebot<gebote.at(3)) << (gebot>=100) << '\n';

  vector<Lot> lots;
  try {
    lots.push_back(Lot{"Muell",100});
    lots.push_back(Lot{"",100});
  }
  catch (runtime_error&) {
    cout << "Error 1\n";
  }
  try {
    lots.push_back(Lot{"Kohinoor",1000000,Kategorie::Schmuck});
    lots.push_back(Lot{"Schrank",42,Kategorie::Moebel});
    lots.push_back(Lot{"Error",10000001});
  }
  catch (runtime_error&) {
    cout << "Error 2\n";
  }
  try {
    lots.push_back(Lot{"Katze im Sack",10000000});
    lots.push_back(Lot{"Error",0});
  }
  catch (runtime_error&) {
    cout << "Error 3\n";
  }
  try {
    lots.push_back(Lot{"Mona Lisa",50,Kategorie::Kunst});
    lots.push_back(Lot{"Test",1,Kategorie::Sonstiges});
    lots.push_back(Lot{"Error",-42});
  }
  catch (runtime_error&) {
    cout << "Error 4\n";
  }
  for (const auto& lot : lots) 
    cout << lot << ": " << lot.ist_offen() << '\n';
  cout << Lot{"Faust",666,Kategorie::Kunst} << '\n';  
  cerr << Lot{"Faust",666,Kategorie::Kunst} << '\n';  

  for (const auto& gebot : gebote)
    try {
      cout << lots.at(0).bieten(gebot);
    }
    catch(runtime_error&) {
      cout << " selbst unterboten:" << gebot << ' ';
    }
  cout << '\n';
  for (const auto& gebot : gebote)
    try {
      cout << lots.at(1).bieten(gebot);
    }
    catch(runtime_error&) {
      cout << " selbst unterboten:" << gebot << ' ';
    }
  cout << '\n';

  lots.at(2).bieten(Gebot{"Bart",2});
  lots.at(2).bieten(gebote.at(3));
  lots.at(2).bieten(gebote.at(0));
  lots.at(2).bieten(gebote.at(8));
  lots.at(3).bieten(gebote.at(3));
  lots.at(3).bieten(gebote.at(0));
  lots.at(3).bieten(gebote.at(8));
  lots.at(4).bieten(gebote.at(7));
  lots.at(4).bieten(gebote.at(3));
  lots.at(4).bieten(gebote.at(4));
  lots.at(4).bieten(gebote.at(7));
  for (const auto& lot : lots)
    cout << lot << '\n';
 
  //Dekommentieren fuer Erweiterung zuschlag
  /*
  cout << "\nErweiterung zuschlag\n";
  for (const auto& lot : lots)
    try {
      cout << lot.zuschlag() << '\n';
    }
    catch(runtime_error&) {
      cout << "Zuschlag gescheitert\n";
    }
  */

  //Dekommentieren fuer Erweiterung probleme
  /*
  cout << "\nErweiterung probleme\n";
  cout << "Probleme:\n";
  vector<Lot> resultat{Lot::probleme(lots)};
  for (const auto& lot : resultat)
    cout << lot << '\n';
  cout << "Rest:\n";
  for (const auto& lot : lots)
    cout << lot << '\n';
  */

  return 0;  
}







Editor is loading...