Question

In: Computer Science

With PHP Create a class called Account that a bank might use to represent customers' bank...

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.

Solutions

Expert Solution

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.


Related Solutions

with PHP Create a class called Invoice that a hardware store might use to represent an...
with PHP Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables — a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method...
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
----------------------------------------------------------------------------------------------- Create a class called MathOperations that a teacher might use to represent the basic type...
----------------------------------------------------------------------------------------------- Create a class called MathOperations that a teacher might use to represent the basic type of mathematical operations that may be performed. The class should include the following: Three double private variables as instance variables, number1, number2, and result. Your class should have a default constructor that initializes the three instance variables to zero. Your class should also have a constructor that initializes the two instance variables (number1 and number2) to the value entered by the user from keyboard....
3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent...
3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables-a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
with PHP Create a class called Employee that includes three instance variables—a first name (type String),...
with PHP Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary int). Provide a constructor that initializes the three instance data member. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its 0. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary....
Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
. Create a class called Book to represent a book. A Book should include four pieces...
. Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. Inaddition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT