import java.awt.*;
員工經(jīng)過長期磨合與沉淀,具備了協(xié)作精神,得以通過團隊的力量開發(fā)出優(yōu)質(zhì)的產(chǎn)品。成都創(chuàng)新互聯(lián)公司堅持“專注、創(chuàng)新、易用”的產(chǎn)品理念,因為“專注所以專業(yè)、創(chuàng)新互聯(lián)網(wǎng)站所以易用所以簡單”。公司專注于為企業(yè)提供網(wǎng)站建設(shè)、做網(wǎng)站、微信公眾號開發(fā)、電商網(wǎng)站開發(fā),微信小程序定制開發(fā),軟件按需求定制開發(fā)等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。
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);
}
}
ok,稍等
呵呵,已經(jīng)給你拆分成了兩個獨立的類了。
我再吧注釋加起吧。
哪兒不清楚的可以給我留言嘛,剛開始學(xué)習(xí)的時候就要多看看別人寫的代碼,然后從中學(xué)習(xí)。這里寫的用到了簡單的封裝面向?qū)ο箪o態(tài)類,你可以在多了解下,不難的。
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)?{
//創(chuàng)建一個account的對象
Account?account?=?new?Account();
System.out.println("*******歡迎使用**********");
//循環(huán)操作提示
while?(true)?{
showMenu();//調(diào)用顯示菜單的方法
//得到用戶的輸入
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是取出的存款數(shù)
public?void?out(float?out)?{
if?(money??out)?{
System.out.println("賬戶余額不足");
}
this.money?-=?money;
}
//?存入,in是輸入的存款數(shù)
public?void?in(float?in)?{
this.money?+=?in;
}
}
package demo;
import java.io.*;
/*該類為實現(xiàn)客戶信息及部分功能*/
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;
}
/*得到剩余的錢的數(shù)目*/
protected void set_Balance(double mon) {
money -= mon;
}
/*得到剩余的錢的數(shù)目*/
protected void set_Deposit(double mon) {
money += mon;
}
}
/**********實現(xiàn)具體取款機功能*********/
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.密碼設(shè)置."+"\n"+
" 5.退出系統(tǒng)."+"\n");
}
/**********登陸系統(tǒng)**********/
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();
}
/**********系統(tǒng)操作**********/
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碼轉(zhuǎn)換的整數(shù)
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("請輸入您要取的數(shù)目:");
str=br.readLine();
str1=String.valueOf(act.get_Money());
System.out.println(str1);
if(Double.parseDouble(str)Double.parseDouble(str1)) {
count++;
System.out.println("超過已有的錢數(shù),請重新輸入您要取的數(shù)目:");
if(count=3){
System.out.println("超過已有的錢數(shù),請重新輸入您要取的數(shù)目:");
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("請輸入您要存的數(shù)目:");
str=br.readLine();
/*操作成功*/
act.set_Deposit(Double.parseDouble(str));
System.out.println("取款成功,請收好您的錢.");
Welcome();
SysOpter();
}while(true);
}
/**********判斷卡內(nèi)是否有錢**********/
protected boolean isBalance() {
if(act.get_Money()0) {
System.out.println("對不起,您的錢數(shù)不夠或卡已透支.");
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);
}
/**********結(jié)束系統(tǒng)**********/
protected void Exit_Sys() {
System.out.println("感謝您使用本系統(tǒng),歡迎下次在來,再見!");
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 默認(rèn)50000金額。簡單版本的存取款。
名稱欄目:atm代碼java設(shè)計,用Java編寫一個ATM機的代碼
標(biāo)題來源:http://jinyejixie.com/article22/hsehcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、域名注冊、服務(wù)器托管、企業(yè)建站、做網(wǎng)站、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)