In: Computer Science
I have an ATM Machine program. I'm not sure where to place the: public void io() { section so the program gives the following output:
Welcome to ATM
Press Enter to begin;
Enter amount to begin:
Enter amount to withdraw: 230
Cash dispensed as follows:
$100 Bills: 2
$20 Bills: 1
$10 Bills: 1
The progam consists of
Main.java
class Main {
public static void main(String[] args) {
ATM atm = new ATM();
atm.init();
atm.run();
System.out.println();
System.out.println("ATM Version 0.1.0");
System.out.println("Copyright (C) 2020 " /*+ CHASE_BANK*/);
System.out.println("Code by Quang Pham");
}
}
/* public void io() {
lcd.print("Welcome to ATM\n");
lcd.print("Press ENTER to begin: ");
cardReader.readCard();
//Get amount to withdraw
lcd.print("Enter amount to withdraw: ");
int amount = keypad.getAmount();
//TODO: Verify amount
//Withdraw the amount
int result = dispenser.getCash(amount);
if (result == -1) {
lcd.print("Cash dispense error%n");
}
else {
lcd.print("Cash dispense as follows:\n");
lcd.print(dispenser.num100s() + "\n");
lcd.print(dispenser.num20s() + "\n");
lcd.print(dispenser.num10s() + "\n");
}
}
}*/
System.out.println();
System.out.println("ATM Version 0.1.0");
System.out.println("Copyright (C) 2020 " /*+ CHASE_BANK*/);
System.out.println("Code by Quang Pham");
}
}
ATM.java
class ATM {
private LCD lcd;
private CardReader cardReader;
private Keypad keypad;
private CashDispenser cashDispenser;
public void init() {
System.out.println("ATM.init()");
lcd = new LCD();
lcd.init();
cardReader = new CardReader();
cardReader.init();
keypad = new Keypad();
keypad.init();
cashDispenser = new CashDispenser();
cashDispenser.init();
}
public void run() {
System.out.println("ATM.run()");
}
}
CardReader.java
class CardReader {
public void init() {
System.out.println("CardReader.init()");
}
}
CashDispenser.java
class CashDispenser {
private int num100s;
private int num20s;
private int num10s;
public void init() {
System.out.println("CashDispenser.init()");
}
public int getCash(int amount) {
num100s = amount / 100; //get number of $100 bills
amount = amount % 100; //get remaining amount
num20s = amount / 20; //get number of $20 bills
amount = amount % 20; //get remaining amoung
num10s = amount /10; //get number of $10 bills
amount = amount % 10; //none should be remaining
if (amount != 0) return -1;//error
return 0; //success
}
public String num100s() {
return "$100 Bills: " + num100s;
}
public String num20s() {
return "$20 Bills: " + num20s;
}
public String num10s() {
return "$10 Bills: " + num10s;
}
}
Keypad.java
import java.util.Scanner;
class Keypad {
public void init() {
System.out.println("Keypad.init()");
}
public int getAmount() {
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}
}
LCD.java:
class LCD {
public void init() {
System.out.println("LCD.init()");
}
public void print(String s) {
System.out.print(s);
}
}
User.java
class User {
private LCD lcd;
private CardReader cardReader;
private Keypad keypad;
private CashDispenser dispenser;
public User(LCD lcd, CardReader cardReader, Keypad keypad, CashDispenser dispenser) {
this.lcd = lcd;
this.cardReader = cardReader;
this.keypad = keypad;
this.dispenser = dispenser;
}
public void io() {
System.out.println("User.io()");
try {
Thread.sleep(5000);
}
catch (InterruptedException e) {
System.out.println(e);
}
}
}
The program is at: https://repl.it/@quangnvpham1/ATM-Machine#Main.java .
Any help with this problem is greatly appreciated. Yours truly, Quang Pham
Your code in ATM.java was modified to :
class ATM {
private LCD lcd;
private CardReader cardReader;
private Keypad keypad;
private CashDispenser cashDispenser;
public void init() {
System.out.println("ATM.init()");
lcd = new LCD();
lcd.init();
cardReader = new CardReader();
cardReader.init();
keypad = new Keypad();
keypad.init();
cashDispenser = new CashDispenser();
cashDispenser.init();
lcd.print("Welcome to ATM\n");
lcd.print("Press ENTER to begin: ");
//cardReader.readCard();
//Get amount to withdraw
lcd.print("Enter amount to withdraw: ");
int amount = keypad.getAmount();
//TODO: Verify amount
//Withdraw the amount
int result = cashDispenser.getCash(amount);
if (result == -1) {
lcd.print("Cash dispense error%n");
}
else {
lcd.print("Cash dispense as follows:\n");
lcd.print(cashDispenser.num100s() + "\n");
lcd.print(cashDispenser.num20s() + "\n");
lcd.print(cashDispenser.num10s() + "\n");
}
}
public void run() {
System.out.println("ATM.run()");
}
}
I saw that the main function was calling the atm.init() function in the ATM class so I pasted the io() function code in init() function of the ATM class. Wherever you had used dispenser object, I changed those to cashDispenser and commented the cardreader.readcard() call as no such function exists.
The output can be seen: