סדנא תרגיל 4 שאלה 1

benny++C, class, סדנא ב++C, תרגילים בסדנא ב++CLeave a Comment

פונקציות חברות, סטטיות וטיפול בחריגות

תכנית לניהול כספומט, בתוכנית זו יש שימוש בשתי מחלקות מחלקה בתוך מחלקה, תרגול בבנאים (קונסטרקטורים), זריקת חריגים

תכנית ראשית (נכתב ע"י benny ) מצורף פיתרון נוסף ומומלץ לעיין בו


/******************************
targil: 4
lecturer: david Cohen
discription: bank Management 

*******************************/



#include "Clock.h"
#include "Account.h"

enum action
{
	stop,
	balance,
	deposit,
	withdraw,
	sumDeposit,
	sumWithdraw
};
action menu()
{
	cout<<"enter 1 to get account balance"<<endl;
	cout<<"enter 2 to deposit money"<<endl;
	cout<<"enter 3 to withdraw money"<<endl;
	cout<<"enter 4 to see the sum of all withdraws"<<endl;
	cout<<"enter 5 to see the sum of all Deposits"<<endl;
	cout<<"enter 0 to stop"<<endl;
	int x;
	cin>>x;
	return (action)x;
}
int findAccount(Account* bank,int size)
{
	int number,code;
	cout<<"please enter account number: ";
	cin>>number;
	int i=0;
	while (i<size && bank[i].getAccountNumber()!=number)
		i++;


	if (i==10)       // the acuont not exsist
		{	
			return -1;  // all the function Below  Defined: if i=-1 throw "ERROR: no such account number!.
		}
	
	cout<<"please enter the code: ";
	cin>>code;
	if (bank[i].getCode()!=code)  // if the input code is not maching to the acount number 
		{
			return -2;   // all the function Below  Defined: if i=-2 throw "ERROR: wrong code!.
		}
	if (bank[i].getCode()==code)
		return i;
}
void printTransaction(Account a, action ac,Clock& c)
{
	cout<<c<<"\t";    // c is the time
	
	switch (ac)
	{
	case balance: cout<<"account #: "<<a.getAccountNumber()<<"\t";
				  cout<<"balance: "<<a.getBalance()<<endl;
		break;
	case deposit: 
	case withdraw: cout<<"account #: "<<a.getAccountNumber()<<"\t";
				   cout<<"new balance: "<<a.getBalance()<<endl;
		break;
	case sumDeposit:
			cout<<"sum of all deposits: "<<Account::getSumDeposit()<<endl;
			break;
	case sumWithdraw:
			cout<<"sum of all withdraws: "<<Account::getSumWithdraw()<<endl;
			break;	
	}
}
void getBalance(Account* bank,int size,Clock& c)
{
	int i=findAccount(bank,size);

	try
	{
		if (i==-1)
		{
			cout<<c<<"\t";
			throw "ERROR: no such account number!\n";
			
		}
		if (i==-2)
		{	cout<<c<<"\t";

			throw "ERROR: wrong code!\n";
			
		}
		if (i>=0)
		{
			c+=20;    // there is no exeptions, so update the time. 
				printTransaction(bank[i],balance,c);
		}
		
	}
	catch(const char*s)
	{
		cout<<s<<endl;
	}


}
void cashDeposit(Account* bank,int size,Clock& c)
{
	int i=findAccount(bank,size);


	try
	{
		if (i==-1)
		{
			cout<<c<<"\t";
			throw "ERROR: no such account number!\n";
			
		}
		if (i==-2)
		{	cout<<c<<"\t";

			throw "ERROR: wrong code!\n";
			
		}
		if (i>=0)
		{
			float amount;
			cout<<"enter the amount of the check: ";
			cin>>amount;

			bool x = bank[i].deposit(amount);
			try
			{
				if (x)
				{
					c+=30;   // there is no exeptions, so update the time. 
					printTransaction(bank[i],deposit,c);
				}
				else
				{
					cout<<c<<"\t";
					throw "ERROR: cannot deposit more than 10000 NIS!\n";
				}
			}
			catch(const char*s)
			{
				cout<<s<<endl;
			}
			
		}
		
	}
	catch(const char*s)
	{
		cout<<s<<endl;
	}

	
}
void cashWithdraw(Account* bank,int size,Clock& c)
{
	int i=findAccount(bank,size);

	try
	{
		if (i==-1)
		{
			cout<<c<<"\t";
			throw "ERROR: no such account number!\n";
			
		}
		if (i==-2)
		{	cout<<c<<"\t";

			throw "ERROR: wrong code!\n";
			
		}

		if (i>=0)  // between 0 to 9 (there is no exeptions so i is the correct index of the acount number in the array)
		{
			float amount;
			cout<<"enter the amount of money to withdraw: ";
			cin>>amount;
			int x= bank[i].withdraw(amount);
			try
			{
				if (x==-1)
				{
					cout<<c<<"\t";
					throw "ERROR: cannot have less than - 6000 NIS!\n";
				}
				if (x==-2)
				{
					cout<<c<<"\t";
					throw "ERROR: cannot withdraw more than 2500 NIS\n";
				}
				if (x==20)
				{
					c+=50;    // there is no exeptions, so update the time. 
					printTransaction(bank[i],withdraw,c);
				}
			
			
			}
			catch(const char*s)
			{
				cout<<s<<endl;
			}
		}
		
	}
	catch(const char*s)
	{
		cout<<s<<endl;
	}
}



int main()
{
	Clock c(8,0,0);
	Account bank[10];
	cout<<"enter account number and code for 10 accounts:\n";
	for(int i=0;i<10;i++)
	{
	    try{
	        cin>>bank[i];
	        for (int j=0;j<i;j++)
	            if (bank[i].getAccountNumber()==bank[j].getAccountNumber()) // the accounts number are the same.
	                throw "ERROR: account number must be unique!\n";
	    }
	    catch (const char* msg)
	    {
	       cout<<c<<'\t'<<msg;
	       i--;           // to get input again.
	    }
	}
	action ac=menu();
	while(ac)
	{
		switch (ac)
		{
		case balance: getBalance(bank,10,c);
			break;
		case withdraw:cashWithdraw(bank,10,c);
			break;
		case deposit:cashDeposit(bank,10,c); 
			break;		
		case sumDeposit:c+=60;
			printTransaction(bank[0],sumDeposit,c);
			break;
		case sumWithdraw:c+=60;
			printTransaction(bank[0],sumWithdraw,c);			
	
		}
		ac=menu();
	}
	return 0;
}


/********OUTPUT************

enter account number and code for 10 accounts:
1
1111
2
2222
3
3333
4
4444
5
5555
6
6666
7
7777
8
8888
9
9999
10
1010
enter 1 to get account balance
enter 2 to deposit money
enter 3 to withdraw money
enter 4 to see the sum of all withdraws
enter 5 to see the sum of all Deposits
enter 0 to stop
2
please enter account number: 1
please enter the code: 1111
enter the amount of the check: 1000
08:00:30        account #: 1    new balance: 1000
enter 1 to get account balance
enter 2 to deposit money
enter 3 to withdraw money
enter 4 to see the sum of all withdraws
enter 5 to see the sum of all Deposits
enter 0 to stop
1
please enter account number: 2
please enter the code: 2222
08:00:50        account #: 2    balance: 0
enter 1 to get account balance
enter 2 to deposit money
enter 3 to withdraw money
enter 4 to see the sum of all withdraws
enter 5 to see the sum of all Deposits
enter 0 to stop
2
please enter account number: 4
please enter the code: 4444
enter the amount of the check: 5000
08:01:20        account #: 4    new balance: 5000
enter 1 to get account balance
enter 2 to deposit money
enter 3 to withdraw money
enter 4 to see the sum of all withdraws
enter 5 to see the sum of all Deposits
enter 0 to stop
3
please enter account number: 1
please enter the code: 1111
enter the amount of money to withdraw: 50
08:02:10        account #: 1    new balance: 950
enter 1 to get account balance
enter 2 to deposit money
enter 3 to withdraw money
enter 4 to see the sum of all withdraws
enter 5 to see the sum of all Deposits
enter 0 to stop
4
08:03:10        sum of all deposits: 6000
enter 1 to get account balance
enter 2 to deposit money
enter 3 to withdraw money
enter 4 to see the sum of all withdraws
enter 5 to see the sum of all Deposits
enter 0 to stop
5
08:04:10        sum of all withdraws: 50
enter 1 to get account balance
enter 2 to deposit money
enter 3 to withdraw money
enter 4 to see the sum of all withdraws
enter 5 to see the sum of all Deposits
enter 0 to stop
3
please enter account number: 100
08:04:10        ERROR: no such account number!

enter 1 to get account balance
enter 2 to deposit money
enter 3 to withdraw money
enter 4 to see the sum of all withdraws
enter 5 to see the sum of all Deposits
enter 0 to stop
30
enter 1 to get account balance
enter 2 to deposit money
enter 3 to withdraw money
enter 4 to see the sum of all withdraws
enter 5 to see the sum of all Deposits
enter 0 to stop
3
please enter account number: 1
please enter the code: 1234
08:04:10        ERROR: wrong code!

enter 1 to get account balance
enter 2 to deposit money
enter 3 to withdraw money
enter 4 to see the sum of all withdraws
enter 5 to see the sum of all Deposits
enter 0 to stop
3
please enter account number: 2
please enter the code: 2222
enter the amount of money to withdraw: 3000
08:04:10        ERROR: cannot withdraw more than 2500 NIS

enter 1 to get account balance
enter 2 to deposit money
enter 3 to withdraw money
enter 4 to see the sum of all withdraws
enter 5 to see the sum of all Deposits
enter 0 to stop
2
please enter account number: 6
please enter the code: 6666
enter the amount of the check: 11000
08:04:10        ERROR: cannot deposit more than 10000 NIS!

enter 1 to get account balance
enter 2 to deposit money
enter 3 to withdraw money
enter 4 to see the sum of all withdraws
enter 5 to see the sum of all Deposits
enter 0 to stop


*******************************/

המחלקה Account

#pragma once
#include <iostream>
using namespace std;
class Account
{
	int accountNumber;
	int code;
	float balance;
	static int sumWithdraw;
	static int sumDeposit; 
public:
	void set_accountNumber(int);
	void set_code(int);

	int getAccountNumber();
	int getCode();
	
	float	getBalance();

	static int getSumWithdraw();
	static int	getSumDeposit();
    int withdraw(float nis);
	bool deposit(float);
	void print();
	friend istream& operator>>(istream&, Account&);

	Account();
	Account(int,int,float);
	~Account();
};


מימוש המחלקה Account

#include "Account.h"
#include <iostream>
#include <string>
#include "Clock.h"
using namespace std;



int Account::sumWithdraw=0; // static varible
int Account::sumDeposit=0;  // static varible
Account::Account()
{
	this->accountNumber=0;
	this->code=0;
	this->balance=0;
}
Account::Account(int accountNumber,int code,float balance)
{
	this->accountNumber=accountNumber;
	this->code=code;
	this->balance=balance;
}
/****set/get fun***********/

void Account:: set_accountNumber(int accountNumber)
{
	this->accountNumber=accountNumber;
}

void Account::set_code(int code)
{
	try
	{
		if ((code/10)<100)
		{
			throw "ERROR: code must be of 4 digits!\n"; 
		}
		else
		{
			this->code=code;
		}
	}
	catch(char * s)
	{
	cout<<s<<endl;
	}

}

int Account::getAccountNumber()
{
	return accountNumber;
}


float Account::getBalance()
{
	return balance;
}

int Account::getCode()
{
	return code;

}



istream& operator>>(istream& in, Account& bank)
{
	int accountNumber;
	int code;

	cin>>accountNumber>>code;
	try
	{
		if ((code/10)<100)   // if the number is less then 4 digits
		{
			throw "ERROR: code must be of 4 digits!\n"; 
		}
		else
		{
			bank.set_code(code);      // insert the code to the bank data base
		}
	}
	catch(char * s)
	{
	cout<<s<<endl;
	}
	bank.set_accountNumber(accountNumber);
	bank.balance=0;    //  balace can be change only by Withdraw or Deposit; 
	return in;
}



bool Account::deposit(float nis)
{
	
		if (nis<=10000)
		{
				this->balance+=nis;   // update the balance 
				sumDeposit+=nis;
				return true;	 //in main, true means: no exeption
		}
		else
		{
			return false;   //in main, false activate throw "ERROR: cannot deposit more than 10000 NIS! 
		}
	
	
	
}


int Account::withdraw(float nis)
{
	if ((balance-nis)<-6000)
		{
			// in main, -1throw activate "ERROR: cannot have less than - 6000 NIS!
			return -1;
		}
		else
		{
			if (nis>2500)
			{
				return -2;   //in main, -2 activate throw "ERROR: cannot withdraw more than 2500 NIS!
			}
			else
			{
				balance-=nis;
				sumWithdraw+=nis;       // count +1 Withdraw
				return 20;    // //in main, 20 means: no exeption
			}
	}

}

void Account::print()
{
	cout<<"Acount number :"<<accountNumber<<endl;
	cout<<"the code is :"<<code<<endl;
	cout<<"balance is :"<<balance<<endl;
}

//static func
int Account::getSumWithdraw()
{
	return sumWithdraw;
}
int	Account::getSumDeposit()
{
	return sumDeposit;

}


Account::~Account(void)
{
}

המחלקה Clock

#include <iostream>
using namespace std;
#pragma once
class Clock
{
	int hour;
	int minute;
	int second;

public:
	void set_hour(int);
	void set_minute(int);
	void set_second(int);

	int get_hour();
	int get_minute();
	int get_second();

	void operator+=(int);

	friend ostream& operator<<(ostream&, const Clock&);
	friend istream& operator>>(istream&, Clock&);

	Clock();                          //default c-tor
	Clock(int ,int ,int );            //patameters c-tor
	Clock(const Clock&temp);           //copy c-tor
	~Clock(void);
};


מימוש המחלקה Clock

#include "Clock.h"
#include <iostream>
#include <iomanip>
using namespace std;

Clock::Clock(): hour(0), minute(0),second(0)  //default c-tor with 0 value
{
	
}



Clock::Clock(int hour,int minute,int second)    // c-tor  with parameters
{
	if ((hour>=0&&hour<24)&&(minute>=0&&minute<60)&&(second>=0&&second<60))
	{
		this->hour=hour;
		this->minute=minute;
		this->second=second;
	}
	else     // if one input is wrong so all the values is 0.
	{       
		this->hour=0;      
		this->minute=0;
		this->second=0;
	}

}


Clock::Clock(const Clock&Temp)        //copy c-tor
 {
 	this->hour=Temp.hour;
 	this->minute=Temp.minute;
 	this->second=Temp.second;
 }
 


/*******SET FUNCTIONS*******************/
void Clock::set_hour( int hour )
{
	this->hour=hour;
}

void Clock::set_minute(int minute)
{
	this->minute=minute;
}

void Clock::set_second(int second)
{
	this->second=second;
}

/*******GET FUNCTIONS*******************/

int Clock::get_hour()
{
	return hour;
}

int Clock::get_minute()
{
	return minute;
}

int Clock::get_second()
{
	return second;
}



 void Clock::operator+=(int sec)
 {
	 
	 if (second+sec>=60)   // so allso the minute will be Affected
	 {
		 if ((second+sec+minute*60)>=3600)   // so allso the hours will be Affected
		 {
			 hour=(second+sec+minute*60+hour*3600)/86400;
		 }
		
		minute=((second+sec+minute*60)/60)%60;
		second=(second+sec)%60;
		
	 }
	 else
	 {
		if (second+sec<60)    // only second need to be updateted.
		 {
			second+=sec;
			return ;
		 }
	   if (second+sec==60)   // in thet case second need to be 0.
		 {
			second=0;
			minute++;
		 }
	 }


 	

	
	  
 
 }

istream& operator>>(istream& in, Clock& input)
{
	cout << "Enter a new time: ";
	int h;
	int min;
	int sec;
	char a;
	in>>h>>a>>min>>a>>sec;




	if ((h>=0&&h<24)&&(min>=0&&min<60)&&(sec>=0&&sec<60))
	{
		input.set_hour(h);
		input.set_minute(min);
		input.set_second(sec);
	}
	else
	{
		try
		{
			if (sec>59)
			{
				throw "Invalid time - more than 60 seconds \n"; 
			}
			if (min>59)
			{
				throw "Invalid time - more than 60 minutes \n"; 
			}
			if (h>23)
			{
				throw "Invalid time - more than 24 hours \n"; 
			}

			// negative
			if (sec<0)
			{
				throw "Invalid time - negative number of seconds \n"; 
			}
			if (min<0)
			{
				throw "Invalid time - negative number of minutes \n"; 
			}
			if (h<0)
			{
				throw "Invalid time - negative number of hours \n"; 
			}



		}
		catch(char * s)
		{
		
		cout<<s;
		}
		input.set_hour(0);
		input.set_minute(0);
		input.set_second(0);
	}
	
	return in;
}


ostream& operator<<(ostream& out , const Clock& A)
{
 	// print the time in format: (01:02:03)
	out<<setfill('0')<<setw(2)<<A.hour<<':'<<setfill('0')<<setw(2)<<A.minute<<':'<<setfill('0')<<setw(2)<<A.second;

	return out;
}

Clock::~Clock(void)
{
}

פיתרון נוסף

תכנית ראשית

#include "Clock.h"
#include "Account.h"
#include<iostream>
using namespace std;
enum action
{
	stop,
	balance,
	deposit,
	withdraw,
	sumDeposit,
	sumWithdraw
};
action menu()
{
	cout << "enter 1 to get account balance" << endl;
	cout << "enter 2 to deposit money" << endl;
	cout << "enter 3 to withdraw money" << endl;
	cout << "enter 4 to see the sum of all withdraws" << endl;
	cout << "enter 5 to see the sum of all Deposits" << endl;
	cout << "enter 0 to stop" << endl;
	int x;
	cin >> x;
	return (action)x;
}
int findAccount(Account* bank, int size)
{
	int number, code;
	cout << "please enter account number: " << endl;
	cin >> number;
	int i = 0;
	while (i<size && bank[i].getAccountNumber() != number)
		i++;
	if (i == size)
		throw "ERROR: no such account number!";
	cout << "please enter the code: " << endl;
	cin >> code;
	if (bank[i].getCode() == code)
		return i;
	return -1;

}
void printTransaction(Account a, action ac, Clock& c)
{
	cout << c << "\t";
	;
	switch (ac)
	{
	case balance: cout << "account #: " << a.getAccountNumber() << "\t";
		cout << "balance: " << a.getBalance() << endl;
		break;
	case deposit:
	case withdraw: cout << "account #: " << a.getAccountNumber() << "\t";
		cout << "new balance: " << a.getBalance() << endl;
		break;
	case sumDeposit:
		cout << "sum of all deposits: " << Account::getSumDeposit() << endl;
		break;
	case sumWithdraw:
		cout << "sum of all withdraws: " << Account::getSumWithdraw() << endl;
		break;
	}
}
void getBalance(Account* bank, int size, Clock& c)
{
	int i = findAccount(bank, size);


	c += 20;
	printTransaction(bank[i], balance, c);



}
void cashDeposit(Account* bank, int size, Clock& c)
{
	int i = findAccount(bank, size);
	try{
		if (i < 0)
			throw "ERROR: wrong code!";
	}
	catch (const char* a)
	{
		cout << c << endl;
		cout << a << endl;
		return;
	}
	float amount;
	cout << "enter the amount of the check: " << endl;
	cin >> amount;

	try{
		bank[i].deposit(amount);
	}
	catch (const char* a)
	{
		cout << c << endl;
		cout << a << endl;

		return;

	}
	c += 30;
	printTransaction(bank[i], deposit, c);
}
void cashWithdraw(Account* bank, int size, Clock& c)
{
	int i;
	try{
		i = findAccount(bank, size);
	}
	catch (const char* a)
	{
		cout << c << endl;
		cout << a << endl;
		return;
	}
	float amount;
	cout << "enter the amount of money to withdraw: " << endl;
	cin >> amount;
	try{
		bank[i].withdraw(amount);
	}
	catch (const char* a)
	{
		cout << c << endl;
		cout << a << endl;
		return;
	}

	c += 50;
	printTransaction(bank[i], withdraw, c);
}
int main()
{
	Clock c(8);
	Account bank[10];
	cout << "enter account number and code for 10 accounts:\n";
	for (int i = 0; i<10; i++)
	{


		try{
			cin >> bank[i];
			for (int j = 0; j<i; j++)
				if (bank[i].getAccountNumber() == bank[j].getAccountNumber()){
					bank[i].set_code( 0);
					throw "ERROR: account number must be unique!\n";
				}
		}
		catch (const char* msg)
		{
			cout << c << '\t' << msg;
			i--;
		}
	}
	action ac = menu();
	while (ac)
	{
		switch (ac)
		{
		case balance: getBalance(bank, 10, c);
			break;
		case withdraw:cashWithdraw(bank, 10, c);
			break;
		case deposit:cashDeposit(bank, 10, c);
			break;
		case sumDeposit:c += 60;
			printTransaction(bank[0], sumDeposit, c);
			break;
		case sumWithdraw:c += 60;
			printTransaction(bank[0], sumWithdraw, c);

		}
		ac = menu();
	}
	return 0;
}



המחלקה Acount

#include "Clock.h"
#include "Account.h"
#include<iostream>
using namespace std;
enum action
{
	stop,
	balance,
	deposit,
	withdraw,
	sumDeposit,
	sumWithdraw
};
action menu()
{
	cout << "enter 1 to get account balance" << endl;
	cout << "enter 2 to deposit money" << endl;
	cout << "enter 3 to withdraw money" << endl;
	cout << "enter 4 to see the sum of all withdraws" << endl;
	cout << "enter 5 to see the sum of all Deposits" << endl;
	cout << "enter 0 to stop" << endl;
	int x;
	cin >> x;
	return (action)x;
}
int findAccount(Account* bank, int size)
{
	int number, code;
	cout << "please enter account number: " << endl;
	cin >> number;
	int i = 0;
	while (i<size && bank[i].getAccountNumber() != number)
		i++;
	if (i == size)
		throw "ERROR: no such account number!";
	cout << "please enter the code: " << endl;
	cin >> code;
	if (bank[i].getCode() == code)
		return i;
	return -1;

}
void printTransaction(Account a, action ac, Clock& c)
{
	cout << c << "\t";
	;
	switch (ac)
	{
	case balance: cout << "account #: " << a.getAccountNumber() << "\t";
		cout << "balance: " << a.getBalance() << endl;
		break;
	case deposit:
	case withdraw: cout << "account #: " << a.getAccountNumber() << "\t";
		cout << "new balance: " << a.getBalance() << endl;
		break;
	case sumDeposit:
		cout << "sum of all deposits: " << Account::getSumDeposit() << endl;
		break;
	case sumWithdraw:
		cout << "sum of all withdraws: " << Account::getSumWithdraw() << endl;
		break;
	}
}
void getBalance(Account* bank, int size, Clock& c)
{
	int i = findAccount(bank, size);


	c += 20;
	printTransaction(bank[i], balance, c);



}
void cashDeposit(Account* bank, int size, Clock& c)
{
	int i = findAccount(bank, size);
	try{
		if (i < 0)
			throw "ERROR: wrong code!";
	}
	catch (const char* a)
	{
		cout << c << endl;
		cout << a << endl;
		return;
	}
	float amount;
	cout << "enter the amount of the check: " << endl;
	cin >> amount;

	try{
		bank[i].deposit(amount);
	}
	catch (const char* a)
	{
		cout << c << endl;
		cout << a << endl;

		return;

	}
	c += 30;
	printTransaction(bank[i], deposit, c);
}
void cashWithdraw(Account* bank, int size, Clock& c)
{
	int i;
	try{
		i = findAccount(bank, size);
	}
	catch (const char* a)
	{
		cout << c << endl;
		cout << a << endl;
		return;
	}
	float amount;
	cout << "enter the amount of money to withdraw: " << endl;
	cin >> amount;
	try{
		bank[i].withdraw(amount);
	}
	catch (const char* a)
	{
		cout << c << endl;
		cout << a << endl;
		return;
	}

	c += 50;
	printTransaction(bank[i], withdraw, c);
}
int main()
{
	Clock c(8);
	Account bank[10];
	cout << "enter account number and code for 10 accounts:\n";
	for (int i = 0; i<10; i++)
	{


		try{
			cin >> bank[i];
			for (int j = 0; j<i; j++)
				if (bank[i].getAccountNumber() == bank[j].getAccountNumber()){
					bank[i].set_code( 0);
					throw "ERROR: account number must be unique!\n";
				}
		}
		catch (const char* msg)
		{
			cout << c << '\t' << msg;
			i--;
		}
	}
	action ac = menu();
	while (ac)
	{
		switch (ac)
		{
		case balance: getBalance(bank, 10, c);
			break;
		case withdraw:cashWithdraw(bank, 10, c);
			break;
		case deposit:cashDeposit(bank, 10, c);
			break;
		case sumDeposit:c += 60;
			printTransaction(bank[0], sumDeposit, c);
			break;
		case sumWithdraw:c += 60;
			printTransaction(bank[0], sumWithdraw, c);

		}
		ac = menu();
	}
	return 0;
}



מימוש המחלקה Acount

#include "Account.h"
#include<iostream>
using namespace std;

int Account::num_w = 0;
int Account::num_d = 0;
int Account::sum_w = 0;
int Account::sum_d = 0;
int Account::m = 0;

Account::~Account()
{
}
Account& Account::withdraw(int nis)
{
		if (nis > 2500&&(balance - nis) > -6000)
			throw "ERROR:cannot withdraw more than 2500 NIS!";
		if ((balance - nis) < -6000)
		
			throw " ERROR: cannot have less than - 6000 NIS!";
			balance -= nis;
		sum_w+=nis;
		num_w++;
	


	return *this;
}
Account& Account::deposit(int sum)
{


	if (sum > 10000)
		throw "ERROR: cannot deposit more than 10000 NIS!";
		balance += sum;
		sum_d+=sum;
		num_d++;


	return *this;
}
int Account::getSumWithdraw(){
	return sum_w;

}
int	Account::getSumDeposit(){
	return sum_d;
}
int Account::sumWithdraw()
{
	return num_w;
}
int Account::sumDeposit()
{
	return num_d;
}

המחלקה Clock

#pragma once
#include<iostream>
using namespace std;
class Clock
{
	int hour;
	int  minute;
	int second;

public:
	Clock(int _hour = 0, int  _minute = 0, int _second = 0){
		set_hour(_hour);
		set_minute(_minute);
		set_second(_second);
	}
	~Clock();
	Clock(const Clock& other);
	void set_hour(int _hour);
	int get_hour(){ return hour; }
	void set_minute(int hour);
	int get_minute(){ return minute; }
	void set_second(int _second);
	int get_second(){ return second; }
	Clock& operator+=(int _socond);//operator+=
	friend ostream& operator<<(ostream& out, const Clock& other)
	{
		if (other.hour < 10)
			out << '0' << other.hour;
		else
			out << other.hour;
		out << "::";
		if (other.minute < 10)
			out << '0' << other.minute;
		else
			out << other.minute;
		out << "::";
		if (other.second < 10)
			out << '0' << other.second;
		else
			out << other.second;

		return out;
	}
	friend istream& operator>>(istream& in, Clock& time)
	{

		char tav;
		in >> time.hour >> tav >> time.minute >> tav >> time.second;
		return in;
	}


};


מימוש המחלקה Clock

#include "Clock.h"
#include<iostream>
using namespace std;


Clock::Clock(const Clock& other)
{
	hour = other.hour;
	minute = other.minute;
	second = other.second;

}
Clock::~Clock()
{
}
void Clock::set_hour(int _hour)
{


	try
	{
		if (_hour>23)
			throw "more than 24 hours";
		if (_hour<0)
			throw " negative number of hours.";
		hour = _hour;
	}
	catch (char* a)
	{
		hour = 0;
	}
}

void Clock::set_minute(int _minute)
{

	try
	{
		if (_minute>59)
			throw "more than 60 minutes";
		if (_minute<0)
			throw " negative number of minutes";
		minute = _minute;
	}
	catch (char* a)
	{
		minute = 0;
	}
}

void Clock::set_second(int _second)
{
	try
	{
		if (_second>59)
			throw "more than 60 seconds";
		if (_second<0)
			throw " negative number of seconds";
		
		second = _second;
	}
	catch (char* a)
	{
		second = 0;
	}
}
Clock& Clock::operator+=(int _second)//operator+=
{
	second +=_second;
	if (second > 59)
	{
		minute += second / 60;
		second %= 60;
	}
	if (minute > 59)
	{
		hour += minute / 60;
		minute %= 60;
	}
	if (hour > 24)
		hour %= 24;
	return *this;
}


כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *