atm代码java设计,用Java编写一个ATM机的代码
使用Java语言进行面向对象设计:ATM柜员机模拟程序
import java.awt.*;
员工经过长期磨合与沉淀,具备了协作精神,得以通过团队的力量开发出优质的产品。成都创新互联公司坚持“专注、创新、易用”的产品理念,因为“专注所以专业、创新互联网站所以易用所以简单”。公司专注于为企业提供网站建设、做网站、微信公众号开发、电商网站开发,微信小程序定制开发,软件按需求定制开发等一站式互联网企业服务。
import java.awt.event.*;
import javax.swing.*;
public class AtmSystem extends JFrame implements ActionListener
{
private JPanel buttonPanel=new JPanel();
private JPanel area1Panel=new JPanel();
private JPanel area2Panel=new JPanel();
private JTextField note=new JTextField(33);
private JButton buttons[];
private JButton dot = new JButton(".");
private JButton doublezero = new JButton("00");
private JButton ok = new JButton("确定");
private JButton exit = new JButton("退出");
private JButton cancel = new JButton("取消");
private JButton change=new JButton("更正");
private JButton getmoney = new JButton("取款");
private JButton search = new JButton("查询余额");
private JButton changekey = new JButton("修改密码");
private JButton putmoney= new JButton("存款");;
private boolean flag0, flag1, flag2,flag3,flag4;
int status=0;
int k,s,k1;
String str1="\t欢迎使用ATM柜员机!";
String str2="\t请输入用户名:";
String str3="\t请输入密码:";
String str4="\t请输入取款金额:";
String str5="\t请输入存款金额:";
String str6="\t请输入新密码:";
String str7="\t您的余额为:";
String str8="\t用户名错误,请重新输入:";
String str9="\t密码错误,请重新输入:";
String str10="\t余额不足,请重新输入:";
String str11="\t密码修改成功!";
String store1="",store2="";
double moneysum=10000;
double getMoney;
String username="123456";
String password="123456";
public AtmSystem()
{
super("ATM柜员机");
Container container=getContentPane();
buttons=new JButton[10];
dot.addActionListener(this);
doublezero.addActionListener(this);
change.addActionListener(this);
cancel.addActionListener(this);
ok.addActionListener(this);
exit.addActionListener(this);
changekey.addActionListener(this);
putmoney.addActionListener(this);
getmoney.addActionListener(this);
search.addActionListener(this);
area2Panel.add(changekey);
area2Panel.add(putmoney);
area2Panel.add(getmoney);
area2Panel.add(search);
area2Panel.setLayout(new GridLayout(1,3));
container.add(area2Panel,BorderLayout.NORTH);
note.setText(str1);
note.setEditable(false);
area1Panel.add(note);
buttonPanel.setLayout(new GridLayout(4,4));
for(int count=0;countbuttons.length;count++)
{
buttons[count]=new JButton(""+count);
buttons[count].addActionListener(this);
buttonPanel.add(buttons[count]);
}
buttonPanel.add(doublezero);
buttonPanel.add(dot);
buttonPanel.add(change);
buttonPanel.add(cancel);
buttonPanel.add(ok);
buttonPanel.add(exit);
container.add(buttonPanel,BorderLayout.SOUTH);
container.add(area1Panel);
area1Panel.setVisible(true);
area2Panel.setVisible(false);
setSize(400,250);
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
JButton operation = (JButton) event.getSource();
if(operation==ok)
{
if(status==0)
{
note.setText(""+str2);
status=1;
}
else if(flag4==true)
{
area2Panel.setVisible(true);
area1Panel.setVisible(false);
flag4=false;
}
else if(status==1k==1)
{
note.setText(""+str3);
status=2;
k=0;
}
else if(status==1k==0)
{
note.setText(""+str8);
store1="";
}
else if(status==2k1==1)
{
k1=0;
area1Panel.setVisible(false);
area2Panel.setVisible(true);
status=3;
}
else if(status==2k1==0)
{
note.setText(""+str9);
store2="";
}
else if(status==3)
{
if(flag0==true)
{
getMoney=Double.valueOf(store1).doubleValue();
if(getMoneymoneysum)
{
area2Panel.setVisible(true);
area1Panel.setVisible(false);
flag0=false;
moneysum=moneysum-getMoney;
}
else
{
area2Panel.setVisible(false);
area1Panel.setVisible(true);
note.setText(""+str10);
store1="";
}
}
else if(flag1==true)
{
area2Panel.setVisible(true);
area1Panel.setVisible(false);
flag1=false;
}
else if(flag2==true)
flag2=false;
else if(flag3=true)
{
flag4=true;
note.setText(""+str11);
password=store2;
flag3=false;
}
}
}
else if(operation==cancel)
{
if(status==1)
{
note.setText(""+str1);
status=0;
store1="";
store2="";
}
else if(status==2)
{
note.setText(""+str2);
status=1;
store1="";
store2="";
}
else if(status==3flag2)
{
area1Panel.setVisible(false);
area2Panel.setVisible(true);
flag2=false;
store1="";
store2="";
}
else if(status==3flag0)
{
area1Panel.setVisible(false);
area2Panel.setVisible(true);
flag0=false;
store1="";
store2="";
}
else if(status==3flag3)
{
area1Panel.setVisible(false);
area2Panel.setVisible(true);
flag3=false;
store1="";
store2="";
}
}
else if(operation==change)
{
if(status==1)
{
note.setText(""+str2);
store1="";
store2="";
}
else if(status==2)
{
note.setText(""+str3);
store1="";
store2="";
}
else if(status==3flag2)
{
note.setText(""+str5);
store1="";
store2="";
}
else if(status==3flag0)
{
note.setText(""+str4);
store1="";
store2="";
}
else if(status==3flag3)
{
note.setText(""+str6);
store1="";
store2="";
}
}
else if(operation==doublezerostatus==3flag0==true)
{
note.setText(note.getText()+"00");
}
else if(operation==dotstatus==3flag0==true)
{
note.setText(note.getText()+".");
}
else if(operation==getmoney)
{
note.setText(""+str4);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
store1="";
flag0=true;
}
else if(operation==search)
{
note.setText(""+str7+moneysum);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
flag1=true;
}
else if(operation==putmoney)
{
note.setText(""+str5);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
flag2=true;
}
else if(operation==changekey)
{
note.setText(""+str6);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
store2="";
flag3=true;
}
else if(operation==exit)
{
note.setText(""+str1);
area2Panel.setVisible(false);
area1Panel.setVisible(true);
status=0;
flag0=flag1=flag2=flag3=flag4=false;
store1="";
store2="";
k=k1=s=0;
}
else if(status==1||(status==3flag2==true)||(status==3flag0==true))
{
for(int count=0;countbuttons.length;count++)
if (operation== buttons[count])
{
note.setText(note.getText() + count);
store1+=event.getActionCommand();
}
user(store1);
}
else if(status==2||(status==3flag3==true))
{
for(int count=0;countbuttons.length;count++)
if (operation== buttons[count])
{
note.setText(note.getText()+ "*");
store2+=event.getActionCommand();
}
key(store2);
}
}
public void user(String load)
{
if(username.equals(load))
{
k=1;
s=1;
}
}
public void key(String load)
{
if(password.equals(store2))
{
k1=1;
}
}
public static void main(String args[])
{
AtmSystem application=new AtmSystem();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
用JAVA制作简单的ATM的代码 求教
ok,稍等
呵呵,已经给你拆分成了两个独立的类了。
我再吧注释加起吧。
哪儿不清楚的可以给我留言嘛,刚开始学习的时候就要多看看别人写的代码,然后从中学习。这里写的用到了简单的封装面向对象静态类,你可以在多了解下,不难的。
import java.util.Scanner;
public class Atm {
//显示菜单
static void showMenu() {
System.out.println();
System.out.print("1.查询账户余额\n");
System.out.print("2.存款\n");
System.out.print("3.取款\n");
System.out.print("0.退出\n");
System.out.print("请选择操作:");
}
public static void main(String[] arg) {
//创建一个account的对象
Account account = new Account();
System.out.println("*******欢迎使用**********");
//循环操作提示
while (true) {
showMenu();//调用显示菜单的方法
//得到用户的输入
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
switch (input) {
case 1:
account.query();
break;
case 2:
System.out.print("请输入存款额:");
float in = scanner.nextFloat();
account.in(in);
account.query();
break;
case 3:
System.out.print("请输入取款额:");
float out = scanner.nextFloat();
account.out(out);
account.query();
break;
case 0:
System.out.println("谢谢使用");
System.exit(0); //终止程序
break;
default:
System.out.println("输入有误");
}
}
}
}
//帐号类
class Account {
private float money = 8000;
// 查询账户余额
public void query() {
System.out.println("账户余额:" + money);
}
// 取出,out是取出的存款数
public void out(float out) {
if (money out) {
System.out.println("账户余额不足");
}
this.money -= money;
}
// 存入,in是输入的存款数
public void in(float in) {
this.money += in;
}
}
atm机的java怎么写啊
package demo;
import java.io.*;
/*该类为实现客户信息及部分功能*/
class Account {
private String code =null; //信用卡号
private String name =null; //客户姓名
private String password=null; //客户密码
private double money =0.0; //卡里金额
/********************/
public Account(String code,String name,String password,double money)
{
this.code=code;
this.name=name;
this.password=password;
this.money=money;
}
protected String get_Code() {
return code;
}
protected String get_Name() {
return name;
}
protected String get_Password() {
return password;
}
public double get_Money() {
return money;
}
/*得到剩余的钱的数目*/
protected void set_Balance(double mon) {
money -= mon;
}
/*得到剩余的钱的数目*/
protected void set_Deposit(double mon) {
money += mon;
}
}
/**********实现具体取款机功能*********/
class ATM {
Account act;
// private String name;
// private String pwd;
public ATM() {
act=new Account("000000","Devil","123456",50000);
}
/***********欢迎界面***********/
protected void Welcome()
{
String str="---------------------------------";
System.out.print(str+"\n"+
"欢迎使用Angel模拟自动取款机程序.\n"+str+"\n");
System.out.print(" 1.取款."+"\n"+
" 2.存款."+"\n"+
" 3.查询信息."+"\n"+
" 4.密码设置."+"\n"+
" 5.退出系统."+"\n");
}
/**********登陆系统**********/
protected void Load_Sys() throws Exception
{
String card,pwd;
int counter=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do {
System.out.println("请输入您的信用卡号:");
card=br.readLine();
System.out.println("请输入您的密码:");
pwd=br.readLine();
if(!isRight(card,pwd))
{
System.out.println("您的卡号或密码输入有误.");
counter++;
}
else
Welcome();
SysOpter();
}while(counter3);
Lock_Sys();
}
/**********系统操作**********/
protected void SysOpter() throws Exception
{
int num;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请选择您要操作的项目(1-5):");
num=br.read(); //num为ASICC码转换的整数
switch(num) {
case 49: BetBalance(); break;
case 50: Deposit(); break;
case 51: Inqu_Info(); break;
case 52: Set_Password(); break;
case 53: Exit_Sys(); break;
}
System.exit(1);
}
/**********信息查询
* @throws Exception **********/
protected void Inqu_Info() throws Exception {
System.out.print("---------------------\n"+
act.get_Code()+"\n"+
act.get_Name()+"\n"+
act.get_Money()+"\n"+
"-----------------------");
Welcome();
SysOpter();
}
/**********取款**********/
public void BetBalance() throws Exception
{
String str=null,str1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int count=0;//取款错误超过3次自动退出
do {
System.out.println("请输入您要取的数目:");
str=br.readLine();
str1=String.valueOf(act.get_Money());
System.out.println(str1);
if(Double.parseDouble(str)Double.parseDouble(str1)) {
count++;
System.out.println("超过已有的钱数,请重新输入您要取的数目:");
if(count=3){
System.out.println("超过已有的钱数,请重新输入您要取的数目:");
Exit_Sys();
}
}
else {
/*操作成功*/
act.set_Balance(Double.parseDouble(str));
System.out.println("取款成功,请收好您的钱.");
Welcome();
SysOpter();
}
}while(true);
}
/*******存款********/
public void Deposit() throws Exception{
String str=null;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do {
System.out.println("请输入您要存的数目:");
str=br.readLine();
/*操作成功*/
act.set_Deposit(Double.parseDouble(str));
System.out.println("取款成功,请收好您的钱.");
Welcome();
SysOpter();
}while(true);
}
/**********判断卡内是否有钱**********/
protected boolean isBalance() {
if(act.get_Money()0) {
System.out.println("对不起,您的钱数不够或卡已透支.");
return false;
}
return true;
}
/********卡号密码是否正确******/
protected boolean isRight(String card,String pwd)
{
if(act.get_Code().equals(card) act.get_Password().equals(pwd))
return true;
else
return false;
}
/**********密码修改**********/
protected void Set_Password() throws Exception
{
String pwd=null;
int counter=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do {
System.out.println("请输入旧密码:");
pwd=br.readLine();
if(act.get_Password().equals(pwd))
{
do {
System.out.println("请输入新密码:");
String pwd1=br.readLine();
System.out.println("请再次输入新密码:");
String pwd2=br.readLine();
if(!pwd1.equals(pwd2))
{
System.out.println("两次输入不一致,请再次输入.");
}
else
{
System.out.println("密码修改成功,请使用新密码.");
Welcome();
SysOpter();
}
}while(true);
}
}while(counter3);
}
/**********锁定机器**********/
protected void Lock_Sys() {
System.out.println("对不起,您的操作有误,卡已被没收.");
System.exit(1);
}
/**********结束系统**********/
protected void Exit_Sys() {
System.out.println("感谢您使用本系统,欢迎下次在来,再见!");
System.exit(1);
}
}
public class Text
{
public static void main(String[] args) throws Exception
{
ATM atm=new ATM();
atm.Load_Sys();
// atm.Exit_Sys();
}
}
卡号:00000 密码123456 默认50000金额。简单版本的存取款。
网站栏目:atm代码java设计,用Java编写一个ATM机的代码
标题链接:http://myzitong.com/article/hsehcc.html