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?