Difference between revisions of "Directory:Derek Elder/Programs/CheckingAccount2"

MyWikiBiz, Author Your Legacy — Thursday November 07, 2024
Jump to navigationJump to search
(Start of article)
 
(Modifications)
 
Line 114: Line 114:
 
         transAmt = currentTransAmt;
 
         transAmt = currentTransAmt;
 
     }
 
     }
     public String toString() //Modify like CD example
+
     public String toString()
 
     {
 
     {
 
         NumberFormat fmt = NumberFormat.getCurrencyInstance();
 
         NumberFormat fmt = NumberFormat.getCurrencyInstance();
Line 152: Line 152:
 
     private float balance;
 
     private float balance;
 
     private float totalServiceCharge; //Changed from double
 
     private float totalServiceCharge; //Changed from double
     private int transCount; //static?
+
     private int transCount;
 
     private Transaction[] tList;
 
     private Transaction[] tList;
  
     public void addTrans(int transID, int transNumber, float transAmt) //change to float
+
     public void addTrans(int transID, int transNumber, float transAmt)
 
     {
 
     {
 
         if(transCount == tList.length)
 
         if(transCount == tList.length)
Line 161: Line 161:
 
              
 
              
 
         tList[transCount] = new Transaction(transID, transNumber, transAmt);
 
         tList[transCount] = new Transaction(transID, transNumber, transAmt);
        //balance += transAmt;
 
 
         transCount++;
 
         transCount++;
 
     }
 
     }
     public void getTrans(int checkOrDeposit) //void?
+
     public void getTrans(int checkOrDeposit)
 
     {
 
     {
 
         int i;
 
         int i;
         for(i = 0;i < tList.length;i++)
+
         for(i = 0;i < transCount;i++)
 
         {
 
         {
 
             if(tList[i].getTransID() == checkOrDeposit)
 
             if(tList[i].getTransID() == checkOrDeposit)
Line 174: Line 173:
 
             }
 
             }
 
         }
 
         }
        System.out.println("NOT DONE");
 
 
     }
 
     }
     public int getTransCount() //static
+
     public int getTransCount()
 
     {
 
     {
 
         return transCount;
 
         return transCount;
Line 203: Line 201:
 
     public CheckingAccount() //Needed?
 
     public CheckingAccount() //Needed?
 
     {
 
     {
 +
        balance = 0;
 +
        totalServiceCharge = 0;
 
         tList = new Transaction[10];
 
         tList = new Transaction[10];
 
         transCount = 0;
 
         transCount = 0;
        //totalCost = 0.0;
 
 
     }
 
     }
 
     public CheckingAccount(float currentBalance, float currentServiceCharge)
 
     public CheckingAccount(float currentBalance, float currentServiceCharge)
Line 213: Line 212:
 
         tList = new Transaction[10];
 
         tList = new Transaction[10];
 
         transCount = 0;
 
         transCount = 0;
        //totalCost = 0.0;
 
 
     }
 
     }
     public String toString() //Modify like CDCollection example
+
     public String toString()
 
     {
 
     {
 
         NumberFormat fmt = NumberFormat.getCurrencyInstance();
 
         NumberFormat fmt = NumberFormat.getCurrencyInstance();
Line 261: Line 259:
 
     public static JFrame frame;
 
     public static JFrame frame;
 
     public static CheckingAccount account = null;
 
     public static CheckingAccount account = null;
     //Figure out how to deal with below500 stuff
+
     public static boolean below500 = false;
 
    
 
    
 
     public static void main(String[] args)
 
     public static void main(String[] args)
Line 272: Line 270:
 
         frame.setVisible(true);
 
         frame.setVisible(true);
 
     }
 
     }
     public static void doTransactions() //static?
+
     public static void doTransactions()
 
     {
 
     {
        //Some flags need to be moved to global as well
 
 
         int tCode = 0;
 
         int tCode = 0;
        int doAnother;
 
 
String stringBalance, stringTCode, stringTransAmt, message;
 
String stringBalance, stringTCode, stringTransAmt, message;
 
         float balance, transAmt, charge, balanceBeforeCharge;
 
         float balance, transAmt, charge, balanceBeforeCharge;
        boolean below500 = false;
 
 
         //boolean done = false;
 
         //boolean done = false;
 
         charge = 0.00f;
 
         charge = 0.00f;
Line 428: Line 423:
 
                           "Final Balance : $" + fmt.format(account.getBalance());
 
                           "Final Balance : $" + fmt.format(account.getBalance());
 
                 JOptionPane.showMessageDialog(null, message);
 
                 JOptionPane.showMessageDialog(null, message);
                doAnother = JOptionPane.NO_OPTION;
 
 
                 //done = true;
 
                 //done = true;
                 //When 0 chosen, terminate program
+
                 System.exit(0); //Modify to window listener?
 
             }
 
             }
 
         }
 
         }

