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

📄 recordviewer.java

📁 本程序用Java语言描述了一个基本的银行管理系统
💻 JAVA
字号:
package banking.applet;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import javax.swing.DefaultListModel;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import banking.domain.Bank;
import banking.domain.CheckingAccount;
import banking.domain.Customer;
import banking.domain.SavingsAccount;
import banking.io.CustomerIO;

public class RecordViewer extends JApplet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6406344980078887952L;

	public static final String localHost = "";
	
	public static final String fileName = "bankRecords.dat";

	private JList jltCustomer;

	private JList jltAccount;

	private DefaultListModel dlmCustomer;

	private DefaultListModel dlmAccount;

	private JPanel jpAccount = new JPanel();

	private JLabel jlbFirstName = new JLabel();

	private JLabel jlbLastName = new JLabel();

	private JLabel jlbAccType = new JLabel();

	private JLabel jlbAccBalance = new JLabel();

	private JLabel jlbAccAddition = new JLabel();

	private Bank bank;

	private RandomAccessFile raf;

	public void init() {
		File file = new File(localHost+RecordViewer.fileName);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		this.setLayout(new GridLayout(2, 2));//

		this.bank = Bank.getBank();

		this.jltCustomer = new JList();
		this.dlmCustomer = new DefaultListModel();
		this.jltCustomer.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.jltCustomer.setModel(this.dlmCustomer);

		this.jltAccount = new JList();
		this.dlmAccount = new DefaultListModel();
		this.jltAccount.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.jltAccount.setModel(this.dlmAccount);

		JPanel jpButtons = new JPanel();
		jpButtons.setLayout(new GridLayout(1, 6));
		jpButtons.setBorder(new BevelBorder(BevelBorder.RAISED));

		JButton jbtAdd = new JButton("Add");
		JButton jbtDel = new JButton("Del");
		JButton jbtUpdates = new JButton("Updates");
		JButton jbtSearch = new JButton("Search");
		JButton jbtSort = new JButton("Sort");
		JButton jbtExit = new JButton("Exit");

		jpButtons.add(jbtAdd);
		jpButtons.add(jbtDel);
		jpButtons.add(jbtUpdates);
		jpButtons.add(jbtSearch);
		jpButtons.add(jbtSort);
		jpButtons.add(jbtExit);

		jbtAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addRecord();
			}
		});
		jbtDel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				delRecord();
			}
		});
		jbtUpdates.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveAll();
			}
		});
		jbtSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				search();
			}
		});
		jbtSort.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bank.sortCustomers();
				updateCustomerList();
			}
		});
		jbtExit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {				
				try{
					writeBank();
					System.exit(0);
				}catch(Exception ex){
					ex.printStackTrace();//
				}
			}
		});
		this.jltCustomer.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				int index = jltCustomer.getSelectedIndex();
				setAccLabels(bank.getCustomer(index));
			}
		});
		this.jltAccount.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				showAccInfo();
			}
		});
		
		JPanel jpNameMessage = new JPanel();
		jpNameMessage.setLayout(new GridLayout(2, 1));
		jpNameMessage.add(this.jlbFirstName);
		jpNameMessage.add(this.jlbLastName);

		JPanel jpAccMessage = new JPanel(new GridLayout(3, 1));
		jpAccMessage.setBorder(new TitledBorder("Account Information"));
		jpAccMessage.add(this.jlbAccType);
		jpAccMessage.add(this.jlbAccBalance);
		jpAccMessage.add(this.jlbAccAddition);

		JScrollPane jscpAccount = new JScrollPane(this.jltAccount);
		jscpAccount.setBorder(new TitledBorder("Account List"));

		this.jpAccount.setBorder(new TitledBorder("Customer Detail"));
		this.jpAccount.setLayout(new GridLayout(3, 1));
		this.jpAccount.add(jpNameMessage);
		this.jpAccount.add(jscpAccount);
		this.jpAccount.add(jpAccMessage);		

		this.setAccLabels(null);

		this.setLayout(new BorderLayout());
		this.add(jpButtons, BorderLayout.SOUTH);

		JScrollPane customerPane = new JScrollPane(this.jltCustomer);
		customerPane.setBorder(new TitledBorder("Customer List"));
		this.add(customerPane, BorderLayout.CENTER);
		this.add(this.jpAccount, BorderLayout.EAST);

		this.setSize(500, 500);//
	}

	public void start() { // load the file
		try {
			readBank();
			this.updateCustomerList();
		} catch (Exception e) {
			e.printStackTrace();//
			JOptionPane.showMessageDialog(null, "readBank error");
		}
	}

	public void destroy() { // save when destroy
		try {
			writeBank();
		} catch (Exception e) {
			e.printStackTrace();//
			JOptionPane.showMessageDialog(null, "may not save successfully");
		}
	}

	public void readBank() throws IOException { // file ---> bank
		raf = new RandomAccessFile(localHost+RecordViewer.fileName, "rw");
		if (0 != raf.length()) {
			int loop = raf.readInt();
			for (int i = 0; i < loop; ++i) {
				this.bank.addCustomer(CustomerIO.readCustomer(raf));
			}
		}
		raf.close();
		raf = null;
	}

	public void writeBank() throws IOException { // bank ---> file
		raf = new RandomAccessFile(localHost+RecordViewer.fileName, "rw");
		raf.setLength(0);
		raf.writeInt(this.bank.getNumOfCustomers());
		for (int i = 0; i < this.bank.getNumOfCustomers(); ++i)
			CustomerIO.writeCustomer(this.bank.getCustomer(i), raf);
		raf.close();
		raf = null;
	}

	public void addRecord() {
		NewCustomerFrame frame = new NewCustomerFrame();
		frame.setTitle("New Customer");
		frame.setSize(500, 500);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				updateCustomerList();
			}
			public void windowClosed(WindowEvent e){
				updateCustomerList();
			}
		});
	}

	public void delRecord() {
		int index = this.jltCustomer.getSelectedIndex();
		if (-1 != index) {
			this.bank.kickCustomer(index);
		}
		this.dlmAccount.clear();
		this.updateCustomerList();
	}

	public void search() {
		try {
			String f = JOptionPane.showInputDialog("firstName: ");
			String l = JOptionPane.showInputDialog("lastName: ");
			int index = this.bank.searchCustomers(f, l);
			if (-1!=index)
				this.jltCustomer.setSelectedIndex(index);
			else
				JOptionPane.showMessageDialog(null, "no such a customer");
		} catch (Exception e) {
			e.printStackTrace();//
		}
	}

	public void saveAll() {
		try {
			this.writeBank();
			JOptionPane.showMessageDialog(null, "save successfully");
			this.updateCustomerList();
		} catch (Exception e) {
			e.printStackTrace();//
			JOptionPane.showMessageDialog(null, "may not save successfully");
		}
	}

	public void updateCustomerList() {
		this.dlmCustomer.clear();
		this.dlmAccount.clear();
		for (int i = 0; i < this.bank.getNumOfCustomers(); ++i) {
			String f = this.bank.getCustomer(i).getFirstName();
			String l = this.bank.getCustomer(i).getLastName();
			f = fuckSpace(f);
			l = fuckSpace(l);
			this.bank.getCustomer(i).setFirstName(f);
			this.bank.getCustomer(i).setLastName(l);
			this.dlmCustomer.addElement(f+" "+l);
		}
	}

	public void setAccLabels(Customer c) {
		this.jlbAccType.setText("Account Type:");
		this.jlbAccBalance.setText("Balance:");
		this.jlbAccAddition.setText("Additional Infomation:");
		if (null == c) {
			this.jlbFirstName.setText("First Name: ");
			this.jlbLastName.setText(("Last Name: "));
		} else {
			this.jlbFirstName.setText("First Name: " + c.getFirstName());
			this.jlbLastName.setText(("Last Name: ") + c.getLastName());
			updatesAccList(c);
		}
	}

	public void showAccInfo() {
		Customer c = this.bank.getCustomer(this.jltCustomer.getSelectedIndex());
		int index = this.jltAccount.getSelectedIndex();
		if (-1!=index){
			String accType = null;
			String addition = null;
			if (c.getAccount(index) instanceof SavingsAccount) {
				accType = "Savings Account";
				addition = "Interest Rate: "+new StringBuffer().append(
						(((SavingsAccount) c.getAccount(index)).getInterestRate()))
						.toString();
			} else if (c.getAccount(index) instanceof CheckingAccount){
				accType = "Checking Account";
				addition = "Overdraft Protection: "+new StringBuffer().append(
						(((CheckingAccount) c.getAccount(index))
								.getOverdraftProtection())).toString();
			}
			String balance = new StringBuffer().append(
					c.getAccount(index).getBalance()).toString();
			this.jlbAccType.setText("Account Type:" + accType);
			this.jlbAccBalance.setText("Balance:" + balance);
			this.jlbAccAddition.setText(addition);
		}
	}

	public void updatesAccList(Customer c) {
		this.dlmAccount.clear();
		for (int i = 0; i < c.getNumOfAccounts(); ++i) {
			this.dlmAccount.addElement("" + (i + 1));
		}
	}
	
	public static String fuckSpace(String str){
		char[] chars = str.toCharArray();
		int cutOff = 0;
		for (int i=chars.length-1; i>=0; --i){
			if (chars[i]!=' '){
				cutOff = i;
				break;
			}
		}
		return  new StringBuffer().insert(0, chars, 0, cutOff+1).toString();
	}

	public static void main(String[] args) {
		RecordViewer frame = new RecordViewer();
		frame.setVisible(true);
	}

}

⌨️ 快捷键说明

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