Constructors

 * We can call another constructor by keyword : this();

package main;

public class Account {
private String number;
private double balance;
private String customerName;
private String customerEmailAddress;
private String customerPhoneNumber;

public Account() {
this("11", 11, "Parvin", "parvin", "11");
System.out.println("Empty constructor");
}

public Account(String number, double balance, String customerName, String customerEmailAddress, String customerPhoneNumber) {
setNumber(number);
setBalance(balance);
setCustomerName(customerName);
setCustomerEmailAddress(customerEmailAddress);
setCustomerPhoneNumber(customerPhoneNumber);
}

public void deposit(double depositAmount) {
this.balance += depositAmount;
System.out.println("Deposit of " + depositAmount + " made. New balance is " + this.balance);
}

public void withdrawal(double withdrawalAmount) {
if (this.balance - withdrawalAmount < 0) {
System.out.println("Only " + this.balance + " available. Withdrawal not processed");
} else {
this.balance -= withdrawalAmount;
System.out.println("Withdrawal of " + withdrawalAmount + " processed. Remaining balance = " + this.balance);
}
}

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public String getCustomerName() {
return customerName;
}

public void setCustomerName(String customerName) {
this.customerName = customerName;
}

public String getCustomerEmailAddress() {
return customerEmailAddress;
}

public void setCustomerEmailAddress(String customerEmailAddress) {
this.customerEmailAddress = customerEmailAddress;
}

public String getCustomerPhoneNumber() {
return customerPhoneNumber;
}

public void setCustomerPhoneNumber(String customerPhoneNumber) {
this.customerPhoneNumber = customerPhoneNumber;
}

@Override
public String toString() {
return "Account{" +
"number='" + number + '\'' +
", balance=" + balance +
", customerName='" + customerName + '\'' +
", customerEmailAddress='" + customerEmailAddress + '\'' +
", customerPhoneNumber='" + customerPhoneNumber + '\'' +
'}';
}
}
Constructors Bad Example
package main;

public class Rectangle {
private int x;
private int y;
private int width;
private int height;

public Rectangle() {
this.x = 0;
this.y = 0;
this.width = 0;
this.height = 0;
}

public Rectangle(int width, int height) {
this.x = 0;
this.y = 0;
this.width = width;
this.height = height;
}

public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
Constructors Good Example
package main;

public class Rectangle {
private int x;
private int y;
private int width;
private int height;

public Rectangle() {
this(0, 0);
}

public Rectangle(int width, int height) {
this(0, 0, width, height);
}

public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}

Комментарии

Популярные сообщения из этого блога

IoC:ApplicationContext, BeanFactory. Bean

Lesson1: JDK, JVM, JRE

Lesson_2: Operations in Java