0% found this document useful (0 votes)
16 views4 pages

BankAccount Class Classes and Objects Summary

Uploaded by

hazemalmelbe
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)
16 views4 pages

BankAccount Class Classes and Objects Summary

Uploaded by

hazemalmelbe
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/ 4

//Create a plan for BankAccount objects - blueprint-

public class BankAccount {

// Attributes, Instance(Non-Static) Variables, properties, or Fields


private String customerName; // private for Data Encapsulation
private String password;
private int age;
private double balance;

//Static Variable, static field or class variable


private static String bankName = "SNB";
private static int totalCustomer;
private static double totalCash;

//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;
}

//constructor with 3 parameters


public BankAccount(String n, double b, String p){
this(n,b);
password = p;
}

//create public methods to access instance or class variables in other classes


//Get or Accessor Methods
public String getName() // it is not static because it is different from one customer to another
{
return customerName;
}

public String getPassword()


{
return password;
}

public int getAge()


{
return age;
}

public double getBalance()


{
return balance;
}

public static String getBankName() {


return bankName;
}

public static int getTotalCustomer() {


return totalCustomer;
}
//Set methods or Mutator Methods --> Restarts/update fields/ return type is always void
//set methods are troubleshooters
//custo1.setName("Mariam");
public void setCustomerName(String customerName)
{
this.customerName = customerName;
}

public void setPassword(String p)


{
password = p;
}

public void setAge(int a )


{
age = a;
}

public void setBalance(double b)


{
balance = b;
}

public static void setBankName(String bank) {


bankName = bank;
}

//Other mutator methods


public void withdraw(double amount)
{
if(balance>=amount){
balance-=amount;
totalCash -= amount;
}
}

public void deposit(double amount)


{
balance+=amount;
totalCash += amount;
}

//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() {

return "Customer Name: " + this.customerName


+ "\nCustomer Balance: " + this.balance + "\n";
}

//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 {

public static void main(String[] args) {

java.util.Scanner input = new java.util.Scanner(System.in);

//Invoking the default constructor


BankAccount customer1 = new BankAccount();
BankAccount customer2 = new BankAccount();

//customer 1
customer1.setCustomerName("Mahmoud");
customer1.setBalance(500000);
customer1.setPassword("My1234");

//customer 2
customer2.setCustomerName("Fares");
customer2.setBalance(0.01);
customer2.setPassword("My973947");

System.out.println("\nCustomer 1: " + customer1 ); // customer1.toString()


System.out.println("\nCustomer 2: " + customer2);

//Money transfer
customer1.transfer(customer2, 100000);

System.out.println("\nCustomer 1: " + customer1 ); // customer1.toString()


System.out.println("\nCustomer 2: " + customer2);

//Invoking the constructor with parameter


BankAccount customer3 = new BankAccount("Nassier", 76000,"aaoR");

//modifying field through set method


customer3.setCustomerName("Nasier");

//User-input
System.out.println("Enter the customer name: ");
String name = input.nextLine();
System.out.println("Enter the customer password: ");
String pass = input.nextLine();

BankAccount customer4 = new BankAccount(name,pass);

//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);

System.out.println("\n\nTotal Customers: " + BankAccount.getTotalCustomer());


}

You might also like