import java.io.IOException;

成都創(chuàng)新互聯(lián)長(zhǎng)期為上1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為寶山企業(yè)提供專業(yè)的網(wǎng)站制作、網(wǎng)站設(shè)計(jì),寶山網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
/**
* 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);
}
}
}
參考了別人的代碼。略作修改,已經(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)輸入給定的卡號(hào)和密碼(初始卡號(hào)和密碼為123456)時(shí),系統(tǒng)能登錄ATM柜員機(jī)系統(tǒng),用戶可以按照以下規(guī)則進(jìn)行:
1、查詢余額:初始余額為10000元
2、ATM取款:每次取款金額為100的倍數(shù),總額不超過(guò)5000元,支取金額不允許透支。
3、ATM存款:不能出現(xiàn)負(fù)存款。
4、修改密碼:新密碼長(zhǎng)度不小于6位,不允許出現(xiàn)6位完全相同的情況,只有舊密碼正確,新密碼符合要求,且兩次輸入相同的情況下才可以成功修改密碼。
(卡號(hào)密碼余額放到文件中)
*/
public?class?Test?{
private?static?int?account;
private?static?int?password;
private?static?int?money;
private?static?boolean?isLogin;
static{
account?=?123456;
password?=?123456;
money?=?10000;
isLogin?=?false;
}
//存款
public?void?cun(int?cunKuan){
if(cunKuan=0){
this.money?+=?cunKuan;
}else{
System.out.println("存款不能為負(fù)!");
}
}
//取款
public?void?qu(int?quKuan){
if(this.money?-?quKuan??0){
System.out.println("余額不足!");
return;
}
if(isValid(quKuan)){
this.money?-=?quKuan;
}else{
System.out.println("取款不能為負(fù),且應(yīng)為100的倍數(shù)!");
}
}
//判斷是否為有效的金額
private?boolean?isValid(int?money){
if(money?=?0??money?%?100?==?0){
return?true;
}
return?false;
}
//登陸
public?void?login(){
System.out.println("請(qǐng)輸入賬號(hào)和密碼【格式為:賬號(hào)/密碼】");
String?login?=?new?Scanner(System.in).next();
if(login.equalsIgnoreCase("123456/123456")){
this.isLogin?=?true;
}else{
System.out.println("賬號(hào)或者密碼錯(cuò)誤,請(qǐng)重新輸入!");
login();
}
}
//主菜單
public?void?show(){
System.out.println("[1]存款");
System.out.println("[2]取款");
System.out.println("[3]退出");
System.out.println("請(qǐng)輸入:");
int?key?=?new?Scanner(System.in).nextInt();
switch?(key)?{
case?1:
cun(new?Scanner(System.in).nextInt());
break;
case?2:
qu(new?Scanner(System.in).nextInt());
break;
case?3:
System.exit(0);
default:
break;
}
}
public?static?void?main(String[]?args)?{
Test?t?=?new?Test();
t.login();
if(t.isLogin){
for(;;){
t.show();
System.out.println("您當(dāng)前的余額為:"?+?t.money);
}
}
}
}
新聞名稱:java代碼模擬atm機(jī) 用java編寫atm機(jī)程序
路徑分享:http://chinadenli.net/article30/ddocoso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、小程序開(kāi)發(fā)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站導(dǎo)航、靜態(tài)網(wǎng)站、自適應(yīng)網(wǎng)站
聲明:本網(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)