Latest revision as of 23:48, 14 October 2008

GUI.java

//*******************************************************************
//  GUI Class       Author: Derek Elder
//********************************************************************
package program02;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GUI extends JPanel
{
   private JLabel prompt;
   private JRadioButton one, two, three, four;

   public GUI()
   {
      prompt = new JLabel("Choose your action: ");
      prompt.setFont (new Font("Helvetica", Font.BOLD, 30));
      one = new JRadioButton("Enter a transaction");
      one.setBackground(Color.blue);
      two = new JRadioButton("List all transactions");
      two.setBackground(Color.blue);
      three = new JRadioButton("List all checks");
      three.setBackground(Color.blue);
      four = new JRadioButton("List all deposits");
      four.setBackground(Color.blue);

      ButtonGroup group = new ButtonGroup();
      group.add(one);
      group.add(two);
      group.add(three);
      group.add(four);

      GUIListener listener = new GUIListener();
      one.addActionListener(listener);
      two.addActionListener(listener);
      three.addActionListener(listener);
      four.addActionListener(listener);

      //Add the components to the JPanel
      add(prompt);
      add(one);
      add(two);
      add(three);
      add(four);
      setBackground(Color.blue);
      setPreferredSize(new Dimension(350, 100));
   }
   //*****************************************************************
   //  Represents the listener for the radio buttons
   //*****************************************************************
   private class GUIListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         Object source = event.getSource();

         if(source == one)
         {
             Main.doTransactions();
         }
         else if(source == two)
         {
             Main.listTransactions();
         }
         else if(source == three)
         {
             Main.listChecks();
         }
         else //if(source == four)
         {
             Main.listDeposits();
         }
      }
   }
}

Transaction.java

//*******************************************************************
//  Transaction Class       Author: Derek Elder
//********************************************************************

package program02;

import java.text.NumberFormat;

public class Transaction
{
    private int transID;
    private int transNumber; //Current value of transCount
    private float transAmt;
    private String transType;
    
    public int getTransID()
    {
        return transID; //1 = check, 2 = deposit 3 = service charge
    }
    public int getTransNumber()
    {
        return transNumber;
    }
    public float getTransAmt()
    {
        return transAmt;
    }
    public Transaction(int currentTransID, int currentTransNumber, float currentTransAmt)
    {
        transID = currentTransID;
        transNumber = currentTransNumber;
        transAmt = currentTransAmt;
    }
    public String toString()
    {
        NumberFormat fmt = NumberFormat.getCurrencyInstance();
        String description;
        
        if(transID == 1)
        {
            transType = "Check";
        }
        else if(transID == 2)
        {
            transType = "Deposit";
        }
        else //(transID == 3)
        {
            transType = "S.Ch.";
        }

        description = transNumber + "\t\t" + transType + "\t\t" + fmt.format(transAmt);

        return description;
    }
}

CheckingAccount.java

//*******************************************************************
//  CheckingAccount Class       Author: Derek Elder
//********************************************************************

package program02;

import java.text.NumberFormat;

public class CheckingAccount
{
    private float balance;
    private float totalServiceCharge; //Changed from double
    private int transCount;
    private Transaction[] tList;

