0% found this document useful (0 votes)
6 views3 pages

Prac 10

The document contains two C++ programs: one for managing a bank account with deposit and withdrawal functionalities, and another for product management with cart operations. Both programs implement custom exception classes to handle various error scenarios such as invalid deposits, out-of-stock products, and payment failures. The main functions demonstrate the usage of these classes and exception handling, showcasing the output of successful operations and error messages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Prac 10

The document contains two C++ programs: one for managing a bank account with deposit and withdrawal functionalities, and another for product management with cart operations. Both programs implement custom exception classes to handle various error scenarios such as invalid deposits, out-of-stock products, and payment failures. The main functions demonstrate the usage of these classes and exception handling, showcasing the output of successful operations and error messages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like