ATM代码

银行取款转账系统(Java编写)  

dalongwangchao2011-03-14上传
这是用Java编写的一个简单的银行转账系统,包括取款,存款,转账等功能,其中用到了数据库的连接,采用Eclipse编写,包含数据库的设计文件。非常适合有一定基础的Java初学者使用。
package com.gujunjia.bank;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.sql.*;

/**
 * 
 * @author gujunjia
 */
public class DataBase
{

	static Connection conn;
	static PreparedStatement st;
	static ResultSet rs;

	/**
	 * 加载驱动
	 */
	public static void loadDriver()
	{
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
		}
		catch (ClassNotFoundException e)
		{
			System.out.println("加载驱动失败");
		}
	}

	/**
	 * 创建数据库的连接
	 * 
	 * @param database
	 *            需要访问的数据库的名字
	 */
	public static void connectionDatabase(String database)
	{
		try
		{
			String url = "jdbc:mysql://localhost:3306/" + database;
			String username = "root";
			String password = "gujunjia";
			conn = DriverManager.getConnection(url, username, password);
		}
		catch (SQLException e)
		{
			System.out.println(e.getMessage());
		}
	}

	/**
	 * 关闭数据库连接
	 */
	public static void closeConnection()
	{
		if (rs != null)
		{ // 关闭记录集
			try
			{
				rs.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		if (st != null)
		{ // 关闭声明
			try
			{
				st.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		if (conn != null)
		{ // 关闭连接对象
			try
			{
				conn.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}

package com.gujunjia.bank;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

/**
 * 本类主要实现整个系统的界面
 * 
 * @author gujunjia
 */
public class MainFrame extends JFrame implements ActionListener, FocusListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static String userId;
	JTextField userIdText;
	JPasswordField passwordText;
	JButton registerButton;
	JButton logInButton;

	public MainFrame()
	{
		super("个人银行系统");
		this.setSize(400, 500);
		this.setLocation(getMidDimension(new Dimension(400, 500)));
		getAppearance();
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	/**
	 * 获取屏幕的中间尺寸
	 * 
	 * @param d
	 *            Dimension类型
	 * @return 一个Point类型的参数
	 */
	public static Point getMidDimension(Dimension d)
	{
		Point p = new Point();
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		p.setLocation((dim.width - d.width) / 2, (dim.height - d.height) / 2);
		return p;
	}

	/**
	 * 布局
	 * 
	 * @return Container
	 */
	public Container getAppearance()
	{
		Container container = this.getContentPane();
		container.setLayout(new GridLayout(4, 0));
		JLabel label1 = new JLabel("个人银行系统");
		label1.setFont(new Font("楷体", Font.BOLD, 40));
		JLabel label2 = new JLabel("账号:");
		label2.setFont(new Font("楷体", Font.PLAIN, 15));
		JLabel label3 = new JLabel("密码:");
		label3.setFont(new Font("楷体", Font.PLAIN, 15));
		userIdText = new JTextField(20);
		userIdText.addFocusListener(this);
		passwordText = new JPasswordField(20);
		passwordText.addFocusListener(this);
		JPanel jp1 = new JPanel();
		JPanel jp2 = new JPanel();
		JPanel jp3 = new JPanel();
		JPanel jp4 = new JPanel();
		jp1.add(label1);
		jp2.add(label2);
		jp2.add(userIdText);
		jp3.add(label3);
		jp3.add(passwordText);
		registerButton = new JButton("注册");
		registerButton.addActionListener(this);
		registerButton.setFont(new Font("楷体", Font.BOLD, 15));
		logInButton = new JButton("登录");
		logInButton.addActionListener(this);
		logInButton.setFont(new Font("楷体", Font.BOLD, 15));
		jp4.add(registerButton);
		jp4.add(logInButton);
		container.add(jp1);
		container.add(jp2);
		container.add(jp3);
		container.add(jp4);
		return container;
	}

	public void actionPerformed(ActionEvent e)
	{
		Object btn = e.getSource();
		if (btn == registerButton)
		{
			new Register();
		}
		else if (btn == logInButton)
		{
			String id = userIdText.getText().trim();
			String password = new String(passwordText.getPassword());
			Bank bank = new Bank();
			if (id.equals("") || password.equals(""))
			{
				JOptionPane.showMessageDialog(null, "请输入账号和密码");
			}
			else
			{
				String dPassword = bank.getPassword(id);
				if (password.equals(dPassword))
				{
					userId = id;
					this.dispose();
					new UserGUI();
				}
				else
				{
					JOptionPane.showMessageDialog(this, "密码或用户名错误", "错误",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}

	@Override
	public void focusGained(FocusEvent e)
	{
		Object text = e.getSource();
		if (text == userIdText)
		{
			userIdText.setText("");
			userIdText.setFont(new Font("宋体", Font.BOLD, 15));
		}
		else if (text == passwordText)
		{
			passwordText.setText("");
		}

	}

	@Override
	public void focusLost(FocusEvent e)
	{
		Object text = e.getSource();
		if (text == userIdText)
		{
			if (userIdText.getText().equals(""))
			{
				userIdText.setText("请输入账号");
				userIdText.setFont(new Font("楷体", Font.ITALIC, 15));
			}
		}
	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<iostream> #include<string> using namespace std; class Consumer { private: long Accnumber; string conname; double Balance; long password; public: Consumer(); void login(); int Query(); int transfer(); int withdraw(); int update();//修改密码 void choose(); }; Consumer::Consumer() { Accnumber=1030070128; conname="林杰东"; Balance=10000.00; password=123456; } void Consumer::login() { long password0; int i=0; cout<<"**********欢迎登入林杰东银行**********"<<endl; cout<<endl; cout<<endl; cout<<endl; cout<<"请输入您的6位密码(错误输入不得超过3次):"; cout<<endl; cout<<endl; cout<<endl; cout<<endl; cout<<"**********欢迎登入林杰东银行**********"<<endl; while(i<3) { cout<<"密码:"; cin>>password0; i++; if(password0!=password) { cout<<"对不起,您的输入有误,请重新输入!!!"<<endl; if(i==3) { cout<<"您的错误输入已满3次,请取出您的银行卡,以免被吞卡!!!"<<endl; login(); break; } } else { cout<<"登入成功!!!"<<endl; break;} } } int Consumer::Query() { cout<<"您的用户名:"; cout<<conname<<endl; cout<<"您的账号:"; cout<<Accnumber<<endl; cout<<"您的当前余额:"; cout<<Balance<<endl; cout<<"密码:"; cout<<password<<endl; return 1; } int Consumer::transfer() { long Accnumber1,Accnumber2; double money; cout<<"请输入您要转入的账号:"; cin>>Accnumber1; if(Accnumber1==Accnumber) { cout<<"对不起,您要转入的账号是当前银行卡的账号,无法转账!!!请重新输入其他账号!!!"<<endl; return transfer(); } else { cout<<"请重新输入您要转入的账号:"; cin>>Accnumber2; if(Accnumber1==Accnumber2 ) { cout<<"请输入您要转账的金额:"; cin>>money; cout<<endl; if(Balance>=money) { Balance=Balance-money; cout<<"转账成功!!!"<<endl; cout<<"您的当前余额:"; return Balance; } else { cout<<"对不起,您当前余额不足,请重新转账!!!"<<endl; return transfer(); } } else { cout<<"您两次输入的账号有误,请重新输入!!!"<<endl; return transfer(); } } } int Consumer::update() { long password1; long password2; long password3; cout<<"请输入您的6位原密码:"; cin>>password1; if(password1!=password) { cout<<"对不起,您的输入有误,请重新输入!!!"<<endl; return update(); } else { cout<<"请输入您的6位新密码:"; cin>>password2; cout<<"请重新输入您的6位新密码:"; cin>>password3; if(password2==password3) { password=password3; cout<<"恭喜您,您的密码修改成功!!!"<<endl; cout<<"您的新密码:"; return password; } else { cout<<"对不起,您的两次输入有误,请重新输入!!!"<<endl; return update(); } } } int Consumer::withdraw() { double number; cout<<"输入要取的钱数:"; cin>>number; if(Balance<number) { cout<<"你的账号余额不足 "<<number<<"."<<endl; cout<<"取款失败!"<<endl; return withdraw(); } else { cout<<"取款成功!!!"<<endl; Balance=Balance-number; cout<<"您的当前账号余额为:"; return Balance; } } void Consumer::choose() { cout<<"1.查 询"<<endl; cout<<"2.转 账"<<endl; cout<<"3.修改密码"<<endl; cout<<"4.取 款"<<endl; cout<<"0.退出系统"<<endl; } class ATM { public: Consumer c; int menu(); }; int ATM::menu() { c.login(); int choice=0; while(choice!=5) { cout<<"**********欢迎登入林杰东银行**********"<<endl; c.choose(); cout<<"**********欢迎登入林杰东银行**********"<<endl; cout<<"请输入您的选择:"; cin>>choice; switch(choice) { case 1: cout<<c.Query()<<endl; break; case 2: cout<<c.transfer()<<endl; break; case 3: cout<<c.update()<<endl; break; case 4: cout<<c.withdraw()<<endl; break; case 0: cout<<"请取出您的银行卡,欢迎下次使用!!!"<<endl; return 0; break; default: cout <<"输入错误!请重新输入!\n\n"; break; } } } int main() { ATM atm; atm.menu(); return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值