In this assignment you are required to develop a program using JAVA that simulat
ID: 3903898 • Letter: I
Question
In this assignment you are required to develop a program using JAVA that simulates an advanced Automated Teller Machine (ATM). The reason that this machine is an advanced machine is because it does more than just deposits, withdrawals, and balance inquiries. The additional functionality of this machine contains user control over the date on which the transaction occurred. It also has the ability to list transactions on a specific date. The program goes as follows: a) You will start out by asking the user about the maximum number of transactions (This is an integer number). Once the number is entered the program starts with a balance of $0.00. b) You will display the following menu: Enter your Transaction: 1. Deposit. 2. Withdrawal. 3. Balance Inquiry. 4. Display the largest change in balance (positive or negative) and the date on which it occurred. 5. Display All Transactions 6. Display All Transactions on a specific date. 7. Exit. Choice: 1. When the user enters a 1, the system should ask the user to input a real number representing the deposit (in dollars and cents in the format of x.y where x are the dollars and y are the cents). Once that is done, the system will ask the user for a date of the format MMDD to be considered as the date of the transaction. The format MMDD may also be MDD in the case of single digit months. Once both amount and date are entered, then the system will update the balance accordingly, as well as record the transaction as a deposit as well as record the date for that same transaction. Do not enter the year because it is assumed to be 2017. They system should check to see if the deposit amount entered is a real number greater than zero. Also, the system should check to see if the date is a valid date, otherwise the transaction shouldn't be recorded. Examples of invalid dates include 230 (February 29th, and 30th), or 1790 (the 90th day of the 17th month of the year). Then the system should redisplay the menu. 2. When the user enters a 2, the system should ask the user to input a real number representing the withdrawal (in dollars and cents in the format of x.y where x are the dollars and y are the cents). Once that is done, the system will ask the user for a date of the format MMDD to be considered as the date of the transaction. The format MMDD may also be MDD in the case of single digit months. Once both amount and date are entered, then the system will update the balance accordingly, as well as record the transaction as a withdrawal as well as record the date for that same transaction. Do not enter the year because it is assumed to be 2017. You'll need to print a warning message if the user is trying to withdraw money and the balance is $0.0, even before the user inputs the amount to withdraw. Also, you'll need to display an error message if the user is trying to withdraw an amount larger than what's in the balance. Also, here you'll need to validate the amount(>= 0.0) as well as the date. Then the system should redisplay the menu. 3. When the user enters a 3, the system will ask the user for a date of the format MMDD to be considered as the date of the transaction. The format MMDD may also be MDD in the case of single digit months. Once the date are entered, record the transaction as a balance inquiry as well as record the date for that same transaction. Do not enter the year because it is assumed to be 2017. Also, here you'll need to validate the date. Then the system should redisplay the menu. 4. When the user enters a 4, the system will ask the user for a date of the format MMDD to be considered as the date of the transaction. The format MMDD may also be MDD in the case of single digit months. Once the date are entered, record the transaction as a “largest change†as well as record the date for that same transaction. Do not enter the year because it is assumed to be 2017. Then the program should display the date and amount of the largest change in balance, not the largest deposit or withdrawal because you could have multiple deposits and multiple withdrawals on a specific date, therefore, you need to look at what the balance was before the first transaction on that date and what it was after the last transaction on the same date. The program should display the amount of change either positive or negative. Then the system should redisplay the menu. 5. When the user enters a 4. The user is asking to print all the transactions that occurred so far, as a result one of the following will happen. a. A message saying that sorry but there are no transactions on the date specified. This message should only be displayed if and only if there are no transactions on the date specified. OR b. A Listing of all the transactions as a neatly designed and annotated table of the following format: Date Transaction Amount ========== =========== ======== 06/21/2017 Deposit $ 120.78 02/04/2017 Withdrawal $ 10.23 ... ... ... 03/22/2017 largest change $ -20.45 01/11/2017 Balance Inquiry -------- 04/30/2017 Withdrawal $ 0.21 Then the system should redisplay the menu. 6. Repeat No. 5, except you must specify a date as input. Once the date is specified, then you just display all the transactions that happened on that date in the same format as No. 5. If there are no transactions on that date, then display a message indicating that. Then the system should redisplay the menu. 7. When the user enters a 7, then the system should exit. 8. If the user enters a number less than 1 or larger than 7, then an error message should be displayed indicating that an illegal choice was entered, try again. c. Your program should be able to handle any number of transactions in total (deposits, withdrawals, balance inquiries, and largest change). Also, when you print an error or a warning (e.g. illegal date), you need to print 5 stars ***** before the error and do not tab the error message making the 5 start the first thing on the line. d. IN THIS ASSIGNMENT, YOU ARE NOT TO SORT ANY DATA IN ANY WAY. Transactions should always be store and displayed in the order in which they occur. ----- Implementation requirements: 1. YOU MUST IMPLEMENT A CLASS CALLED ATM THAT HAS ALL THE FUNCTIONALITY GIVEN ABOVE. You will need to implement this assignment using 2 different files: ATM.java, ATM_Driver.java. ATM.java will contain the class ATM and the the methods that it implements (1-6 above). The driver program, should continue to display the menu, perform the selected operations and only exit when the number 7 is pushed. Feel free to implement additional classes that could very well be appropriate for this assignment, this will add additional class files to be used and that's OK. The logic to perform all of the operations 1-6 should be contained in the ATM class, the driver program should handle the IO and only calling the methods in the ATM class. HINT: A Transaction class may be needed.
Explanation / Answer
Answer:
Transaction.java
import java.util.Calendar;
import java.util.Date;
public class Transaction {
public static int WITHDRAWAL = 1;
public static int DEPOSIT = 2;
public static int BALANCE_INQUIRY = 3;
public static int LARGEST_CHANGE = 4;
private int type;
private double amount;
private Date date;
public Transaction(int type)
{
this.type = type;
}
public void setAmount(double amount)
{
this.amount = amount;
}
public double getAmount()
{
return amount;
}
public void setDate(Date date)
{
this.date = date;
}
public Date getDate()
{
return date;
}
public int getType()
{
return type;
}
}
ATM.java
import java.text.SimpleDateFormat;
import java.util.Date;
public class ATM {
private double balance;
private Transaction[] transactions;
private int transactionNo;
private static SimpleDateFormat dateFormater = new SimpleDateFormat("MM/dd/YYYY");
public ATM(int maxTransactions)
{
transactions = new Transaction[maxTransactions];
transactionNo = 0;
}
public int getTransactionCount()
{
return transactionNo;
}
public boolean withdraw(Date date, double amount)
{
if(transactionNo < transactions.length && amount >0 && amount <= balance)
{
balance -= amount;
Transaction t = new Transaction(Transaction.WITHDRAWAL);
t.setAmount(amount);
t.setDate(date);
transactions[transactionNo++] = t;
return true;
}
else
return false;
}
public boolean deposit(Date date, double amount)
{
if(transactionNo < transactions.length && amount >0)
{
balance += amount;
Transaction t = new Transaction(Transaction.DEPOSIT);
t.setAmount(amount);
t.setDate(date);
transactions[transactionNo++] = t;
return true;
}
else
return false;
}
public double balanceEnquiry(Date date)
{
if(transactionNo < transactions.length )
{
Transaction t = new Transaction(Transaction.BALANCE_INQUIRY);
t.setDate(date);
transactions[transactionNo++] = t;
return balance;
}
else
return -1;
}
public double getBalance()
{
return balance;
}
public double getLargestChange(Date transactionDate)
{
if(transactionNo < transactions.length )
{
Transaction t = new Transaction(Transaction.LARGEST_CHANGE);
t.setDate(transactionDate);
transactions[transactionNo++] = t;
t.setAmount(calculateLargestChange());
return t.getAmount();
}
else
return -1;
}
public double calculateLargestChange()
{
//TODO: implement the logic for largest change
return 0;
}
public void displayTransactions(Date date)
{
System.out.printf(" %-15s %-20s %10s",
"Date", "Transaction", "Amount");
System.out.printf(" %-15s %-20s %10s",
"====", "===========", "======");
Transaction t;
for(int i = 0; i < transactionNo; i++)
{
if(date == null || date.equals(transactions[i].getDate()))
{
t = transactions[i];
if(t.getType() == Transaction.DEPOSIT)
System.out.printf(" %-15s %-20s %10s",
dateFormater.format(transactions[i].getDate()), "Deposit", "$"+String.format("%.2f", t.getAmount()));
else if(t.getType() == Transaction.WITHDRAWAL)
System.out.printf(" %-15s %-20s %10s",
dateFormater.format(transactions[i].getDate()), "Withdrawal","$"+String.format("%.2f", t.getAmount()));
else if(t.getType() == Transaction.BALANCE_INQUIRY)
System.out.printf(" %-15s %-20s %10s",
dateFormater.format(transactions[i].getDate()), "Balance Enquiry", "------");
else if(t.getType() == Transaction.LARGEST_CHANGE)
System.out.printf(" %-15s %-20s %10s",
dateFormater.format(transactions[i].getDate()), "Largest change", "$"+String.format("%.2f", t.getAmount()));
}
}
System.out.println(" =============================================== ");
}
}
ATM_Driver.java
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class ATM_Driver {
private static Calendar calendar = Calendar.getInstance();
private static int CURRENT_YEAR = 2017;
private static Date getDateInput(Scanner keyboard)
{
String str;
int day, month;
while(true)
{
System.out.print("Enter date in MDD / MMDD format: ");
str = keyboard.next();
try{
if(str.length() == 3)
{
month =Integer.parseInt(str.substring(0, 1));
day = Integer.parseInt(str.substring(1));
}
else if(str.length() == 4)
{
month =Integer.parseInt(str.substring(0, 2));
day = Integer.parseInt(str.substring(2));
}
else
throw new Exception();
calendar.set(Calendar.YEAR, CURRENT_YEAR);
calendar.set(Calendar.MONTH, month - 1);//months start from 0
calendar.set(Calendar.DATE, 1);
if(day > calendar.getActualMaximum(Calendar.DATE))
throw new Exception();
else
calendar.set(Calendar.DATE, day);
return calendar.getTime();
}
catch(Exception e)
{
System.out.println("***** Illegal date!");
}
}
}
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
int choice = 0, max;
double amount;
Date date;
System.out.print("Enter the maximum number of transactions: ");
max = keyboard.nextInt();
ATM atm = new ATM(max);
while(choice != 7)
{
System.out.println(" 1. Deposit");
System.out.println("2. Withdrawal");
System.out.println("3. Balance enquiry");
System.out.println("4. Display largest change");
System.out.println("5. Display all transactions");
System.out.println("6. Display all transactions on specific date");
System.out.println("7. Quit");
System.out.print(" Enter your choice: ");
choice = keyboard.nextInt();
switch(choice)
{
case 1:
System.out.print(" Enter deposit amount: ");
amount = keyboard.nextDouble();
date = getDateInput(keyboard);
if(amount > 0 && atm.deposit(date, amount))
System.out.println("Amount deposited successfully");
else
System.out.println("***** Invalid deposit amount!");
break;
case 2:
if(atm.getBalance() == 0)
{
System.out.println("");
}
System.out.print(" Enter withdrawal amount: ");
amount = keyboard.nextDouble();
date = getDateInput(keyboard);
if(amount > 0 )
{
if(atm.withdraw(date, amount))
System.out.println("Amount withdrawn successfully");
else
System.out.println("***** Insufficient balance!");
}
else
System.out.println("***** Invalid withdrawal amount!");
break;
case 3:
date = getDateInput(keyboard);
System.out.printf(" Current balance is $%.2f", atm.balanceEnquiry(date));
break;
case 4:
date = getDateInput(keyboard);
atm.displayTransactions(date);
break;
case 7:
break;
default:
System.out.println("***** Invalid menu choice");
}
}
}
}
Output
Enter the maximum number of transactions: 100
1. Deposit
2. Withdrawal
3. Balance enquiry
4. Display largest change
5. Display all transactions
6. Display all transactions on specific date
7. Quit
Enter your choice: 1
Enter deposit amount: 120
Enter date in MDD / MMDD format: 230
***** Illegal date!
Enter date in MDD / MMDD format: 228
Amount deposited successfully
1. Deposit
2. Withdrawal
3. Balance enquiry
4. Display largest change
5. Display all transactions
6. Display all transactions on specific date
7. Quit
Enter your choice: 2
Enter withdrawal amount: 20
Enter date in MDD / MMDD format: 310
Amount withdrawn successfully
1. Deposit
2. Withdrawal
3. Balance enquiry
4. Display largest change
5. Display all transactions
6. Display all transactions on specific date
7. Quit
Enter your choice: 3
Enter date in MDD / MMDD format: 315
Current balance is $100.00
1. Deposit
2. Withdrawal
3. Balance enquiry
4. Display largest change
5. Display all transactions
6. Display all transactions on specific date
7. Quit
Enter your choice: 5
Date Transaction Amount
==== =========== ======
02/28/2017 Deposit $120.00
03/10/2017 Withdrawal $20.00
03/15/2017 Balance Enquiry ------
===============================================
1. Deposit
2. Withdrawal
3. Balance enquiry
4. Display largest change
5. Display all transactions
6. Display all transactions on specific date
7. Quit
choice: 7