Question: JAVA PROGRAM Lab5 Implement Account, Transition classes and Add GUI to it. Account #name : string transactiontype #balance : double public enum transitiontype{ deposit,withdraw;} +
JAVA PROGRAM
Lab5 Implement Account, Transition classes and Add GUI to it.
Account
#name : string transactiontype
#balance : double public enum transitiontype{ deposit,withdraw;}
+ static dailywithdrawlimit :double
account ( string name, double balance) transaction
//add get, some methods for each member amount : double
// return updated balance type : transactiontype
withdraw(doubleamount) : double date : localdate
totaldepositatday(localdate dt) : double transaction(doubleamount,transactiontype t)
totalwithdrawatday(localdate dt) : double tostring() : string
totaltransactionsatday((localdate dt) : stringbuildertostring() : string
Be creative when add GUI to your project.
Minimum Requirement:A user can enter the name of the account and click create Account; Once an account is created successfully, the user can deposit, withdraw, clear Amount text field, and display all of todays transaction. Your program need do error checking and catch proper exceptions.
//add class to yoursource package, name it TransactionType
//TransactionType.java
//proper package statement
public enum TransitionType {
DEPOSIT, WITHDRAW;
}
class OverdraftExceptionextends Exception{
private double overdraftAmount;
public OverdraftException(String msg, double overAmount){
super(msg);
overdraftAmount = overAmount;
}
@Override
public String toString(){return super.getMessage() + " Overdraft amount: " + overdraftAmount;}
}
//Transition.java
import java.time.LocalDate;
import java.util.Date;
public class Transition {
LocalDate date;
double amount;
TransitionType type; //enum
public Transition(double amount, TransitionType type){
date = LocalDate.now();//current date and time
this.amount = amount;
this.type = type;
}
@Override
public String toString(){
switch(type){
case DEPOSIT:
return String.format("Deposit: $%.2f on ", amount) + date.toString();
case WITHDRAW:
return String.format("Withdraw: $%.2f on ", amount) + date.toString () ;
default:
return Error";
}
}
}
// read but do not use following Account class. Just use one ArrayList for all transactions instead of use two as in following implementation
public class Account {
protected String name;
protected double balance;
public static double dailyWithdrawLimit = 5000;
private ArrayList
private ArrayList
public Account(String nm){this(nm, 500);}
public Account(String nm, double b){
name = nm;
balance = b;
allWithdraw = new ArrayList<>();
allDeposit = new ArrayList();
}
public Account(){this("Nobody", 0);}
public double withdraw(double amount) throws OverdraftException, Exception{
if(amount < 0){
throw new Exception("Cannot withdraw a negative amount);
}
else if (amount > balance){
throw new OverdraftException(" OverdraftException: amount over balance. , amount -balance);
}
double availableLimit = dailyWithdrawLimit - totalWithdrawAtDay(LocalDate.now());
if(amount > availableLimit){
throw new OverdraftException(" OverdraftException:
+amount exceed left over daily limit of $
+ availableLimit, amount -availableLimit);
}
balance -= amount;
allWithdraw.add(new Transition(amount, TransitionType.WITHDRAW));
return balance;
}
public double deposit(double amount) throws OverdraftException, Exception{
if(amount < 0){
throw new Exception("Cannot deposit a negative amount);
}
balance += amount;
allDeposit.add(new Transition(amount, TransitionType.DEPOSIT));
return balance;
}
public double totalWithdrawAtDay(LocalDate dt){
double total = 0;
int y = dt.getYear();
int m = dt.getMonthValue();
int d = dt.getDayOfMonth();
for (Transition t : allWithdraw){
if (t.date.getYear()==y && t.date.getMonthValue()==m
&& t.date.getDayOfMonth() == d){
total += t.amount;
}
}
return total;
}
public StringBuilder totalTransactionsAtDay(LocalDate dt){
StringBuilder buffer = new StringBuilder();
int y = dt.getYear();int m = dt.getMonthValue();
int d = dt.getDayOfMonth();
for (Transition t : allWithdraw){
if (t.date.getYear()==y && t.date.getMonthValue()==m
&& t.date.getDayOfMonth() == d){
buffer.append(t.toString()+ ");
}
}
buffer.append( ");
for (Transition t : allDeposit){
if (t.date.getYear()==y && t.date.getMonthValue()==m
&& t.date.getDayOfMonth() == d){
buffer.append(t.toString()+ ");
}
}
return buffer;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