     public void addTrans(int transID, int transNumber, float transAmt)
     {
         if(transCount == tList.length)
             increaseArray();
             
         tList[transCount] = new Transaction(transID, transNumber, transAmt);
         transCount++;
     }
     public void getTrans(int checkOrDeposit)
     {
         int i;
         for(i = 0;i < transCount;i++)
         {
             if(tList[i].getTransID() == checkOrDeposit)
             {
                 System.out.println(tList[i]);
             }
         }
     }
     public int getTransCount()
     {
         return transCount;
     }
     public float getBalance()
     {
         return balance;
     }
     public float setBalance(float currentBalance, int tCode)
     {
         if(tCode == 1 || tCode == 0)
             balance -= currentBalance;
         else //if(tCode == 2)
             balance += currentBalance;
         return balance;
     }
     public float getServiceCharge()
     {
         return totalServiceCharge;
     }
     public float setServiceCharge(float currentServiceCharge)
     {
         totalServiceCharge += currentServiceCharge;
         return totalServiceCharge;
     }
     public CheckingAccount() //Needed?
     {
         balance = 0;
         totalServiceCharge = 0;
         tList = new Transaction[10];
         transCount = 0;
     }
     public CheckingAccount(float currentBalance, float currentServiceCharge)
     {
         balance = currentBalance;
         totalServiceCharge = currentServiceCharge;
         tList = new Transaction[10];
         transCount = 0;
     }
     public String toString()
     {
         NumberFormat fmt = NumberFormat.getCurrencyInstance();
         int i;

         String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
         report += "My Transaction Status\n\n";
         report += "Balance: " + balance + "\n";
         report += "Total Service Charge: " + fmt.format(totalServiceCharge) + "\n";
         report += "Number of Transactions: " + transCount;
         report += "\n\nChecking Account List:\n\n";
         report += "ID\t\tType\t\tAmount\n";
         report += "---------------------------------------\n";
         
         for(i = 0; i < transCount; i++)
              report += tList[i] + "\n";

         return report;
     }
     private void increaseArray()
     {
         int i;
         Transaction[] temp = new Transaction[tList.length * 2];

         for(i = 0; i < tList.length; i++)
             temp[i] = tList[i];
         tList = temp;
     }
}

Main.java

//*******************************************************************
//  Program 2       Author: Derek Elder
//********************************************************************

