Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

pls help me.. i'm very new in java..

843810Jul 25 2007 — edited Jul 25 2007
there is an error error message telling me that nonstatic method cannot be referenced to a context one... pls help me... i'm new in java and i'm still a student... it's my first time in java... thanks a lot... i really want to learn...

here are my codes...

package com.mybank.domain;

public class SavingsAccount extends Account
{
private double interestRate;

public SavingsAccount(double initBalance, double interestRate)
{
super(initBalance);
this.interestRate = interestRate;
}

public void accumulateInterest()
{
balance = balance + (interestRate * balance);
//bka klngn ng return
}
}



package com.mybank.domain;

public class CheckingAccount extends Account
{
private double overdraftAmount;

public CheckingAccount(double initBalance, double overdraftAmount)
{
super(initBalance);
this.overdraftAmount = overdraftAmount;
}
public CheckingAccount(double initBalance)
{
this(initBalance, 0.0);
}
public boolean withdraw(double amount)
{
boolean result = true;
if (balance < amount)
{
double overdraftNeeded = amount - balance;
if (overdraftAmount < overdraftNeeded)
{
result = false;
}
else
{
balance = 0.0;
overdraftAmount -= overdraftNeeded;
}
}
else
{
balance -= amount;
}
return result;
}
}




package com.mybank.domain;

public class Account
{
protected double balance;
public boolean deposit (double amt)
{
balance = balance + amt;
return true;
}
public boolean withdraw (double amt)
{
boolean result = false;
if (amt <= balance)
{
balance = balance - amt;
result = true;
}
return result;
}
public double getBalance()
{
return balance;
}
protected Account(double initBalance)
{
balance = initBalance;
}
}




package com.mybank.domain;

public class Customer
{
private String firstName;
private String lastName;
private Account[] accounts;
private int numberOfAccounts;

public Customer (String f, String l)
{
firstName = f;
lastName = l;
accounts = new Account[10];
numberOfAccounts = 0;
}

public String getFirstName()
{
return firstName;
}

public String getLastName()
{
return lastName;
}

public Account getAccount(int account_index)
{
return accounts[account_index];
}

public void addAccount(Account acct)
{
int i = numberOfAccounts++;
accounts[i] = acct;
}
public int getNumOfAccount()
{
return numberOfAccounts;
}
}



package com.mybank.report;

import com.mybank.domain.*;

public class CustomerReport
{

private Bank bank;

public CustomerReport()
{

}

public Bank getBank()
{
return bank;
}

public void setBank(Bank bank)
{
this.bank = bank;
}

public void generateReport()
{

// Print report header
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================");

// For each customer...
for ( int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++ )
{
Customer customer = bank.getCustomer(cust_idx);

// Print the customer's name
System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());

// For each account for this customer...
for ( int acct_idx = 0; acct_idx < customer.getNumOfAccount(); acct_idx++ )
{
Account account = customer.getAccount(acct_idx);
String account_type = "";

// Determine the account type
/*** Use the instanceof operator to test what type of account
**** we have and set account_type to an appropriate value, such
**** as "Savings Account" or "Checking Account". ***/
// YOUR CODE HERE
if (account instanceof SavingsAccount)
{
account_type = "Savings Account";
}
else if (account instanceof CheckingAccount)
{
account_type = "Checking Account";
}
else
{
account_type = "Unknown Account Type";
}

// Print the current balance of the account
/*** Print out the type of account and the balance. ***/
// YOUR CODE HERE
System.out.println(" " + account_type + ": current balance is" + account.getBalance());
}
}
}
}




package com.mybank.test;

import com.mybank.domain.*;
import com.mybank.report.*;
import com.mybank.batch.*;

public class TestBatch {

public static void main(String[] args) {
Bank bank = new Bank();
initializeCustomers(bank);

// run the customer report
CustomerReport report = new CustomerReport();
report.setBank(bank);
report.generateReport();

// run savings accumulation batch job
AccumulateSavingsBatch job = new AccumulateSavingsBatch();
job.setBank(bank);
job.doBatch();
System.out.println();
System.out.println("ACCUMULATE SAVINGS BATCH EXECUTED");
System.out.println();

// run the customer report again
report.generateReport();
}

private static void initializeCustomers(Bank bank) {
Customer customer;

// Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingsAccount(500.00, 0.03));
customer.addAccount(new CheckingAccount(200.00, 400.00));

bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.addAccount(new CheckingAccount(200.00));

bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
customer.addAccount(new SavingsAccount(1500.00, 0.075));
customer.addAccount(new CheckingAccount(200.00));

bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
// Maria and Tim have a shared checking account
customer.addAccount(bank.getCustomer(2).getAccount(1));
customer.addAccount(new SavingsAccount(150.00, 0.05));
}
}




package com.mybank.batch;//not sure yan ha

import com.mybank.domain.*;

public class AccumulateSavingsBatch
{

private Bank bank;

public void doBatch()
{
for ( int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++ )
{
Customer customer = bank.getCustomer(cust_idx);
for ( int acct_idx = 0; acct_idx < customer.getNumOfAccount(); acct_idx++)
{
Account account = customer.getAccount(acct_idx);
if (account instanceof SavingsAccount)
{
// SavingsAccount s = accumulateInterest();
// account.accumulateInterest();
// SavingsAccount s = new SavingsAccount();
// s();
// accumulateInterest();
// SavingsAccount aa = new SavingsAccount();
// aa.accumulateInterest();

SavingsAccount.accumulateInterest();

}

}
}
}

public void setBank(Bank bank)
{
this.bank = bank;
}
}


i think the error is within my code in SavingsAccount class and AccumulateSavingsBatch class... please help me.... thanks^_^
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 22 2007
Added on Jul 25 2007
1 comment
258 views