In: Computer Science
Design the Class Diagram (UML) for a banking system with its clients’ accounts and an ATM machine. Each client account holds the user’s name (String of Full Name), account number (int) and balance (int). All client account details should be hidden from other classes but the system should provide tools/methods to access, change, and display (toString for account) each of these details.
The ATM machine holds the available money inside it (double) and the maximum money it can hold (double).
A user can request to withdraw, deposit or display balance through their account after checking the possibility of the action based on the ATM machine attributes. The balance display can be directly sent to the ATM. However, the withdraw request needs checking the user’s balance before checking the cash availability in the ATM machine. Also the deposit requires ensuring that the amount of cash will not exceed the maximum of the ATM.
For example, if the machine is full with cash then a user cannot deposit his money and a message should be displayed to the client that the machine is full and money cannot be deposited. In addition, if the machine has lower amount than what the withdraw request requires, the operation will be cancelled with an appropriate message on the standard output.
Use a default constructor (no formal parameters) and a full constructor (all class variables are initialized from formal parameters) for the client account.
object oriented programming "" Eclipse java"
AccountType.java
// enumeration for account type
public enum AccountType {
SAVING,CHECKING
}
Account.java
public class Account {
// instance variables
private int accountNo;
private String name;
private String username;
private String password;
private double savingAccount;
private double checkingAccount;
// default constructor
public Account() {
super();
}
// parameterized constructor
public Account(int accountNo, String name, String
username, String password, double savingAccount,
double
checkingAccount) {
super();
this.accountNo = accountNo;
this.name = name;
this.username = username;
this.password = password;
this.savingAccount =
savingAccount;
this.checkingAccount =
checkingAccount;
}
// method to deposit amount
public boolean deposit(AccountType type,double amount)
{
if(type.equals(AccountType.SAVING))
savingAccount+=amount;
else
checkingAccount+=amount;
return true;
}
// method to withdraw amount
public boolean withdraw(AccountType type,double
amount) {
if(type.equals(AccountType.SAVING))
{
if(savingAccount>=amount)
savingAccount-=amount;
else
return false;
}
else {
if(checkingAccount>=amount)
checkingAccount-=amount;
else
return false;
}
return true;
}
// method for balance inquiry
public double balanceEnquiry(AccountType type){
if(type.equals(AccountType.SAVING))
return
savingAccount;
else
return
checkingAccount;
}
// method for transfer balance
public boolean transferBalance(Account
toAccount,AccountType type,double amount) {
if(withdraw(type, amount))
if(toAccount.deposit(type, amount))
return true;
return false;
}
// setter and getter methods
public int getAccountNo() {
return accountNo;
}
public void setAccountNo(int accountNo) {
this.accountNo = accountNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
ATM.java
import java.util.ArrayList;
import java.util.Scanner;
// ATM
public class ATM {
// method to display menu for operation provided by
ATM machine
public static void operationMenu() {
System.out.println("*************************************************");
System.out.println("1.
Deposit");
System.out.println("2.
Withdrawal");
System.out.println("3. Balance
Inquiry");
System.out.println("4. Transfer
Balance");
System.out.println("5. Log
Out");
System.out.println("*************************************************");
System.out.print("Enter your
choice: ");
}
// method to show currently logged in account
details
public static void showLoggedAccount(Account
loggedAccount) {
System.out.println("---------------------------------------------------------------------------------------");
System.out.println("| Customer |
Username | Password | Saving Account | Checking Account |");
System.out.println("---------------------------------------------------------------------------------------");
System.out.println("|
"+loggedAccount.getName()+" | "+loggedAccount.getUsername()+" |
"+loggedAccount.getPassword()+" | $"
+loggedAccount.balanceEnquiry(AccountType.SAVING)+" |
$"+loggedAccount.balanceEnquiry(AccountType.CHECKING)+" |");
System.out.println("---------------------------------------------------------------------------------------");
}
// method to get account type
public static AccountType getAccountType() {
System.out.println("Select account
type:");
System.out.println("1. Saving
Account");
System.out.println("2. Checking
Account");
System.out.print("Enter your
choice: ");
@SuppressWarnings("resource")
int choice=new
Scanner(System.in).nextInt();
if(choice==1)
return
AccountType.SAVING;
else
return
AccountType.CHECKING;
}
// main method
public static void main(String[] args) {
// create array list with Account
type to store accounts
ArrayList<Account> bank=new
ArrayList<>();
// create and add account into
array list
bank.add(new Account(1001, "Robert
Brown", "rbrown", "blue123", 2500.00, 35.00));
bank.add(new Account(1002, "Steve
Rogers", "rsteve", "cap123", 1750.00, 851.25));
@SuppressWarnings("resource")
// Scanner class object to get the
input from user
Scanner sc=new
Scanner(System.in);
Account loggedAccount=null; //
Account class reference
System.out.println("*** Log In
***");
while(true) {
System.out.print("Enter username: ");
String
user=sc.next(); // get username from user
System.out.print("Enter password: ");
String
pass=sc.next(); // get password from user
// logging in
with credentials
for (Account
account : bank) {
if(account.getUsername().equals(user))
if(account.getPassword().equals(pass))
loggedAccount=account;
}
if(loggedAccount!=null)
break; // if credentials are correct
else
// if credentials are wrong
System.out.println("Invalid username
password..try again...!!!\n");
}
// show account details
showLoggedAccount(loggedAccount);
while(true) {
double
amount=0;
System.out.println();
//. display
operations menu
operationMenu();
int
choice=sc.nextInt(); // get choice from user
// working on
user's choice
switch (choice)
{
case 1:
System.out.print("Enter amount to deposit:
");
amount=sc.nextDouble(); // get amount to deposit
from user
// deposit amount
if(!loggedAccount.deposit(getAccountType(),
amount))
System.out.println("Deposit
failed");
else
System.out.println("Amount
deposited successfully...!!!");
break;
case 2:
System.out.print("Enter amount to withdraw:
");
amount=sc.nextDouble(); // get amount to
withdraw from user
if(!loggedAccount.withdraw(getAccountType(),
amount))
System.out.println("Insufficient account balance...!!!");
else
System.out.println("Amount
withdrawn successfully...!!!");
break;
case 3:
// get account balance
amount=loggedAccount.balanceEnquiry(getAccountType());
System.out.println("Account Balance is:
$"+amount);
break;
case 4:
System.out.print("Enter to account number:
");
int toAccountNo=sc.nextInt(); // get to-account
number
Account toAccount=null;
for (Account account : bank) {
if(account.getAccountNo()==toAccountNo)
toAccount=account;
}
System.out.print("Enter amount to transfer:
");
amount=sc.nextDouble(); // get amount to
transfer
if(toAccount!=null) {
// transfer amount
if(!loggedAccount.transferBalance(toAccount, getAccountType(),
amount))
System.out.println("Insufficient account balance...!!!");
else
System.out.println("Amount transferred successfully...!!!");
}else
System.out.println("Wrong to
account number...!!!");
break;
case 5:
showLoggedAccount(loggedAccount);
System.out.println("\n\n*** LOgged Out
***\nThank you for banking with us...!!!");
System.exit(0); // exit from program
break;
default:
System.out.println("Wrong choice...!!!");
break;
}
}
}
}
Output