当前位置:文档之家› Java编写的ATM取款机程序

Java编写的ATM取款机程序

import java.util.Scanner;

public class ATM {

private static String theName = "admin";
private static String thePassword = "123456";

private static int balance = 10000;

public static void getBalance(){
System.out.println("当前余额:" + balance);
}

public static void drawMoney(Scanner sc){
int money = 0;
System.out.println("请输入取款金额:");
money = sc.nextInt();
String type = "";
if (balance > 0) {
if (balance >= money) {
if (money <= 5000) {
balance = balance - money;
type = "请在30秒内提取现金...\n剩余余额:"+balance;
} else if (money <= 0) {
type = "金额错误";
} else {
type = "超出最大限制金额";
}
} else {
type = "超出最大余额";
}
} else {
type = "余额不足";
}
System.out.println(type);
}

public static void bankMoney(Scanner sc){
int money = 0;
System.out.println("请输入存储金额:");
money = sc.nextInt();
String type = "";
if (money > 0) {
balance = balance + money;
type = "存储成功,现有余额:" + balance;
} else {
type = "存储金额不能为负";
}
System.out.println(type);
}

public static void updatePass(Scanner sc){
String oldPass = "";
String newPass1 = "";
String newPass2 = "";
while(true){
System.out.println("请输入原密码:");
oldPass = sc.next();
if (oldPass.equals(thePassword)) {
break;
} else {
System.out.println("密码错误,请重新输入");
}
}
while(true){
System.out.println("请输入新密码");
newPass1 = sc.next();
System.out.println("再次输入");
newPass2 = sc.next();
if (newPass1.equals(newPass2)) {
if (!isSame(newPass1)) {
thePassword = newPass1;
System.out.println("修改成功");
break;
} else {
System.out.println("所有字符不能相同,重新输入");
}
} else {
System.out.println("两次输入不一致,重新输入");
}
}
}

public static boolean isSame(String string){
boolean bool = false;
for (int i = 0; i < string.length() - 1; i++) {
char char1 = string.charAt(i);
for (int j = i + 1; j < string.length(); j++) {
char char2 = string.charAt(j);
if (char1 == char2) {
bool = true;
break;
}
}
}
return bool;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
String name = "";
String password = "";
System.out.println("请输入账号:");
name = sc.next();
System.out.println("请输入密码:");
password = sc.next();
if (name.equals(theName) && password.equals(thePassword)) {
break;
} else {
System.out.println("账号或密码错误,请重新输入!");
}
}
while(true){
int operate = 0;
System.out.println("请选择你要进行的操作:\n1、查询 2、取款 3

、存款 4、修改密码 0、退出");
operate = sc.nextInt();
if (0 == operate) {
System.out.println("谢谢使用!");
break;
} else if (1 == operate) {
getBalance();
} else if (2 == operate) {
drawMoney(sc);
} else if (3 == operate) {
bankMoney(sc);
} else if (4 == operate) {
updatePass(sc);
}
}

}
}

相关主题
文本预览
相关文档 最新文档