Java.util.Date null output - java

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.

Related

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

Issues with overriding another class

I'm getting this error for some reason when I try to make a new withdraw method for the class "CheckingAccount." I also have a class named Account that has its own withdraw method.
Here's the code:
class CheckingAccount extends Account {
double overdraftmax = -50;
public CheckingAccount(int id, double balance) {
}
public void withdraw(double money) {
if (this.getBalance() - money >= overdraftmax) {
withdraw(money);
}
}
}
class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private java.util.Date dateCreated;
Account() {
dateCreated = new java.util.Date();
}
Account(int newId,double newBalance) {
this();
id = newId;
balance = newBalance;
}
int getId() {
return id;
}
double getBalance() {
return balance;
}
double getAnnualInterestRate() {
return annualInterestRate;
}
void setId(int newId) {
id = newId;
}
void setBalance(double newBalance) {
balance = newBalance;
}
void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
String getDateCreated() {
return dateCreated.toString();
}
double getMonthlyInterestRate() {
return (annualInterestRate / 100) / 12;
}
double getMonthlyInterest() {
return balance * getMonthlyInterestRate();
}
double withdraw(double money) {
return balance -= money;
}
double deposit(double money) {
return balance += money;
}
}
And here are the two errors I'm getting.
The return type is incompatible with Account.withdraw(double)
overrides Account.withdraw
I'm not sure what to fix.
when overring a method , u need to keep the same prototype of the method in the parent .
so here you are mixing the return type .
class CheckingAccount extends Account {
double overdraftmax = -50;
public CheckingAccount(int id, double balance) {
}
public double withdraw(double money) {
if (this.getBalance() - money >= overdraftmax) {
withdraw(money);
}
return "double var";
}
}
class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private java.util.Date dateCreated;
Account() {
dateCreated = new java.util.Date();
}
Account(int newId,double newBalance) {
this();
id = newId;
balance = newBalance;
}
int getId() {
return id;
}
double getBalance() {
return balance;
}
double getAnnualInterestRate() {
return annualInterestRate;
}
void setId(int newId) {
id = newId;
}
void setBalance(double newBalance) {
balance = newBalance;
}
void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
String getDateCreated() {
return dateCreated.toString();
}
double getMonthlyInterestRate() {
return (annualInterestRate / 100) / 12;
}
double getMonthlyInterest() {
return balance * getMonthlyInterestRate();
}
double withdraw(double money) {
return balance -= money;
}
double deposit(double money) {
return balance += money;
}
}
In Account class you make method withdraw() with returning type double, but in class CheckingAccount you overriding this method with returning type void.
In Java you can't change returning type in overriding method, because compiler don't understand what method you want to use.

Wrong output when creating an account (Object)

I'm having issues with my program which is a subclass of another program I wrote called account. I create an account (george) and set values to it, a string for name, int id, and double balance. I'm not getting any errors but the only correct value is the string for name. I think it has to do something with my Overridden toString() method in the account class not grabbing the correct values.
My output:
Account holder name: George
Account id: 0
Annual interest: 1.5
Monthly Interest Rate: 0.00125
Balance 109.0
Transaction:
Correct ouput:
Account holder name: George
Account id: 1122
Annual interest: 0.015
Monthly Interest Rate: 0.013862499999999998
balance: 1109.0
Transaction:
type: d amount: 30.0 balance: 1030.0 deposit
type: d amount: 40.0 balance: 1070.0 deposit
type: d amount: 50.0 balance: 1120.0 deposit
type: w amount: 5.0 balance: 1115.0 withdrawal
type: w amount: 4.0 balance: 1111.0 withdrawal
type: w amount: 2.0 balance: 1109.0 withdrawal
CustomerAccount.class :
public class CustomerAccount extends Account {
public static String name;
public static void main(String[] args) {
// TODO Auto-generated method stub
CustomerAccount george = new CustomerAccount("George", 1122, 1000.0);
george.setannualInterest(1.5);
Account c1 = george;
george.deposit(30);
george.deposit(40);
george.deposit(50);
george.withdraw(5);
george.withdraw(4);
george.withdraw(2);
System.out.println(c1.toString());
CustomerAccount john = new CustomerAccount("John", 1123, 500);
john.setannualInterest(2.5);
Account c2 = john;
ArrayList<CustomerAccount> sort = new ArrayList<CustomerAccount>();
//sort.add(c1);
//sort.addAll(c2);
}
CustomerAccount(String name, int id, double balance) {
this.name = name;
id = getId();
balance = getBalance();
}
ArrayList<Transaction> transactions = new ArrayList<Transaction>();
}
Transaction class:
class Transaction extends Account {
private java.util.Date dateCreated;
private char type;
private double amount;
private double balance;
private String description;
private double transaction;
Transaction() {
}
Transaction(char type, double amount, double balance, String description) {
dateCreated = new java.util.Date();
this.type = type;
this.amount = amount;
this.balance = balance;
this.description = description;
}
public String getDateCreated() {
return dateCreated.toString();
}
public char getType() {
return type;
}
public double getAmount() {
return amount;
}
public double getBalance() {
return balance;
}
public String getDescription() {
return description;
}
public double getTransaction() {
return this.transaction;
}
public void setType(char type) {
this.type = type;
}
public void setAmount(double amount) {
this.amount = amount;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setDescription(String description) {
this.description = description;
}
public void setTransaction(double amount) {
this.transaction = amount;
}
}
Account.class :
class Account {
private int id = 0;
private double balance = 0.0;
private double annualInterestRate = 0.0;
private java.util.Date dateCreated;
// set dateCreated for the time and date the account was created
Account() {
dateCreated = new java.util.Date();
}
// Set accounts id and balance equal to this objects
Account(int id, double balance){
this();
this.id = id;
this.balance = balance;
}
// Getters and Setters manipulating variables to be set to the created account
// Setters have no return value and set itself equal to the variable and getters
// grab the variables and return them.
public int getId() {
return this.id;
}
public double getBalance() {
return this.balance;
}
public double getannualInterestRate() {
return this.annualInterestRate;
}
public String getDateCreated() {
return this.dateCreated.toString();
}
public void setId(int id) {
this.id = id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setannualInterest(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMonthlyInterestRate() {
return (annualInterestRate / 100) / 12;
}
public double getMonthlyInterest() {
return balance * getMonthlyInterestRate();
}
// set balance of withdraw to balance - amount = balance
public void withdraw (double amount) {
if(amount < balance) {
balance -= amount;
}
}
// set balance of deposit to balance + amount = balance
public void deposit(double amount) {
balance += amount;
//Transaction.add(new Transaction('D', amount, balance, "Deposit to account"));
}
#Override
public String toString() {
return "Account holder name: " + CustomerAccount.name +
"\nAccount id: " + id +
"\nAnnual interest: " + this.getannualInterestRate() +
"\nMonthly Interest Rate: " + this.getMonthlyInterestRate() +
"\nBalance " + this.getBalance() +
"\nTransaction: ";
}
}
To set the values in the Account class, you need to call
super(id, balance)
in the CustomerAccount constructor to call the constructor for Account
Try that

Debit and Credit for Savings Class

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!

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