单用户ATM模拟程序:
import javax.swing.JOptionPane;
public class ATM {
public static int money =8000; //银行卡里面现有的金额为8000
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "欢迎光临,工商银行");
boolean land = denglu();{
if(land==false){
JOptionPane.showMessageDialog(null, "非法用户");
System.exit(0);
}
}
while(true){
String s =JOptionPane.showInputDialog(null,"1、存款"+"\n"+"2、取款"+"\n"+"3、查询余额"+"\n"+"4、取卡");
int item =Integer.parseInt(s);
switch (item) {
case 1:
cunkuan();
break;
case 2:
qukuan();
break;
case 3:
cxye();
break;
case 4:
JOptionPane.showMessageDialog(null, "谢谢使用");
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "非法输入,请输入1-4");
break;
}
}
}
/*
* 登陆框
* 登陆是否成功
* */
public static boolean denglu(){
for(int i=0;i<3;i++){
String card =JOptionPane.showInputDialog(null,"请输入卡号");
String psw = JOptionPane.showInputDialog(null,"请输入密码");
if(card.equals("123456")&& psw.equals("aaaaaa")){
return true;
}
}
return false;
}
/*
* 查询余额
* */
public static void cxye(){
JOptionPane.showMessageDialog(null, "账号余额:"+money);
}
/*
* 存款
* */
public static void cunkuan(){
String x = JOptionPane.showInputDialog(null,"请输入存入的金额:");
int m = Integer.parseInt(x);
money = money+m;
String str =JOptionPane.showInputDialog(null, "是否显示余额? y/n");
if(str.equals("y")){
cxye();
}
}
/*
* 取款
* */
public static void qukuan(){
String y =JOptionPane.showInputDialog(null,"请输入取款金额");
int s =Integer.parseInt(y);
if(money<s){
JOptionPane.showMessageDialog(null, "余额不足,请重新输入");
}
else{
money =money -s;
String str =JOptionPane.showInputDialog(null, "是否显示余额?y/n");
if(str.equals("y")){
cxye();
}
}
}
}
2、多用户ATM机模拟程序:
import javax.swing.JOptionPane;
public class DyhATM {
public static String[] code ={"123456","21323","23256","12313"};
public static String[] pwd ={"aaa","bbb","ccc","ddd"};
public static int[] money={5000,6000,4000,2000};
//记录用户登陆系统的下标
public static int c=-1;
public static void main(String[] aaa){
JOptionPane.showMessageDialog(null, "欢迎光临,工商银行");
denglu();
if(c==-1){
JOptionPane.showMessageDialog(null, "非法用户");
System.exit(0);
}
while(true){
String s =JOptionPane.showInputDialog(null,"1、存款"+"\n"+"2、取款"+"\n"+"3、查询余额"+"\n"+"4、取卡");
int item =Integer.parseInt(s);
switch (item) {
case 1:
cunkuan();
break;
case 2:
qukuan();
break;
case 3:
cxye();
break;
case 4:
JOptionPane.showMessageDialog(null, "谢谢使用");
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "非法输入,请输入1-4");
break;
}
}
}
//登陆界面
public static void denglu(){
for(int i=0;i<3;i++){
String a =JOptionPane.showInputDialog(null,"请输入卡号");
String b = JOptionPane.showInputDialog(null,"请输入密码");
for(int x=0;x<code.length;x++){
if(a.equals(code[x]) && b.equals(pwd[x])){
c=x;
return;
}
}
}
}
//查询界面
public static void cxye(){
JOptionPane.showMessageDialog(null, "账号余额:"+money[c]);
}
//存款界面
public static void cunkuan(){
String x = JOptionPane.showInputDialog(null,"请输入存入的金额:");
int m = Integer.parseInt(x);
money[c] = money[c]+m;
String str =JOptionPane.showInputDialog(null, "是否显示余额? y/n");
if(str.equals("y")){
cxye();
}
}
public static void qukuan(){
String y =JOptionPane.showInputDialog(null,"请输入取款金额");
int s =Integer.parseInt(y);
if(money[c]<s){
JOptionPane.showMessageDialog(null, "余额不足,请重新输入");
}
else{
money[c] =money[c] -s;
String str =JOptionPane.showInputDialog(null, "是否显示余额?y/n");
if(str.equals("y")){
cxye();
}
}
}
}