Pages

Sunday, 18 September 2016

Chapter 10 Exercise 7, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

10.7 (Game: ATM machine) Use the Account class created in Programming Exercise 9.7 to simulate an ATM machine. Create ten accounts in an array with id 0, 1, . . . , 9, and initial balance $100. The system prompts the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct id. Once an id is accepted, the main menu is displayed as shown in the sample run. You can enter a choice 1 for viewing the current balance, 2 for withdrawing money, 3 for depositing money, and 4 for exiting the main menu. Once you exit, the system will prompt for an id again. Thus, once the system starts, it will not stop.

import java.util.ArrayList;
import java.util.Date;

public class Account {

    protected String mName;
    protected int mId;
    protected double mBalance;
    protected double mAnnualInterestRate; // AnnualInterestRate is percentage.
    protected Date mDateCreated;
    protected ArrayList<Transaction> mTransactions;


    public Account() {
        mDateCreated = new Date();
        mTransactions = new ArrayList<>();
    }

    public Account(int id, double balance) {
        this();
        mId = id;
        mBalance = balance;
    }

    public Account(String name, int id, double balance) {
        this(id, balance);
        mName = name;

    }

    public int getId() {
        return mId;
    }

    public void setId(int id) {
        mId = id;
    }

    public double getBalance() {
        return mBalance;
    }

    public void setBalance(double balance) {
        mBalance = balance;
    }

    public double getAnnualInterestRate() {
        return mAnnualInterestRate;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
        mAnnualInterestRate = annualInterestRate;
    }

    public Date getDateCreated() {
        return mDateCreated;
    }

    public double getMonthlyInterestRate() {
        return mBalance * (mAnnualInterestRate / 12) / 100;
    }

    public void withdraw(double amount) {
        mTransactions.add(new Transaction('W', amount, mBalance, "withdraw"));
        mBalance -= amount;
    }

    public void deposit(double amount) {
        mTransactions.add(new Transaction('D', amount, mBalance, "deposit"));
        mBalance += amount;
    }

    @Override
    public String toString() {
        return "Account name: " + mName + "\n" + "Interest rate: " + mAnnualInterestRate + "\n" + mTransactions;
    }

    public ArrayList<Transaction> getTransactions() {
        return new ArrayList<>(mTransactions);
    }

}

import java.util.Scanner;

public class Exercise_07 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Account[] accounts = new Account[10];

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

        int option = 0;
        int id = askForID(accounts);
        while (option != 4) {
            Account user = getAccount(accounts, id);
            System.out.print(
                    "Main menu\n" +
                            "1: check balance\n" +
                            "2: withdraw\n" +
                            "3: deposit\n" +
                            "4: exit\n" +
                            "Enter a choice: ");
            option = input.nextInt();
            switch (option) {
                case 1:
                    System.out.println("The balance is $" + user.getBalance() + ".\n");
                    break;
                case 2:
                    System.out.print("Enter an amount to withdraw: ");
                    user.withdraw(input.nextDouble());
                    break;
                case 3:
                    System.out.print("Enter an amount to deposit: ");
                    user.deposit(input.nextDouble());
                    break;
                case 4:
                    System.out.println("Logged out...");
                    System.out.println("");
                    id = askForID(accounts);
                    option = 0;
                    break;
                    default:
                        System.out.println("Invalid input.. Enter a number 1-4");
            }

        }

    }

    public static int askForID(Account[] accounts) {

        Scanner input = new Scanner(System.in);

        int id = 0;
        boolean isValidID = false;

        while (!isValidID) {
            System.out.print("Enter an ID: ");
            id = input.nextInt();

            if (!hasID(accounts, id)) {
                System.out.println("INVALID ID: Try again.");

            } else {
                isValidID = true;
            }
        }

        return id;
    }

    public static Account getAccount(Account[] accounts, int id) {

        for (Account account : accounts) {
            if (id == account.getId()) {
                return account;
            }
        }
        return null;
    }
    public static boolean hasID(Account[] accounts, int id) {

        for (Account account : accounts) {
            if (id == account.getId()) {
                return true;
            }
        }

        return false;
    }


}

3 comments:

  1. Hey Man!! It shows an error-"cannot find symbol-class transaction"....
    It is on line 9 of the 1st part of the program.
    Please check it out! Thanks!

    ReplyDelete
    Replies
    1. Hi, I just want to share my experience with everyone. I have being hearing about this blank ATM card for a while and i never really paid any interest to it because of my doubts. Until one day i discovered a hacking Mr Frank. he is really good at what he is doing. Back to the point, I inquired about The Blank ATM Card. If it works or even Exist. They told me Yes and that its a card programmed for only money withdraws without being noticed and can also be used for free online purchases of any kind. This was shocking and i still had my doubts. Then i gave it a try and asked for the card and agreed to their terms and conditions.. Four days later I received my card and tried with the closest ATM machine close to me, to my greatest surprise It worked like magic. I was able to withdraw up to $5,000 daily. ATM has really change my life. If you want to contact them, Here is the email : mrfrankblankatmcard@gmail.com

      Delete