Account//用户类
package entity;
import javax.swing.JOptionPane;
public class Account {
long id;
String password;
String name;
String personId;
double balance;
public Account() {
super();
// TODO Auto-generated constructor stub
}
public Account(long id, String password, String name, String personId,
double balance) {
super();
this.id = id;
this.password = password;
this.name = name;
this.personId = personId;
this.balance = balance;
}
public void deposit(double bal)
{
balance+=bal;
}
public void withdraw(double bal)
{
if(bal-balance>0)
JOptionPane.showMessageDialog(null, "余额不足", null, JOptionPane.ERROR_MESSAGE);
else
{
balance-=bal;
}
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPersonId() {
return personId;
}
public void setPersonId(String personId) {
this.personId = personId;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
信用账户(可预支10000)
package entity;
import javax.swing.JOptionPane;
public class CreditAccount extends Account{
public CreditAccount() {
super();
// TODO Auto-generated constructor stub
}
public CreditAccount(long id, String password, String name,
String personId, double balance) {
super(id, password, name, personId, balance);
// TODO Auto-generated constructor stub
}
@Override
public void withdraw(double bal) {
// TODO Auto-generated method stub
if(bal-balance>10000)
{
JOptionPane.showMessageDialog(null, "余额不足", null, JOptionPane.ERROR_MESSAGE);
return ;
}
else
{
balance-=bal;
}
}
}
普通账户
package entity;
import javax.swing.JOptionPane;
public class SavingAccount extends Account{
public SavingAccount() {
super();
// TODO Auto-generated constructor stub
}
public SavingAccount(long id, String password, String name,
String personId, double balance) {
super(id, password, name, personId, balance);
// TODO Auto-generated constructor stub
}
@Override
public void withdraw(double bal) {
// TODO Auto-generated method stub
if(balance<bal)
{
JOptionPane.showMessageDialog(null, "余额不足", null, JOptionPane.ERROR_MESSAGE);
return;
}
else
{
balance-=bal;
}
}
}
银行
package biz;
import java.awt.List;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import entity.Account;
public class Bank {
LinkedList bank=new LinkedList();//数组这方面不如链表
Account acount;
int length=0;
public Bank() {
super();
// TODO Auto-generated constructor stub
}
public void open_acount(long id,String password,String name,String personId,double balance)
{
acount=new Account(id, password, name, personId, balance);
bank.add(length,acount);
length++;
}
public Account comein(long id,String password)
{
for(int i=0;i<length;i++)
{
acount=(Account)bank.get(i);
if(acount.getId()==id &&acount.getPassword().equals(password))
return acount;
}
JOptionPane.showMessageDialog(null, "请检查用户名或密码是否正确", null, JOptionPane.ERROR_MESSAGE);
return null;
}
public void whithin(long id,double balance)
{
for(int i=0;i<length;i++)
{
acount=(Account)bank.get(i);
if(acount.getId()==id)
{
acount.deposit(balance);
bank.set(i, acount);
}
}
}
public void whithdraw(long id,double balance)
{
for(int i=0;i<length;i++)
{
acount=(Account)bank.get(i);
if(acount.getId()==id)
{
acount.withdraw(balance);
bank.set(i, acount);
}
}
}
public double showbalabce(long id)
{
for(int i=0;i<length;i++)
{
acount=(Account)bank.get(i);
if(acount.getId()==id)
{
return acount.getBalance();
}
}
return 0;
}
}
用户测试界面
package view;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import entity.Account;
import biz.Bank;
public class TextView extends JFrame implements ActionListener{
JLabel Lname,Lid,Lpassword,LpersonId,Lbalance,Linbalance,Loutbalance;
JTextField Tname,Tid,Tpassword,TpersonId,Tbalance,Tinbalance,Toutbalance;
JPanel Pname,Pid,Ppassword,PpersonId,Pbalance,Pinbalance,Poutbalance;
JButton opencount,comein,within,withdraw,showbalance;
Bank bank;
Account account;
public TextView() throws HeadlessException {
super();
// TODO Auto-generated constructor stub
bank=new Bank();
Lid =new JLabel("账户号码");
Lbalance=new JLabel("账户余额");
Linbalance=new JLabel("存款金额");
Loutbalance=new JLabel("取款金额");
Lname=new JLabel("真实姓名");
LpersonId=new JLabel("身份证号码");
Lpassword=new JLabel("账户密码");
Tbalance=new JTextField(10);
Tinbalance=new JTextField(10);
Toutbalance=new JTextField(10);
// Tbalance.setEditable(false);
Tname=new JTextField(10);
// Tname.setEditable(false);
Tid=new JTextField(10);
// Tid.setEditable(false);
TpersonId=new JTextField(10);
// TpersonId.setEditable(false);
Tpassword=new JTextField(10);
// Tpassword.setEditable(false);
Pname=new JPanel();
Pid=new JPanel();
Ppassword=new JPanel();
PpersonId=new JPanel();
Pbalance=new JPanel();
Pinbalance=new JPanel();
Poutbalance=new JPanel();
opencount=new JButton("用户开户");
comein=new JButton("用户登录");
within=new JButton("用户存款");
withdraw=new JButton("用户取款");
showbalance=new JButton("查询余额");
Pname.add(Lname);
Pname.add(Tname);
Pbalance.add(Lbalance);
Pbalance.add(Tbalance);
Pinbalance.add(Linbalance);
Pinbalance.add(Tinbalance);
Poutbalance.add(Loutbalance);
Poutbalance.add(Toutbalance);
Pid.add(Lid);
Pid.add(Tid);
Ppassword.add(Lpassword);
Ppassword.add(Tpassword);
PpersonId.add(LpersonId);
PpersonId.add(TpersonId);
add(Pid);
add(Ppassword);
add(Pname);
add(PpersonId);
add(Pinbalance);
add(Poutbalance);
add(Pbalance);
add(opencount);
add(comein);
add(within);
add(withdraw);
add(showbalance);
setLayout(new FlowLayout());
setBounds(100, 100, 200,400 );
setVisible(true);
validate();
opencount.addActionListener(this);
comein.addActionListener(this);
within.addActionListener(this);
withdraw.addActionListener(this);
showbalance.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource()==opencount)
{
long id=Long.parseLong(Tid.getText());
String password=Tpassword.getText();
String name=Tname.getText();
String personId=TpersonId.getText();
double balance=Double.parseDouble(Tinbalance.getText());
bank.open_acount(id, password, name, personId, balance);
Tbalance.setText("");
Tinbalance.setText("");
Toutbalance.setText("");
Tname.setText("");
Tid.setText("");
TpersonId.setText("");
Tpassword.setText("");
}
if(arg0.getSource()==comein)
{
long id=Long.parseLong(Tid.getText());
String password=Tpassword.getText();
account=new Account();
if((account=bank.comein(id, password))!=null)
{
Tname.setText(account.getName());
}
}
if(arg0.getSource()==within)
{
double balance=Double.parseDouble(Tinbalance.getText());
long id=Long.parseLong(Tid.getText());
bank.whithin(id, balance);
Tbalance.setText(bank.showbalabce(id)+"");
}
if(arg0.getSource()==withdraw)
{
double balance=Double.parseDouble(Toutbalance.getText());
long id=Long.parseLong(Tid.getText());
bank.whithdraw(id, balance);
Tbalance.setText(bank.showbalabce(id)+"");
}
if(arg0.getSource()==showbalance)
{
long id=Long.parseLong(Tid.getText());
bank.showbalabce(id);
Tbalance.setText(bank.showbalabce(id)+"");
}
}
}
测试图片

测试主函数
package view;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TextView te=new TextView();
}
}