⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientbillingproc.java

📁 挺好的JAVA例子,可用MYSQL,ACCESS,ORACLE做后台,例子中包括联结及查询等涵数
💻 JAVA
字号:
import java.sql.SQLException;
import java.util.*;

public class ClientBillingProc {

	class IDComparator implements Comparator {
		//A comparator for Clients based on clientID

		public int compare(Object arg0, Object arg1) {
			Client one = (Client) arg0;
			Client two = (Client) arg1;
			return one.clientID - two.clientID;
		}
	}

	class NameComparator implements Comparator {
		//A comparator for Clients based on lastname, firstname

		public int compare(Object arg0, Object arg1) {
			Client one = (Client) arg0;
			Client two = (Client) arg1;
			String o = one.lastName + " " + one.firstName;
			String t = two.lastName + " " + two.firstName;
			int ret = o.compareToIgnoreCase(t);

			if (ret != 0)
				return ret;
			else //Settle ties with lower clientID.
				return one.clientID - two.clientID;
		}
	}

	//Possible modes for viewing the list of clients.
	public static final int VIEW_BY_ID = 0, VIEW_BY_NAME = 1;
	int activeClient = -1;

	//List of ALL clients, and index of currently active client.
	private Client[] clientsList;

	//The current client list viewing mode.
	private int curMode = -1;

	//Our database object.
	private ClientBillingDB database;

	//Information for maintaining an "external" filter on the client list.
	private ArrayList filter;
	private int lastFilterMode;
	private String lastFilterString;

	//List of transactions for the currently active client.
	private Transaction[] transList;

