Creating subclasses - java

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.

Related

Getting a private access error in a subclass

I am trying to override the withdraw method so that in the savings account it cannot be overdrawn. When I compile this I get an error telling me that balance has private access in the class BankAccount. I am also required to leave the double balance as private.
import java.util.*;
public class BankAccount {
private int id;
private double balance,
public BankAccount(int id,double balance) {
this.id = id;
this.balance = balance;
}
public void setBalance (double balance){
this.balance = balance;
}
public double getBalance (){
return balance;
}
public void withdraw(double amount) {
balance = balance - amount;
}
}
public class SavingsAccount extends BankAccount {
public SavingsAccount(int id, double balance) {
super(id,balance);
}
public void withdraw(double amount) {
if(balance >= amount) {
super.withdraw(amount);
}
}
}
You have a public getter for balance, you should use that.
Since your member variable is private, compiler is going to complain surely. And that is what it is going.

The method is no applicable to the arguments in Java

I am fairly new to objects and classes in Java, so sorry for the newbie question. In the main method's print statement I get the error:
The method getId(int) in the type Account is not applicable for the arguments ()
I am not quite sure what exactly is happening here, but I am just trying to print user2's id. Thank you in advance.
I also do apologize for trying to get all the code into a single block, but I had some issues, clearly.
package problems;
import java.time.LocalDate;
public class AccountTest {
public static void main(String[] args) {
//User 1 with no input values
Account user1 = new Account();
//User 2 with the specified id and balance values
Account user2 = new Account(1122, 20000);
//Test to see if the getters and setters work
System.out.println("The id is + " + user2.getId());
}
}
//Create the class
class Account {
private int id;
private double balance;
private static double annualInterestRate;
private LocalDate dateCreated;
//Object for when no values are passed
Account() {
id = 0;
balance = 0;
annualInterestRate = 4.5;
System.out.println("There are no parameters set for the object");
}
//Object for when values are passed
Account(int id, int balance) {
this.id = id;
this.balance = balance;
annualInterestRate = 4.5;
dateCreated = LocalDate.now();
}
//Getters
public int getId(int id) {
return this.id;
}
public double getBalance(double balance) {
return this.balance;
}
public double getMonthlyInterest(double annualInterestRate) {
return (annualInterestRate / 100 /12 * balance);
}
//Setters
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//Deposit and withdraw methods
public double withdraw(double balance) {
this.balance = balance - 2500;
return this.balance;
}
public double deposit(double balance) {
this.balance = balance + 3000;
return this.balance;
}
//Get the date for when the account was made
public long getDate(long dateCreated) {
return dateCreated;
}
}
As the name implies, when you do a getter, you get a value, so there is no point in giving it an argument when all it does is return the value of the private variable.
Example:
private String myVar;
public String getMyVar()
{
return this.myVar;
}
public void setMyVar(String theValueIWantMyVar)
{
this.myVar = theValueIWantMyVar;
}
I recommend you read a little to see how object oriented programming works
https://dev.to/codegym_cc/18-best-java-books-for-beginners-in-2019-fme

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);

Categories

Resources