java窗口小程序atm_简单的小程序实现ATM机操作

这是一个简单的Java程序,用于模拟ATM机操作,包括查看余额、取款和存款功能。用户可以通过输入ID选择账户并进行相应操作。
摘要由CSDN通过智能技术生成

简单的小程序实现ATM机操作

代码如下:

package Day06;

import java.util.Scanner;

public class TestAccount {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

Account[] accountArray = new Account[3];

for (int i = 0; i < accountArray.length; i++) {

accountArray[i] = new Account(i, 0.0);

}

boolean loopFlag = true;

while (loopFlag) {

loopFlag = printAccount(sc, accountArray);

}

}

/**

* 用来模拟ATM机打印的主方法

* @param sc

* @param accountArray

* @return

*/

private static boolean printAccount(Scanner sc, Account[] accountArray) {

boolean loopFlag = true;

System.out.println("Enter an id: ");

int id = -1;

if (sc.hasNextInt()) {

id = sc.nextInt();

}

int index = -1;

for (int i = 0; i < accountArray.length; i++) {

if (accountArray[i].getId() == id) {

index = i;

}

}

if (index >= 0) {

System.out.println("Main menu ");

System.out.println("1: check balance");

System.out.println("2: withdraw");

System.out.println("3: deposit");

System.out.println("4: exit");

System.out.print("Enter a choice:");

if (sc.hasNextInt()) {

id = sc.nextInt();

}

switch (id) {

case 1:

System.out.println("the balance is " + accountArray[index].getAccount());

break;

case 2:

accountArray[index].setAccount(accountArray[index].getAccount() - 100);

break;

case 3:

accountArray[index].setAccount(accountArray[index].getAccount() + 100);

break;

case 4:

loopFlag = false;

break;

default:

loopFlag = false;

break;

}

}

return loopFlag;

}

}

第二部分

package Day06;

public class Account {

private int id;

private double account;

/**

*

*/

public Account() {

}

/**

* @param id

* @param account

*/

public Account(int id, double account) {

this.id = id;

this.account = account;

}

/**

* @return the id

*/

public int getId() {

return this.id;

}

/**

* @param id the id to set

*/

public void setId(int id) {

this.id = id;

}

/**

* @return the account

*/

public double getAccount() {

return this.account;

}

/**

* @param account the account to set

*/

public void setAccount(double account) {

this.account = account;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值