/** * @author admin * 該程序的功能為實(shí)現(xiàn)模擬銀行ATM自動(dòng)取款機(jī)提款,查詢等功能. */ import Java.io.*; /*該類為實(shí)現(xiàn)客戶信息及部分功能*/ class Account { private String code =null; //信用卡號(hào) 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í)現(xiàn)具體取款機(jī)功能*********/ 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模擬自動(dòng)取款機(jī)程序.\n" str "\n"); System.out.print(" 1.取款." "\n" " 2.查詢信息." "\n" " 3.密碼設(shè)置." "\n" " 4.退出系統(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("請(qǐng)輸入您的信用卡號(hào):"); card=br.readLine(); System.out.println("請(qǐng)輸入您的密碼:"); pwd=br.readLine(); if(!isRight(card,pwd)) { System.out.println("您的卡號(hào)或密碼輸入有誤."); counter ; } else SysOpter(); }while(counter3); Lock_Sys(); } 回復(fù)獲取全部
為寧國(guó)等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及寧國(guó)網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、寧國(guó)網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
很簡(jiǎn)單的例子,我把代碼貼出來(lái)吧
import java.util.Scanner;
public class ATM {
/**
* @param args
*/
public static void main(String[] args) {
Scanner in = null;
int result;
double drawMoney;
double depositMoney;
Account account = new Account();
while (true) {
System.out.println("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆(ABC)中國(guó)農(nóng)業(yè)銀行ATM自動(dòng)存取款機(jī)☆☆☆☆☆☆☆☆☆☆☆☆☆");
System.out.println("\n\t\t\t\t1.存款業(yè)務(wù)");
System.out.println("\n\t\t\t\t2.取款業(yè)務(wù)");
System.out.println("\n\t\t\t\t3.查詢余額");
System.out.println("\n\t\t\t\t4.退出ATM系統(tǒng)");
System.out.println("\n☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆中國(guó)農(nóng)業(yè)銀行歡迎您的使用☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
in = new Scanner(System.in);
switch (in.nextInt()) {
case 1:
System.out.println("請(qǐng)輸入您的存款數(shù)額!");
depositMoney = in.nextDouble();
if(depositMoney 0)
System.out.println("您的輸入無(wú)效,請(qǐng)重新輸入!");
result = account.deposit(depositMoney);
if(result == 0){
System.out.println("存款業(yè)務(wù)完成,是否顯示余額?Y/N");
if("Y".equalsIgnoreCase(in.next())){
System.out.println("您當(dāng)前的余額為:"+account.checkCurrent());
}else {
break;
}
}else {
System.out.println("無(wú)法完成交易!");
break;
}
break;
case 2:
System.out.println("請(qǐng)輸入您的取款數(shù)額!");
drawMoney = in.nextDouble();
if (drawMoney 0) {
System.out.println("您的輸入無(wú)效,請(qǐng)重新輸入!");
}
result = account.withDraw(drawMoney);
if (result == 0) {
System.out.println("存款業(yè)務(wù)完成,是否顯示余額?Y/N");
if("Y".equalsIgnoreCase(in.next())){
System.out.println("您當(dāng)前的余額為:"+account.checkCurrent());
}else {
break;
}
} else {
System.out.println("無(wú)法完成交易!");
break;
}
case 3:
System.out.println("您當(dāng)前的余額為:"+account.checkCurrent());
break;
case 4:
break;
default:
break;
}
}
}
}
public class Account {
private double currentMoney;//當(dāng)前余額
public double getCurrentMoney() {
return currentMoney;
}
public void setCurrentMoney(double currentMoney) {
if (currentMoney 0) {
this.currentMoney = currentMoney;
}
}
/**
* 取款業(yè)務(wù)
* @param drawMoney
* @return 0代表成功,1代表失敗
*/
public int withDraw(double drawMoney) {
if (currentMoney 0 drawMoney = currentMoney) {
currentMoney -= drawMoney;
} else {
System.out.println("您的余額已不足!");
return 1;
}
return 0;
}
/**
* 存款業(yè)務(wù)
* @param depositMoney
* @return 0代表成功,1代表失敗
*/
public int deposit(double depositMoney) {
if (depositMoney 0) {
currentMoney += depositMoney;
return 0;
}else {
System.out.println("您提交的存款為負(fù)數(shù),無(wú)法完成存款交易");
return -1;
}
}
/**
* 查詢余額業(yè)務(wù)
* @return
*/
public double checkCurrent() {
return currentMoney;
}
}
import java.io.IOException;
/**
* ATM機(jī)類
*
* 查看余額
*
* 取款
*
* 存款
*
* 退出系統(tǒng)
*
*
*
*/
public class ATM {
static double yue = 1200.00;
public static void main(String[] arg) {
ATM localTest1 = new ATM();
localTest1.ATM_Operate();
}
/**
* ATM機(jī)的操作
*/
private void ATM_Operate() {
System.out.println("歡迎使用中國(guó)工商銀行ATM取款機(jī)");
System.out.println("1、查看余額 2、取款");
System.out.println("3、存款 0、退出");
System.out.print("請(qǐng)輸入您需要的服務(wù):");
byte[] buffer = new byte[512];
try {
int count = System.in.read(buffer);// 返回實(shí)際讀取的字節(jié)數(shù)
System.out.print("您輸入的是:");
for (int i = 0; i count; i++) {
System.out.print("" + (char) buffer[i]);
}
if ((char) buffer[0] == '1') {
// 查看余額
System.out.println("您的余額是:¥" + yue + "元");
System.out.println();
ATM_Operate();
} else if ((char) buffer[0] == '2') {
// 取款
withdrawal();
System.out.println();
ATM_Operate();
} else if ((char) buffer[0] == '3') {
// 存款
deposit();
System.out.println();
ATM_Operate();
} else if ((char) buffer[0] == '0') {
// 退出
System.out.println("您已經(jīng)成功退出系統(tǒng),謝謝你的使用");
System.exit(0);
} else {
System.out.println("輸入不合法,請(qǐng)重新輸入");
System.out.println();
ATM_Operate();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 取款
*
* @throws IOException
*/
private void withdrawal() throws IOException {
byte[] buffer = new byte[512];
System.out.print("請(qǐng)輸入您要取出的金額:¥");
int count2 = System.in.read(buffer);// 返回實(shí)際讀取的字節(jié)數(shù)
System.out.print("您輸入的金額是:");
for (int i = 0; i count2 - 1; i++) {
System.out.print("" + (char) buffer[i]);
}
System.out.println();
// 字符0 ~ 9對(duì)應(yīng)ASCII值48 ~ 57
boolean flag = false;
for (int i = 0; i count2 - 1; i++) {
if ((char) buffer[i] 47 (char) buffer[i] 58) {
if (i == count2 - 2) {
flag = true;
}
} else {
// 輸入的字符不是數(shù)值
System.out.println("輸入不合法,請(qǐng)重新輸入");
withdrawal();
break;
}
}
System.out.println();
if (flag) {
System.out.print("您已成功取出¥:");
String num = "";
for (int i = 0; i count2 - 1; i++) {
System.out.print("" + (char) buffer[i]);
num += (char) buffer[i];
}
yue -= Double.valueOf(num);
System.out.print(",現(xiàn)在余額¥:" + yue);
}
}
/**
* 存款
*
* @throws IOException
*/
private void deposit() throws IOException {
byte[] buffer = new byte[512];
System.out.print("請(qǐng)輸入您要存入的金額:¥");
int count2 = System.in.read(buffer);// 返回實(shí)際讀取的字節(jié)數(shù)
System.out.print("您輸入的金額是:");
for (int i = 0; i count2 - 1; i++) {
System.out.print("" + (char) buffer[i]);
}
System.out.println();
// 字符0 ~ 9對(duì)應(yīng)ASCII值48 ~ 57
boolean flag = false;
for (int i = 0; i count2 - 1; i++) {
if ((char) buffer[i] 47 (char) buffer[i] 58) {
if (i == count2 - 2) {
flag = true;
}
} else {
// 輸入的字符不是數(shù)值
System.out.println("輸入不合法,請(qǐng)重新輸入");
withdrawal();
break;
}
}
System.out.println();
if (flag) {
System.out.print("您已成功存入¥:");
String num = "";
for (int i = 0; i count2 - 1; i++) {
System.out.print("" + (char) buffer[i]);
num += (char) buffer[i];
}
yue += Double.valueOf(num);
System.out.print(",現(xiàn)在余額¥:" + yue);
}
}
}
Java編寫(xiě)的模擬ATM取款機(jī)程序/*** @version 1.0
* @author Devil_Angel
* 該程序的功能為實(shí)現(xiàn)模擬銀行ATM自動(dòng)取款機(jī)提款,查詢等功能.
*
*/import java.io.*;/*該類為實(shí)現(xiàn)客戶信息及部分功能*/
class Account {
private String code =null; //信用卡號(hào)
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í)現(xiàn)具體取款機(jī)功能*********/
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模擬自動(dòng)取款機(jī)程序.\n"+str+"\n");
System.out.print(" 1.取款."+"\n"+
" 2.查詢信息."+"\n"+
" 3.密碼設(shè)置."+"\n"+
" 4.退出系統(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("請(qǐng)輸入您的信用卡號(hào):");
card=br.readLine();
System.out.println("請(qǐng)輸入您的密碼:");
pwd=br.readLine();
if(!isRight(card,pwd))
{
System.out.println("您的卡號(hào)或密碼輸入有誤.");
counter++;
}
else
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("請(qǐng)選擇您要操作的項(xiàng)目(1-4):");
num=br.read(); //num為ASICC碼轉(zhuǎn)換的整數(shù)
switch(num) {
case 49: BetBalance(); break;
case 50: Inqu_Info(); break;
case 51: Set_Password(); break;
case 52: Exit_Sys(); break;
}
System.exit(1);
}
/**********信息查詢**********/
protected void Inqu_Info() {
System.out.print("---------------------\n"+
act.get_Code()+"\n"+
act.get_Name()+"\n"+
act.get_Money()+"\n"+
"-----------------------");
}
/**********取款**********/
public void BetBalance() throws Exception
{
String str=null,str1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do {
System.out.println("請(qǐng)輸入您要取的數(shù)目:");
str=br.readLine();
str1=String.valueOf(act.get_Money());
if(str.compareTo(str1)0) {
System.out.println("超過(guò)已有的錢數(shù),請(qǐng)重新輸入您要取的數(shù)目:");
}
else {
/*操作成功*/
// act.set_Balance(str);
System.out.println("取款成功,請(qǐng)收好您的錢.");
Welcome();
SysOpter();
}
}while(true);
}
/**********判斷卡內(nèi)是否有錢**********/
protected boolean isBalance() {
if(act.get_Money()0) {
// System.out.println("對(duì)不起,您的錢數(shù)不夠或卡已透支.");
return false;
}
return true;
}
/********卡號(hào)密碼是否正確******/
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("請(qǐng)輸入舊密碼:");
pwd=br.readLine();
if(act.get_Password().equals(pwd))
{
do {
System.out.println("請(qǐng)輸入新密碼:");
String pwd1=br.readLine();
System.out.println("請(qǐng)?jiān)俅屋斎胄旅艽a:");
String pwd2=br.readLine();
if(!pwd1.equals(pwd2))
{
System.out.println("兩次輸入不一致,請(qǐng)?jiān)俅屋斎?");
}
else
{
System.out.println("密碼修改成功,請(qǐng)使用新密碼.");
Welcome();
SysOpter();
}
}while(true);
}
}while(counter3);
}
/**********鎖定機(jī)器**********/
protected void Lock_Sys() {
System.out.println("對(duì)不起,您的操作有誤,卡已被沒(méi)收.");
System.exit(1);
}
/**********結(jié)束系統(tǒng)**********/
protected void Exit_Sys() {
System.out.println("感謝您使用本系統(tǒng),歡迎下次在來(lái),再見(jiàn)!");
System.exit(1);
}
}public class Text
{
public static void main(String[] args) throws Exception
{
ATM atm=new ATM();
atm.Welcome();
atm.Load_Sys();
// atm.Exit_Sys();
}
} //模擬ATM取款機(jī)工作流程 import java.util.Scanner;public class Atm {
public Atm() {
}
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int password = 0;
int count = 0;
int choice = 0;
int type = 0;
int input = 0;
int acount = 1000;
boolean exit = false;
int flag = 0;
do{
System.out.println("請(qǐng)輸入您的密碼:");
password = sc.nextInt();
count++;
}while(password != 12345 count3);
if(password == 12345){
//密碼正確繼續(xù)后面的操作。
do{
System.out.println("請(qǐng)選擇您的操作,1.查詢 2.取款");
choice = sc.nextInt();
switch(choice){
case 1:
do{
System.out.println("請(qǐng)選擇帳戶類型:1. 美元 2. 人民幣");
type = sc.nextInt();
if(type == 1){
System.out.println("You have $"+acount+"!");
}else if(type == 2){
System.out.println("您有"+acount+"圓!");
}else{
System.out.println("類型選擇錯(cuò)誤,請(qǐng)重新選擇!");
}
System.out.println("1.繼續(xù) 2.離開(kāi)");
flag = sc.nextInt();
if(flag == 1){
exit = false;
}else{
exit = true;
}
}while(type!=1 type!=2);
break;
case 2: do{
System.out.println("請(qǐng)選擇帳戶類型:1. 美元 2. 人民幣");
if(type == 1){
System.out.println("Please input number of your money!");
input = sc.nextInt();
if(input acount){
System.out.println("You have not enough money!");
}else{
System.out.println("You take care of your money!");
}
System.out.println("1.continue 2.exit");
flag = sc.nextInt();
if(flag == 1){
exit = false;
}else{
exit = true;
}
}else if(type == 2){
System.out.println("請(qǐng)輸入您要取的金額!");
input = sc.nextInt();
if(input acount){
System.out.println("您的余額不足!");
}else{
System.out.println("請(qǐng)妥善保管您的錢!");
acount = acount - input;
}
System.out.println("1.繼續(xù) 2.離開(kāi)");
flag = sc.nextInt();
if(flag == 1){
exit = false;
}else{
exit = true;
}
}else{
System.out.println("類型選擇錯(cuò)誤,請(qǐng)重新選擇!");
}
}while(type!=1 type!=2);
break;
default: System.out.println("類型選擇錯(cuò)誤,請(qǐng)重新選擇!");
}
}while(choice!=1 choice!=2 || exit == false);
}else{
//密碼錯(cuò)誤,退出。
System.out.println("三次密碼錯(cuò)誤,吞卡!");
}
}
}
參考了別人的代碼。略作修改,已經(jīng)很簡(jiǎn)單了:
InfoATM.java:
public?class?InfoATM?{
double?money?=?0;
public?InfoATM(double?cash)?{
super();
this.money?=?cash;
}
//?存款的方法
public?void?save(double?count)?{
money?+=?count;
}
//?取款的方法
public?void?draw(double?count)?{
money?-=?count;
}
public?double?getMoney()?{
return?money;
}
public?void?setMoney(double?money)?{
this.money?=?money;
}
}
TestATM.java:
import?java.awt.BorderLayout;
import?java.awt.GridLayout;
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;
public?class?TestATM?extends?JFrame?{
private?static?final?long?serialVersionUID?=?2531222181184935595L;
//?主面板pnBasic是用來(lái)裝pnDate和標(biāo)簽文字的。
private?JPanel?pnBasic;
//?添加到主面板中的中間?pnDate面板是為了裝表單的。
private?JPanel?pnDate;
//?添加到主面板中的北邊?pnLabel面板是為了裝歡迎詞的
private?JPanel?pnLabel;
InfoATM?atm?=?new?InfoATM(0);
public?TestATM()?{
pnBasic?=?new?JPanel();
//?主面板pnBasic是用來(lái)裝pnDate和標(biāo)簽文字的。
pnDate?=?new?JPanel(new?GridLayout(2,?2));
//?pnDate面板是為了裝表單的。
pnLabel?=?new?JPanel();
JLabel?top?=?new?JLabel("歡迎來(lái)到中國(guó)銀行!");
pnLabel.add(top);
//?先將數(shù)值添加在一個(gè)容器中并設(shè)置其在容器的右邊,在將容器添加在網(wǎng)格的第一格
JPanel?jp1?=?new?JPanel();
JLabel?number?=?new?JLabel("數(shù)值:");
final?JTextField?box?=?new?JTextField(5);
jp1.add(number);
jp1.add(box);
JPanel?jp2?=?new?JPanel();
JButton?create?=?new?JButton("新建銀行賬戶");
jp2.add(create);
JButton?take?=?new?JButton("取款");
JButton?in?=?new?JButton("存款");
pnDate.add(jp1);
pnDate.add(jp2);
pnDate.add(take);
pnDate.add(in);
//?加一句下面的就好了
JPanel?jpS?=?new?JPanel();
final?JLabel?total?=?new?JLabel("您現(xiàn)在的賬戶余額是:0?元");
jpS.add(total);
pnBasic.setLayout(new?BorderLayout());
pnBasic.add(pnLabel,?BorderLayout.NORTH);
pnBasic.add(pnDate,?BorderLayout.CENTER);
pnBasic.add(jpS,?BorderLayout.SOUTH);
setContentPane(pnBasic);
setBounds(400,?250,?500,?500);
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
pack();
in.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
if?(box.getText()?!=?null??box.getText()?!=?"")?{
try?{
double?count?=?Double.parseDouble(box.getText());
if?(count??0)?{
atm.save(count);
total.setText("您現(xiàn)在的賬戶余額是:"?+?atm.getMoney()?+?"元");
box.setText("");
}
}?catch?(Exception?e1)?{
System.out.println("您輸入的數(shù)值必須是數(shù)字");
}
}
}
});
take.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
if?(box.getText()?!=?null??box.getText()?!=?"")?{
try?{
double?count?=?Double.parseDouble(box.getText());
if?(count?=?0??count?=?atm.getMoney())?{
atm.draw(count);
total.setText("您現(xiàn)在的賬戶余額是:"?+?atm.getMoney()?+?"元");
box.setText("");
}?else?{
System.out.println("你的余額不足,取款失敗");
}
}?catch?(Exception?e1)?{
System.out.println("您輸入的數(shù)值必須是數(shù)字");
}
}
}
});
create.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
total.setText("您現(xiàn)在的賬戶余額是:0元");
atm.setMoney(0);
box.setText("");
}
});
}
public?static?void?main(String[]?args)?{
new?TestATM();
}
}
當(dāng)前題目:自助取款機(jī)的代碼JAVA 自動(dòng)取款機(jī)代碼
當(dāng)前鏈接:http://m.kartarina.com/article18/dodspgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站排名、網(wǎng)站導(dǎo)航、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)