Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java RMI eclipse BANK, help!

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Java RMI eclipse BANK, help!

    It's okey! Hold it whit them answers!! I figured it out, I added new 'account servers' ... ex: AccountServer klskl = new AccountServer, then set name with 'Naming'.

    But feel free to come with help anyway, on other things you see (this version is not updates doh, so don't worry about users)

    If any of you saw this post before, you may notice it's all edited, anyhow.

    I'm working on this banksystem, over RMI, So I have a server, a client an interface, and a class for Accounts, who is used by the interface, client and the server, What i need is when you 'login', if u enter a 'id' number, and an existing account has that number, you login to that account, and get that information (name, balance.. etc).

    Server:
    package bankrmi2;
     
    import java.rmi.Naming;
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.server.UnicastRemoteObject;
    import java.util.ArrayList;
    import java.util.List;
     
    interface Account extends Remote {
        public void deposit(int amount) throws RemoteException;
        public void withdraw(int amount) throws RemoteException;
        public int getBalance() throws RemoteException;
    }
     
     
    public class AccountServer extends UnicastRemoteObject implements Account {
     
        private static final long serialVersionUID = 446895079020690009L;
     
        private int balance;
     
     
        public AccountServer() throws RemoteException {
            super();
        }
     
        public void deposit(int amount) {
            balance += amount;
        }
     
        public void withdraw(int amount) {
            balance -= amount;
        }
     
        public int getBalance() {
            return balance;
        }
        public static void main(String[] args) throws Exception {
            LocateRegistry.createRegistry(1099);
            AccountServer server = new AccountServer();
            Naming.rebind("Bank", server);
            System.out.println("Account RMI server running");
        }
     
    }

    Client:
    package bankrmi2;
     
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.rmi.Naming;
    import java.rmi.RemoteException;
     
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
     
    public class AccountClient extends JPanel { 
     
        private static final long serialVersionUID = 3255779371281665000L;
     
        private Account account;
     
        private JTextField amountField = new JTextField(30);
        private JTextArea balanceField = new JTextArea(4, 20);
        private JPanel buttonPanel = new JPanel();
     
        public AccountClient() {
            setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            setLayout(new BorderLayout(10, 10));
            add(amountField, BorderLayout.NORTH);
            new Operation("Deposit") {
                protected void execute() throws RemoteException {
                    int amount = Integer.parseInt(amountField.getText());
                    account.deposit(amount);
                }
            };
            new Operation("Withdraw") {
                protected void execute() throws RemoteException {
                    int amount = Integer.parseInt(amountField.getText());
                    account.withdraw(amount);
                }
            };
            add(new JScrollPane(balanceField), BorderLayout.CENTER);
            add(buttonPanel, BorderLayout.SOUTH);
        }
     
        private void showBalance() {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    try {
                    	balanceField.setText("Balance: " + account.getBalance());
                    } catch (RemoteException e) {
                    	balanceField.setText(e.toString());
                    }
                }
            });
        }
        private abstract class Operation implements ActionListener {
            public Operation(String label) {
                JButton button = new JButton(label);
                buttonPanel.add(button);
                button.addActionListener(this);
            }
            public void actionPerformed(ActionEvent event) {
                try {
                    execute();
                    showBalance();
                } catch (Exception e) {
                	balanceField.setText(e.toString());
                }
            }
            protected abstract void execute() throws RemoteException;
        }
        public static void main(String[] args) throws Exception {
            AccountClient client = new AccountClient();
            JFrame frame = new JFrame("ATM");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            String serverAddress = JOptionPane.showInputDialog(
                null, "Welcome to the Remote Account ATM\n" +
                "Enter IP Address of the Server:");
            client.account = (Account)Naming.lookup(
                "rmi://" + serverAddress + "/Bank");
            frame.getContentPane().add(client, BorderLayout.CENTER);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            client.showBalance();
        }
    }

    Any thoughs on how to add accounts, and then have an option to 'login' to it?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java RMI eclipse BANK, help!

    I haven't seen many questions about RMI on the forums. When I tried the RMI examples in a textbook years ago I thought it was hard to use and very touchy about putting its parts in the right place to work.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Java RMI eclipse BANK, help!

    Quote Originally Posted by Norm View Post
    I haven't seen many questions about RMI on the forums. When I tried the RMI examples in a textbook years ago I thought it was hard to use and very touchy about putting its parts in the right place to work.
    Yeah, this is what I'm talking about! I can do the printer example, since they show exactly how it's done, but I cant even begin to figure out how I would make a banksystem ..

Similar Threads

  1. java rmi and socket programming
    By sumit kumar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 21st, 2013, 12:28 AM
  2. Java Socket and RMI programming help
    By Yogesh4591 in forum Java Networking
    Replies: 0
    Last Post: November 17th, 2012, 10:57 PM
  3. JAVA RMI question
    By rlk in forum Java Networking
    Replies: 6
    Last Post: March 21st, 2012, 02:22 PM
  4. Help for write Bank application in Java
    By clapton in forum Object Oriented Programming
    Replies: 1
    Last Post: March 1st, 2012, 05:55 AM