Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

can't compile the TestBanking.java

  Asked By: Tracey    Date: Sep 16    Category: Java    Views: 2228
  

Please help me. I can't compile the
TestBanking.java.

Share: 

 

7 Answers Found

 
Answer #1    Answered By: Willie Gomez     Answered On: Sep 16

**Create the banking directory. Copy the previous Banking project
**files in this package directory.

**Modify the Customer class to handle the accounts association with
**generic multiplicity; just as you did for in exercise #2 in the
**Arrays module. It must include the public methods: addAccount
(Account), getAccount(int), and getNumOfAccounts().

**Complete the TestBanking Program
**This program creates a set of customers and accounts and generates
**a report of customers and their account balances.

**In the TestBanking.java file you will find comment blocks that
**start and end with /*** ... ***/. These comments indicate the
**location in the code that you must supply.

**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".

**Print out the type of account and the balance. Feel free to use the
**currency_format formatter to generate a "currency string" for the
**balance.

**Compile and run this program. You should see the following output.
CUSTOMERS REPORT
================

**Customer: Simms, Jane
Savings Account: current balance is ñ 500.00
Checking Account: current balance is ñ 200.00

**Customer: Bryant, Owen
Checking Account: current balance is ñ 200.00

**Customer: Soley, Tim
Savings Account: current balance is ñ 1,500.00
Checking Account: current balance is ñ 200.00

**Customer: Soley, Maria
Checking Account: current balance is ñ 200.00
Savings Account: current balance is ñ 150.00





* This class creates the program to test the banking classes.
* It creates a set of customers, with a few accounts each,
* and generates a report of current account balances.
*/

import banking.*;
import java.text.NumberFormat;

public class TestBanking {

public static void main(String[] args) {
NumberFormat currency_format = NumberFormat.getCurrencyInstance();
Bank bank = new Bank();
Customer customer;
String account_type = "";


// Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingsAccount(500.00, 0.05));
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.05));
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));

// Generate a report
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================");

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

System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());

for ( int acct_idx = 0; acct_idx < customer.getNumOfAccounts();
acct_idx++ ) {
Account account = customer.getAccount(acct_idx);


// Determine the account type
/*** Step 1:
**** 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".
***/

if (account instanceof SavingsAccount){
account_type = "Savings Account";
}


if (account instanceof CheckingAccount)
account_type = "Checking Account";
}


System.out.println(account_type + ": current balance is " +
account.getBalance());

// Print the current balance of the account
/*** Step 2:
**** Print out the type of account and the balance.
**** Feel free to use the currency_format formatter
**** to generate a "currency string" for the balance.
***/
}
}
}

// Account.java
package banking;

public class Account {

protected double balance;

public Account(double init_balance){
balance = init_balance;
}

public boolean deposit(double amount) {
balance = balance + amount;
return true;
}

public boolean withdraw(double amount) {
boolean result = true;
if ( balance < amount ) {
result = false;
} else {
balance = balance - amount;
}
return result;
}

public double getBalance(){
return balance;
}
}
// CheckingAccount
package banking;

public class CheckingAccount extends Account {
private double overdraftProtection;
public CheckingAccount(double balance) {
super(balance);
overdraftProtection = -1;
}
public CheckingAccount(double balance, double protect) {
super(balance);
overdraftProtection = protect;
}
public boolean withdraw(double amount) {
if ( amount <= balance) {
balance = balance - amount;
return true;
}
else
if (( overdraftProtection != -1) && (amount - balance) <=
overdraftProtection) {
balance = 0;
overdraftProtection = overdraftProtection + (balance -
amount);
return true;
}
else
{
return false;
}
}
}

// SavingsAccount
package banking;

public class SavingsAccount extends Account {


private double interestRate;



public SavingsAccount(double balance, double interest_rate){

super(balance);

}

}

// Customer
package banking;

public class Customer{

private String firstName;
private String lastName;
private Account account;



private int numberOfAccounts;
private Account [] accounts;
public double Accounts;








public Customer(String f, String l) {
firstName = f;
lastName = l;
}

public Customer(){
accounts = new Account[10];
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public void setAccount(Account acct) {
account = acct;
}





public double getAccount() {
return Accounts;
}

public int getNumOfAccounts(){
return numberOfAccounts;
}

public Account addAccount(int i){
return accounts[i];

}


}

 
Answer #2    Answered By: Pravat Jainukul     Answered On: Sep 16


You said you can't compile  TestBanking.java: why not? Do you get an
error message?

FWIW - no one is going to do your work for you, but we will try to help
you at least get it to compile and answer well-defined questions.

 
Answer #3    Answered By: Rocco Anderson     Answered On: Sep 16

I am so sorry for not explaining myself. I just cant compile  the
TestBanking.java here is the error message. I hope you can tell me
why it won't compile.
A:\MOD06\exercise2\TestBanking.java:22: addAccount(int) in
banking.Customer cannot be applied to (banking.SavingsAccount)
customer.addAccount(new SavingsAccount(500.00, 0.05));
^
A:\MOD06\exercise2\TestBanking.java:23: addAccount(int) in
banking.Customer cannot be applied to (banking.CheckingAccount)
customer.addAccount(new CheckingAccount(200.00, 400.00));
^
A:\MOD06\exercise2\TestBanking.java:27: addAccount(int) in
banking.Customer cannot be applied to (banking.CheckingAccount)
customer.addAccount(new CheckingAccount(200.00));
^
A:\MOD06\exercise2\TestBanking.java:31: addAccount(int) in
banking.Customer cannot be applied to (banking.SavingsAccount)
customer.addAccount(new SavingsAccount(1500.00, 0.05));
^
A:\MOD06\exercise2\TestBanking.java:32: addAccount(int) in
banking.Customer cannot be applied to (banking.CheckingAccount)
customer.addAccount(new CheckingAccount(200.00));
^
A:\MOD06\exercise2\TestBanking.java:37: getAccount() in
banking.Customer cannot be applied to (int)
customer.addAccount(bank.getCustomer(2).getAccount(1));
^
A:\MOD06\exercise2\TestBanking.java:38: addAccount(int) in
banking.Customer cannot be applied to (banking.SavingsAccount)
customer.addAccount(new SavingsAccount(150.00, 0.05));
^
A:\MOD06\exercise2\TestBanking.java:53: getAccount() in
banking.Customer cannot be applied to (int)
Account account = customer.getAccount(acct_idx);
^
A:\MOD06\exercise2\TestBanking.java:73: cannot resolve symbol
symbol : variable account
location: class TestBanking
System.out.println(account_type + ": current balance is " +
account.getBalance());
^
9 errors

Tool completed with exit code 1

 
Answer #4    Answered By: Scott Simmons     Answered On: Sep 16

Well, here are some problems I see:

1.) customer.addAccount() takes an int as its arg, but you tried to send
it Account objects.
2.) customer.getAccount() takes no arg and returns a double (I am sure
that is not what you want).
3.) You are missing an opening bracket at these lines:

if (account instanceof CheckingAccount)
account_type = "Checking Account";
}

It should be:

If (account instanceof CheckingAccount){
account_type = "Checking Account";
}


There are other errors as well, but since the compiler hasn’t found them
yet, I will let you see them when they arrive. Part of the hardest part
of programming is debugging, as you are undoubtly now learning. Stick
with it though and it will make sense.

 
Answer #5    Answered By: Raju Srinivas     Answered On: Sep 16

One of the ways to prevent a thousand compile  errors after you enter
your "program" is to incrementally compile. You enter a portion of
your class, then compile it. That way you should make sure all your
references are consistent, you arguments are clean, your returns are
correct....

Part of developing a program in to incrementally develop it.

I define the objects I need and how they are going to interface (what
methods I in each class) first. Then, I get them to compile.

I begin developing the internals of the methods and use a good step
debugger to step through the code, see if the values I THOUGHT I was
getting is what I am ACTUALLY getting. Each method is debugged as I
develop it.

By the time I've finished doing the entire program, I usually end up
with code that AT LEAST plays well with itself. Then, I integrate it
into the overall system and test how it plays with others.

If I have gotten the requirments down correctly, then I usually
release "bug-free" code. It take a little longer in the development
cycle, but when you realize that if you develop "monolithically" over
60% of your time is spend debugging... It's worth it.

Anthony is right... Debugging is the biggest headache in the
development cycle. A good IDE (Integrated Development Environment)
will help  in the debugging. Eclipse ( http://www.eclipse.org ) (a
free one) also has a "scrapbook" where you can write "snippets" of
code and execute them to see if they work BEFORE you put them into
your code...

Now you have my clock... and all you asked was the time.

 
Answer #6    Answered By: Neil Turner     Answered On: Sep 16

Where's the Bank class code? Or is that supplied by your instructor?
I don't like how it's designed. What you need is to create a Customer
within the TestBanking class, then add the customer to the Bank
instance's list of customers. Adding a customer and then immediatley
retrieving them as a positional value within a customers array is
silly and inefficient.

 
Answer #7    Answered By: Katrina Edwards     Answered On: Sep 16

I'll give you some general advice, then some specific advice:

Generally--never write your entire solution set and then try to get
it running. You should first create the most basic class, get it to
compile, then add code incrementally. If you blast out 1000 lines of
code and it won't even compile, you are in big trouble. You haven't
even figured out the logical problems, which usually crop up.

Specific--you need to edit your code in an editor that has syntax
highlighting and auto-indenting. Then if you do something dumb like
forget to use a closing brace, the tool will make it obvious when the
indentation remains.

Also, whenever you create a new block, start with the opening and
closing braces before you put any code within the block, ala:

if() {

}

Then you never forget to close the block and don't get confused as to
in which level you are.

 
Didn't find what you were looking for? Find more on can't compile the TestBanking.java Or get search suggestion and latest updates.




Tagged: