Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Bank account access simulation: The bank account should have withdraw and dep

ID: 3716058 • Letter: 1

Question

1. Bank account access simulation:

The bank account should have withdraw and deposit methods (subtract and add 1 from

the balance respectively)

You should have many (in the thousands) threads representing account users who, upon

invocation, sleep for a random number of seconds, and then withdraw or deposit

There should be an associated GUI that allows the user to start the simulation and it should have a representation of the current amount in the bank account, continually

updating as the simulation proceeds.? ?

Make use of the code examples I have provided. Take note of how the synchronized keyword and the Thread.sleep() and Thread.join() methods are being used, you will need them.

Explanation / Answer

public static void main(String[] args) throws IOException {       

    Option option1 = new Option("Start");

    Option option2 = new Option("Deposit");

    Option option3 = new Option("Withdrawal");

    Option option4 = new Option("Balance");

    Option option5 = new Option("Exit");

    option1.addOption(option2);

    option1.addOption(option3);

    option1.addOption(option4);

    option1.addOption(option5);

    Option actualOption = option1;

    while (1 < 2) {

        System.out.println(actualOption.tellUsWhatWeCanDo());

        actualOption = actualOption.whereToGo();

    }

}

private List<Option> options = new ArrayList<>();

private String text;

public Option(String text) {

    this.text = text;

}

public void addOption(Option option) {

    getOptions().add(option);

}

/**

* @return the options

*/

public List<Option> getOptions() {

    return options;

}

/**

* @return the text

*/

public String getText() {

    return text;

}

public String tellUsWhatWeCanDo() {

    String ret = "------------------------ ";

    int count = 0;

    for (Option option : options) {

        count++;

        ret += count + ". " + option.getText() + " ";

    }

    ret += "------------------------ ";

    return ret;

}

public Option whereToGo() {

    while (1<2) {

        Scanner scan = new Scanner(System.in);

        int num = scan.nextInt();

        if ((num >= 0) && (num < options.size())){

            return options.get(num);

        } else {

            System.out.println("wrong number");

        }

    }

}