I am working on an inheritance bank account which has 5 classes.In one of the classes(InterestFreeDeposit) ,the account should be created with at least 10 $. How should I write the code of this part?
here is what I have done until now:
superClass:
import java.util.Scanner;
import java.io.FileReader;
public class Account {
private String owner;
private double balance;
private int accountNumber;
private double interestRate;
public Account( String owner,double balance, int accountNumber , double interestRate){
this.balance=balance;
this.owner=owner;
this.accountNumber=accountNumber;
this.interestRate=interestRate;
}
public void deposit(double amount) {
if(amount>0) {
this.balance+=amount;
}
}
public void withdraw(double amount) {
if(amount>0 && balance>=amount) {
this.balance-=amount;
}
}
public double getBalance() {
return balance;
}
public void setBalance(double amount) {
this.balance = amount;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public double getInterestRate() {
return interestRate;
}
public double setInterestRate(double interestRate) {
//System.out.println("Enter the period(month):");
return this.balance +=balance * interestRate/100;
}
subClass(InterestFrreClass):
public class InterestFreeDeposit extends Account {
public InterestFreeDeposit(String owner, double balance, int accountNumber, double interestRate) {
super(owner, balance, accountNumber, interestRate);
// TODO Auto-generated constructor stub
}
public void interest() {
super.setInterestRate(0.0) ;
}
}
You need to add the check when the value is set, which in this case happens in the constructor of InterestFreeDeposit. The question is how you want to react when the value is below 10. You could throw an IllegalArgumentException:
public InterestFreeDeposit(String owner, double balance, int accountNumber, double interestRate) {
super(owner, balance, accountNumber, interestRate);
if(balance < 10){
throw new IllegalArgumentException("balance must be at least 10");
}
}
The problem with this approach is that IllegalArgumentException is an unchecked exception, meaning that the caller of the constructor is not forced to deal with it if it is thrown.
You can use a ternary operator in your subclass constuctor to default to a 10.0 minimum value:
public class InterestFreeDeposit extends Account {
public InterestFreeDeposit(String owner, double balance, int accountNumber, double interestRate) {
super(owner, balance < 10.0 ? 10.0 : balance, accountNumber, interestRate);
}
}
A ternary operator can be seen as an inline if. So you have following structure:
condition ? condition is true : condition is false
In the above snippet it just checks if the provided balance is below the 10.0. if it is it just sets it to 10, else it is passed along:
balance < 10.0 ? 10.0 : balance
Related
I am supposed to Add the following method to allow BankAccounts to transfer money to another BankAccount. Where would I add this method to make sure it works for all BankAccounts, including SavingsAccount and CheckingAccount?
The method im supposed to use is transferTo(BankAccount destinationAccount, int transferAmount)
public class BankAccount {
private String accountHolderName;
private String accountNumber;
private int balance;
public BankAccount(String accountHolder, String accountNumber) {
this.accountHolderName = accountHolder;
this.accountNumber = accountNumber;
this.balance = 0;
}
public BankAccount(String accountHolder, String accountNumber, int balance) {
this.accountHolderName = accountHolder;
this.accountNumber = accountNumber;
this.balance = balance;
}
public String getAccountHolderName() {
return accountHolderName;
}
public String getAccountNumber() {
return accountNumber;
}
public int getBalance() {
return balance;
}
// Update the balance by using the DollarAmount.Plus method
public int deposit(int amountToDeposit) {
balance = balance + amountToDeposit;
return balance;
}
// Update the balance by using the DollarAmount.Minus method
public int withdraw(int amountToWithdraw) {
balance = balance - amountToWithdraw;
return balance;
}
public int transferTo(BankAccount destinationAccount, int transferAmount) {
return balance;
}
}
Assumptions:
This is only an exercise and not a real application for a bank.
SavingsAccount and CheckingAccount are subclasses of BankAccount
The method transferTo can be implemented like the following:
public int transferTo(BankAccount destinationAccount, int transferAmount) {
this.balance -= transferAmount;
destinationAccount.deposit(transferAmount);
return balance;
}
In a real world application you need to ensure that this operation will be always atomic and thread-safe. Additionally, using int for the balance is strongly not recommended.
I'm trying to apply a method that will check if the account has went over the OVERDRAFT_LIMIT. I have tried several things but haven't succeeded.
This was something I did but did not know how to apply it:
public void testForOverdraft(double balancE)
{
if(balancE <= 0 && balancE >= OVERDRAFT_LIMIT)
{
withdraw(10);
System.out.println("The account is overdrawn.");
}
else if(balancE <= 0 && balancE <= OVERDRAFT_LIMIT)
{
withdraw(20);
System.out.println("The account is locked until a deposit is made to bring the account up to a positive value.");
}
}
Account:
import java.util.Date;
public class Account
{
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
private double monthlyInterestRate;
public Account()
{
id = 0;
balance = 0;
annualInterestRate = 0;
}
public Account(int iD, double balancE)
{
id = iD;
balance = balancE;
}
public void setID(int iD)
{
id = iD;
}
public int getID()
{
return(id);
}
public void setBalance(double balancE)
{
balance = balancE;
}
public double getBalance()
{
return(balance);
}
public void setAnnualInterestRate(double AIR)
{
annualInterestRate = AIR;
}
public double getAnnualInterestRate()
{
return(annualInterestRate);
}
public void setDateCreated(Date dateCreated)
{
this.dateCreated = dateCreated;
}
public double getMonthlyInterestRate()
{
return((annualInterestRate / 100) / 12);
}
public double getMonthlyInterest()
{
return(balance * monthlyInterestRate);
}
public void withdraw(double ammount)
{
balance = balance - ammount;
setBalance(balance);
}
public void deposit(double ammount) {
balance = balance + ammount;
setBalance(balance);
}
}
CheckingAccount:
public class CheckingAccount extends Account
{
final static double OVERDRAFT_LIMIT = -50.00;
private double annualInterest;
public CheckingAccount()
{
super();
}
public CheckingAccount(int iD, double balancE)
{
super(iD, balancE);
}
public double getAnnualInterest()
{
return((getBalance() * getAnnualInterestRate()) / 100);
}
}
Test:
public class Test extends CheckingAccount
{
public static void main(String [] args)
{
CheckingAccount a1 = new CheckingAccount(1122, 15.00);
a1.withdraw(5.00);
a1.deposit(00.00);
a1.setAnnualInterestRate(4.5);
Date dat = new Date();
System.out.println("Balance: " +
"\nMonthly Interest: " + a1.getMonthlyInterest() +
"\nDate Created: " + dat);
}
}
Check the new balance before assinging it in the withdraw method
The withdraw method will return true if it was success full
public boolean withdraw(double ammount)
{
boolean success ;
double aux ;
aux = balance - ammount ;
if(aux < OVERDRAFT_LIMIT)
{
success = false ;
}
else
{
setBalance(aux);
success = true ;
}
return success ;
}
Then to use it just go like this
if(!withdraw(60))
{
System.out.println("The account is locked until a deposit is made to bring the account up to a positive value.");
}
I have an abstract class that has 4 methods for crediting, debiting, depositing and withdrawing. I am stuck on how I should go about setting up the methods for crediting and debiting the account, since I would assume it would be the same as deposit and withdrawal, but I don't believe it is. There is already a test-harness that she gave us, so I just need to setup the classes. This is the abstract class
abstract public class Account implements PostInterest
{
private String fName;
private String lName;
private String nbrSSN;
private char acctStatus = 'A';
private double balance = 0.0;
//five-argument constructor
public Account(String first, String last, String ssn, char status, double begBalance)
{
setName(first);
setLast(last);
setSSN(ssn);
setStatus(status);
setBalance(begBalance);
}
public Account(String first, String last, String ssn)
{
setName(first);
setLast(last);
setSSN(ssn);
}
public void setName(String first)
{
fName = first;
}
public String getName()
{
return fName;
}
public void setLast(String last)
{
lName = last;
}
public String getLast()
{
return lName;
}
public void setSSN(String ssn)
{
nbrSSN = ssn;
}
public String getSSN()
{
return nbrSSN;
}
public void setStatus(char status)
{
acctStatus = status;
}
public char getStatus()
{
return acctStatus;
}
public void setBalance(double begBalance)
{
balance = begBalance;
}
public double getBalance()
{
return balance;
}
public abstract void debitAccount(double amount);
public abstract void creditAccount(double amount);
public abstract void deposit(double amount);
public abstract void withdraw(double amount);
}
And this is one of the classes that I have for savings. I need a few other classes, but I was stuck on the difference in setting up the credit and debit compared to the deposit and withdrawal if that makes sense.
public class Savings extends Account
{
private double intRate;
public Savings(String first, String last, String ssn, double begBalance, double rate)
{
super(first, last, ssn, begBalance);
setRate(rate);
}
Public Savings(String first, String last, String ssn, double rate)
{
super(first, last, ssn);
setRate(rate);
}
public void setRate(double rate)
{
intRate = rate;
}
public double getRate()
{
return intRate;
}
#Override
public void debitAccount(double amount)
{
}
#Override
public void creditAccount(double amount)
{
}
#Override
public void deposit(double amount)
{
balance += amount;
}
#Override
public void withdraw(double amount)
{
if(amount > balance)
{
balance = -1;
}
else
{
balance -= amount;
}
}
#Override
public void postMonthly()
{
((intRate/12)* balance);
}
}
I haven't setup the postMonthly method yet, but she wants us to calculate the interest for the month based on the current balance and intRate and it invokes creditAcct(), passing result of interest calculation, which confused me a bit too. Any help would be greatly appreciated!
I have no idea why my code is giving me a null for java.util.date.
Question: Write a test program that creates an Account object with an account ID of 1122, a balance of 20000, and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2500, use the deposit method to deposit $3000, and print the balance, the monthly interest, and the date when this account was created
Here is my code:
import java.util.*;
public class Account {
private int ID;
private double Balance;
private double annualInterestRate;
private java.util.Date dateCreated;
public Account(){}
public Account(int ID, double Balance, double annualInterestRate){
this.ID=ID;
this.Balance=Balance;
this.annualInterestRate= annualInterestRate;
}
public void setID(int ID){
this.ID=ID;
}
public void setBalance(double Balance){
this.Balance=Balance;
}
public void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate= annualInterestRate;
}
public int getID(){
return ID;
}
public double getBalance(){
return Balance;
}
public double getInterestRate(){
return annualInterestRate;
}
public java.util.Date getDateCreated(){
return dateCreated;
}
public double getMonthlyInterestRate(){
return annualInterestRate/12;
}
public void withDraw(double val){
if ((Balance - val) <0)
{
System.out.println("Offensive content removed from this line");
}
else
{
Balance -= val;
}
}
public void dePosits(double value){
Balance += value;
}
public static void main(String [] arges){
Account account = new Account(1122, 20000,.045);
account.withDraw(2500);
account.dePosits(3000);
System.out.println(account.getBalance());
System.out.println(account.getDateCreated());
}
}
You haven't initialized dateCreated. For example, in the constructor (or somewhere else depending on your use case)
dateCreated = new java.util.Date();
or any other way to initialize with the date it needs.
Okay guys, im having trouble with these constructors for this code and some logic. I did most of it, just confused on how to finish this off.
Here is the main code i have. (it might be a little sloppy but its "good enough")
public class Accountdrv {
public static void main (String[] args) {
Account account = new Account(1122, 20000, 4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " +
account.getMonthlyInterest());
System.out.println("This account was created at " +
account.getDateCreated());
}
}
class Account {
private int id;
private double balance;
private double annualInterestRate;
private java.util.Date dateCreated;
public Account() {
dateCreated = new java.util.Date();
}
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
dateCreated = new java.util.Date();
}
public int getId() {
return this.id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int id) {
this.id =id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMonthlyInterest() {
return balance * (annualInterestRate / 1200);
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
}
Now, i wanted to create a Savings, and Checking account. I need help with what i need to add for constructors, i put in the comments the parts i know im missing but confused on how to do them.
Savings:
class Savings extends Account{
//need to create a constructor
public Savings(int id, double balance, double annualInterestRate) {
//need to invoke the constructor for Account
super(id, balance, annualInterestRate);
}
// need to override the withdraw method in Account
public void withdraw(double amount) {
// place logic to prevent the account from being overdrawn
// that is do not allow a negative balance
}
}
You just need to check id the amount < balance Or whatever logic you have (eg: you want
some threshold amount to be present in the account).
Also I would recommend to look into synchronized methods as you should synchronize access to account of a given user to ensure that when a withdraw function is invoked...another withdraw on the same user account has to wait...or else it will lead to problems...I will leave that to you to figure out.
public void withdraw(double amount) {
// place logic to prevent the account from being overdrawn
// that is do not allow a negative balance
if(balance < amount)
{
// print error message or do something
}
else
{
// withdraw the money
balance -= amount;
// print message or do something
}
}
super() invokes the constructor for the super-class, in the Savings case, Account.