Debit and Credit for Savings Class - java

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!

Related

Linking Credit Card and Debit Card classes to a Bank Account Class

I'm learning Java, and one of my assignments is to create Credit and Debit Cards classes so that you can create new cards linked to a new individual account when one is created. The credit card should store purchases, and when the user makes a payment (total or partial), substract the amount from the account (plus 3% interest). The debit card immediately substracts the purchase's amount from the account.
I have written everything and made it work, but only as nested classes within the Account class. Is there a way to have Account, Credit and Debit as three separate classes and make it so that every time that you create a new account, you can optionally create cards linked to each new individual account?
Disclaimer: I'm trimming some of the code because it's all in spanish so I don't have to translate it all (mainly booleans to check for positive amounts and stuff like that), but the code is still functional. Thanks a lot in advance!
package accounts;
public class Account {
protected static double balance;
protected static String accountNumber;
public Account() {
}
public Account(String accountNumber, double balance) {
Account.accountNumber = accountNumber;
Account.balance = balance;
}
public double getBalance() {
return balance;
}
public String getAccountNumber() {
return accountNumber;
}
public void deposit(double depositAmount) {
Account.balance += depositAmount;
}
public void extraction(double extractionAmount) {
Account.balance -= extractionAmount;
}
public void showBalance() {
System.out.println("Your current balance is: " + getBalance());
}
protected static class DebitCard {
private String userName;
private int cardNumber;
public DebitCard () {
}
public DebitCard (String userName, int cardNumber) {
this.userName = userName;
this.cardNumber = cardNumber;
}
public String getUserName() {
return userName;
}
public int getCardNumber() {
return cardNumber;
}
public void debitPurchase(double purchaseAmount) {
Account.balance -= purchaseAmount;
}
}
protected static class CreditCard {
private String userName;
private int cardNumber;
private double creditCardDebt;
private double limit;
public CreditCard () {
}
public CreditCard(String userName, int cardNumber, double limit) {
this.userName = userName;
this.cardNumber = cardNumber;
this.limit = limit;
}
public String getUserName() {
return userName;
}
public int getCardNumber() {
return cardNumber;
}
public double getLimit() {
return limit;
}
public double getCrediCardDebt() {
return creditCardDebt;
}
public void creditCardPurchase(double purchaseAmount) {
if (this.creditCardDebt + purchaseAmount > this.limit) {
Error notEnoughLimit = new Error("There's not enough limit to make this purchase");
throw notEnoughLimit ;
} else {
this.creditCardDebt += purchaseAmount + (purchaseAmount * 0.03);
this.limit -= purchaseAmount;
this.creditCardDebt += purchaseAmount + (purchaseAmount* 0.03);
this.limit -= purchaseAmount;
}
}
public void payCard(double payAmount) {
Account.balance -= payAmount;
this.creditCardDebt = this.creditCardDebt - payAmount;
}
}
}
Yes it is possible. But before you do that, you should solve the problem caused by declaring balance and accountNumber as static variables. By doing that, you have made every Account instance share one account number and one balance.
Those variables should also be private so that other classes can't access or change them directly.
Once you have fixed that, you should then change the constructors for Credit and Debit to take an instance of Account as a parameter. They store this in a private field, and then perform operations to add and remove money from the Account ... via operations on the Account object.

Inheritance java-bank account (java)

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

How to avoid init override in Java

I am new to java and I am facing a problem like this.
public class Account {
private String name;
private double balance;
private static int nextID;
int acctID;
Account(String n, double bal) {
init();
this.name = n;
this.balance = bal;
print();
}
void init() {
this.acctID = nextID++;
}
}
public class SavingsAccount extends Account {
private double interest;
public SavingsAccount(String n, double bal, double interest) {
super(n, bal);
this.interest = interest;
}
void init() {
}
}
The class SavingsAccount will use its init() and this is wrong. I know I can do this to solve the problem.
public class Account {
private String name;
private double balance;
private static int nextID;
int acctID;
Account(String n, double bal) {
init();
this.name = n;
this.balance = bal;
print();
}
{
this.acctID = nextID++;
}
}
I would like to ask if there any other way to solve this problem, for example force other class to use the Account.init() or don't allow others to make their only init()?
If you make the init() method final, then nobody can override it.
final void init() {
this.acctID = nextID++;
}
http://docs.oracle.com/javase/tutorial/java/IandI/final.html

Java.util.Date null output

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.

Constructors for subclasses

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.

Categories