	public ClientBillingProc() {
		try {
			database = new ClientBillingDB();
			clientsList = database.retrieveClients();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		curMode = VIEW_BY_NAME;
		Arrays.sort(clientsList, new NameComparator());
		setFilter("");
		setActiveClient(0);
	}

	public boolean addTransaction(
		int transID,
		int amt,
		String date,
		String description) {

		Transaction t = new Transaction();
		t.setFields(
			transID,
			clientsList[activeClient].clientID,
			amt,
			date,
			description);

		boolean success = database.create(t);
		updateTransList();
		return success;
	}

	public boolean updateTransaction(
		int index,
		int transID,
		int amt,
		String date,
		String description) {

		Transaction t = transList[index];

		//Keep the old ID, to be used by the database
		int oldID = t.transactionID;

		t.setFields(
			transID,
			clientsList[activeClient].clientID,
			amt,
			date,
			description);

		//Pass the ID of the Transaction to be updated, in case
		//the transactionID itself is modified.
		boolean success = database.update(oldID, t);
		updateTransList();
		return success;
	}

	public void deleteTransaction(int index) {
		try {
			database.delete(transList[index]);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		updateTransList();
	}

	public boolean addClient(
		int clientID,
		String fName,
		String lName,
		String phone,
		String e,
		String bdate,
		String add,
		String info) {

		Client c = new Client();
		c.setFields(clientID, fName, lName, phone, e, bdate, add, info);

		boolean success = database.create(c);

		updateClientList();

		//Make the new client the active one, if it is visible under the current filter
		for (int i = 0; i < filter.size(); i++) {
			Integer listIndex = (Integer) (filter.get(i));
			Client c2 = clientsList[listIndex.intValue()];
			if (c2.clientID == clientID) {
				activeClient = i;
				break;
			}
		}

		return success;
	}

	public boolean updateClient(
		int clientID,
		String fName,
		String lName,
		String phone,
		String e,
		String bdate,
		String add,
		String info) {

		int oldID = clientsList[activeClient].clientID;

		Client c = new Client();
		c.setFields(clientID, fName, lName, phone, e, bdate, add, info);

		boolean success = database.update(oldID, c);
		updateClientList();

		//Ensure the edited client is still the active one, if it is visible under the current filter
		for (int i = 0; i < filter.size(); i++) {
			Integer listIndex = (Integer) (filter.get(i));
			Client c2 = clientsList[listIndex.intValue()];
			if (c2.clientID == clientID) {
				activeClient = i;
				break;
			}
		}

		return success;
	}

	public void deleteClient(int index) {
		try {
			Client c = clientsList[((Integer) filter.get(index)).intValue()];
			database.delete(c);
			database.deleteTransactionsByClientID(c.clientID);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		updateClientList();
		setActiveClient(0);
	}

	private void doFilter(String f, int mode) {
		//Private method to carry out filtering.
		//Perform filter, with specified viewing mode, with filter f.

		filter = new ArrayList();
		if (mode == VIEW_BY_NAME) {
			for (int i = 0; i < clientsList.length; i++) {
				Client c = clientsList[i];
				String check =
					c.firstName.toUpperCase() + ", " + c.lastName.toUpperCase();

				if (check.indexOf(f.toUpperCase()) != -1)
					filter.add(new Integer(i));
			}
		} else if (mode == VIEW_BY_ID) {
			for (int i = 0; i < clientsList.length; i++)
				if (("" + clientsList[i].clientID).indexOf(f) != -1)
					filter.add(new Integer(i));
		} else
			System.err.println(
				"Internal Error: Unreachable code in setFilter()");

	}

	public Client getActiveClient() {
		if (activeClient < 0 || activeClient > clientsList.length - 1)
			return null;
		return clientsList[activeClient];
	}

	public int getActiveClientIndex() {
		return filter.indexOf(new Integer(activeClient));
	}

	public String[] getClientList() {
		//Create and return a list of Strings representing
		//the available clients for the current filter mode.

		String[] list = new String[filter.size()];
		for (int i = 0; i < filter.size(); i++) {
			int realIndex = ((Integer) filter.get(i)).intValue();
			if (curMode == VIEW_BY_NAME)
				list[i] =
					clientsList[realIndex].lastName
						+ ", "
						+ clientsList[realIndex].firstName;
			else if (curMode == VIEW_BY_ID)
				list[i] = "" + clientsList[realIndex].clientID;
		}
		return list;

	}

	public int getNumTransactions() {
		return transList.length;
	}

	public Transaction getTransactionByID(int transID) {
		for (int i = 0; i < transList.length; i++) {
			if (transList[i].transactionID == transID)
				return transList[i];
		}
		return null;
	}

	public Transaction getTransactionByIndex(int i) {
		if (i < 0 || i >= transList.length)
			return null;

		Transaction t = transList[i];
		return t;
	}

	public int getTransTotal() {
		int total = 0;
		for (int i = 0; i < transList.length; i++)
			total += transList[i].amount;
		return total;
	}

	public void setActiveClient(int index) {
		//Ignore incorrect indexes
		if (index < 0 || index >= filter.size())
			return;

		//Set the activeClient index into clientsList.
		//ie. translate filter (external) index into "real" index.
		activeClient = ((Integer) filter.get(index)).intValue();

		//Refresh the transaction list after changing the active Client.
		updateTransList();

	}

	public void setClientView(int newMode) {
		//Ignore modes that don't exist.
		if (newMode != VIEW_BY_ID && newMode != VIEW_BY_NAME)
			return;

		if (curMode != newMode) {
			curMode = newMode;
			sortClients();
		}
	}

	private void sortClients() {
		//Store the ID of the active client, so
		//it can be maintained after our sorting.
		int activeClientID = clientsList[activeClient].clientID;

		//Sort the client list using the new viewing mode.
		if (curMode == VIEW_BY_ID)
			Arrays.sort(clientsList, new IDComparator());
		else if (curMode == VIEW_BY_NAME)
			Arrays.sort(clientsList, new NameComparator());
		else
			System.err.println("Error: unreachable code in setClientView");

		//Perform the same filter that was already active.
		doFilter(lastFilterString, lastFilterMode);

		//Restore the index of the activeClient to its new value.
		for (int i = 0; i < clientsList.length; i++) {
			if (clientsList[i].clientID == activeClientID) {
				activeClient = i;
				break;
			}
		}

	}

	public void setFilter(String fstring) {
		//Store the client's requested filter and current view mode.
		//This is used to restore the filter when we switch views back and forth.
		lastFilterMode = curMode;
		lastFilterString = fstring;

		doFilter(fstring, curMode);
	}

	private void updateTransList() {
		try {
			transList =
				database.retrieveTransactions(
					clientsList[activeClient].clientID);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	private void updateClientList() {
		try {
			clientsList = database.retrieveClients();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		sortClients();

	}

	public void close() {
		try {
			database.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

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