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);
Related
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
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;
}
}
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;
}
}
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.
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.