package program02;

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class Main
{
    public static JFrame frame;
    public static CheckingAccount account = null;
    public static boolean below500 = false;
   
    public static void main(String[] args)
    {
        frame = new JFrame("Checking Account Actions");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GUI panel = new GUI();
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }
    public static void doTransactions()
    {
        int tCode = 0;
	String stringBalance, stringTCode, stringTransAmt, message;
        float balance, transAmt, charge, balanceBeforeCharge;
        //boolean done = false;
        charge = 0.00f;
        frame.setVisible(false);
        if(account == null)
        {
            stringBalance = JOptionPane.showInputDialog("Enter your initial balance: ");
            balance = Float.parseFloat(stringBalance);
            //CheckingAccount account = new CheckingAccount();  //Old version
            account = new CheckingAccount(balance, charge);
        }

        //while(!done)
        //do
        {
            stringTCode = JOptionPane.showInputDialog("Enter the trans code: ");
            tCode = Integer.parseInt(stringTCode);
            DecimalFormat fmt = new DecimalFormat ("0.00"); //Round to two decimal places
            if(tCode == 1)
            {
                stringTransAmt = JOptionPane.showInputDialog("Enter the trans amount: ");
                transAmt = Float.parseFloat(stringTransAmt);
                account.setBalance(transAmt, tCode);
                account.addTrans(1,account.getTransCount(),transAmt);
                if(account.getBalance() < 0.00)
                {
                    if(below500 == true)
                    {
                        charge = 10.15f; //Cost of Check + Below $0 charge
                        account.setServiceCharge(charge);
                        message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" +
                                "Current Balance : $" + fmt.format(account.getBalance()) + "\n" +
                                "Service charge : Check -- charge $0.15" + "\n" +
                                "Warning : Balance below $50.00" + "\n" +
                                "Service charge : Below $0 -- charge $10.00" + "\n" +
                                "Total service charge : $" + fmt.format(account.getServiceCharge());
                        JOptionPane.showMessageDialog(null, message);
                        account.addTrans(3,account.getTransCount(),0.15f);
                        account.addTrans(3,account.getTransCount(),10.00f);
                    }
                    else
                    {
                        charge = 5.15f; //Cost of Check + Below $500 charge
                        account.setServiceCharge(charge);
                        message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" +
                                "Current Balance : $" + fmt.format(account.getBalance()) + "\n" +
                                "Service charge : Check -- charge $0.15" + "\n" +
                                "Service charge : Below $500.00 -- charge $5.00" + "\n" +
                                "Total service charge : $" + fmt.format(account.getServiceCharge());
                        JOptionPane.showMessageDialog(null, message);
                        below500 = true;
                        account.addTrans(3,account.getTransCount(),0.15f);
                        account.addTrans(3,account.getTransCount(),5.00f);
                    }
                    //done = false;
                }
                else if(account.getBalance() < 500.00 && below500 == false)
                {
                    charge = 5.15f; //Cost of Check + Below $500 charge
                    account.setServiceCharge(charge);
                    below500 = true;
                    if(account.getBalance() < 50.00)
                    {
                        message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" +
                            "Current Balance : $" + fmt.format(account.getBalance()) + "\n" +
                            "Service charge : Check -- charge $0.15" + "\n" +
                            "Warning : Balance below $50.00" + "\n" +
                            "Total service charge : $" + fmt.format(account.getServiceCharge());
                        JOptionPane.showMessageDialog(null, message);
                        account.addTrans(3,account.getTransCount(),0.15f);
                        account.addTrans(3,account.getTransCount(),5.00f);
                    }
                    else
                    {
                        message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" +
                                "Current Balance : $" + fmt.format(account.getBalance()) + "\n" +
                                "Service charge : Check -- charge $0.15" + "\n" +
                                "Service charge : Below $500.00 -- charge $5.00" + "\n" +
                                "Total service charge : $" + fmt.format(account.getServiceCharge());
                        JOptionPane.showMessageDialog(null, message);
                        account.addTrans(3,account.getTransCount(),0.15f);
                        account.addTrans(3,account.getTransCount(),5.00f);
                    }
                }
                else
                {
                    charge = 0.15f;
                    account.setServiceCharge(charge);
                    if(account.getBalance() < 50.00)
                    {
                        message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" +
                                "Current Balance : $" + fmt.format(account.getBalance()) + "\n" +
                                "Service charge : Check -- charge $0.15" + "\n" +
                                "Warning : Balance below $50.00" + "\n" +
                                "Total service charge : $" + fmt.format(account.getServiceCharge());
                        JOptionPane.showMessageDialog(null, message);
                        account.addTrans(3,account.getTransCount(),0.15f);
                    }
                    else
                    {
                        message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" +
                                "Current Balance : $" + fmt.format(account.getBalance()) + "\n" +
                                "Service charge : Check -- charge $0.15" + "\n" +
                                "Total service charge : $" + fmt.format(account.getServiceCharge());
                        JOptionPane.showMessageDialog(null, message);
                        account.addTrans(3,account.getTransCount(),0.15f);
                    }
                }
                //done = false;
            }
            else if(tCode == 2)
            {
                stringTransAmt = JOptionPane.showInputDialog("Enter the trans amount: ");
                transAmt = Float.parseFloat(stringTransAmt);
                charge = 0.10f;
                account.setServiceCharge(charge);
                account.setBalance(transAmt, tCode);
                account.addTrans(2,account.getTransCount(),transAmt);
                if(account.getBalance() <= 50.00)
                {
                    message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" +
                            "Current Balance : $" + fmt.format(account.getBalance()) + "\n" +
                            "Service charge : Deposit -- charge $0.10" + "\n" +
                            "Warning : Balance below $50.00" + "\n" +
                            "Total service charge : $" + fmt.format(account.getServiceCharge());
                    JOptionPane.showMessageDialog(null, message);
                    account.addTrans(3,account.getTransCount(),0.10f);
                }
                else
                {
                    message = "Transaction : Deposit in the amount of $" + fmt.format(transAmt) + "\n" +
                            "Current Balance : $" + fmt.format(account.getBalance()) + "\n" +
                            "Service charge : Deposit -- charge $0.10" + "\n" +
                            "Total service charge : $" + fmt.format(account.getServiceCharge());
                    JOptionPane.showMessageDialog(null, message);
                    account.addTrans(3,account.getTransCount(),0.10f);
                }
                //done = false;
            }
            else //tCode = 0
            {
                balanceBeforeCharge = account.getBalance();
                charge = account.getServiceCharge();
                account.setBalance(charge, tCode);
                message = "Transaction : End" + "\n" +
                          "Current Balance : $" + fmt.format(balanceBeforeCharge) + "\n" +
                          "Total service charge : $" + fmt.format(account.getServiceCharge()) + "\n" +
                          "Final Balance : $" + fmt.format(account.getBalance());
                JOptionPane.showMessageDialog(null, message);
                //done = true;
                System.exit(0); //Modify to window listener?
            }
        }
        frame.setVisible(true);
    }
    public static void listTransactions()
    {
        System.out.println(account);
    }
    public static void listChecks()
    {
        account.getTrans(1);
    }
    public static void listDeposits()
    {
        account.getTrans(2);
    }
}