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


    简单的小程序实现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;
    }


    }

    只相信苦尽甘来
  • 相关阅读:
    C#/.NET CTS和CLS:公共类型系统和公共语言规范
    MongoDB分布式集群架构(3种模式)
    Elastic Search 入门
    转:计算机视觉部分会议/期刊名称缩写
    神经网络模型的参数量和FLOPs的计算,torchstat,thop,循环神经网络RNN
    latex自己调配颜色
    latex图像和表格手动设置位置(清除自动寻找位置)
    转:图像泊松融合
    latex调整表格行高、列宽和表格整体的左右宽度
    转:目标检测评价标准mAP
  • 原文地址:https://www.cnblogs.com/F001li/p/7055854.html
Copyright © 2020-2023  润新知