Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Bank Account GUI

    here is everything I have so far I don't know how to implement these two classes with the array of 10 and get them into the gui fields properly. If anyone can help I would appreciate it I tried asking my teacher for help and he really didn't give me a straight answer he talked me in circles.

    [code=java]class BankAccount
    {
    int accnum;
    static int amount;
    private static double withdraw;
    CPerson person;

    public int getAccnum() {
    return accnum;
    }
    public void setAccnum(int accnum) {
    this.accnum = accnum;
    }
    public static int getAmount() {
    return amount;
    }
    public void setAmount(int amount) {
    this.amount = amount;
    }
    BankAccount(int num,int amt,String nam,int ag)
    {
    accnum=num;
    amount=amt;
    person = new CPerson("",0);
    }
    public void deposit(int amt)
    {
    amount=amount+amt;
    }
    public void withdraw(int amt) throws FundsInsufficientException
    {
    if(amt>amount)
    throw new FundsInsufficientException(amount,amt);
    else
    amount=amount-amt;
    }
    public static double getWithdraw() {
    return withdraw;
    }
    public static void setWithdraw(double withdraw) {
    BankAccount.withdraw = withdraw;
    }
    }


    [code=java]import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.*;
    public class BankGui {
    JTextField text[];
    JButton deposit,withdraw,exit,clear;
    JLabel label[];
    BankGui(){
    text = new JTextField[4];
    label = new JLabel[4];
    for(int i=0;i<4;i++){

    label[0]= new JLabel("Deposit");
    text[0]= new JTextField("0");
    label[1]= new JLabel("Withdraw");
    text[1]= new JTextField("0");
    label[2]= new JLabel("Name");
    text[2]= new JTextField("Chris");
    label[3]= new JLabel("Balance");
    text[3]= new JTextField("0");
    }
    deposit = new JButton("Deposit");
    withdraw = new JButton("Withdraw");
    exit = new JButton("Exit");
    clear = new JButton("Clear");
    JPanel jp = new JPanel();//center
    JPanel jb = new JPanel();//south
    JPanel jd = new JPanel();//west
    JFrame jc= new JFrame("bank");
    jp.setLayout(new GridLayout(4,2));
    for(int i=0;i<3;i++){
    jp.add(label[0]);
    jp.add(text[0]);
    jp.add(label[1]);
    jp.add(text[1]);
    jp.add(label[2]);
    jp.add(text[2]);
    jp.add(label[3]);
    jp.add(text[3]);




    }
    jb.setLayout(new GridLayout(3,1));
    jb.add(deposit);
    jb.add(withdraw);
    jb.add(exit);
    jb.add(clear);

    jc.setLayout(new BorderLayout());
    jc.add(jp, BorderLayout.CENTER);
    jc.add(jb,BorderLayout.SOUTH);

    jc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jc.setSize(300,500);
    jc.setVisible(true);
    exit.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
    System.exit(0);};
    ;});
    clear.addActionListener(new ActionListener() {
    public void actionPerformed( ActionEvent e) {
    for(int i=0;i<4;i++){
    text[0].setText("0");
    text[1].setText("0");
    text[2].setText("Enter name");
    text[3].setText("0");
    }

    }
    });
    withdraw.addActionListener(new ActionListener() {
    public void actionPerformed( ActionEvent e) {

    double wd = Double.parseDouble(text[1].getText());
    double wdamount = BankAccount.getWithdraw() - wd;
    //BankAccount.withdraw(wdamount);
    wd = wdamount;
    text[3].setText("Balance: " + wd);



    }

    });
    }
    public static void main(String[] args) {
    BankGui inc = new BankGui();
    BankAccount users[] = new BankAccount[10];
    users[0]= new BankAccount(56,1000,"Jack",47);
    }
    }



    public class CPerson {
    private static String name;
    private static int age;
    public static String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public static int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public CPerson(String name, int age) {
    super();
    this.name = name;
    this.age = age;
    }
    CPerson aperson[] = new CPerson[10];
    }


    class FundsInsufficientException extends Exception
    {
    int balance;
    int withdraw_amount;
    FundsInsufficientException(int bal,int w_amt)
    {
    balance=bal;
    withdraw_amount=w_amt;
    }
    public String toString()
    {
    return "Your withdraw amount ("+withdraw_amount+")
    is less than the balance ("+balance+"). No withdrawal was recorded.";
    }
    }
    [code]

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,027
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    REALLY REALLY bad code, in any case.

    Code:
    private static double withdraw;
    Se even if you have a million people in your system, each with his/her own BankAccount instance, there is only *ONE* common withdraw number.

    That makes no sense at all.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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