I need to Create Transaction form to manage account balance. Using GUI interface
ID: 3861251 • Letter: I
Question
I need to Create Transaction form to manage account balance. Using GUI interface for JAVA.
1. Create transaction Options:
Deposits add to balance
Checks subtract from balance
if balance > check then subtract check from balance
else Display Insufficient Funds Message
and Subtract Service Charge ($10) from balance
Service Charge ($10) subtract from balance
2. Use Abstract Class Transaction to create classes:
class Deposit
class ServiceCharge
class Check
override the abstract function calculateBalance() in each class to perform the appropriate calculations on balance
throw an Exception in class Check.calculateBalance()
Explanation / Answer
1)
import java.util.*;
public class Bank
{
private String bankName;
private Terminal atm;
private int balance = 0;
private int transactionCount = 0;
private Month month;
private Map accountList; .
private int checkFee = 2;
private int transactionFee = 1;
private int monthlyCharge = 5;
private double interestRate = 0.05;
private int maxFreeTransactions = 3;
private static final String BANKER_COMMANDS =
"Banker commands: " +
"exit, open, customer, nextmonth, report, help.";
private static final String CUSTOMER_TRANSACTIONS =
" Customer transactions: " +
"deposit, withdraw, transfer, balance, cash check, quit, help.";
public Bank( String bankName, Terminal atm )
{
this.atm = atm;
this.bankName = bankName;
accountList = new TreeMap();
month = new Month();
}
public void visit()
{
instructUser();
String command;
while (!(command =
atm.readWord("banker command: ")).equals("exit")) {
if (command.startsWith("h")) {
help( BANKER_COMMANDS );
}
else if (command.startsWith("o")) {
openNewAccount();
}
else if (command.startsWith("n")) {
newMonth();
}
else if (command.startsWith("r")) {
report();
}
else if (command.startsWith( "c" ) ) {
BankAccount acct = whichAccount();
if ( acct != null ) {
processTransactionsForAccount( acct );
}
}
else {
atm.println( "unknown command: " + command );
}
}
report();
atm.println( "Goodbye from " + bankName );
}
private void openNewAccount()
{
String accountName = atm.readWord("Account name: ");
char accountType =
atm.readChar( "Type of account (r/c/f/s): " );
try {
int startup = readPosAmt( "Initial deposit: " );
BankAccount newAccount;
switch( accountType ) {
case 'c':
newAccount = new CheckingAccount(startup, this);
break;
case 'f':
newAccount = new FeeAccount(startup, this);
break;
case 's':
newAccount = new SavingsAccount(startup, this);
break;
case 'r':
newAccount = new RegularAccount( startup, this );
break;
default:
atm.println("invalid account type: " + accountType);
return;
}
accountList.put( accountName, newAccount );
atm.println( "opened new account " + accountName
+ " with $" + startup );
}
catch (NegativeAmountException e) {
atm.errPrintln(
"can't start with a negative balance");
}
catch (InsufficientFundsException e) {
atm.errPrintln("Initial deposit less than fee");
}
}
private void processTransactionsForAccount( BankAccount acct )
{
help( CUSTOMER_TRANSACTIONS );
String transaction;
while (!(transaction =
atm.readWord(" transaction: ")).equals("quit")) {
try {
if ( transaction.startsWith( "h" ) ) {
help( CUSTOMER_TRANSACTIONS );
}
else if ( transaction.startsWith( "d" ) ) {
int amount = readPosAmt( " amount: " );
atm.println(" deposited "
+ acct.deposit( amount ));
}
else if ( transaction.startsWith( "w" ) ) {
int amount = readPosAmt( " amount: " );
atm.println(" withdrew "
+ acct.withdraw( amount ));
}
else if ( transaction.startsWith( "c" ) ) {
int amount = readPosAmt( " amount of check: " );
try {
atm.println(" cashed check for " +
((CheckingAccount) acct).honorCheck( amount ));
}
catch (ClassCastException e) {
atm.errPrintln(
" Sorry, not a checking account." );
}
}
else if (transaction.startsWith("t")) {
atm.print( " to ");
BankAccount toacct = whichAccount();
if (toacct != null) {
int amount = readPosAmt(" amount to transfer: ");
atm.println(" transfered "
+ toacct.deposit(acct.withdraw(amount)));
}
}
else if (transaction.startsWith("b")) {
atm.println(" current balance "
+ acct.requestBalance());
}
else {
atm.println(" sorry, unknown transaction" );
}
}
catch (InsufficientFundsException e) {
atm.errPrintln( " Insufficient funds " +
e.getMessage() );
}
catch (NegativeAmountException e) {
atm.errPrintln(" Sorry, negative amounts disallowed." );
}
atm.println();
}
}
private BankAccount whichAccount()
{
String accountName = atm.readWord( "account name: " );
BankAccount account = (BankAccount) accountList.get(accountName);
if (account == null) {
atm.println( "not a valid account" );
}
return account;
}
private void newMonth()
{
month.next();
Iterator i = accountList.keySet().iterator();
while ( i.hasNext() ) {
String name = (String) i.next();
BankAccount acct = (BankAccount)accountList.get(name);
try {
acct.newMonth();
}
catch (InsufficientFundsException exception) {
atm.errPrintln(
"Insufficient funds in account "" +
name + "" for monthly fee" );
}
}
}
private void report()
{
atm.println( bankName + " report for " + month );
atm.println( " Summaries of individual accounts:" );
atm.println( "account balance transaction count" );
for (Iterator i = accountList.keySet().iterator();
i.hasNext(); ) {
String accountName = (String) i.next();
BankAccount acct = (BankAccount) accountList.get(accountName);
atm.println(accountName + " $" + acct.getBalance() + " "
+ acct.getTransactionCount());
}
atm.println( " Bank totals");
atm.println( "open accounts: " + getNumberOfAccounts() );
atm.println( "cash on hand: $" + getBalance());
atm.println( "transactions: " + getTransactionCount());
atm.println();
}
private void instructUser()
{
atm.println( "Welcome to " + bankName );
atm.println( month.toString() );
atm.println( "Open some accounts and work with them." );
help( BANKER_COMMANDS );
}
private void help( String helpString )
{
atm.println( helpString );
atm.println();
}
private int readPosAmt( String prompt )
throws NegativeAmountException
{
int amount = atm.readInt( prompt );
if (amount < 0) {
throw new NegativeAmountException();
}
return amount;
}
public void incrementBalance(int amount)
{
balance += amount;
}
public void countTransaction()
{
transactionCount++;
}
public int getTransactionCount( )
{
return transactionCount ;
}
public int getCheckFee( )
{
return checkFee ;
}
public int getTransactionFee( )
{
return transactionFee ;
}
public int getMonthlyCharge( )
{
return monthlyCharge;
}
public double getInterestRate( )
{
return interestRate;
}
public int getMaxFreeTransactions()
{
return maxFreeTransactions;
}
public int getBalance()
{
return balance;
}
public int getNumberOfAccounts()
{
return accountList.size();
}
public static void main( String[] args )
{
boolean echo = false; // default does not echo
String bankName = "River Bank"; // default bank name
for (int i = 0; i < args.length; i++ ) {
if (args[i].equals("-e")) {
echo = true;
}
else {
bankName = args[i];
}
}
Bank aBank = new Bank( bankName, new Terminal(echo) );
aBank.visit();
}
}
2)
public abstract class Transaction {
static final double SERVICE_CHARGE = 10.0;
double transactionamount = 0.0;
static double balance = 0.0;
static double totalDeposits = 0.0;
static double totalChecks = 0.0;
static double totalServiceCharges = 0.0;
static int numberDeposits = 0;
static int numberChecks = 0;
static int numberServiceCharges = 0;
public Transaction() {
transactionamount = 0.0;
}
public Transaction(double amount) {
this.transactionamount = amount;
}
public Transaction(String str) {
transactionamount = Double.parseDouble(str);
}
public void deposit(double amount) {
balance += amount;
}
public void deposit(String str) {
balance += Double.parseDouble(str);
}
static public double getBalance() {
return balance;
}
abstract void calculateBalance() throws Exception;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.