In: Computer Science
With PHP Create a class called Account that a bank might use to represent customers' bank accounts. Your class should include one data member of type int to represent the account balance. Your class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0. If not, the balance should be set to 0 and the constructor should display an error message, indicating that the initial balance was invalid. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account and should ensure that the debit amount does not exceed the account's balance. If it does, the balance should be left unchanged and the function should print a message indicating "Debit amount exceeded account balance." Member function getBalance should return the curreCreate a program that creates two Account objects and tests the member functions of class Account.
Code
<?php
class Account
{
private $balance;
/**
* initial amount
* @param int $initialAmount
*/
public function
__construct($initialAmount)
{
if($initialAmount<0)
{
echo "The initial balance was invalid";
$this->balance=0;
}
else
$this->balance = $initialAmount;
}
/**
* deposit money to the bank
account
* @param int $amount amount to
deposit
*/
public function
deposit($amount){
$this->balance += $amount;
}
public function
withdraw($amount)
{
if($amount >
$this->balance)
echo"Debit amount exceeded account
balance";
else
$this->balance -= $amount;
}
/**
* returns balance
* @return int balance
*/
public function getBalance(){
return
$this->balance;
}
}
$account1 = new Account(1000);
$account2 = new Account(-100);
echo sprintf("Initial balance of the accout1 is
%0.2f.<br/>",$account1->getBalance());
echo sprintf("Initial balance of the accout2 is
%0.2f.<br/>",$account2->getBalance());
echo sprintf("</br>Deposit $2000 to the bank
account1.<br/>");
$account1->deposit(2000);
echo sprintf("Total balance of the account1 is
$%0.2f<br/>", $account1->getBalance());
echo sprintf("</br>Deposit $3000 to the bank
account1.<br/>");
$account2->deposit(3000);
echo sprintf("Total balance of account2 is
$%0.2f<br/>", $account2->getBalance());
echo sprintf("</br>Withdraw $100 from the bank
account1.<br/>");
$account1->withdraw(100);
echo sprintf("Total balance of account1 is
$%0.2f<br/>", $account1->getBalance());
echo sprintf("</br>Withdraw $5000 from the bank
account1.<br/>");
$account2->withdraw(5000);
echo sprintf("<br>Total balance of account1 is
$%0.2f<br/>", $account2->getBalance());
?>
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.