In Java netbeans According to the following information, define Account, Checkin
ID: 3908927 • Letter: I
Question
In Java netbeans
According to the following information, define Account, CheckingAccount, and TestPart1 classes.
1. Account and CheckingAccount classes
Account (parent class)
- number: int
- openDate: String
- name: String
- balance: double
+ Account(int, String, String, double)
+ deposit(double): void
+ withdraw (double): boolean
+ transferTo(Account, double): int
+ toString(): String
CheckingAccount (child class)
+ CheckingAccount(int, String, String, double)
+ transferTo(Account, double): int
Given the above UML diagam, define Account and CheckingAccount classes as follow:
Account
• public Account(int nu, String op, String na, double ba): is a constructor and uses passed-in arguments to initialize instance variables.
• public void deposit(double amount): updates the account balance using the passed-in amount.
• public boolean withdraw(double amount): if the amount is not more than the balance, updates the balance with the amount, and then returns true; otherwise, returns false.
• public abstract int transferTo(Account account, double amount): is an abstract method.
• public String toString(): returns all attribute values in this format, number<>openDate<>name<>balance, for example, 1001<>2018/6/21<>John Smith<>750.42
CheckingAccount (5 points)
• This is a child class of Account and implements the transferTo() method as follows.
• public int transferTo(Account account, double amount): the first argument is the beneficiary account, and the second argument is the transfer amount. This method transfers the money from this current account to the beneficiary account, and returns
? 0: transfer is successful and a $2 transfer fee is not applied
? 1: transfer is successful and the $2 transfer fee is applied
? -1: transfer is unsuccessful because balance is less than transfer amount and $2 transfer fee
? -2: transfer is unsuccessful because balance is less than transfer amount
For our projects, the business rules are:
? If the balance of transferring account is $10000 or higher, a $2 transfer fee is waived.
? When making a transfer to a beneficiary account, if the balance of the transferring account is less than $10000, $2 transfer fee is deducted from the balance when the transfer occurs.
? If the balance is not sufficient to cover transfer amount (and the $2 transfer fee if the fee applies), the transfer should not occur.
• public CheckingAccount(int nu, String op, String na, double ba): is a constructor and uses passed-in arguments to initialize instance variables.
2. TestPart1 class (7 points)
• This class has the main() method.
• You first creates a CheckingAccount object, and use the object to test deposit() and withdraw() methods, and use the toString() and System.out.println() to clearly display results for this CheckingAccount object after the deposit action and after the withdrawal action, respectively.
• You can use hard-code values for deposit and withdrawal amounts and for Account and/or CheckingAccount attribute values.
• Next, you create another CheckingAccount object, and test the transferTo() method. You can use hard-code values and use toString() and System.out.println() to clearly display results for the two CheckingAccount objects before and after the transfer action, respectively.
Explanation / Answer
import java.io.*;
import java.util.*;
abstract class Account {
private int number;
private String openDate;
private String name;
private double balance;
public Account(int n, String od, String nm, double bal){
number = n;
openDate = od;
name = nm;
balance = bal;
}
public void deposit( double a){
balance = balance + a;
}
public boolean withdraw(double a){
if (a > balance)
return false;
else
balance = balance - a;
return true;
}
abstract public int transferTo(Account a, double b);
public String toString(){
return Integer.toString(number) + "<>" + openDate + "<>" + name + "<>" + Double.toString(balance);
}
}
class CheckingAccount extends Account{
public CheckingAccount(int n, String od, String nm, double bal){
super(n,od,nm,bal);
}
public int transferTo(Account a, double b){
int res = 0;
if (withdraw(b)){
if (b <10000){
res = 0;
}
else {
res = 1;
}
}
else {
if (b <10000){
res = -1;
}
else {
res = -2;
}
}
return res;
}
}
public class TestPart1{
public static void main(String[] args){
CheckingAccount c1 = new CheckingAccount(100,"21/2/2018","name1",200);
CheckingAccount c2 = new CheckingAccount(200,"21/2/2018","name2",200);
System.out.println(c1);
c1.deposit(100);
System.out.println(c1);
c1.withdraw(100);
System.out.println(c1);
System.out.println(c2);
System.out.println(c1.transferTo(c2,100));
System.out.println(c2);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.