BankAccount Class Classes and Objects Summary
BankAccount Class Classes and Objects Summary
//No-Argument Constructor
//set all variables to default values
public BankAccount (){
totalCustomer++;
}
//Overloading constructor
public BankAccount(String customerName, String password){
this.customerName = customerName; //this.customerName refers to the instance variable
this.password = password; //customerName refers to the local variable, formal parameter
totalCustomer++;
}
//Overloading constructor
public BankAccount(String n, double b){
customerName = n;
balance = b;
totalCustomer++;
totalCash += b;
}
//when the Customer object is printed, the compiler automatically calls this method
//System.out.println(custo1); is equivalent to System.out.println(custo1.toString());
public String toString() {
//custo1.transfer(custo2, 500);
//If custo2 contains LocA, then otherCustomer accepts the reference LocA * /
//otherCustomer = LocA
//this keyword refers to the object(custo1) that calls the method
public void transfer(BankAccount otherCustomer, double amount) {
// this.withdraw(amount);
// otherCustomer.deposit(amount);
// withdraw(amount); --> the compiler will add a default this keyword at runtime
// otherCustomer.deposit(amount);
this.balance -= amount;
otherCustomer.balance += amount;
}
//Reset the balance and return the remaining amount before the reset
public double resetBalance(){
//implementation
double temp = this.balance;
this.balance = 0;
return temp;
}
}
public class BankAccountTest {
//customer 1
customer1.setCustomerName("Mahmoud");
customer1.setBalance(500000);
customer1.setPassword("My1234");
//customer 2
customer2.setCustomerName("Fares");
customer2.setBalance(0.01);
customer2.setPassword("My973947");
//Money transfer
customer1.transfer(customer2, 100000);
//User-input
System.out.println("Enter the customer name: ");
String name = input.nextLine();
System.out.println("Enter the customer password: ");
String pass = input.nextLine();
//Static variable
BankAccount.setBankName("Nojoud Bank");
System.out.println(customer1.getBankName()); // the same copy on all objects
System.out.println(customer2.getBankName());
// print details
System.out.println("\nCustomer 1: " + customer1 ); // customer1.toString()
System.out.println("\nCustomer 2: " + customer2);
System.out.println("\nCustomer 3: " + customer3);
System.out.println("\nCustomer 4: " + customer4);