I wrote a code where customer can buy an item according to customer balance, price and quantity of item to be bought. Here is the code:
#include "bits/stdc++.h"
using namespace std;
class Item{
private:
string itemName;
string itemidNo;
int itemQuantity;
double itemPrice;
public:
Item(){
itemPrice = 500;
itemQuantity = 1;
}
//getters and setters
void setName(string name){itemName = name;}
void setId(string id){itemidNo = id;}
void setQuantity(int quantity){itemQuantity = quantity;}
void setPrice(double price){itemPrice = price;}
string getName(){return itemName;}
string getId(){return itemidNo;}
int getQuantity(){return itemQuantity;}
double getPrice(){return itemPrice;}
};
class customer {
private:
string Custname;
string CustidNo;
double Custbalance;
Item Custitem;
public:
customer(){
Custbalance = 5000;
}
//getters and setters
void setName(string name){Custname = name;}
void setidNo(string id){CustidNo = id;}
void setbalance(double balance){Custbalance = balance;}
string getName(){return Custname;}
string getidNo(){return CustidNo;}
double getbalance(){return Custbalance;}
void print(){
cout << Custitem.getName() << " " << Custitem.getId() << " " << Custitem.getQuantity() << " " << Custitem.getPrice() << "\n";
Custbalance -= Custitem.getQuantity()*Custitem.getPrice();
cout << Custbalance << "\n";
}
void buyItem(Item item){
if(Custbalance < (item.getQuantity()*item.getPrice())){
cout << "Insufficient Balance for " << item.getName() << "\n";
return ;
}
if(item.getQuantity() < 1){
cout << "order not valid";
return;
}
Custitem = item;
print();
}
};
int main(int argc, char const *argv[])
{
customer harsh;
harsh.setName("harsh");
harsh.setidNo("23");
Item item1;
item1.setName("mobile");
item1.setId("44");
harsh.buyItem(item1);
Item item2;
item2.setName("speakers");
item2.setId("F&D");
item2.setQuantity(2);
item2.setPrice(3000);
harsh.buyItem(item2);
return 0;
}
I am a bit confused as I think there is too much redundancy in my code, such as the getters and setters. How can I make my code more object-oriented?