bank.java

来自「用jsp+servlet」· Java 代码 · 共 99 行

JAVA
99
字号
package server;import java.io.*;import java.util.ArrayList;import java.net.*;/** * A bank contains customers with bank accounts. */public class Bank {	/**	 * Constructs a bank with no customers.	 */	public Bank() {		customers = new ArrayList<Customer>();	}	/**	 * Reads the customer numbers and pins and initializes the bank accounts.	 * 	 * @param filename	 *            the name of the customer file	 */	@SuppressWarnings("unchecked")	public void readCustomers(String filename) throws IOException {		URLConnection con = getServletConnection("login");		OutputStream outstream = con.getOutputStream();		ObjectOutputStream oos = new ObjectOutputStream(outstream);		oos.writeObject(null);		oos.flush();		oos.close();		// receive result from servlet		InputStream instr = con.getInputStream();		ObjectInputStream inputFromServlet = new ObjectInputStream(instr);		try {			ArrayList<String> result = (ArrayList<String>) inputFromServlet.readObject();			for(String s :result) {				String[] a = s.split("&");				Customer c = new Customer(Integer.parseInt(a[0]),Integer.parseInt(a[1]));				addCustomer(c);			}		} catch (Exception e) {			e.printStackTrace();		}				inputFromServlet.close();		instr.close();	}	/**	 * Adds a customer to the bank.	 * 	 * @param c	 *            the customer to add	 */	public void addCustomer(Customer c) {		customers.add(c);	}	/**	 * Finds a customer in the bank.	 * 	 * @param aNumber	 *            a customer number	 * @param aPin	 *            a personal identification number	 * @return the matching customer, or null if no customer matches	 */	public Customer findCustomer(int aNumber, int aPin) {		for (int i = 0; i < customers.size(); i++) {			Customer c = (Customer) customers.get(i);			if (c.match(aNumber, aPin))				return c;		}		return null;	}	private URLConnection getServletConnection(String addr)			throws MalformedURLException, IOException {		// Connection zum Servlet 鰂fnen		URL urlServlet = new URL("http://localhost:8080/mid3t/" + addr);		URLConnection con = urlServlet.openConnection();		// konfigurieren		con.setDoInput(true);		con.setDoOutput(true);		con.setUseCaches(false);		con.setRequestProperty("Content-Type",				"application/x-java-serialized-object");		// und zur點kliefern		return con;	}	private ArrayList<Customer> customers;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?