Line 60: |
Line 60: |
| add(six); | | add(six); |
| setBackground(Color.blue); | | setBackground(Color.blue); |
− | setPreferredSize(new Dimension(350, 100)); | + | setPreferredSize(new Dimension(400, 100)); |
| } | | } |
| //***************************************************************** | | //***************************************************************** |
Line 106: |
Line 106: |
| | | |
| package program04; | | package program04; |
− | | + | import java.io.Serializable; |
| import java.text.NumberFormat; | | import java.text.NumberFormat; |
| | | |
− | public class Transaction | + | public class Transaction implements Serializable |
| { | | { |
| private int transID; | | private int transID; |
− | private int transNumber; //Current value of transCount | + | private int transNumber; |
| private float transAmt; | | private float transAmt; |
| private String transType; | | private String transType; |
− |
| + | |
| public int getTransID() | | public int getTransID() |
| { | | { |
− | return transID; //1 = check, 2 = deposit 3 = service charge | + | return transID; |
| } | | } |
| public int getTransNumber() | | public int getTransNumber() |
Line 138: |
Line 138: |
| NumberFormat fmt = NumberFormat.getCurrencyInstance(); | | NumberFormat fmt = NumberFormat.getCurrencyInstance(); |
| String description; | | String description; |
− |
| + | |
| if(transID == 1) | | if(transID == 1) |
− | {
| |
| transType = "Check"; | | transType = "Check"; |
− | }
| |
| else if(transID == 2) | | else if(transID == 2) |
− | {
| |
| transType = "Deposit"; | | transType = "Deposit"; |
− | }
| |
| else //(transID == 3) | | else //(transID == 3) |
− | {
| |
| transType = "S.Ch."; | | transType = "S.Ch."; |
− | }
| |
| | | |
| description = transNumber + "\t\t" + transType + "\t\t" + fmt.format(transAmt); | | description = transNumber + "\t\t" + transType + "\t\t" + fmt.format(transAmt); |
Line 163: |
Line 157: |
| // Account Class Author: Derek Elder | | // Account Class Author: Derek Elder |
| //******************************************************************** | | //******************************************************************** |
| + | |
| package program04; | | package program04; |
| + | import java.io.Serializable; |
| | | |
− | public class Account | + | public class Account implements Serializable |
| { | | { |
− | private String name; | + | protected String name; |
− | private double balance;
| + | protected float balance; |
− | | + | |
− | public String getName()
| + | public Account(String initialName, float initialBalance) |
− | {
| + | { |
− | return name;
| + | name = initialName; |
− | }
| + | balance = initialBalance; |
− | public double getBalance()
| + | } |
− | {
| + | public String getName() |
− | return balance;
| + | { |
− | }
| + | return name; |
| + | } |
| + | 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; |
| + | } |
| } | | } |
| </pre> | | </pre> |
Line 190: |
Line 199: |
| import java.text.NumberFormat; | | import java.text.NumberFormat; |
| | | |
− | public class CheckingAccount | + | public class CheckingAccount extends Account |
| { | | { |
− | private float balance;
| + | private float totalServiceCharge; |
− | private float totalServiceCharge; //Changed from double | |
| private int transCount; | | private int transCount; |
| private Transaction[] tList; | | private Transaction[] tList; |
Line 201: |
Line 209: |
| if(transCount == tList.length) | | if(transCount == tList.length) |
| increaseArray(); | | increaseArray(); |
− |
| + | |
| tList[transCount] = new Transaction(transID, transNumber, transAmt); | | tList[transCount] = new Transaction(transID, transNumber, transAmt); |
| transCount++; | | transCount++; |
Line 215: |
Line 223: |
| } | | } |
| } | | } |
| + | } |
| + | public Transaction getSingleTrans(int i) |
| + | { |
| + | return tList[i]; |
| } | | } |
| public int getTransCount() | | public int getTransCount() |
Line 220: |
Line 232: |
| return transCount; | | return transCount; |
| } | | } |
− | public float getBalance() | + | public void setTransCount(int tCount) |
| { | | { |
− | return balance; | + | transCount = tCount; |
− | }
| |
− | 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() | | public float getServiceCharge() |
Line 241: |
Line 245: |
| return totalServiceCharge; | | return totalServiceCharge; |
| } | | } |
− | public CheckingAccount() //Needed? | + | public CheckingAccount() |
| { | | { |
− | balance = 0; | + | //balance = 0; |
| + | super("", 0.0f); |
| totalServiceCharge = 0; | | totalServiceCharge = 0; |
| tList = new Transaction[10]; | | tList = new Transaction[10]; |
| transCount = 0; | | transCount = 0; |
| } | | } |
− | public CheckingAccount(float currentBalance, float currentServiceCharge) | + | public CheckingAccount(String currentName, float currentBalance, float currentServiceCharge) |
| { | | { |
− | balance = currentBalance; | + | //balance = currentBalance; |
| + | super(currentName, currentBalance); |
| totalServiceCharge = currentServiceCharge; | | totalServiceCharge = currentServiceCharge; |
| tList = new Transaction[10]; | | tList = new Transaction[10]; |
Line 262: |
Line 268: |
| String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; | | String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; |
| report += "My Transaction Status\n\n"; | | report += "My Transaction Status\n\n"; |
| + | report += "Name: " + name + "\n"; |
| report += "Balance: " + balance + "\n"; | | report += "Balance: " + balance + "\n"; |
| report += "Total Service Charge: " + fmt.format(totalServiceCharge) + "\n"; | | report += "Total Service Charge: " + fmt.format(totalServiceCharge) + "\n"; |
Line 268: |
Line 275: |
| report += "ID\t\tType\t\tAmount\n"; | | report += "ID\t\tType\t\tAmount\n"; |
| report += "---------------------------------------\n"; | | report += "---------------------------------------\n"; |
− |
| + | |
| for(i = 0; i < transCount; i++) | | for(i = 0; i < transCount; i++) |
| report += tList[i] + "\n"; | | report += tList[i] + "\n"; |
Line 295: |
Line 302: |
| import java.text.DecimalFormat; | | import java.text.DecimalFormat; |
| import javax.swing.JOptionPane; | | import javax.swing.JOptionPane; |
− | import javax.swing.JFrame; | + | import javax.swing.*; //JFrame |
| + | import java.io.*; |
| | | |
| public class Main | | public class Main |
| { | | { |
− | public static JFrame frame; | + | //public static JFrame frame; |
| + | public static JFrameL frame; |
| public static CheckingAccount account = null; | | public static CheckingAccount account = null; |
| public static boolean below500 = false; | | public static boolean below500 = false; |
− | public static String filename = "C:\\student\\accounts.dat"; | + | public static String filename = "C:\\accounts.txt"; |
− |
| + | |
| public static void main(String[] args) | | public static void main(String[] args) |
| { | | { |
− | frame = new JFrame("Checking Account Actions"); | + | frame = new JFrameL("Checking Account Actions"); |
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| GUI panel = new GUI(); | | GUI panel = new GUI(); |
Line 316: |
Line 325: |
| { | | { |
| int tCode = 0; | | int tCode = 0; |
− | String stringBalance, stringTCode, stringTransAmt, message;
| + | String stringBalance, stringTCode, stringTransAmt, message; |
| + | String name; |
| float balance, transAmt, charge, balanceBeforeCharge; | | float balance, transAmt, charge, balanceBeforeCharge; |
| //boolean done = false; | | //boolean done = false; |
Line 323: |
Line 333: |
| if(account == null) | | if(account == null) |
| { | | { |
| + | name = JOptionPane.showInputDialog("Enter your name: "); |
| stringBalance = JOptionPane.showInputDialog("Enter your initial balance: "); | | stringBalance = JOptionPane.showInputDialog("Enter your initial balance: "); |
| balance = Float.parseFloat(stringBalance); | | balance = Float.parseFloat(stringBalance); |
| //CheckingAccount account = new CheckingAccount(); //Old version | | //CheckingAccount account = new CheckingAccount(); //Old version |
− | account = new CheckingAccount(balance, charge); | + | account = new CheckingAccount(name, balance, charge); |
| } | | } |
| | | |
Line 347: |
Line 358: |
| charge = 10.15f; //Cost of Check + Below $0 charge | | charge = 10.15f; //Cost of Check + Below $0 charge |
| account.setServiceCharge(charge); | | account.setServiceCharge(charge); |
− | message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + | + | message = account.getName() + "'s account" + "\n" + |
| + | "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + |
| "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + | | "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + |
| "Service charge : Check -- charge $0.15" + "\n" + | | "Service charge : Check -- charge $0.15" + "\n" + |
Line 361: |
Line 373: |
| charge = 5.15f; //Cost of Check + Below $500 charge | | charge = 5.15f; //Cost of Check + Below $500 charge |
| account.setServiceCharge(charge); | | account.setServiceCharge(charge); |
− | message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + | + | message = account.getName() + "'s account" + "\n" + |
| + | "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + |
| "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + | | "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + |
| "Service charge : Check -- charge $0.15" + "\n" + | | "Service charge : Check -- charge $0.15" + "\n" + |
Line 380: |
Line 393: |
| if(account.getBalance() < 50.00) | | if(account.getBalance() < 50.00) |
| { | | { |
− | message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + | + | message = account.getName() + "'s account" + "\n" + |
| + | "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + |
| "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + | | "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + |
| "Service charge : Check -- charge $0.15" + "\n" + | | "Service charge : Check -- charge $0.15" + "\n" + |
Line 391: |
Line 405: |
| else | | else |
| { | | { |
− | message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + | + | message = account.getName() + "'s account" + "\n" + |
| + | "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + |
| "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + | | "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + |
| "Service charge : Check -- charge $0.15" + "\n" + | | "Service charge : Check -- charge $0.15" + "\n" + |
Line 407: |
Line 422: |
| if(account.getBalance() < 50.00) | | if(account.getBalance() < 50.00) |
| { | | { |
− | message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + | + | message = account.getName() + "'s account" + "\n" + |
| + | "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + |
| "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + | | "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + |
| "Service charge : Check -- charge $0.15" + "\n" + | | "Service charge : Check -- charge $0.15" + "\n" + |
Line 417: |
Line 433: |
| else | | else |
| { | | { |
− | message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + | + | message = account.getName() + "'s account" + "\n" + |
| + | "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + |
| "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + | | "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + |
| "Service charge : Check -- charge $0.15" + "\n" + | | "Service charge : Check -- charge $0.15" + "\n" + |
Line 437: |
Line 454: |
| if(account.getBalance() <= 50.00) | | if(account.getBalance() <= 50.00) |
| { | | { |
− | message = "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + | + | message = account.getName() + "'s account" + "\n" + |
| + | "Transaction : Check in the amount of $" + fmt.format(transAmt) + "\n" + |
| "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + | | "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + |
| "Service charge : Deposit -- charge $0.10" + "\n" + | | "Service charge : Deposit -- charge $0.10" + "\n" + |
Line 447: |
Line 465: |
| else | | else |
| { | | { |
− | message = "Transaction : Deposit in the amount of $" + fmt.format(transAmt) + "\n" + | + | message = account.getName() + "'s account" + "\n" + |
| + | "Transaction : Deposit in the amount of $" + fmt.format(transAmt) + "\n" + |
| "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + | | "Current Balance : $" + fmt.format(account.getBalance()) + "\n" + |
| "Service charge : Deposit -- charge $0.10" + "\n" + | | "Service charge : Deposit -- charge $0.10" + "\n" + |
Line 467: |
Line 486: |
| JOptionPane.showMessageDialog(null, message); | | JOptionPane.showMessageDialog(null, message); |
| //done = true; | | //done = true; |
− | System.exit(0); //Modify to window listener? | + | System.exit(0); |
| } | | } |
| } | | } |
Line 478: |
Line 497: |
| public static void listChecks() | | public static void listChecks() |
| { | | { |
| + | System.out.println("\nListing all Checks for " + account.getName() + ":"); |
| + | System.out.println("ID\t\tType\t\tAmount"); |
| + | System.out.println("---------------------------------------"); |
| account.getTrans(1); | | account.getTrans(1); |
| } | | } |
| public static void listDeposits() | | public static void listDeposits() |
| { | | { |
| + | System.out.println("\nListing all Deposits for " + account.getName() + ":"); |
| + | System.out.println("ID\t\tType\t\tAmount"); |
| + | System.out.println("---------------------------------------"); |
| account.getTrans(2); | | account.getTrans(2); |
| + | } |
| + | public static void readFromFile() |
| + | { |
| + | chooseFile(1); |
| + | |
| + | try |
| + | { |
| + | FileInputStream fis = new FileInputStream(filename); |
| + | ObjectInputStream in = new ObjectInputStream(fis); |
| + | |
| + | account = (CheckingAccount)in.readObject(); |
| + | in.close(); |
| + | } |
| + | catch(ClassNotFoundException e) |
| + | { |
| + | System.out.println(e); |
| + | } |
| + | catch(IOException e) |
| + | { |
| + | System.out.println(e); |
| + | } |
| + | } |
| + | public static void writeToFile() |
| + | { |
| + | chooseFile(2); |
| + | |
| + | try |
| + | { |
| + | FileOutputStream fos = new FileOutputStream(filename); |
| + | ObjectOutputStream out = new ObjectOutputStream(fos); |
| + | |
| + | out.writeObject(account); |
| + | out.close(); |
| + | } |
| + | catch(IOException e) |
| + | { |
| + | System.out.println(e); |
| + | } |
| + | } |
| + | public static void chooseFile(int ioOption) |
| + | { |
| + | int status, confirm; |
| + | String message = "Would you like to use the current default file: \n" + filename; |
| + | |
| + | confirm = JOptionPane.showConfirmDialog (null, message); |
| + | |
| + | if(confirm == JOptionPane.YES_OPTION) |
| + | { |
| + | return; |
| + | } |
| + | |
| + | JFileChooser chooser = new JFileChooser(); |
| + | |
| + | if(ioOption == 1) |
| + | { |
| + | status = chooser.showOpenDialog(null); |
| + | } |
| + | else |
| + | { |
| + | status = chooser.showSaveDialog(null); |
| + | } |
| + | |
| + | if(status == JFileChooser.APPROVE_OPTION) |
| + | { |
| + | File file = chooser.getSelectedFile(); |
| + | filename = file.getPath(); |
| + | } |
| } | | } |
| } | | } |
| </pre> | | </pre> |
− | ==Code from Class== | + | ==JFrameL.java== |
| <pre> | | <pre> |
− | public static void readElements()
| + | //******************************************************************* |
− | {
| + | // JFrameL - WindowListener Options Author: Derek Elder |
− | chooseFile(1);
| + | //******************************************************************** |
− | try
| + | |
− | {
| + | package program04; |
− | FileInputStream fis = new
| + | import javax.swing.*; |
− | FileInputStream(filename);
| + | import java.awt.*; |
− | ObjectInputStream in = new
| + | import java.awt.event.*; |
− | ObjectInputStream(fis);
| + | import java.io.*; |
− | Integer eCountObj = (Integer)in.readObject();
| |
− | int eCount = eCountObj.intValue();
| |
− | System.out.println("count of objects in file = "+ eCount);
| |
− | for (int i = 0; i < eCount; i++)
| |
− | {
| |
− | System.out.println("reading object "+ i);
| |
− | eList[i] = (ChemicalElement)in.readObject();
| |
− | }
| |
− | in.close();
| |
− | ChemicalElement.setNumOfElements(eCount);
| |
− | }
| |
− | catch(ClassNotFoundException e)
| |
− | {
| |
− | System.out.println(e);
| |
− | }
| |
| | | |
− | catch (IOException e)
| + | public class JFrameL extends JFrame implements WindowListener |
− | {
| + | { |
− | System.out.println(e);
| + | public JFrameL(String title) |
− | }
| + | { |
− | }
| + | super(title); |
− | public static void writeElements()
| + | addWindowListener(this); |
− | {
| + | } |
− | chooseFile(2); | + | public void windowClosing(WindowEvent e) |
− | try
| + | { |
− | {
| + | int fileYea; |
− | FileOutputStream fos = new
| + | String selection; |
− | FileOutputStream(filename);
| + | selection = JOptionPane.showInputDialog("Save the account to a file? " + |
− | ObjectOutputStream out = new
| + | "(Enter 1 to save)"); |
− | ObjectOutputStream(fos);
| + | fileYea = Integer.parseInt(selection); |
− | int eCount = ChemicalElement.getNumOfElements();
| + | if(fileYea == 1) |
− | Integer eCountObj = new Integer(eCount);
| + | { |
− | out.writeObject(eCountObj);
| + | Main.writeToFile(); |
− | for (int i = 0; i < eCount; i++)
| + | } |
− | {
| + | this.setVisible(false); |
− | System.out.println("writing object "+ i);
| + | System.exit(0); |
− | out.writeObject(eList[i]);
| + | } |
− | }
| + | public void windowClosed(WindowEvent e) |
− | out.close();
| + | {} |
− |
| + | public void windowOpened(WindowEvent e) |
− | }
| + | {} |
− | catch(IOException e)
| + | public void windowIconified(WindowEvent e) |
− | {
| + | {} |
− | System.out.println(e);
| + | public void windowDeiconified(WindowEvent e) |
− | }
| + | {} |
− |
| + | public void windowActivated(WindowEvent e) |
− | }
| + | {} |
− | public static void chooseFile(int ioOption)
| + | public void windowDeactivated(WindowEvent e) |
− | {
| + | {} |
− | int status, confirm;
| + | } |
− |
| |
− | String message = "Would you like to use the current default file: \n" +
| |
− | filename;
| |
− | confirm = JOptionPane.showConfirmDialog (null, message);
| |
− | if (confirm == JOptionPane.YES_OPTION)
| |
− | return;
| |
− | JFileChooser chooser = new JFileChooser();
| |
− | if (ioOption == 1)
| |
− | status = chooser.showOpenDialog (null);
| |
− | else
| |
− | status = chooser.showSaveDialog (null);
| |
− | if (status == JFileChooser.APPROVE_OPTION)
| |
− | {
| |
− | File file = chooser.getSelectedFile();
| |
− | filename = file.getPath();
| |
− | }
| |
− | }
| |
| </pre> | | </pre> |