Java分配问题切换

时间:2019-03-11 00:49:42

标签: java

我有一个必须创建ATM的任务。该程序正在运行,但是好像出现了问题,因为我的余额连续读$ 100。我不确定代码在哪里出错。我的if语句或开关中可能有问题,但是我不确定自己在做什么错。我已经看了几个小时的这段代码,但我似乎只是想念该错误。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ATMDemo {
    public static void main(String[] args) throws IOException {
        Account[] accounts = new Account[10];

        for(int i=0; i<10; i++) {
            accounts[i] = new Account(100,5);
        }

        System.out.println("Enter Id (0-9)");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int id = Integer.parseInt(br.readLine());

        while(!(id>=0 && id <10)) {
            System.out.println("Wrong Id, enter again");
            id = Integer.parseInt(br.readLine());
        }

        printMenu();
        int c = Integer.parseInt(br.readLine());
        while (c!=4) {
            switch (c) {
                case 1:
                    System.out.println("Balance : " + accounts[id].getBalance());
                    break;
                case 2:
                    System.out.println("Enter amount");
                    double amt = Double.parseDouble(br.readLine());
                    accounts[id].withdraw(amt);
                    break;
                case 3:
                    System.out.println("Enter amount");
                    double amt1 = Double.parseDouble(br.readLine());
                    accounts[id].deposit(amt1);
                    break;
            }
        }

        if (c==4) {
            System.out.println("Program exited!!!");
        }
    }

    public static void printMenu() {
        System.out.println("Enter a choice: \n " +
                "1) check balance,\n" +
                "2) withdraw,\n" +
                "3) deposit, and \n" +
                "4) exit");
    }
}

class Account {
    double balance;
    double transaction[][];
    int tCount;
    int size;

    public Account(double balance, int size) {
        this.balance = balance;
        this.transaction = new double[size][2];
        this.tCount = 0;
        this.size= size;
    }

    public void deposit(double amt) {
        if(amt>0) {
            transact(amt);
        } else {
            System.out.println("Amount should be > 0 to deposit");
        }
    }

    public void withdraw(double amt) {
        if(amt<=balance) {
            transact(amt*-1);
        } else {
            System.out.println("Insufficient Balance");
        }
    }

    public void print() {
        for(int i=0; i<tCount; i++) {
            System.out.println(transaction[i][0] +", "+ transaction[i][1]);
        }
        System.out.println("Current available balance :"+balance);
    }

    public int count() {
        return transaction.length;
    }

    public int count(double base) {
        int count = 0;
        for(int i=0; i<tCount; i++) {
            if(transaction[i][0]<0) {
                double amt = -1 * transaction[i][0];
                if(amt > base) {
                    count++;
                }
            }
        }
        return count;
    }

    public void transact(double val) {
        if(tCount+1>size)
            resize();

        balance = balance + val;
        transaction[tCount][0] = val;
        transaction[tCount][1] = balance;
        tCount++;
    }

    public void resize() {
        double[][] tNew = new double[this.size+10][2];
        for(int i=0; i<tCount; i++) {
            tNew[i] = transaction[i];
        }
        this.size = this.size + 10;
        this.transaction = tNew;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double[][] getTransaction() {
        return transaction;
    }

    public void setTransaction(double[][] transaction) {
        this.transaction = transaction;
    }
}

0 个答案:

没有答案