it is showing me "cannot find symbol method printIn(java.lang.string)
please what is the error, i have used 3 different IDES.
whats wrong with the System.out.printIn()
public class Account
{
private String owner;
private int balance;
public Account (String name)
{
owner = name;
balance = 0;
}
public void deposit(int anAmount)
{
balance += anAmount;
}
public int withdraw(int anAmount)
{
int amountWithdrawn;
if (anAmount <= balance )
amountWithdrawn = anAmount;
else
amountWithdrawn = balance;
balance -= amountWithdrawn;
return amountWithdrawn;
}
public void print()
{
System.out.printIn("Owner =" + owner +" Balance=" + balance);
}
}
That is not printIn(). You typed letter I for Ikea
That is println() you had to use letter l for lima
Related
I'm trying to use a setter/getter class in multiple classes to modify a single variable. I understand that I have to use the same object in order for this to work, but I'm unsure on how to make a single object accessible to multiple classes.
public class accountbalance {
private double balance;
//getter
public double getBalance() {
return balance;
}
//Setter
public void setBalance(double newBalance) {
this.balance = newBalance;
}
}
This is where I'm trying to use it first:
public class CreateAccount {
String name;
double social;
int pin;
public CreateAccount()
{
name = "null";
social = 0;
pin = 0;
}
public CreateAccount(String enteredName, double enteredSocial,int enteredPin, double value)
{
name = enteredName;
social = enteredSocial;
pin = enteredPin;
double accountnum = 87495;
accountbalance a1 = new accountbalance();
a1.setBalance(value);
System.out.println(name);
System.out.println(social);
System.out.println("Your new PIN is " + pin);
System.out.println("Your new account balance is " + (a1.getBalance()));
}
}
And then I'm trying to use it again here:
public class deposit {
double enteredAmt;
double amt;
public void deposit() {
System.out.println("Enter an amount to desposit: ");
Scanner in = new Scanner(System.in);
enteredAmt = in.nextDouble();
accountbalance ab1 = new accountbalance();
System.out.println("current balence: " + ab1.getBalance());
amt = ab1.getBalance() + enteredAmt;
ab1.setBalance(amt);
System.out.println("Your new balance is " + (ab1.getBalance()));
}
}
I believe what you're trying to do is use the Command Design Pattern.
If you changed your classes to look more like this, things might work a tad bit better.
public class Account {
// Please do not use SS for an identifier.
private String identifier;
private String name;
// Money needs to be stored in it's lowest common denominator.
// Which in the united states, is pennies.
private long balance;
public Account(String identifier, String name) {
this.identifier = identifier;
this.name = name;
this.balance = 0L;
}
public String getIdentifier() {
return this.identifier;
}
public String getName() {
return this.name;
}
public long getBalance() {
return balance;
}
public void credit(long amount) {
balance =+ amount;
}
public void debit(long amount) {
balance =- amount;
}
}
Then here would be your CreateAccountCommand. It is a bit anemic, but that is ok.
public class CreateAccountCommand {
private String identifier;
private String name;
public CreateAccount(String identifier, String name) {
// Identifier sould actually be generated by something else.
this.identifier = identifier;
name = name;
}
public Account execute() {
return new Account(identifier, name);
}
}
Here is your DepositCommand:
public class DepositCommand {
private Account account;
private long amount;
public DepositCommand(Account account, long amount) {
this.account = account;
this.amount = amount;
}
public void execute() {
this.account.credit(amount);
}
}
WithdrawCommand:
public class WithdrawCommand {
private Account account;
private long amount;
public DepositCommand(Account account, long amount) {
this.account = account;
this.amount = amount;
}
public void execute() {
this.account.debit(amount);
}
}
Then run everything:
public class Run {
public static void main(String... args) {
CreateAccountCommand createAccountCommand = new CreateAccountCommand("12345678", "First_Name Last_Name");
Account account = createAccountCommand.execute();
System.out.println(account.getIdentifier());
System.out.println(account.getName());
System.out.println("Your new account balance in pennies: " + account.getBalance());
// Deposit $100.00
DepositCommand depositCommand = new DepositCommand(account, 10000);
depositCommand.execute();
System.out.println(account.getIdentifier());
System.out.println(account.getName());
System.out.println("Your new account balance in pennies: " + account.getBalance());
// Withdraw $75.00
WithdrawCommand withdrawCommand = new WithdrawCommand(account, 7500);
withdrawCommand.execute();
System.out.println(account.getIdentifier());
System.out.println(account.getName());
System.out.println("Your new account balance in pennies: " + account.getBalance());
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
My withdraw method doesnt work, maybe someone see things I dont see?
This is part of my abstract class Account and abstract method withdraw
import java.util.ArrayList;
public abstract class Account {
private String accountType;
private static double balance = 0;
protected int accountId;
private static int accountNumberCounter = 1000;
private ArrayList<Account> accounts;
public Account(String acType, int acNumber){
accountType = acType;
accountNumberCounter ++;
accountId = accountNumberCounter;
}
public Account() {
accountNumberCounter++;
accountId = accountNumberCounter;
}
public abstract boolean withdraw(double value);
This is SavingsAccount class and its method withdraw()
public class SavingsAccount extends Account {
private static double balance = 0;
private static final double RATE = 1.0;
private static String accountType = "Savings Account";
private int accountId;
public SavingsAccount(){
super();
}
public double getBalance(){
return balance;
}
public void deposit(double amount){
balance = balance + amount;
}
public boolean withdraw(double amount){
if (balance<= amount){
System.out.println("You have only" + amount + "left on your account.");
return false;
}
else{
balance -= amount;
System.out.println("You took:" + amount);
return true;
}
}
Method from BankLogic as well, I think this part should be correct:
public boolean withdraw(String pNo, int accountId, double amount){
for(Customer pers: customerList) {
if (pers.getPersonalNumber().equals(pNo)) {
for (Account account : pers.getAllAccounts()) {
if (account.getAccountNumber() == accountId) {
account.withdraw(amount);
}
}
}
}
return false;
}
Well, I am not sure what you mean by it's not working, but I see two problems.
Balance shouldn't be static. It should be a class variable that has its own value for each object created.
In your withdraw-method you are saying "You have only" + amount + "left on your account.", while it should be balance instead of amount.
Hope I could help.
You never return the result of the withdraw method.
Change the bank code to return the value from the account's withdraw method and it should be fine (as you already fix the static issue)
public boolean withdraw(String pNo, int accountId, double amount){
for(Customer pers: customerList) {
if (pers.getPersonalNumber().equals(pNo)) {
for (Account account : pers.getAllAccounts()) {
if (account.getAccountNumber() == accountId) {
return account.withdraw(amount); // return the account withdrawing value
}
}
}
}
return false; // false as no account found if this line is reached
}
The problem is:
private boolean testingWithdraw(String pNr, int accountId, double amount) {
System.out.println("withdraw(" + pNr + "," + accountId + "," + amount + ")");
return bank.withdraw(pNr, accountId, amount);
}
That's because inside bank withdraw method, you return false, even if it is success.
public boolean withdraw(String pNo, int accountId, double amount){
for(Customer pers: customerList) {
if (pers.getPersonalNumber().equals(pNo)) {
for (Account account : pers.getAllAccounts()) {
if (account.getAccountNumber() == accountId) {
account.withdraw(amount);
}
}
}
}
return false; //this should be true. this denotes the whole process
}
If you want to maintain the status of updation of each accouunt, then you may use a map of (String,boolean) to maintaion id->updation status
I'm trying to get the interest and the total balance but I don't understand whats wrong in my code
public class Account {
private double bal; //The current balance
private int accnum; //The account number
public String owner;
public int balance;
public Account() {
}
public Account(int a)
{
bal=0.0;
accnum=a;
}
public void deposit(double sum)
{
if (sum>0)
bal+=sum;
else
System.err.println("Account.deposit(...): "
+"cannot deposit negative amount.");
}
public void withdraw(double sum)
{
if (sum>0)
bal-=sum;
else
System.err.println("Account.withdraw(...): "
+"cannot withdraw negative amount.");
}
public double getBalance()
{
return bal;
}
public double getAccountNumber()
{
return accnum;
}
public String toString()
{
return "Acc: " + accnum + ": " + "Balance: " + bal;
}
public final void print()
{
//Don't override this,
//override the toString method
System.out.println( toString() );
}
}
Class SavingsAccount
public class SavingsAccount extends Account{
private double monthlyInterestRate;
public SavingsAccount( int a, double mI)
{
super(a);
monthlyInterestRate = mI;
}
public double getInterest()
{
return monthlyInterestRate*super.getBalance();
}
public double totalBalance(){
return super.getBalance() + getInterest();
}
public String toString()
{
return "Interest: " + getInterest() + "\n" +
"Total balance: " + totalBalance();
}
}
Class CurrentAccount
public class CurrentAccount extends Account {
private double overdraftLimit;
public CurrentAccount() {}
public CurrentAccount(int a, double overdraftLimit)
{
super(a);
setOverdraftLimit(overdraftLimit);
}
public void setOverdraftLimit(double overdrafLimit)
{
this.overdraftLimit = overdrafLimit ;
}
public double getOverdraftLimit()
{
return overdraftLimit;
}
public String toString()
{
return "Overdraft limit: " + overdraftLimit;
}
}
Class AccountMain
public class AccountMain {
public static void main(String[] args) {
Account ac = new Account(100555);
ac.deposit(2000);
SavingsAccount sa = new SavingsAccount(100555, 0.2);
CurrentAccount ca = new CurrentAccount(100555, 800);
System.out.println(ac.toString());
System.out.println(sa.toString());
System.out.println(ca.toString());
The output is
Acc: 100555: Balance: 2000.0
Interest: 0.0
Total balance: 0.0
Overdraft limit: 800.0
In your code
public SavingsAccount( int a, double mI) {
super(a);
monthlyInterestRate = mI; }
You are calling
super(a);
in super that is account you have
public Account(int a) {
bal=0.0;
accnum=a; }
So your balance is 0 hence public double getInterest() return 0 because
monthlyInterestRate*super.getBalance();
equal to monthlyInterestRate * 0
I'm trying to apply a method that will check if the account has went over the OVERDRAFT_LIMIT. I have tried several things but haven't succeeded.
This was something I did but did not know how to apply it:
public void testForOverdraft(double balancE)
{
if(balancE <= 0 && balancE >= OVERDRAFT_LIMIT)
{
withdraw(10);
System.out.println("The account is overdrawn.");
}
else if(balancE <= 0 && balancE <= OVERDRAFT_LIMIT)
{
withdraw(20);
System.out.println("The account is locked until a deposit is made to bring the account up to a positive value.");
}
}
Account:
import java.util.Date;
public class Account
{
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
private double monthlyInterestRate;
public Account()
{
id = 0;
balance = 0;
annualInterestRate = 0;
}
public Account(int iD, double balancE)
{
id = iD;
balance = balancE;
}
public void setID(int iD)
{
id = iD;
}
public int getID()
{
return(id);
}
public void setBalance(double balancE)
{
balance = balancE;
}
public double getBalance()
{
return(balance);
}
public void setAnnualInterestRate(double AIR)
{
annualInterestRate = AIR;
}
public double getAnnualInterestRate()
{
return(annualInterestRate);
}
public void setDateCreated(Date dateCreated)
{
this.dateCreated = dateCreated;
}
public double getMonthlyInterestRate()
{
return((annualInterestRate / 100) / 12);
}
public double getMonthlyInterest()
{
return(balance * monthlyInterestRate);
}
public void withdraw(double ammount)
{
balance = balance - ammount;
setBalance(balance);
}
public void deposit(double ammount) {
balance = balance + ammount;
setBalance(balance);
}
}
CheckingAccount:
public class CheckingAccount extends Account
{
final static double OVERDRAFT_LIMIT = -50.00;
private double annualInterest;
public CheckingAccount()
{
super();
}
public CheckingAccount(int iD, double balancE)
{
super(iD, balancE);
}
public double getAnnualInterest()
{
return((getBalance() * getAnnualInterestRate()) / 100);
}
}
Test:
public class Test extends CheckingAccount
{
public static void main(String [] args)
{
CheckingAccount a1 = new CheckingAccount(1122, 15.00);
a1.withdraw(5.00);
a1.deposit(00.00);
a1.setAnnualInterestRate(4.5);
Date dat = new Date();
System.out.println("Balance: " +
"\nMonthly Interest: " + a1.getMonthlyInterest() +
"\nDate Created: " + dat);
}
}
Check the new balance before assinging it in the withdraw method
The withdraw method will return true if it was success full
public boolean withdraw(double ammount)
{
boolean success ;
double aux ;
aux = balance - ammount ;
if(aux < OVERDRAFT_LIMIT)
{
success = false ;
}
else
{
setBalance(aux);
success = true ;
}
return success ;
}
Then to use it just go like this
if(!withdraw(60))
{
System.out.println("The account is locked until a deposit is made to bring the account up to a positive value.");
}
So I have an Account class, and a TestAccount class. In the TestAccount class I need to call my getAverageBalance method to find the average balance of all the accounts I have created. Here is the code for my average balance method:
public static double getAverageBalance()
{
if (numberOfAccounts > 0)
return totalValueOfAccounts / numberOfAccounts;
else
return 0;
}
The problem is, it is always returning false on the if statement and I don't know why. Here is the rest of my account class code
public class Account
{
private int id;
private double balance;
private static double annualInterestRate;
private java.util.Date dateCreated;
private static int numberOfAccounts;
private static double totalValueOfAccounts;
public Account()
{
dateCreated = new java.util.Date();
}
public Account(int newId, double newBalance, double newAnnualInterestRate)
{
this.id = newId;
this.balance = newBalance;
dateCreated = new java.util.Date();
this.annualInterestRate = newAnnualInterestRate;
}
public int getId()
{
return this.id;
}
public double getBalance()
{
return balance;
}
public double getAnnualInterestRate()
{
return annualInterestRate;
}
public static double getNumberOfAccounts()
{
return numberOfAccounts;
}
public static double getTotalValueOfAccounts()
{
return totalValueOfAccounts;
}
public void setId(int newId)
{
this.id = newId;
}
public void setAnnualInterestRate(int newAnnualInterestRate)
{
this.annualInterestRate = newAnnualInterestRate;
}
public void setBalance(double newBalance)
{
this.balance = newBalance;
}
public static void setAnnualInterestRate(double newAnnualInterestRate)
{
annualInterestRate = newAnnualInterestRate;
}
public double getMonthlyInterest()
{
return this.balance * (annualInterestRate / 1200);
}
public java.util.Date getDateCreated()
{
return this.dateCreated;
}
public void withdraw(double amount)
{
this.balance -= amount;
}
public void deposit(double amount)
{
this.balance += amount;
}
public void awardMonthlyInterestRate()
{
this.balance += getMonthlyInterest();
}
public void closeAccount()
{
System.out.println("Closing account " + id);
this.numberOfAccounts -= 1;
this.totalValueOfAccounts -= balance;
}
Here is the testaccount code:
public class TestAccount
{
public static void printAccount(Account acct)
{
System.out.printf("%5d $%9.2f %5.2f%% %29s\n\n", acct.getId(), acct.getBalance(), acct.getAnnualInterestRate(), acct.getDateCreated());
}
public static void main(String[] args)
{
System.out.println("The average account balance is: " + Account.getAverageBalance());
System.out.println();
Account a = new Account();
System.out.println("Default account: ");
printAccount(a);
a.setId(1122);
a.setBalance(20000);
a.setAnnualInterestRate(4.5);
System.out.println("Modified account: ");
printAccount(a);
a.withdraw(2500);
a.deposit(3000);
System.out.println("After withdraw and deposit: ");
printAccount(a);
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
System.out.println("After 6 months of interest: ");
printAccount(a);
System.out.println("The average account balance is: " + Account.getAverageBalance());
a.closeAccount();
System.out.println();
Account[] accts = new Account[5];
for (int i = 0; i < accts.length; i++)
{
accts[i] = new Account(i+1, (double) Math.floor(Math.random()*100000), 3.0 );
}
System.out.println("Array of five accounts with random balances:");
for (int i = 0; i < accts.length; i++)
{
printAccount(accts[i]);
}
System.out.println("The average account balance is: " + Account.getAverageBalance());
System.out.println();
System.out.println("Array after awarding 6 months of interest: ");
for (int i = 0; i < accts.length; i++)
{
Account b = accts[i];
for (int j = 0; j < 6; j++)
{
b.awardMonthlyInterestRate();
}
printAccount(accts[i]);
}
System.out.println("The average account balance is: " + Account.getAverageBalance());
}
}
I think perhaps you are missing a line inside your constructor:
public Account() {
dateCreated = new java.util.Date();
numberOfAccounts++;
}
If you never increment numberOfAccounts, I am not sure how you expect to ever get a value above 0. This is particularly unfortunate because that variable is private, so only a method of Account even has a chance of setting it.
You need to add the same line to your second constructor, but you can simplify by calling the first constructor. You also need to add to your totalValueOfAccounts.
public Account(int newId, double newBalance, double newAnnualInterestRate)
{
this(); // call the first constructor
this.id = newId;
this.balance = newBalance;
this.annualInterestRate = newAnnualInterestRate;
totalValueOfAccounts += newBalance;
}
You also need to update the variable totalValueOfAccounts inside every method that modifies an individual balance, such as the following:
public void setBalance(double newBalance)
{
totalValueOfAccounts -= this.balance;
totalValueOfAccounts += newBalance;
this.balance = newBalance;
}