Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30
  1. #21
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    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

  2. #22
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Where do you create the account object?
    I assume its from a line something like
    Java Code:
    Account account = new Account();
    (altho I do not like seeing the same word repeated different contexts. I would prefer something more like)

    Java Code:
    Account anAcc = new Account();
    DebitCard dbtCard = new DebitCard(anAcc);
    Have you tried to strip down the code to as little as possible with just these two lines?
    Last edited by fishtoprecords; 09-17-2008 at 12:44 AM. Reason: account => anAcc

  3. #23
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    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.

  4. #24
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by CirKuT View Post
    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;
    Now I'm really confused. You are declaring "account" as a single static AccountRepository. That is *not* an array.

    Java Code:
    DebitCard debitCard = new DebitCard(account);
             debitCard.DebitCard(account);
    I do not understand the second line here at all. What do you think it is doing?
    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.

  5. #25
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    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

  6. #26
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    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() {
    }


    }

  7. #27
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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,
    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];
                       ^
    First things first.
    Also, there was a missing curley brace in the KeyPadDisplay class, no big deal.

  8. #28
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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.
    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;
        }
    }
    Some changes added to the KeyPadDisplay class:
    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.
        }
    and a test class to start working with:
    Java Code:
    package coldbeveragejava;
    
    public class AccountTest {
        public static void main(String[] args) {
            KeyPadDisplay kpd = KeyPadDisplay.getInstance();
    
        }
    }
    Everything compiles and runs enough to get started.



    The quoted exception from earlier:
    Java Code:
    cannot find symbol
    symbol: Constructor DebitCard(coldbeveragejava.AccountRepository)
    location class: coldbeveragejava.DebitCard
    is referring to a type mismatch.
    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:
    Java Code:
    public class DebitCard {
        private Account accountOnDebitCard; 
    
        public DebitCard(Account account){ 
            accountOnDebitCard=account; 
        }
    You are using a DebitCard constructor with an argument of type AccountRepository.
    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);
    The two types must match.

  9. #29
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    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!

  10. #30
    began1261 is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Talking

    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

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 05:01 PM
  2. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM
  3. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 4
    Last Post: 05-23-2008, 02:05 PM
  4. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 1
    Last Post: 05-21-2008, 06:40 AM
  5. Calling method from another class
    By asahli in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •