5 Replies - 471 Views - Last Post: 09 August 2010 - 05:03 PM Rate Topic: -----

#1 tor.ment0r  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 10
  • Joined: 07-August 10

java casting problem

Posted 07 August 2010 - 03:31 AM

I am having trouble understanding an example our instructor gave us, i am going to post only the main java class and will comment on that particular line of code.
The problem i am having is that i want to modify that line of code ( CASE 2 , LINE 49) so that i can understand it better, he wrote that single line of code because he's a guru. so i was wondering if i could break up that single line of code into something simpler to understand.
The whole program is 6 classes, if you need me to upload the other classes let me know, i didn't post it because it would lead to a lot of scrolling
Here is the code

import javax.swing.JOptionPane;

import java.util.ArrayList;

import javax.swing.JTextArea;
import javax.swing.JScrollPane;

class Client
{

    public static void main(String[] arg)
    {
        boolean done = false;

        final String menu;
        menu ="1. New Account\n2. Make Deposit\n3. Make Withdrawal\n4. " + 
              "Close Account\n5. Show Active Accounts\n6. Show Closed Accounts\n7. Exit";

        Database db = new Database();
        Database close = new Database();


        while (!done)
        {
            int option = GetData.getInt(menu);

            switch (option)
            {
            case 1: // NEW ACCOUNT

                String last = GetData.getWord("Enter last name:");
                String first = GetData.getWord("Enter first name:");
                String street = GetData.getWord("Enter street address:");
                String city = GetData.getWord("Enter city:");
                String state = GetData.getWord("Enter state:");
                String z = GetData.getWord("Enter zip code:");
                String acctNum = GetData.getWord("Enter an account number:");

                Name n = new Name(last, first);
                Address addr = new Address(street, city, state, z);
                Customer cust = new Customer(n, addr, acctNum);
                Bank b = new Bank(cust, acctNum);
                db.add(B)/>;
                break;

            case 2: // MAKE DEPOSIT
                acctNum = GetData.getWord("Enter an account number:");
                double amountDeposit = GetData.getDouble("Enter the amount:");
                ((Bank)(db.getList().get(db.getIndex(acctNum)))).deposit(amountDeposit); // THIS LINE!!
                // that line of code looks nice but is too advanced to a beginner like me.
                // i would like to break this up into something like: 
                // db.getIndex(acctNum)
                // then storing this into a variable but the problem is that deposit is a void method
                // and that is the problem , i dont know how would i link that account number with the 
                // void deposit method.
                // ON CASE 3 it is the same case
            
                //deposit(amountDeposit);
                break;
            case 3: // MAKE WITHDRAWAL
                acctNum = GetData.getWord("Enter an account number:");
                double amountWithdrawl = GetData.getDouble("Enter the amount:");
                ((Bank)(db.getList().get(db.getIndex(acctNum)))).withdraw(amountWithdrawl);
                break;

            case 4: // CLOSE ACCOUNT
                if (!db.empty())
                {
                    acctNum = GetData.getWord("Enter account number:");
                    db.search(acctNum);

                    if (!db.inList(acctNum))
                    {
                        acctNum = "Account number  " + acctNum;
                        display("Account number not valid", "Message",
                                JOptionPane.ERROR_MESSAGE);
                    } else
                    {

                        Bank removed = (Bank)db.remove(acctNum);
                        close.add(removed);
                    }
                } else
                {
                    display("there exists no accounts", "Message",
                            JOptionPane.ERROR_MESSAGE);
                }
                break;

            case 5: // SHOW ACTIVE ACCOUNTS
                ArrayList list = db.getList();
                String str = "Active accounts:\n" +
                    report(list);

                JTextArea text = new JTextArea(str, 10, 30);
                JScrollPane pane = new JScrollPane(text);
                display(pane, "Report", JOptionPane.INFORMATION_MESSAGE);
                break;

            case 6: // SHOW CLOSED ACCOUNTS
                ArrayList listd = close.getList();
                String strd = "Inactive accounts:\n" +
                    report(listd);

                JTextArea textd = new JTextArea(strd, 10, 30);
                JScrollPane paned = new JScrollPane(textd);
                display(paned, "Report", JOptionPane.INFORMATION_MESSAGE);
                break;


            case 7: //EXIT
                display("Press 'OK' to quit", "Message",
                        JOptionPane.WARNING_MESSAGE);
                done = true;
                break;

            default:
                display("Wrong option.", "Message", JOptionPane.ERROR_MESSAGE);
                break;
            }
        }
    }

