Untitled
unknown
java
2 years ago
3.5 kB
13
Indexable
package com.citibanamex;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class Atm {
/*
* atm class: 2 instance attributes balance serialnumber
*
* 1 constructor
*
* encapsulate attributes
*
* getter and setter:
*
* a method that accepts deposits and adds to the balance deposit() it there
* is a neg number it throws an exception
*
* a method to withdraw and subtracts from the balance withdraw() if the
* withdraw is higher that the balance, it throws an exception
*
* Optional: Add a log of the deposits and withdraws with date and name
* private ArrayList<String> log= new ArrayList<>();
*
*
*
*/
private double balance;
private int serialNumber;
private ArrayList<String> log;
static private int counter;
Atm(double balance) {
this.balance = balance;
this.serialNumber = counter++;
this.log = new ArrayList<>();
}
public double getBalance() {
return this.balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getSerialNumber() {
return this.serialNumber;
}
public void setSerialNumber(int serialNumber) {
this.serialNumber = serialNumber;
}
void addLog(String entryData) {
this.log.add(entryData);
}
public String getLog() {
String logs = "";
for (String alog : this.log) {
logs += alog + "\n";
}
return logs;
}
String getCurrentDate() {
Date date = Calendar.getInstance().getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd hh:mm:ss");
String currentDate = dateFormat.format(date);
return currentDate;
}
void deposit(double deposit) {
if (deposit <= 0)
throw new ArithmeticException("This deposit is not allowed");
setBalance(getBalance() + deposit);
addLog(stringOperation("Deposit", deposit));
}
void withdraw(double askedCash) {
if (askedCash >= this.balance)
throw new ArithmeticException("Cash asked is higher than your balance");
setBalance(getBalance() - askedCash);
addLog(stringOperation("Withdraw", askedCash));
}
String stringOperation(String opt, double amount) {
return getCurrentDate() + " " + opt + " of $" + amount;
}
}
package com.citibanamex;
public class AtmTest {
public static void main(String[] args) {
Atm atm1 = new Atm(7500.0);
// test of deposit method
testOfDeposit(atm1, 0.0);
// test of withdraw method
testOfWithdraw(atm1, 8000.0);
// getting the log:
atm1.deposit(1500.0);
atm1.withdraw(300.0);
atm1.deposit(3000.0);
atm1.withdraw(100.0);
System.out.println("log:\n" + atm1.getLog());
}
static void testOfDeposit(Atm objectAtm, double test) {
try {
System.out.println("Trying to make a deposit of $0.0");
objectAtm.deposit(test);
} catch (ArithmeticException e) {
e.printStackTrace();
} finally {
System.out.println("Balance: $" + objectAtm.getBalance());
}
}
static void testOfWithdraw(Atm objectAtm, double test) {
try {
System.out.println("Trying to withdraw $8000.00");
objectAtm.withdraw(test);
} catch (ArithmeticException e) {
e.printStackTrace();
} finally {
System.out.println("Balance: $" + objectAtm.getBalance());
}
}
}
Editor is loading...