create a class toy with the below attributes:
toyId - int
toyName - String
category - String
price - double
discount - double
the above attributes should be privet, write getters, setters and parameterized constructor as required.
create a class solution with main method.
implement two static method- countToyWithCategory and findToyWithLeastPrice in solution class
Answers
Explanation:
ohc-yuvc-yys
come
fast
everyone
good meeting
creating a class toy
Explanation:
#include<bits/stdc++.h>
using namespace std;
class toy{
private:
int toyId;
string toyName, category;
double price, discount;
public:
toy(){
toyId = 0;
toyName = "";
category = "";
price = 0.0;
discount = 0.0;
}
// Getters
int getId(){return toyId;}
string getName(){return toyName;}
string getCategory(){return category;}
double getPrice(){return price;}
double getDiscount() {return discount;}
// Setters
void setId(int id){toyId = id;}
void setName(string name){toyName = name;}
void setCategory(string cat){category= cat;}
void setPrice(double p){price = p;}
void setDiscount(double disc) {discount = disc;}
};
class Solution{
public:
int countToyWithCategory(vector<toy> t,int n, string category){
int count = 0;
for(int i=0; i<n; i++){
if(t[i].getCategory()==category) count++;
}
return count;
}
toy findToyWithLeastPrice(vector<toy> t, int n){
int miniPrice = t[0].getPrice(), index=0;
for(int i=1; i<n; i++){
if(t[i].getPrice()<miniPrice){
miniPrice = t[i].getPrice();
index = i;
}
}
return t[index];
}
};
hence, this is the required algorithm for creating a class toy