Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class AlcoholicDrink {
- protected:
- char name[100];
- char country[100];
- float percentAlch;
- float basePrice;
- public:
- static int discount;
- AlcoholicDrink(){
- strcpy(name,"");
- strcpy(country,"");
- percentAlch=0;
- basePrice=0;
- }
- AlcoholicDrink(float percentAlch1, char *name1,char *country1,float basePrice1){
- strcpy(name,name1);
- strcpy(country,country1);
- percentAlch=percentAlch1;
- basePrice=basePrice1;
- }
- virtual ~AlcoholicDrink(){}
- virtual float computePrice()=0;
- bool operator <(AlcoholicDrink &ad){
- if(this->computePrice()<ad.computePrice()){
- return true;
- }else{
- return false;
- }
- }
- static void changeDiscount(int d){
- discount=d;
- }
- static void total(AlcoholicDrink** ad, int n){
- float suma=0;
- for(int i=0;i<n;i++){
- suma+=ad[i]->computePrice();
- }
- cout<<"Total price: "<<suma<<endl;
- cout<<"Total price with discount: "<<suma-suma*discount/100<<endl;
- }
- friend ostream& operator<<(ostream &out,AlcoholicDrink &ad){
- out<<ad.name<<" "<<ad.country<<" "<<ad.computePrice();
- return out;
- }
- };
- int AlcoholicDrink::discount=5;
- class Beer:public AlcoholicDrink{
- private:
- int bar_wheat;
- public:
- Beer(){
- bar_wheat=0;
- }
- Beer(float percentAlch1, char *name1,char *country1,float basePrice1,int bar_wheat1)
- :AlcoholicDrink(percentAlch1,name1,country1,basePrice1){
- bar_wheat=bar_wheat1;
- }
- float computePrice(){
- if(strcmp(country,"Germany")==0){
- if(bar_wheat==0){
- return basePrice+basePrice*0.05+basePrice*0.1;
- }else{
- return basePrice+basePrice*0.05;
- }
- }else{
- if(bar_wheat==0){
- return basePrice+basePrice*0.1;
- }else{
- return basePrice;
- }
- }
- }
- };
- class Wine:public AlcoholicDrink{
- private:
- int year;
- char grapes[20];
- public:
- Wine(){
- year=0;
- strcpy(grapes,"");
- }
- Wine(float percentAlch1,char *name1,char *country1,float basePrice1,int year1, char *grapes1)
- :AlcoholicDrink(percentAlch1,name1,country1,basePrice1){
- strcpy(grapes,grapes1);
- year=year1;
- }
- float computePrice(){
- if(strcmp(country,"Italy")==0){
- if(year<2005){
- return basePrice+basePrice*0.05+basePrice*0.15;
- }else{
- return basePrice+basePrice*0.05;
- }
- }else{
- if(year<2005){
- return basePrice+basePrice*0.15;
- }else{
- return basePrice;
- }
- }
- }
- };
- void lowestPrice(AlcoholicDrink **a, int n){
- float minPrice=a[0]->computePrice();
- int cuvajIndeks;
- for(int i=0;i<n;i++){
- if(a[i]->computePrice()<minPrice){
- minPrice=a[i]->computePrice();
- cuvajIndeks=i;
- }
- }
- cout<<*(a[cuvajIndeks]);
- }
- int main() {
- int testCase;
- cin >> testCase;
- float p;
- char name[100];
- char country[100];
- float price;
- bool mainI;
- int year;
- char grape [20];
- if(testCase == 1) {
- cout << "===== TESTING CONSTRUCTORS ======" << endl;
- cin >> p;
- cin >> name;
- cin >> country;
- cin >> price;
- cin >> mainI;
- Beer b(p, name, country, price, mainI);
- cout << b << endl;
- cin >> p;
- cin >> name;
- cin >> country;
- cin >> price;
- cin >> year;
- cin >> grape;
- Wine w(p, name, country, price, year, grape);
- cout << w << endl;
- } else if(testCase == 2) {
- cout << "===== TESTING LOWEST PRICE ======" << endl;
- int n;
- cin >> n;
- AlcoholicDrink** ad = new AlcoholicDrink*[n];
- for(int i = 0; i < n; ++i) {
- cin >> p;
- cin >> name;
- cin >> country;
- cin >> price;
- if(i % 2 == 1){
- cin >> mainI;
- ad[i] = new Beer(p, name, country, price, mainI);
- }
- else {
- cin >> year;
- cin >> grape;
- ad[i] = new Wine(p, name, country, price, year, grape);
- }
- }
- lowestPrice(ad, n);
- for(int i = 0; i < n; ++i) {
- delete ad[i];
- }
- delete [] ad;
- } else if(testCase == 3) {
- cout << "===== TESTING DISCOUNT STATIC ======" << endl;
- int n;
- cin >> n;
- AlcoholicDrink** ad = new AlcoholicDrink*[n];
- for(int i = 0; i < n; ++i) {
- cin >> p;
- cin >> name;
- cin >> country;
- cin >> price;
- if(i % 2 == 1){
- cin >> mainI;
- ad[i] = new Beer(p, name, country, price, mainI);
- }
- else {
- cin >> year;
- cin >> grape;
- ad[i] = new Wine(p, name, country, price, year, grape);
- }
- }
- AlcoholicDrink::total(ad, n);
- int d;
- cin >> d;
- AlcoholicDrink::changeDiscount(d);
- AlcoholicDrink::total(ad, n);
- for(int i = 0; i < n; ++i) {
- delete ad[i];
- }
- delete [] ad;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement