public class JavaAccount { private String customerName; private long AccountNumb
ID: 3844847 • Letter: P
Question
public class JavaAccount { private String customerName; private long AccountNumber; private double balance; private double interestRate; JavaAccount(String customerName,long AccountNumber,double balance,double interestRate){ this.customerName=customerName; this.AccountNumber=AccountNumber; this.balance=balance; this.interestRate=interestRate; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public long getAccountNumber() { return AccountNumber; } public void setAccountNumber(long accountNumber) { AccountNumber = accountNumber; } public double getBalance() { return balance; } public void setBalance(double d) { this.balance = d; } public double getInterestRate() { return interestRate; } public void setInterestRate(double d) { this.interestRate = d; } public void deposit(double amount){ this.setBalance(this.getBalance()+amount); System.out.println("Deposit Successful"); System.out.println("New Amount:"+this.getBalance()); return; } public void withdraw(double amount){ this.setBalance(this.getBalance()-amount); System.out.println("Withdrawl Successful"); System.out.println("New Amount:"+this.getBalance()); return; } public void checkBalance(){ System.out.println("Balance:"+getBalance()); } }
Create a PremiumAccount.java class.
(a) When you extend the javaAccount.java, you will need to override the super() constructors.
(b) Add a static field called "minimumBalanceRequired" such that when account balance meets the minimumBalanceRequired, you get unlimited checking without fees. Otherwise, regular checking fees will apply. // hint: inside the withdraw() method, deduct a fee whenever the balance is below the minimum.
(c) Add a static method to reset minimumBalanceRequired.
(d) Add a new data fields called PIN# such that an internal PIN# has to match the input parameters before any transactions. Re-write the following methods with a PIN#: checkBalance(); deposit(), withdraw() and so on.
(e) Add an implementation of the .toString() method to override any default methods: The toString() methods returns the premium account information
#2. Add implementation of exception handling to the PremiumAccount.java class.
(a) Inside each method with a PIN# input parameter, throw a appropriate exception object of your choice.
(b) (OPTIONAL) Implement a new exception mechanism that allows only 3 consecutive trials of invalid PINs.
Explanation / Answer
//JavaAccount.java
package sample;
public class JavaAccount {
private String customerName;
private long AccountNumber;
private double balance;
private double interestRate;
JavaAccount(String customerName, long AccountNumber, double balance,
double interestRate) {
this.customerName = customerName;
this.AccountNumber = AccountNumber;
this.balance = balance;
this.interestRate = interestRate;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public long getAccountNumber() {
return AccountNumber;
}
public void setAccountNumber(long accountNumber) {
AccountNumber = accountNumber;
}
public double getBalance() {
return balance;
}
public void setBalance(double d) {
this.balance = d;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double d) {
this.interestRate = d;
}
public void deposit(double amount) {
this.setBalance(this.getBalance() + amount);
System.out.println("Deposit Successful");
System.out.println("New Amount:" + this.getBalance());
return;
}
public void withdraw(double amount) {
this.setBalance(this.getBalance() - amount);
System.out.println("Withdrawl Successful");
System.out.println("New Amount:" + this.getBalance());
return;
}
public void checkBalance() {
System.out.println("Balance:" + getBalance());
}
}
//PremiumAccount.java
package sample;
public class PremiumAccount extends JavaAccount {
PremiumAccount(String customerName, long AccountNumber, double balance,
double interestRate) {
super(customerName, AccountNumber, balance, interestRate);
// TODO Auto-generated constructor stub
}
private static double minimumBalanceRequired;
private double checkingFee;
private int PIN;
public void deposit(double amount, int pass) throws InvalidPin {
try{
if (pass == this.PIN) {
this.setBalance(this.getBalance() + amount);
System.out.println("Deposit Successful");
System.out.println("New Amount:" + this.getBalance());
return;
} else {
System.out.println("Please enter a valid PIN");
throw new InvalidPin(pass);
}
}
catch(InvalidPin p)
{
System.out.println("Exception thrown invalid pin "+ p.getPin());
}
}
public void withdraw(double amount, int pass) throws InvalidPin {
try{
if (pass == this.PIN) {
this.setBalance(this.getBalance() - amount);
System.out.println("Withdrawl Successful");
if (this.getBalance() < minimumBalanceRequired)
this.setBalance(getBalance() - checkingFee);
System.out.println("New Amount:" + this.getBalance());
return;
} else {
System.out.println("Please enter a valid PIN");
throw new InvalidPin(pass);
}
}
catch(InvalidPin p)
{
System.out.println("Exception thrown invalid pin "+ p.getPin());
}
}
public void checkBalance(int pass) throws InvalidPin {
try{
if (pass == this.PIN) {
System.out.println("Balance:" + getBalance());
} else {
System.out.println("Please enter a valid PIN");
throw new InvalidPin(pass);
}
}
catch(InvalidPin p)
{
System.out.println("Exception thrown invalid pin "+ p.getPin());
}
}
public static void resetMinimumBalanceRequired(double amount) {
minimumBalanceRequired = amount;
}
public String toString()
{
return this.getCustomerName() + " account number "+ this.getAccountNumber() +" having balance "+ this.getBalance() +" with interest rate "+ this.getInterestRate();
}
}
//InvalidPin.java
Below is the file for exception handing in case of wrong pin.
package sample;
public class InvalidPin extends Exception {
private int PIN;
public InvalidPin(int pass) {
this.PIN = pass;
}
public double getPin() {
return PIN;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.