I have tried to embody some basic ATM operations in a Object Oriented manner for practice. Would love some review pointers from structure, Object Oriented principles point of view. Also what should be my next steps in order to get good at OO design(I would like to read more code not books/videos, have pushed my mind out of 5 years of Tutorial Hell).
Assumptions - My ATM implementation includes creating a user and open a bank account from an ATM.
ATM Operations-
Account Class
- Open Account
- Check Balance
- Withdraw
- PIN Change
- Mini Statement
package templates;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Account {
private long accountBalance;
private List<String> miniStatement;
private boolean accountStatus;
private String accountPIN;
public Account() {
this.accountBalance = 10000;
this.accountStatus = true;
this.accountPIN = "1111";
this.miniStatement = new ArrayList<>();
}
public String doWithdraw(long amount) {
if(this.accountStatus) {
if(getBalance() > 0.00 && amount > 0.00 && getBalance() >= amount) {
this.accountBalance -= amount;
String str = "Account debited with $"+amount;
this.miniStatement.add(str);
return str;
}else
return "Amount entered is high, please enter less amount !!! ";
}else
return "Account is already closed";
}
public double getBalance() {
if(checkAccountStatus())
return this.accountBalance;
return -1.00;
}
public boolean changePIN(String newPin) {
if(checkAccountStatus()) {
this.accountPIN = newPin;
return true;
}
return false;
}
private boolean checkAccountStatus() {
return this.accountStatus;
}
public String closeAccount() {
if(checkAccountStatus()) {
String str = "Please collect $"+ getBalance();
this.accountStatus = false;
return str+". Account is now closed";
}
return "Account is already closed";
}
public Iterator<String> printMiniStatement() {
return this.miniStatement.iterator();
}
}
User Class
- Create User
- Set First Name
- Get First Name
- Set Last Name
- Get Last Name
- Set User Address
- Get User Address
- Set Phone Number
- Get Phone Number
package templates;
public class User {
private String firstName;
private String lastName;
private String address;
private String phoneNumber;
public User(String firstName,String lastName,String address,String phoneNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phoneNumber = phoneNumber;
}
public void setFirstName(String fname) {
this.firstName = fname;
}
public String getFirstName() {
return this.firstName;
}
public void setLastName(String lname) {
this.lastName = lname;
}
public String getLastName() {
return this.lastName;
}
public void setAddress(String addr) {
this.address = addr;
}
public String getAddress() {
return this.address;
}
public void setPhoneNumer(String phonenum) {
this.phoneNumber = phonenum;
}
public String getPhoneNumber() {
return this.phoneNumber;
}
}
Main/Test Class
package test;
import java.util.Iterator;
import templates.*;
public class ATMTest {
public static void main(String[] args) {
Account savings = new Account();
User user1 = new User("Dummy","Name","27, First Floor, Suok-I","8888888888");
System.out.printf("%s%n", "Account Balance is $"+savings.getBalance());
System.out.println("-------------------------------------");
System.out.printf("%s%n", savings.doWithdraw(50));
System.out.println("-------------------------------------");
System.out.printf("%s%n", savings.doWithdraw(5540));
System.out.println("-------------------------------------");
System.out.printf("%s%n", savings.doWithdraw(3350));
System.out.println("-------------------------------------");
System.out.printf("%s%n", savings.doWithdraw(1090));
System.out.println("-------------------------------------");
System.out.printf("%s%n", savings.doWithdraw(90));
System.out.println("-------------------------------------");
System.out.printf("%s%n", savings.doWithdraw(966));
System.out.println("-------------------------------------");
System.out.printf("%s%n", "Account Balance is $"+savings.getBalance());
System.out.println("-------------------------------------");
savings.changePIN("1611");
Iterator<String> itr = savings.printMiniStatement();
System.out.printf("%20s%n", "MINI STATEMENT");
while(itr.hasNext()) {
System.out.printf("%s%n", itr.next());
}
System.out.println("-------------------------------------");
System.out.printf("%s%n", user1.getFirstName());
System.out.printf("%s%n", user1.getLastName());
System.out.printf("%s%n", user1.getAddress());
System.out.printf("%s%n", user1.getPhoneNumber());
System.out.println("-------------------------------------");
user1.setFirstName("Rain");
user1.setLastName("Man");
user1.setAddress("32, Second Floor, Suok-I");
user1.setPhoneNumer("9999999999");
System.out.printf("%s%n", user1.getFirstName());
System.out.printf("%s%n", user1.getLastName());
System.out.printf("%s%n", user1.getAddress());
System.out.printf("%s%n", user1.getPhoneNumber());
System.out.println("-------------------------------------");
System.out.printf("%s%n", savings.closeAccount());
System.out.println("-------------------------------------");
System.out.printf("%s%n", savings.doWithdraw(1111));
System.out.println("-------------------------------------");
}
}