java银行卡存取款系统
㈠ java模拟银行ATM完成以下功能: 1)查询余额 2)取款 3)存款 4)退出
import java.util.Scanner;
public class ATM{
public static void main(String... args){
Scanner sc=new Scanner(System.in);
double money=0;
do{
System.out.println("请选择业务:1)查询余额 2)取款 3)存款 4)退出");
int c=sc.nextInt();
if(c==1){
System.out.println("用户余额为:"+money+"元");
}else if(c==2){
while(true){
System.out.println("请输入取款金额:");
double load=sc.nextDouble();
if(load>=money){
System.out.println("账户余额不足,请重新输入取款金额");
continue;
}else{
money-=load;
System.out.println("取款成功");
break;
}
}
}else if(c==3){
System.out.println("请输入存款金额:");
money+=sc.nextDouble();
System.out.println("存款成功");
}else if(c==4){
System.exit(0);
}else{
System.out.println("输入错误,请重新选择业务");
}
}while(true);
}
}
㈡ JAVA 简单银行系统的代码
这个不简单,
从分析设计,到编码实现,
都是需要精力和时间花费的体现,
建议还是自己亲自走一遍吧,
能学点东西。
㈢ 如何用Java编写模拟ATM取款机的程序
import java.io.IOException;
/**
* ATM机类
*
* 查看余额
*
* 取款
*
* 存款
*
* 退出系统
*
*
*
*/
public class ATM {
static double yue = 1200.00;
public static void main(String[] arg) {
ATM localTest1 = new ATM();
localTest1.ATM_Operate();
}
/**
* ATM机的操作
*/
private void ATM_Operate() {
System.out.println("欢迎使用中国工商银行ATM取款机");
System.out.println("1、查看余额 2、取款");
System.out.println("3、存款 0、退出");
System.out.print("请输入您需要的服务:");
byte[] buffer = new byte[512];
try {
int count = System.in.read(buffer);// 返回实际读取的字节数
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("您已经成功退出系统,谢谢你的使用");
System.exit(0);
} else {
System.out.println("输入不合法,请重新输入");
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("请输入您要取出的金额:¥");
int count2 = System.in.read(buffer);// 返回实际读取的字节数
System.out.print("您输入的金额是:");
for (int i = 0; i < count2 - 1; i++) {
System.out.print("" + (char) buffer[i]);
}
System.out.println();
// 字符0 ~ 9对应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 {
// 输入的字符不是数值
System.out.println("输入不合法,请重新输入");
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(",现在余额¥:" + yue);
}
}
/**
* 存款
*
* @throws IOException
*/
private void deposit() throws IOException {
byte[] buffer = new byte[512];
System.out.print("请输入您要存入的金额:¥");
int count2 = System.in.read(buffer);// 返回实际读取的字节数
System.out.print("您输入的金额是:");
for (int i = 0; i < count2 - 1; i++) {
System.out.print("" + (char) buffer[i]);
}
System.out.println();
// 字符0 ~ 9对应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 {
// 输入的字符不是数值
System.out.println("输入不合法,请重新输入");
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(",现在余额¥:" + yue);
}
}
}