In: Computer Science
Account Type: SAVING
Account Owner: Customer B
Interest Rate: 1.1
Balance: 500.0
Account Type: CHECKING
Account Owner: Customer A
Interest Rate: 1.2
Balance: 200.0
Cannot withdraw more than account balance
Account Type: SAVING
Account Owner: Customer B
Interest Rate: 1.1
Balance: 700.0
Account Type: CHECKING
Account Owner: Customer A
Interest Rate: 1.2
Balance: 200.0
solution provided:
package csci1011.csci1011.lab8;
import java.text.NumberFormat;
import java.text.DecimalFormat;
class Account {
enum ACCOUNT_TYPE
{
CHECKING,SAVINGS;
}
//create five variable
private ACCOUNT_TYPE aType;
private String accountOwner;
private double interestRate;
private double Balance;
//setter and getter methods
private ACCOUNT_TYPE getaType() {
return aType;
}
private void setaType(ACCOUNT_TYPE aType) {
this.aType = aType;
}
private String getAccountOwner() {
return accountOwner;
}
private void setAccountOwner(String accountOwner) {
this.accountOwner = accountOwner;
}
private double getInterestRate() {
return interestRate;
}
private void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
private double getBalance() {
return Balance;
}
private void setBalance(double Balance) {
this.Balance = Balance;
}
void initialize(ACCOUNT_TYPE atype,String name,double interestRate,double balance)
{
setaType(atype);
setAccountOwner(name);
setInterestRate(interestRate);
setBalance(balance);
}
void display()
{
System.out.println(" Account Type : "+aType.name());
System.out.println("Account Owner : "+getAccountOwner());
System.out.println("Interest Rate : "+getInterestRate());
System.out.println("Balance : "+getBalance());
}
//deposit to the account
void deposit(double amount)
{
double bal=getBalance();
setBalance(bal+amount);
System.out.println("Successfull Deposit ! ");
}
//withdraw from account
void withdraw(double amount)
{
//if balance is 0 or amount is greater than balance
if(getBalance()<=0 || amount>getBalance())
{
System.out.println("You don't have enough money to withdraw");
}
else
{
double bal=getBalance();
setBalance(bal-amount);
System.out.println("Successfully withdrawn ");
}
}
public static void main(String args[])
{
//two objects
Account ac=new Account();
Account ac1=new Account();
//initialize with Savings
ac.initialize(ACCOUNT_TYPE.SAVINGS, "Customer B ", 1.1, 500);
//initialize with Checking
ac1.initialize(ACCOUNT_TYPE.CHECKING, "Customer A ", 1.2, 200);
//display
ac.display();
ac1.display();
//deposit into account
ac.deposit(20);
//withdraw from account
ac.withdraw(60);
//display
ac.display();
}
}
Two classes added.1.Account 2.Main
ACCOUNT_TYPE of Account is accessed in Main class by creating the object of Account class .
some enhancesment mades in methods for understanding display section.
Java code and screenshort of output shown below
import java.text.NumberFormat;
import java.text.DecimalFormat;
class Account {
enum ACCOUNT_TYPE
{
CHECKING,SAVINGS;
}
//create five variable
private ACCOUNT_TYPE aType;
private String accountOwner;
private double interestRate;
private double Balance;
//setter and getter methods
private ACCOUNT_TYPE getaType() {
return aType;
}
private void setaType(ACCOUNT_TYPE aType) {
this.aType = aType;
}
private String getAccountOwner() {
return accountOwner;
}
private void setAccountOwner(String accountOwner) {
this.accountOwner = accountOwner;
}
private double getInterestRate() {
return interestRate;
}
private void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
private double getBalance() {
return Balance;
}
private void setBalance(double Balance) {
this.Balance = Balance;
}
void initialize(ACCOUNT_TYPE atype,String name,double interestRate,double balance)
{
setaType(atype);
setAccountOwner(name);
setInterestRate(interestRate);
setBalance(balance);
}
void display()
{
System.out.println(" Account Type : "+aType.name());
System.out.println("Account Owner : "+getAccountOwner());
System.out.println("Interest Rate : "+getInterestRate());
System.out.println("Balance : "+getBalance());
}
//deposit to the account
void deposit(double amount)
{
double bal=getBalance();
setBalance(bal+amount);
System.out.println("$"+amount+" Successfull Deposit ! ");
}
//withdraw from account
void withdraw(double amount)
{
//if balance is 0 or amount is greater than balance
if(getBalance()<=0 || amount>getBalance())
{
System.out.println("You don't have enough money to withdraw");
}
else
{
double bal=getBalance();
setBalance(bal-amount);
System.out.println("Successfully withdrawn $"+amount);
}
}
}
class Main
{
public static void main(String args[])
{
//two objects
Account ac=new Account();
Account ac1=new Account();
//acess ACCOUNT_TYPE through object of class in another class
Account.ACCOUNT_TYPE type1=Account.ACCOUNT_TYPE.SAVINGS;
//initialize with Savings
ac.initialize(type1, "Customer A ", 1.1, 500);
Account.ACCOUNT_TYPE type2=Account.ACCOUNT_TYPE.CHECKING;
//initialize with Checking
ac1.initialize(type2, "Customer B ", 1.2, 200);
//display
System.out.println("First Account is:");
ac.display();
System.out.println("Second Account is:");
ac1.display();
//deposit into account
ac.deposit(200);
//withdraw from account
ac1.withdraw(500);
//display
System.out.println("First Account is:");
ac.display();
System.out.println("Second Account is:");
ac1.display();
}
}