Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am making a bank account program. So far it includes BankAccount, Transaction,

ID: 655364 • Letter: I

Question

I am making a bank account program. So far it includes BankAccount, Transaction, and Main. I am having difficulty with putting the pieces together. I must include a transaction in my BankAccount class and run with Main.

This is what I have so far. I am unsure of whether or not everything in Transaction is working.

~~~~

import java.text.DecimalFormat;

public class BankAccount
{
   private double currentBalance;
   private String accountNumber;
   private String customerName;
   private String customerAddress;

   public BankAccount(String accountNumber, String customerName, String customerAddress)
   {

       this.accountNumber = accountNumber;
       this.customerName = customerName;
       this.customerAddress = customerAddress;
       currentBalance = 0.00;
   }
   public void deposit(double amount)
   {
       if (amount >= 0)
       {
           currentBalance += amount;
       }
       else if (amount > currentBalance)
       {
           System.out.println("Insufficient funds. ");
       }
       else
       {
           System.out.println("Please enter a positive integer.");
       }
   }
   public void debit(double amount)
   {
       if (amount >= 0)
       {
           currentBalance -= amount;
       }
       else
       {
           System.out.println("Please enter a positive integer");
       }
   }
   public void print()
   {
       DecimalFormat decimalFormat = new DecimalFormat("#.00");
       System.out.println("Account number: " + accountNumber);
       System.out.println("Customer name: " + customerName);
       System.out.println("Customer address: " + customerAddress);
        System.out.println("Current balance: $" + decimalFormat.format(currentBalance));
   }
}

~~~

public class MainClass
{
   public static void main(String[] args)
   {
       BankAccount myBankAccount = new BankAccount("0123 4567 890A BCDF", "Elaine Dzirko", "304 Hillside Avenue, Nutley, NJ");
       myBankAccount.print();
       System.out.println("Deposit $5.00");
       myBankAccount.deposit(5.00);
       myBankAccount.print();
       System.out.println("Debit $1.50");
       myBankAccount.debit(1.50);
       myBankAccount.print();
      
       Transaction myTransaction = new Transaction();
       myTransaction.print();
   }
}


~~~

import java.text.DecimalFormat;
import java.util.Date;

enum TransactionType
{
   deposit, debit
}

public class Transaction
{
   private Date date;
   private double amount;
   private String description;
   private TransactionType type;
  
   public Transaction(Date date, double amount, String description, TransactionType type)
   {
       this.date = date;
       this.amount = amount;
       this.description = description;
       this.type = type;
       date = new Date();
   }
   public String toString()
   {
       return "Date: " + date + " Amount: " + amount + " Description: " + description + " Type: " + type;
   }
   public void print(Date date, double amount, String description, TransactionType type)
   {
       DecimalFormat decimalFormat = new DecimalFormat("#.00");
       System.out.println("Date: " + date);
       System.out.println("Amount: " + decimalFormat.format(amount));
       System.out.println("Description: " + description);
        System.out.println("Type: " + type);
   }
}

Explanation / Answer

//BankAccount.java
import java.text.DecimalFormat;
public class BankAccount
{
   private double currentBalance;
   private String accountNumber;
   private String customerName;
   private String customerAddress;

   public BankAccount(String accountNumber, String customerName, String customerAddress)
   {

       this.accountNumber = accountNumber;
       this.customerName = customerName;
       this.customerAddress = customerAddress;
       currentBalance = 0.00;
   }
   public void deposit(double amount)
   {
       if (amount >= 0)
       {
           currentBalance += amount;
       }
       else if (amount > currentBalance)
       {
           System.out.println("Insufficient funds. ");
       }
       else
       {
           System.out.println("Please enter a positive integer.");
       }
   }
   public void debit(double amount)
   {
       if (amount >= 0)
       {
           currentBalance -= amount;
       }
       else
       {
           System.out.println("Please enter a positive integer");
       }
   }

//Add a method getBalance that returns the amount in the account  
   public double getBalance()
   {
       return currentBalance;
   }


   public void print()
   {
       DecimalFormat decimalFormat = new DecimalFormat("#.00");
       System.out.println("Account number: " + accountNumber);
       System.out.println("Customer name: " + customerName);
       System.out.println("Customer address: " + customerAddress);
       System.out.println("Current balance: $" + decimalFormat.format(currentBalance));
   }
}

-------------------------------------------------------------


//TransactionType.java
public enum TransactionType
{
   //add none type to TransactionType enumeration class
   deposit, debit,none
}


-------------------------------------------------------------
//Transaction.java
import java.text.DecimalFormat;
import java.util.Date;
public class Transaction
{
   private Date date;
   private double amount;
   private String description;
   private TransactionType type;

   //Create default constructor that sets the default values
   //for the member variabels of Transaction class
   public Transaction()
   {
       date =new Date();
       amount = 0;
       description = "";
       type = TransactionType.none;

   }
   //Create parameterized constructor that sets the values
   //for the member variabels of Transaction class
   public Transaction(Date date,double amount,String description, TransactionType type)
   {
       this.date = date;
       this.amount = amount;
       this.description = description;
       this.type = type;      
   }

   //Overriding of toString method to print the details of Transaction object
   public String toString()
   {
       return "Date: " + date + " Amount: " + amount + " Description: " + description + " Type: " + type;
   }
  
   //The print method that display the deatils of Transaction object
   public void print()
   {
       DecimalFormat decimalFormat = new DecimalFormat("#.00");
       System.out.println("Date: " + date);
       System.out.println("Amount: " + decimalFormat.format(amount));
       System.out.println("Description: " + description);
       System.out.println("Type: " + type);
   }
}
-------------------------------------------------------------

//The java program that test the class BankAccount and Transaction classes
//and print the results of both classes
//MainClass.java
import java.util.Date;
public class MainClass
{
   public static void main(String[] args)
   {
     
       //create an instance of BankAccount and set details of bank account class
       BankAccount myBankAccount = new BankAccount("0123 4567 890A BCDF", "Elaine Dzirko", "304 Hillside Avenue, Nutley, NJ");
       myBankAccount.print();
     
       System.out.println();
       System.out.println("Deposit $5.00");
       myBankAccount.deposit(5.00);     
       myBankAccount.print();
       System.out.println();
       System.out.println("Debit $1.50");
       myBankAccount.debit(1.50);
       myBankAccount.print();
       System.out.println();
     
       //Create an instance of parameter type for Transaction class object
       //and set the date, amount of transtaction balance using the object of BankAccount class
       //and string description of transaction,type of transaction
       Transaction myTransaction =
               new Transaction(new Date(),myBankAccount.getBalance(), "Transaction", TransactionType.deposit);
       //call print method to print the details of myTransaction object.
       myTransaction.print();

//you can use toString method also to print the details of transaction


   }
}

-------------------------------------------------------------
Sample output:

Account number: 0123 4567 890A BCDF
Customer name: Elaine Dzirko
Customer address: 304 Hillside Avenue, Nutley, NJ
Current balance: $.00

Deposit $5.00
Account number: 0123 4567 890A BCDF
Customer name: Elaine Dzirko
Customer address: 304 Hillside Avenue, Nutley, NJ
Current balance: $5.00

Debit $1.50
Account number: 0123 4567 890A BCDF
Customer name: Elaine Dzirko
Customer address: 304 Hillside Avenue, Nutley, NJ
Current balance: $3.50

Date: Mon Apr 27 06:28:39 IST 2015
Amount: 3.50
Description: Transaction
Type: deposit

Hope this helps you...