1.
Bank Account
#include<iostream>
#include<exception>
#include<stdexcept>
using namespace std;
class InvalidDepositException:public exception{
public:
const char* what() const noexcept override{
return "error!invalid deposit";
}
};
class PaymentProcessingException:public exception{
public:
const char* what() const noexcept override{
return "error!payment failed";
}
};
class bank{
int balance;
public:
bank(int b){
balance=b;
}
void deposit(double amt){
if(amt<=0)
throw InvalidDepositException();
balance=balance+amt;
cout<<"deposited="<<amt<<endl<<"new balance="<<balance<<endl;
}
void withdraw(double amt){
if(amt>balance)
throw PaymentProcessingException();
balance=balance-amt;
cout<<"withdraw="<<balance<<endl;
}
void show(){
cout<<"balance="<<balance<<endl;
}
};
int main(){
try{
bank b1(100);
b1.show();
b1.deposit(1000);
b1.withdraw(200);
}
catch(const exception& e){
cout<<e.what();
}
}
OUTPUT-
balance=100
deposited=1000
new balance=1100
error!payment failed
2.Product Management
#include<iostream>
#include<exception>
using namespace std;
class OutOfStockException:public exception{
public:
const char* what() const noexcept override{
return "error!product is out of stock";
}
};
class InvalidQuantityException:public exception{
public:
const char* what() const noexcept override{
return "error!invalid quantity";
}
};
class PaymentProcessingException:public exception{
public:
const char* what() const noexcept override{
return "error!payment failed";
}
};
class ProductNotFoundException:public exception{
public:
const char* what() const noexcept override{
return "error!product not found";
}
};
class product{
public:
int id;
string name;
double price;
int qty;
product(int i,string n,double p,int q){
id=i;
name=n;
price=p;
qty=q;
}
};
class order{
int cart[100];
public:
void addtocart(product& pro,int qty){
if(qty<=0)
throw InvalidQuantityException();
if(pro.qty<qty)
throw OutOfStockException();
cart[pro.id]+=qty;
cout<<"added="<<qty<<" "<<pro.name<<" to the cart"<<endl;
}
};
int main(){
try{
product watch(40,"chronex",10000.00,1);
product dress(38,"h&m",3000,3);
order o;
o.addtocart(dress,2);
o.addtocart(watch,1);
}
catch(const exception &e){
cout<<e.what();
}
}
Output:
added 2 h&m to the cart
added 1 chronex to the cart