如何用Java编写模拟ATM取款机的程序

如何用Java编写模拟ATM取款机的程序,第1张

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 = 0i <counti++) {

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 = 0i <count2 - 1i++) {

System.out.print("" + (char) buffer[i])

}

System.out.println()

// 字符0 ~ 9对应ASCII值48 ~ 57

boolean flag = false

for (int i = 0i <count2 - 1i++) {

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 = 0i <count2 - 1i++) {

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 = 0i <count2 - 1i++) {

System.out.print("" + (char) buffer[i])

}

System.out.println()

// 字符0 ~ 9对应ASCII值48 ~ 57

boolean flag = false

for (int i = 0i <count2 - 1i++) {

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 = 0i <count2 - 1i++) {

System.out.print("" + (char) buffer[i])

num += (char) buffer[i]

}

yue += Double.valueOf(num)

System.out.print(",现在余额¥:" + yue)

}

}

}

我现写的: import java.util.Scannerpublic 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 = 0i <string.length() - 1i++) {

char char1 = string.charAt(i)

for (int j = i + 1j <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)

}

}

}

}

很简单的例子,我把代码贴出来吧

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)中国农业银行ATM自动存取款机☆☆☆☆☆☆☆☆☆☆☆☆☆")

System.out.println("\n\t\t\t\t1.存款业务")

System.out.println("\n\t\t\t\t2.取款业务")

System.out.println("\n\t\t\t\t3.查询余额")

System.out.println("\n\t\t\t\t4.退出ATM系统")

System.out.println("\n☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆中国农业银行欢迎您的使用☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆")

in = new Scanner(System.in)

switch (in.nextInt()) {

case 1:

System.out.println("请输入您的存款数额!")

depositMoney = in.nextDouble()

if(depositMoney <0)

System.out.println("您的输入无效,请重新输入!")

result = account.deposit(depositMoney)

if(result == 0){

System.out.println("存款业务完成,是否显示余额?Y/N")

if("Y".equalsIgnoreCase(in.next())){

System.out.println("您当前的余额为:"+account.checkCurrent())

}else {

break

}

}else {

System.out.println("无法完成交易!")

break

}

break

case 2:

System.out.println("请输入您的取款数额!")

drawMoney = in.nextDouble()

if (drawMoney <0) {

System.out.println("您的输入无效,请重新输入!")

}

result = account.withDraw(drawMoney)

if (result == 0) {

System.out.println("存款业务完成,是否显示余额?Y/N")

if("Y".equalsIgnoreCase(in.next())){

System.out.println("您当前的余额为:"+account.checkCurrent())

}else {

break

}

} else {

System.out.println("无法完成交易!")

break

}

case 3:

System.out.println("您当前的余额为:"+account.checkCurrent())

break

case 4:

break

default:

break

}

}

}

}

public class Account {

private double currentMoney//当前余额

public double getCurrentMoney() {

return currentMoney

}

public void setCurrentMoney(double currentMoney) {

if (currentMoney >0) {

this.currentMoney = currentMoney

}

}

/**

* 取款业务

* @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

}

/**

* 存款业务

* @param depositMoney

* @return 0代表成功,1代表失败

*/

public int deposit(double depositMoney) {

if (depositMoney >0) {

currentMoney += depositMoney

return 0

}else {

System.out.println("您提交的存款为负数,无法完成存款交易")

return -1

}

}

/**

* 查询余额业务

* @return

*/

public double checkCurrent() {

return currentMoney

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12012109.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存