    static String report(ArrayList list)
    {
        int length = list.size();
        String str = "";

        for (int i = 0; i < length; i++)
        {
            Bank b = (Bank)list.get(i);
            Customer s = b.cust;
            str =
str + s.getName().lastname + ", " + s.getName().firstname + " " +
  s.getAddress().street + "  " + s.getAddress().city + " " +
  s.getAddress().state + " " + s.getAddress().zip + " " + s.getacctNum() +
  "\n";
        }
        return str;
    }

    static void display(String info, String heading, int type)
    {
        JOptionPane.showMessageDialog(null, info, heading, type);

    }

    static void display(JScrollPane pane, String heading, int type)
    {
        JOptionPane.showMessageDialog(null, pane, heading, type);
    }
}




This post has been edited by tor.ment0r: 07 August 2010 - 03:33 AM


Is This A Good Question/Topic? 0
  • +

Replies To: java casting problem

#2 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: java casting problem

Posted 07 August 2010 - 03:55 AM

((Bank)(db.getList().get(db.getIndex(acctNum)))).withdraw(amountWithdrawl);


I think you can break it up like this.

int index = db.getIndex(acctNum);
Database something = db.getList().get(index);
//Cast it to Bank and invoke withdraw() method
(Bank)something.withdraw(amountWithdrawl);


Was This Post Helpful? 1
  • +
  • -

#3 Mercurial  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: java casting problem

Posted 07 August 2010 - 04:02 AM

((Bank)(db.getList().get(db.getIndex(acctNum)))).deposit(amountDeposit); // THIS LINE!!

You are basically casting "db.getL..." to Bank. If you want to break it down to be more understandable do:
int index= db.getIndex(acctNum); /* if it indeed is an integer; if not, change the var type */
Bank bank= (Bank) db.getList().get(index);
bank.deposit(amountDeposit);


Was This Post Helpful? 1
  • +
  • -

#4 tor.ment0r  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 10
  • Joined: 07-August 10

Re: java casting problem

Posted 07 August 2010 - 04:34 AM

View PostMercurial, on 07 August 2010 - 03:02 AM, said:

((Bank)(db.getList().get(db.getIndex(acctNum)))).deposit(amountDeposit); // THIS LINE!!

You are basically casting "db.getL..." to Bank. If you want to break it down to be more understandable do:
int index= db.getIndex(acctNum); /* if it indeed is an integer; if not, change the var type */
Bank bank= (Bank) db.getList().get(index);
bank.deposit(amountDeposit);



Thanks a lot man, that was exactly what i was looking for.
:)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java casting problem

Posted 08 August 2010 - 08:00 PM

View PostCuzzie, on 07 August 2010 - 04:55 AM, said:

((Bank)(db.getList().get(db.getIndex(acctNum)))).withdraw(amountWithdrawl);


I think you can break it up like this.

int index = db.getIndex(acctNum);
Database something = db.getList().get(index);
//Cast it to Bank and invoke withdraw() method
(Bank)something.withdraw(amountWithdrawl);


Cuzzie is/are right :)
Such a statemnt is impossibl;e to debug
better to split it in many statement... worst case you can put a System.out.println() at each step
Was This Post Helpful? 2
  • +
  • -

#6 tor.ment0r  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 10
  • Joined: 07-August 10

Re: java casting problem

Posted 09 August 2010 - 05:03 PM

View Postpbl, on 08 August 2010 - 07:00 PM, said:

View PostCuzzie, on 07 August 2010 - 04:55 AM, said:

((Bank)(db.getList().get(db.getIndex(acctNum)))).withdraw(amountWithdrawl);


I think you can break it up like this.

int index = db.getIndex(acctNum);
Database something = db.getList().get(index);
//Cast it to Bank and invoke withdraw() method
(Bank)something.withdraw(amountWithdrawl);


Cuzzie is/are right :)
Such a statemnt is impossibl;e to debug
better to split it in many statement... worst case you can put a System.out.println() at each step


thanks for letting me know that
:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1