Results 21 to 30 of 30
- 09-16-2008, 11:19 PM #21
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
fishtoprecords,
Sorry I didnt post the error, I was at work when I posted that,
So here is the code I used
DebitCard debitCard = new DebitCard(account);
debitCard.DebitCard(account);
The error I am getting is....
The first line gives me ared sqwiggle under the second DebitCard that says..
cannot finsd symbol
symbol: Constructor DebitCard(coldbeveragejava.AccountRepository)
location class: coldbeveragejava.DebitCard
Why does it keep complaining about AccountRepository when I am calling thr DebitCard class not the AccountRepository class? Is it because I used the account variable to call this method earlier in the code?
The second line of code gets a red sqwiggle under DebitCard and gives this error
cannot find symbol
symbol: method DebitCard (coldbeveragejava.AccountRepository)
location: class coldbeveragejava.DebitCard
Again it complains about the AccountRepository and is looking in the location coldbeveragejava.DebitCard when the class is not name this its simply named DebitCard. Only one method in all 12 classes uses the coldbeveragejava.classname approach and I honestly dont know why because all the classes are within the coldbeveragejava package.
I am very confused on why I cannot call this damn DebitCard (Account account) method
- 09-16-2008, 11:33 PM #22
Where do you create the account object?
I assume its from a line something like
(altho I do not like seeing the same word repeated different contexts. I would prefer something more like)Java Code:Account account = new Account();
Have you tried to strip down the code to as little as possible with just these two lines?Java Code:Account anAcc = new Account(); DebitCard dbtCard = new DebitCard(anAcc);
Last edited by fishtoprecords; 09-16-2008 at 11:44 PM. Reason: account => anAcc
- 09-16-2008, 11:59 PM #23
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
fishtoprecords,
The Account with the capitol A is an array and the account with the lowercase a is created like this
private static AccountRepository account=null;
I then used the account variable to call the AccountRepository method earlier in the code like this
account = AccountRepository.getInstance()
and then used account to call this method
account.getAccount (currentID) //CurrentID is a variable I created to hold the account
A user enters an accountID and it is held in the currentID variable, I then pass this on to another method that checks to ensure the accountID is already in the array, If it is I need to then call the DebitCard(Account account) method and pass the accountID the user entered to the method also
So...this is when I and how I call it
DebitCard debitCard = new DebitCard(account);
debitCard.DebitCard(account);
I this why I am getting errors? Because I already used the account variable to call the getaccount() method? Should I be using the currentID variable instead like this
DebitCard debitCard = new DebitCard(account);
debitCard.DebitCard(currentID);???
I had to turn this assignment in yesterday so whats done is done But I really want to learn how to do this because I have four more java classes to take and I know its just gonna get more complicated.
- 09-17-2008, 12:04 AM #24
Now I'm really confused. You are declaring "account" as a single static AccountRepository. That is *not* an array.
I do not understand the second line here at all. What do you think it is doing?Java Code:DebitCard debitCard = new DebitCard(account); debitCard.DebitCard(account);
Are you really using both lines? The first one should create an instance of a DebitCard object, named by you as "debitCard"
The second line makes absolutely no sense at all to me.
- 09-17-2008, 02:50 AM #25
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
woops,
those two lines should have been something like
Account anAcc = new Account();
DebitCard dbtCard = new DebitCard(anAcc);
as for calling the method with AccountRepository I will explain that when I get home in about 30 minutes,
Ill strip the code and post what is relevant so you can see what is going on
- 09-17-2008, 03:21 AM #26
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
Ok, Here is the code in its original format (As it was given to me) Anywhere you see a comment is where I added code. so..
//Comment
Code I added
I also deleted any methods and such that we re not dealing with to ease your eyes.
Anything else was already there, I wish we would have learned to build this code from scratch instead of piece it together, bad way to learn if you ask me.
Here is the AccountRepository class
package coldbeveragejava;
public class AccountRepository {
private static AccountRepository instance = null;
private static final int MAX_ACCOUNTS=2;
public static AccountRepository getInstance(){
if( instance== null)
instance=new AccountRepository();
return instance;
}
private AccountRepository() {
int accountValue = 100;
int accountID=1;
for(int i=0; i< MAX_ACCOUNTS; i++){
accounts[i] = new Account(accountID++, accountValue+=100);
}
}
private static Account[] accounts = new Account[MAX_ACCOUNTS];
public coldbeveragejava.Account getAcount(int accountID){
//Code I added
// loop through accounts searching for matching account number
for ( Account currentAccountID : accounts )
{
// return current account if match found
if ( currentAccountID.getAccountNumber() == accountID )
return currentAccountID;
//End of code I added
}
return null;
}
public void addAccount(int accountID, int cents){
//add an Account object to the accounts array
}
public Account[] getAccounts() {
return accounts;
}
}//end
The KeyPadDisplay class looks like this
package coldbeveragejava;
import java.io.*;
import java.util.Scanner;
/**
*
* @author dan
*/
public class KeyPadDisplay {
private static KeyPadDisplay instance;
//I added this because I cannot seem to call the AccountRepository.getAcount method without doing this first
private static AccountRepository accountCheck;
int price;
//End of code I added
public static KeyPadDisplay getInstance(){
if(instance==null)
instance = new KeyPadDisplay();
return instance;
private KeyPadDisplay() {
//begin while loop for getting input here to drive the program
boolean done = false;
while(done==false){
System.out.println("Enter 'd' to insert debit card / accountID: ");
System.out.println("Enter 'q' to exit menu: ");
int theChar = 0;
try{
theChar = System.in.read();
}catch(java.io.IOException io){
io.printStackTrace();
System.exit(1);
}
switch(theChar){
case 'q':
done = true;
break;
case 'd':
askUserToInsertDebitCard();
break;
}//switch
}//while
}//KeyPadDisplay()
public void askUserToInsertDebitCard() {
//Creates new Scanner object for user input
Scanner inputAccountID = new Scanner(System.in);
int currentAccountID; //will hold user input
displayMessage("Please insert debit card - Enter Account ID" +
"(e.g.; 1 or 2)");
//sets the currentAccountID local variable to user input
currentAccountID = inputAccountID.nextInt();
//Code I added - I used the private static accountCheck
variable I created earlier to call the getAcount method.
I cannot figure out any other way to call it.
accountCheck = AccountRepository.getInstance();
accountCheck.getAcount(currentAccountID);
//End of code I added
//Once the getAcount method returns the accountID the user
//inputed I need to then use this accountID and call DebitCard(Account
//account) with it. Where an I going wrong?
}
public void inserDebitCard(int accountID) {
}
public void displayMessage(String msg) {
System.out.println(msg);
}
public int getPrice() {
return price;
}
public void setPrice(int val) {
this.price = val;
}
public void unLoadBeverage() {
}
}
- 09-17-2008, 07:54 AM #27
Okay. Copying, pasting and compiling your code from your last post and adding the DebitCard class from the first post I get 11 compile errors all concerned/dealing with a missing class Account, eg,
First things first.Java Code:C:\jexp\coldbeveragejava>javac *.java AccountRepository.java:22: cannot find symbol symbol : class Account location: class coldbeveragejava.AccountRepository private static Account[] accounts = new Account[MAX_ACCOUNTS]; ^
Also, there was a missing curley brace in the KeyPadDisplay class, no big deal.
- 09-17-2008, 08:39 AM #28
Looking at the methods called on the Account class in KeyPadDisplay I made up a stub for the Account class to go in your coldbeveragejava package, just to get the ball rolling.
Some changes added to the KeyPadDisplay class:Java Code:package coldbeveragejava; public class Account { int accountID; int accountValue; public Account(int accountID, int accountValue) { this.accountID = accountID; this.accountValue = accountValue; } public int getAccountNumber() { return accountID; } }
and a test class to start working with:Java Code:... public void askUserToInsertDebitCard() { ... //Code I added - I used the private static accountCheck //variable I created earlier to call the getAcount method. //So far, so good... // Here we get the single instance of AccountRepository. accountCheck = AccountRepository.getInstance(); // With it and the entered inputAccountID we fetch the // matching Account element from the accounts array // in AccountRepository. // We need to save a reference to this fetched account // so we can use it to create a new instance of DebitCard. Account account = accountCheck.getAcount(currentAccountID); //End of code I added //Once the getAcount method returns the accountID the user //inputed I need to then use this accountID and call // Again I apologize for being sloppy in my first post. // It seems to be a persistent problem in this next line. // DebitCard(Account account) with it. Where an I going wrong? // Create a new instance of DebitCard and save a reference // to it in a local variable. DebitCard debitCard = new DebitCard(account); // Now you can use the reference "debitCard" to access // methods in the DebitCard class. }
Everything compiles and runs enough to get started.Java Code:package coldbeveragejava; public class AccountTest { public static void main(String[] args) { KeyPadDisplay kpd = KeyPadDisplay.getInstance(); } }
The quoted exception from earlier:
is referring to a type mismatch.Java Code:cannot find symbol symbol: Constructor DebitCard(coldbeveragejava.AccountRepository) location class: coldbeveragejava.DebitCard
The compiler is saying it cannot find a DebitCard constructor that takes an argument of type AccountRepository.
DebitCard has one constructor and it takes an argument of type Account:
You are using a DebitCard constructor with an argument of type AccountRepository.Java Code:public class DebitCard { private Account accountOnDebitCard; public DebitCard(Account account){ accountOnDebitCard=account; }
The two types must match.Java Code:private static AccountRepository account=null; public void askUserToInsertDebitCard() { account = AccountRepository.getInstance(); // this account.getAcount(currentAccountID); // Here you are calling a DebitCard constructor with an argument // of type AccountRepository. But the only constructor in // DebitCard takes an argument of type Account. Type mismatch. DebitCard debitCard = new DebitCard(account); // As fishtoprecords suggested, this next line is to be omitted. // debitCard.DebitCard(account);
- 09-17-2008, 07:06 PM #29
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
ok I think this all makes sense now. You are getting 11 errors cause I deleted classes and methods to keep the post small. I will try this when I get off work.
Thank you all for the help!
- 09-25-2008, 07:55 PM #30
Member
- Join Date
- Sep 2008
- Posts
- 1
- Rep Power
- 0
Thank you for allowing me to join Java Forums. I'm a newbie to Java which is part of my school curriculum for a few more terms. The introductory class was somewhat helpful in understanding object oriented programming but my last assignment was totally confusing.
I will be seeking advice and knowledge about Java in the near future. Java Forums seems like a great network in exchanging ideas and concepts. I look forward to reading the blogs in the different forums and threats. Thanks again.
BE
Similar Threads
-
Calling a method in another class
By uncopywritable in forum New To JavaReplies: 9Last Post: 10-22-2012, 04:01 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
calling a public void method from a class button
By supa_kali_frajilistik in forum AWT / SwingReplies: 4Last Post: 05-23-2008, 01:05 PM -
calling a public void method from a class button
By supa_kali_frajilistik in forum AWT / SwingReplies: 1Last Post: 05-21-2008, 05:40 AM -
Calling method from another class
By asahli in forum New To JavaReplies: 1Last Post: 12-15-2007, 06:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks