Inheritance/Sub-Class Assistance - java

I am trying to create a sub-class of my bank accounts with a checking and savings account sub-class. I have the accounts/sub-classes created, but I am having a hard time implementing the overdraw limit on the checking account and preventing the savings account from being overdrawn. I appreciate anyone who can help show me a way to manipulate these sub-classes more. Thank you!
Here is the code I have so far:
Current Code
import java.util.Date;
public class Accounts
{
public static void main(String[] args)
{
//Account account = new Account(1122, 20000, .045);
Account checkingAccount = new CheckingAccount (1500, 100.00, 0.6);
java.util.Date dateCreated = new java.util.Date();
System.out.println("Checking Account ID: " + checkingAccount.id);
System.out.println("Balance: $" + checkingAccount.balance);
System.out.println("Balance after withdraw of $80: $" + checkingAccount.withdraw(80));
//System.out.println("Overall Account ID: " + account.id);
//System.out.println("Date Created: " + dateCreated);
//System.out.println("Balance: $" + account.getBalance());
//System.out.println("Interest Rate: " + account.getAnnualInterestRate() + "%");
//System.out.println("Monthly Interest: $" + account.getMonthlyInterestRate());
//System.out.println("Balance after withdraw of $2,500: $" + account.withdraw(2500));
//System.out.println("Balance after deposit of $3,000: $" + account.deposit(3000));
}
//Create Account Class
public static class Account
{
//Define Variables
public int id;
public double balance; // Account Balance
public double annualInterestRate;// Store Interest Rate
private Date dateCreated; // Stores Account Creation Date
// Account Constructor
Account ()
{
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
// Account Constructor with id and initial balance
Account(int newId, double newBalance)
{
id = newId;
balance = newBalance;
}
Account(int newId, double newBalance, double newAnnualInterestRate)
{
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
// Methods for id, balance, and annualInterestRate
public int getId()
{
return id;
}
public double getBalance()
{
return balance;
}
public double getAnnualInterestRate()
{
return annualInterestRate * 100;
}
public void setId(int newId)
{
id = newId;
}
public void setBalance(double newBalance)
{
balance = newBalance;
}
public void setAnnualInterestRate(double newAnnualInterestRate)
{
annualInterestRate = newAnnualInterestRate;
}
// Accessor method for dateCreated
public void setDateCreated(Date newDateCreated)
{
dateCreated = newDateCreated;
}
// Define method getMonthlyInterestRate
double getMonthlyInterestRate()
{
return (annualInterestRate/12) * balance;
}
// Define method withdraw
double withdraw(double amount)
{
return balance -= amount;
}
// Define method deposit
double deposit(double amount)
{
return balance += amount;
}
}
//Create Checking Account
public static class CheckingAccount extends Account
{
CheckingAccount (int id, double balance, double annualInterestRate)
{
super(id, balance, annualInterestRate);
}
}
//Create Savings Account
public class SavingsAccount extends Account
{
//SavingsAccount constructor
public SavingsAccount (int id, double balance, double annualInterestRate)
{
super(id, balance, annualInterestRate);
}
}
}

So I have edited the Savings Account to reflect not being able to withdraw if the account is less than zero, but it's not letting me use the statements.
Savings Account Sub-Class
public class SavingsAccount extends Account
{
//SavingsAccount constructor
public SavingsAccount (int id, double balance, double annualInterestRate)
{
super(id, balance, annualInterestRate);
public double withdraw(double amount)
{
if (balance < 0)
{
System.out.println("Can't withdraw");
}
else (balance >= 0)
{
return balance -= amount;
}
}

Related

Reading a file and writing to cmd window using mutators/accessors

I'm working on a project for school and seem to have dug myself into a rabbit hole. I need to read a file consisting of just positive and negative numbers from a file and display them to a command window along with the date. I was able to get the end result but cannot list the marginal results. Thanks in advance!
class Account{
private int id = 0; //private int data field named id for the account (default 0).
private double balance = 0.0; //private double data field named balance for the account (default 0)
private static double annualInterestRate = 0.0; //private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.
private java.util.Date dateCreated; //private Date data field named dateCreated that stores the date when the account was created.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public Account() { //no-arg constructor that creates a default account.
dateCreated = new java.util.Date();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public Account(int id, double balance) { //constructor that creates an account with the specified id and initial balance.
this();
this.id = id;
this.balance = balance;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public int getId() {//accessor and mutator methods for id, balance, and annualInterestRate.
return this.id;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public double getBalance() {//accessor and mutator methods for id, balance, and annualInterestRate.
return this.balance;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public double getAnnualInterestRate() {//accessor and mutator methods for id, balance, and annualInterestRate.
return annualInterestRate;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public String getDateCreated() {//accessor method for dateCreated
return this.dateCreated.toString();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void setId(int id) { //mutator for id
this.id = id;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void setBalance(double balance) { //mutator for balance
this.balance = balance;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void setAnnualInterestRate(double annualInterestRate) { //mutator annual interest rate
this.annualInterestRate = annualInterestRate;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public double getMonthlyInterestRate() { //method named getMonthlyInterestRate() that returns the monthly interest rate.
return (annualInterestRate / 100) / 12 ;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public double getMonthlyInterest() { //method named getMonthlyInterest() that returns the monthly interest.
return balance * getMonthlyInterestRate();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void withdraw(double amount) { //method named withdraw that withdraws a specified amount from the account.
this.balance -= amount;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void deposit(double amount) { //method named deposit that deposits a specified amount to the account.
this.balance += amount;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
}//end class account
I'm pretty sure that class is OK. It's the one with the main method that I need the most help or guidance with.
import java.util.Scanner;
import java.io.*;
public class AccountHomework{
public static void main(String[] args)throws IOException{
File fn = new File("transactions.txt");
Scanner dataIn = new Scanner(fn);
Account account = new Account(1122, 20000);
double[] transactions = new double[10];
account.setAnnualInterestRate(4.5);
account.withdraw(2500.0);
account.deposit(3000.0);
printMethod(account);
}
public static void printMethod(Account acct){
System.out.printf(" %8s %17s %13s","Balance","Monthly Interest","Date Created\n");
System.out.printf(" $%6.2f $%6.2f %s\n",acct.getBalance(),acct.getMonthlyInterest(),acct.getDateCreated());
}
public static void fillTransactions(Scanner dataIn, Stock[] stocks){
double transactions;
for(int indx = 0; indx<stocks.length;indx++){
transactions = dataIn.nextDouble();
dataIn.nextLine(); // read the extra carriage return
if (transactions < 0)
dataIn.withdraw();
else
dataIn.deposit();
account[indx] = new Account();
account[indx].populateStockData(transactions);
}//end for
}//end fillStockArray
}
I noticed 2 things:
1. the scanner dataIn is not used to read the data, I see you have a method fillTransaction, but its not called from main
2. Within fillTransactions you are using the methods withdraw() and deposit() on the scanner, when the methods are actually defined on the class Account

How do I fix an "Incompatible Type" error in my program?

I am trying to make a make a account program for Java. I have most of it type out but I am having trouble with the date and printing.
I am trying to
Print the accounts monthly interest.
Print the accounts monthly interest rate.
Withdraw $7,500 from the account.
Print the accounts monthly interest again.
Deposit $11,0000 in the account.
Print the monthly Interest
ERRORS
In my class in DateCreated.
Incompatible type: Date cannot be converted to String
In my main class, getDateCreated and getAnnualInterest both have cannot find symbol errors.
Here is my code for main
package testaccount;
public class TestAccount {
public static void main(String[] args) {
Account account1 = new Account(5648, 27000, 3.9);
account1.withdraw(7500);
account1.deposit(11000);
System.out.println("Your current balance is" +account1.getBalance());
System.out.println("Monthly interest is"+ account1.getAnnualInterestRate);
System.out.println("The account was created on" + account1.getDateCreated);
}
}
Here's the code for my class
package testaccount;
import java.util.Date;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
Account(int Nid, double NBalance) {
id = Nid;
balance = NBalance;
}
Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
double withdraw(double amount) {
return balance -= amount;
}
double deposit(double amount) {
return balance += amount;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public void setDateCreated(Date newDateCreated){
this.dateCreated = dateCreated;
}
double getannaulnterestRate(){
return annualInterestRate/12;
}
public String getDateCreated() {
return dateCreated;
}
}
You need to return date in String format:
public String getDateCreated() {
return dateCreated.toString();
}
It seems you are also not setting date when instantiating an account object. You need to use it like:
account.setDateCreated(new Date());
Or better pass Date in constructor itself. It seems you need to read about constructor, method and other basic constructs of a class.
The reason you are getting cannot find symbol errors is that you are trying to access them as fields when they are in fact methods:
System.out.println("Monthly interest is"+ account1.getAnnualInterestRate);
System.out.println("The account was created on" + account1.getDateCreated);
Should be:
System.out.println("Monthly interest is"+ account1.getAnnualInterestRate());
System.out.println("The account was created on" + account1.getDateCreated());
Main Class Code
public class TestProgram {
public static void main(String[] args) throws FileNotFoundException {
Date date= new Date();
Account account1 = new Account(5648, 27000, date);
account1.withdraw(7500);
account1.deposit(11000);
System.out.println("Your current balance is" +account1.getBalance());
System.out.println("Monthly interest is"+ account1.getAnnualInterestRate());
System.out.println("The account was created on" + account1.getDateCreated());
}
}
Account class Code
import java.util.Date;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
Account(int Nid, double NBalance) {
id = Nid;
balance = NBalance;
}
Account(double balance, double annualInterestRate, Date dateCreated ) {
this.balance = balance;
this.annualInterestRate = annualInterestRate;
this.dateCreated=dateCreated;
}
Account(int id, double balance, double annualInterestRate, Date dateCreated ) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
this.dateCreated=dateCreated;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
double withdraw(double amount) {
return balance -= amount;
}
double deposit(double amount) {
return balance += amount;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public void setDateCreated(Date newDateCreated){
this.dateCreated = dateCreated;
}
double getannaulnterestRate(){
return annualInterestRate/12;
}
public Date getDateCreated() {
return dateCreated;
}
}

Creating Instances/Testing code

Arlight so i have all of this code that i have done, its quite a bit.. Basically its just making a banking account. I made code for Checking, and Savings account. All of this code is correct. i just need help with making an instance of Savings and Checking accounts in my Testaccount class.
Main Account code:
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;
}
}
Savings:
class Savings extends Account{
public Savings(int id, double balance, double annualInterestRate) {
super(id, balance, annualInterestRate);
}
public void withdraw(double amount) {
if(super.getBalance() < amount)
{
System.out.println("Error");
}
else
{
super.withdraw( amount );
System.out.println("Withdraw Completed");
}
}
}
Checking:
public class Checking extends Account{
private double overdraft_limit = 100;
public Checking(int id, double balance, double annualInterestrate){
//super();
super(id, balance, annualInterestrate);
}
public void withdraw(double amount) {
if(super.getBalance() >= (amount + overdraft_limit))
{
System.out.println(" account Overdrawn");
}
else
{
super.withdraw( amount );
System.out.println("Withdraw Completed");
}
}
}
Okay this is the part where i need help, its probably really simple but i can't wrap my head around how to write it out,i need to create an instance of Savings and Checking and withdraw some money, here is what i did so far.
Testaccount:
public class Testaccount {
public static void main(String[] args) {
Account account = new Account(0, 100, 0.6);
System.out.println(account);
account.withdraw(10.50);
System.out.println(account);
account.deposit(5.0);
System.out.println(account);
// Need to add test cases for Savings and Checking
}
}
Did you want to use the information already existing in the Account instance you created? If not (i.e. you want to create a new checking and savings account with initial amount in them) you could do something like
Checking checking = new Checking(0,100,0.6);
checking.withdraw(50.00);
Otherwise, if you want to use the existing Account instance and make it a Checking account and withdraw, do the following:
Checking checking = (Checking) account;
checking.withdraw(50.00);

Creating subclasses

I had to create a class using Account that was defined to model a bank account. (the account has the properties account number, balance, annual interest rate, date created, and methods to deposit/withdraw funds.)
How would i create two subclasses for checking and saving accounts? A checking account has to have an overdraft limit, but the savings cant be over drawn.
Any help or advice would be awesome, thank you(:
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;
}
}
Consider defining one or more interfaces.
For example:
public interface Account
{
public double getAnnualInterestRate();
public Date getDateCreated();
public int getId();
public double getMonthlyInterest();
public void deposit(double amount);
public void withdraw(double amount);
}
public interface CheckingAccount
extends Account
{
public long getOverdraftLimit();
}
It might be reasonable to add setters, but I prefer to either set the vlues in the implementing class constructor or put the setters in the implementing class, but not in the interface. A reasonable exception might be setOverDraftLimit() in the CheckingAccount interface.
You are making some common money mistakes:
money is not a floating point value. it is a fixed point value. double is never correct for money. Use long instead and store as fractions of a unit (for example 10000 = 1 dollar).
Learn how do perform interest calculations. Annual interest rate is never 12 * monthly interest rate.

Initializing array of objects to $100

This program creates 10 accounts and sets the balance to $100 each.
I wrote a for loop to initialize the balance but doesn't work.
The setBalance() method is not recognize within the AccountArray Class
How do I assign $100 balance to each account?
public class AccountArray{
public static AccountArray[] createAccountArray(){
AccountArray[] atm = new AccountArray [10];
for(int i = 0; i < atm.length; i++){
atm[i] = new AccountArray(setBalance(100));
}
return atm;
}
}
import java.util.Date;
public class Account{
public int id = 0;
public double balance = 0;
public double annualInterestRate = 0;
public double withdraw;
public double deposit;
java.util.Date date = new java.util.Date();
public Account(){
}
public Account(int id, double balance, double annualInterestRate){
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public Account(int id, double balance, double annualInterestRate, Date date){
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
this.date = date;
}
public void setID(int id){
this.id = id;
}
public int getID(){
return id;
}
public double getBalance(){
return balance - withdraw + deposit;
}
public void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate = annualInterestRate;
}
public double getAnnualInterestRate(){
return (annualInterestRate/100);
}
public Date getDateCreated(){
return date;
}
public double getMonthlyInterestRate(){
return getBalance() * ((annualInterestRate/100) / 12);
}
public void withdraw(double withdraw){
this.withdraw = withdraw;
}
public double deposit(){
return balance + deposit;
}
public void deposit (double deposit){
this.deposit = deposit;
}
}
.
public class TestAccount extends AccountArray{
public static void main(String[] args) {
Account account1122 = new Account(1122, 20000, 4.5);
account1122.withdraw(2500);
account1122.deposit(3000);
System.out.println("Your account balance is: \t" + "$"+ account1122.getBalance() +
"\nYour monthly interest rate is: \t" + "$" + account1122.getMonthlyInterestRate() +
"\nYour account was created on: \t" + account1122.getDateCreated());
//Declare account array */
AccountArray[] atm;
//Create Account array */
atm = createAccountArray();
System.out.println(atm[i].getBalance);
}
}
Your program is attempting to create 10 AccountArray instances, not 10 Accounts. Even if that were to work, you're calling a method outside of the context of either a static reference or the object instance - that is never going to work.
What you probably want to do is this:
Account[] atm = new Account[10];
for(int i = 0; i < atm.length; i++){
atm[i] = new Account();
atm[i].setBalance(100);
}
...although ignoring the other constructors in this case seems a bit...wrong to me. I leave this part as an exercise for the reader.

Categories

Resources