Page 1 of 1

Please also fix the atm class according to my other classes or something like this (format: name,id,dollars,cents): 2. A

Posted: Fri May 20, 2022 12:33 pm
by answerhappygod
Please also fix the atm class according to my other
classes
or something like this (format: name,id,dollars,cents):
2. Add to your ATM class the readUserID (IOHandlerInterface)
method , which allows I/O to be standard I/O (keyboard and console)
or dialog boxes,
and call readUserID from your ATM class's main method after step 1.
This code needs the following from the above link:
IOHandlerInterface.java, IOHandlerStandard.java, and
IOHandlerDialog.java -- put these in the same folder as your ATM
class. Run the program twice: see if it uses dialog boxes when the
constructor IOHandlerDialog is invoked, and standard I/O (keyboard
and console/terminal) when you make the following change: invoke
the constructor IOHandlerStandard instead of IOHandlerDialog.

3. Add to your ATM class an isValid method that receives 2
parameters (BankInterface bank, String id), and call it from the
ATM's main method after step 2. The isValid method should return a
boolean, true if an account with the given id is found in the given
bank, and false otherwise. After calling the isValid method from
ATM's main method, have the main method print (using put method of
IOHandlerInterface) the result of the validation. Run the program
with the constructor IOHandlerStandard invoked (see if standard I/O
is used) then see if dialog boxes are used when you make the
following change: invoke the constructor IOHandlerDialog instead of
IOHandlerStandard.
====
====
=====
=== bank interface
public interface BankInterface {
void addAccount(BankTest acc) ;
BankTest search(String id) ;
void deposit(String id, int Money);
void withdraw(String id, int Money) ;
}
========
========
public class Account {
// defining attributes or instance variables
private String name;
private String id;
protected Money balance = new Money(0, 0);
// attributes attributes like name , id and
balance
public Account(String nameInput, String idInput,
Money balanceInput) {
this.name = nameInput;
this.id = idInput;
this.balance = balanceInput;
}
public Account(Account other) {
this.name = other.name;
this.id = other.id;
this.balance = other.balance;
}
// calling a method deposit
// this methods adds money to account
public void deposit(Money other) {
if (other.compareTo(new Money(0, 0)) ==
0 || other.compareTo(new Money(0, 0)) == 1) {
this.balance =
this.balance.add(other);
}
}
// calling a method withdraw
// to withdraw money from account
public void withdraw(Money other) {
if (other.compareTo(this.balance) == 0
|| other.compareTo(this.balance) == -1) {
this.balance =
this.balance.subtract(other);
}
}
// calling a method transfer
// this method transfers money
public void transfer(Account other, Money cash)
{
this.balance =
this.balance.add(cash);
other.balance =
other.balance.subtract(cash);
}
// calling the to string method
public String toString() {
return "" + this.name + ", " + this.id
+ ", " + this.balance.toString();
}
// calling return method
public String returnID() {
return new String(id);
}
// boolean method
// checks equality between two attributes
public boolean equals(Account other) {
return this.name.equals(other.name)
&& this.id.equals(other.id) &&
this.balance.equals(other.balance);
}
}=====
public class Money {

private long totalCents;
// constructor
public Money(int dollars, int cents) {
this.totalCents = dollars * 100L +
cents;
}
// Constructor
public Money(long cents) {
this.totalCents = cents;
}
// getter method for dollars
public int getDollars() {
return (int) this.totalCents /
100;
}
//getter method for get cents
public int getCents() {
return (int) this.totalCents %
100;
}
// method to add money
public Money add (Money money) {
return new Money (this.totalCents +
money.totalCents);
}
//method to subtract money
public Money subtract (Money money) {
return new Money (this.totalCents -
money.totalCents);
}
// to string method to get dollars
public String toString() {
String result = "$" + this.getDollars()
+ ".";
if (this.getCents() < 10) {
result += "0";
}
result += this.getCents();
return result;
}
// method equals to compare the two money
attributes whether equal or not
public boolean equals (Money other) {
return (this.totalCents ==
other.totalCents);
}
// method to compare the value of two money
public int compareTo (Money other) {
return Long.compare(this.totalCents,
other.totalCents);
}
}
=====