java银行管理系统前三题

本文介绍了一个简单的银行账户管理系统的设计与实现,包括用户类、信用账户、普通账户及银行类的定义,实现了用户开户、登录、存款、取款和查询余额等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
		

	}

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值