Directory:Derek Elder/Programs/CheckingAccount6
MyWikiBiz, Author Your Legacy — Saturday December 21, 2024
Jump to navigationJump to searchMain.java
//******************************************************************* // Program 6 Author: Derek Elder //******************************************************************** package program06; import java.text.DecimalFormat; import javax.swing.*; //JFrame import java.io.*; import java.util.Vector; public class Main { public static boolean below500 = false; public static CheckingAccount account; public static EOptionsFrame frame; public static JFrame gameFrame; public static String filename = "C:\\accounts.txt"; public static Vector accountStore; public static JTextArea ta; public static void main(String[] args) { accountStore = new Vector(); frame = new EOptionsFrame("Checking Account Actions"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ta = new JTextArea(25,50); frame.getContentPane().add(ta); frame.pack(); frame.setVisible(true); } public static void addAccount() { String name, stringBalance; float balance, charge; charge = 0.0f; name = JOptionPane.showInputDialog("Enter your name: "); stringBalance = JOptionPane.showInputDialog("Enter your initial balance: "); balance = Float.parseFloat(stringBalance); account = new CheckingAccount(name, balance, charge); accountStore.addElement(account); } public static void doTransactions() { int tCode = 0; String stringTCode, stringTransAmt, message; float transAmt, charge, balanceBeforeCharge; boolean done = false; charge = 0.00f; frame.setVisible(false); while(!done) { stringTCode = JOptionPane.showInputDialog("0: End, 1: Check, 2: Deposit" + "\nEnter 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 = account.getName() + "'s account" + "\n" + "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 = account.getName() + "'s account" + "\n" + "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 = account.getName() + "'s account" + "\n" + "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 = account.getName() + "'s account" + "\n" + "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 = account.getName() + "'s account" + "\n" + "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 = account.getName() + "'s account" + "\n" + "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 = account.getName() + "'s account" + "\n" + "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 = account.getName() + "'s account" + "\n" + "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; } } frame.setVisible(true); } public static void listTransactions() { String message; message = account.toString(); ta.setText(message); } public static void listChecks() { String message; message = ""; message += "Listing all Checks for " + account.getName() + ":"; message += "\nID\t\tType\t\tAmount"; message += "\n" + account.getTrans(1); ta.setText(message); } public static void listDeposits() { String message; message = ""; message += "Listing all Deposits for " + account.getName() + ":"; message += "\nID\t\tType\t\tAmount"; message += "\n" + account.getTrans(2); ta.setText(message); } public static void findAccount() { String name, message; int index; name = JOptionPane.showInputDialog ("Enter the Account holder's name: "); for(index = 0; index != accountStore.size(); index++) { CheckingAccount datum = (CheckingAccount)accountStore.elementAt(index); if(name.equals(datum.getName())) { message = "The account for " + name + " has been found."; ta.setText(message); account = datum; } } } public static void readFromFile() { chooseFile(1); try { FileInputStream fis = new FileInputStream(filename); ObjectInputStream in = new ObjectInputStream(fis); Vector storeIn = (Vector)in.readObject(); in.close(); accountStore = storeIn; } 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(accountStore); 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(); } } public static void doGame() { int selection; String choice, message; frame.setVisible(false); choice = JOptionPane.showInputDialog("Would you like to read the rules first?" + "\n" + "1: Yes or 0: No."); selection = Integer.parseInt(choice); if(selection == 1) { message = "The rules are relatively straightforward." + "\n" + "You and your opponent each choose one of three types of weapons." + "\n" + "Each weapon has a strength over another and a weakness to a third." + "\n" + "Rock smashes Scissors, Scissors cuts Paper, and Paper covers Rock." + "\n" + "Your opponent in this game will be a computer whose weapon is randomly decided." + "\n" + "The game will now commence. Good luck."; JOptionPane.showMessageDialog(null, message); } gameFrame = new JFrame("Checking Account Actions"); gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); RockPaperScissors panel = new RockPaperScissors(); gameFrame.getContentPane().add(panel); gameFrame.pack(); gameFrame.setVisible(true); //frame.setVisible(true); } }
Account.java
//******************************************************************* // Account Class Author: Derek Elder //******************************************************************** package program06; import java.io.Serializable; public class Account implements Serializable { protected String name; protected float balance; public Account(String initialName, float initialBalance) { name = initialName; balance = initialBalance; } public String getName() { 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; } }
CheckingAccount.java
//******************************************************************* // CheckingAccount Class Author: Derek Elder //******************************************************************** package program06; import java.text.NumberFormat; public class CheckingAccount extends Account { private float totalServiceCharge; 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 String getTrans(int checkOrDeposit) { int i; String message = ""; for(i = 0;i < transCount;i++) { if(tList[i].getTransID() == checkOrDeposit) { message += tList[i].toString(); } } return message; } public int getTransCount() { return transCount; } public void setTransCount(int tCount) { transCount = tCount; } public float getServiceCharge() { return totalServiceCharge; } public float setServiceCharge(float currentServiceCharge) { totalServiceCharge += currentServiceCharge; return totalServiceCharge; } public CheckingAccount() { super("", 0.0f); totalServiceCharge = 0; tList = new Transaction[10]; transCount = 0; } public CheckingAccount(String currentName, float currentBalance, float currentServiceCharge) { super(currentName, currentBalance); totalServiceCharge = currentServiceCharge; tList = new Transaction[10]; transCount = 0; } public String toString() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); int i; String report = "Account Name: " + name + "\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; } }
EOptionsFrame.java
//******************************************************************* // EOptionsFrame Class Author: Derek Elder //******************************************************************** package program06; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class EOptionsFrame extends JFrame implements WindowListener { public static final int WIDTH = 300; public static final int HEIGHT = 200; private JMenu fileMenu, accountsMenu, transactionsMenu, gameMenu; private JMenuItem readFile, writeFile, addAccount, listTransactions, findChecks, findDeposits, findAccount, addTransactions, RPSGame; public EOptionsFrame(String title ) { super(title); addWindowListener(this); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenu fileMenu = new JMenu("File"); MenuListener ml = new MenuListener(); JMenuItem readFile = new JMenuItem("Read from file"); readFile.addActionListener(ml); fileMenu.add(readFile); JMenuItem writeFile = new JMenuItem("Write to file"); writeFile.addActionListener(ml); fileMenu.add(writeFile); JMenu accountsMenu = new JMenu("Account"); JMenuItem addAccount = new JMenuItem("Add new account"); addAccount.addActionListener(ml); accountsMenu.add(addAccount); JMenuItem listTransactions = new JMenuItem("List account transactions"); listTransactions.addActionListener(ml); accountsMenu.add(listTransactions); JMenuItem findChecks = new JMenuItem("List all checks"); findChecks.addActionListener(ml); accountsMenu.add(findChecks); JMenuItem findDeposits = new JMenuItem("List all deposits"); findDeposits.addActionListener(ml); accountsMenu.add(findDeposits); JMenuItem findAccount = new JMenuItem("Find an account"); findAccount.addActionListener(ml); accountsMenu.add(findAccount); JMenu transactionsMenu = new JMenu("Transactions"); JMenuItem addTransactions = new JMenuItem("Add transactions"); addTransactions.addActionListener(ml); transactionsMenu.add(addTransactions); JMenu gameMenu = new JMenu("Game"); JMenuItem RPSGame = new JMenuItem("Rock - Paper - Scissors"); RPSGame.addActionListener(ml); gameMenu.add(RPSGame); JMenuBar bar = new JMenuBar( ); bar.add(fileMenu); bar.add(accountsMenu); bar.add(transactionsMenu); bar.add(gameMenu); setJMenuBar(bar); } public void windowClosing(WindowEvent e) { int fileYea; String selection; selection = JOptionPane.showInputDialog("Save the account to a file? " + "(Enter 1 to save)"); fileYea = Integer.parseInt(selection); if(fileYea == 1) { Main.writeToFile(); } this.setVisible(false); System.exit(0); } public void windowClosed(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} //***************************************************************** // Represents the listener for the radio buttons //***************************************************************** private class MenuListener implements ActionListener { public void actionPerformed (ActionEvent event) { String source = event.getActionCommand(); if(source.equals("Add new account")) { Main.addAccount(); } else if(source.equals("List account transactions")) { Main.listTransactions(); } else if(source.equals("List all checks")) { Main.listChecks(); } else if(source.equals("List all deposits")) { Main.listDeposits(); } else if(source.equals("Read from file")) { Main.readFromFile(); } else if (source.equals("Write to file")) { Main.writeToFile(); } else if (source.equals("Find an account")) { Main.findAccount(); } else if (source.equals("Add transactions")) { Main.doTransactions(); } else if (source.equals("Rock - Paper - Scissors")) { Main.doGame(); } } } }
ImagePanel.java
//******************************************************************* // ImagePanel Class Author: Derek Elder //******************************************************************** package program06; import java.awt.*; import javax.swing.*; public class ImagePanel extends JPanel { private final int WIDTH = 500; private final int HEIGHT = 100; public ImageIcon image; public ImagePanel() { setBackground(Color.white); setPreferredSize(new Dimension(WIDTH, HEIGHT)); } public void paintComponent(Graphics g) { super.paintComponent(g); if(image != null) image.paintIcon(this, g, 175, 10); } }
PlayClip.java
//******************************************************************* // PlayClip Class Author: Derek Elder //******************************************************************** package program06; import java.applet.*; import java.net.*; public class PlayClip { public static void play(String url) throws InterruptedException { try { //long delay = 5; AudioClip clip = Applet.newAudioClip(new URL(url)); clip.play(); //Thread.sleep(delay); } catch (MalformedURLException murle) { System.out.println(murle); } } }
Transaction.java
//******************************************************************* // Transaction Class Author: Derek Elder //******************************************************************** package program06; import java.io.Serializable; import java.text.NumberFormat; public class Transaction implements Serializable { private int transID; private int transNumber; private float transAmt; private String transType; public int getTransID() { return transID; } 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; } }
RockPaperScissors.java
//******************************************************************* // RockPaperScissors Class Author: Derek Elder //******************************************************************** package program06; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RockPaperScissors extends JPanel { private final int WIDTH = 500; private final int HEIGHT = 200; private JButton rock, paper, scissors; private JLabel printout; private ImagePanel imagePanel; private int userChoice = 0; private int compChoice = 0; private int result = 0; private String comp, user; private static String winurl = "file:C:/Documents and Settings/Owner/Desktop/Java/Program06/youwin.wav"; //Win Audio File private static String loseurl = "file:C:/Documents and Settings/Owner/Desktop/Java/Program06/youlose.wav"; //Loss Audio File private static String tieurl = "file:C:/Documents and Settings/Owner/Desktop/Java/Program06/youtie.wav"; //Tie Audio File private static String[] imageList = { "rockrock.jpg", "rockpaper.jpg", "rockscissors.jpg", "paperrock.jpg", "paperpaper.jpg", "paperscissors.jpg", "scissorsrock.jpg", "scissorspaper.jpg", "scissorsscissors.jpg" }; public RockPaperScissors() { setLayout(new BorderLayout()); imagePanel = new ImagePanel(); rock = new JButton(" Rock "); paper = new JButton(" Paper "); scissors = new JButton(" Scissors "); printout = new JLabel("Choose your weapon."); //Add the components to the JPanel add(imagePanel, BorderLayout.NORTH); add(rock, BorderLayout.WEST); add(paper, BorderLayout.CENTER); add(scissors, BorderLayout.EAST); add(printout, BorderLayout.SOUTH); ButtonGroup group = new ButtonGroup(); group.add(rock); group.add(paper); group.add(scissors); RockPaperScissorsListener listener = new RockPaperScissorsListener(); rock.addActionListener(listener); paper.addActionListener(listener); scissors.addActionListener(listener); setBackground(Color.cyan); setPreferredSize(new Dimension(WIDTH, HEIGHT)); } //***************************************************************** // Represents the listener for the radio buttons //***************************************************************** private class RockPaperScissorsListener implements ActionListener { public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if(source == rock) { userChoice = 1; user = "Rock"; } else if(source == paper) { userChoice = 2; user = "Paper"; } else { userChoice = 3; user = "Scissors"; } result = mechanics(); //Image Display - Player on the left, computer on the right if(userChoice == 1 && compChoice == 1) { imagePanel.image = new ImageIcon(imageList[0]); imagePanel.repaint(); } else if(userChoice == 1 && compChoice == 2) { imagePanel.image = new ImageIcon(imageList[1]); imagePanel.repaint(); } else if(userChoice == 1 && compChoice == 3) { imagePanel.image = new ImageIcon(imageList[2]); imagePanel.repaint(); } else if(userChoice == 2 && compChoice == 1) { imagePanel.image = new ImageIcon(imageList[3]); imagePanel.repaint(); } else if(userChoice == 2 && compChoice == 2) { imagePanel.image = new ImageIcon(imageList[4]); imagePanel.repaint(); } else if(userChoice == 2 && compChoice == 3) { imagePanel.image = new ImageIcon(imageList[5]); imagePanel.repaint(); } else if(userChoice == 3 && compChoice == 1) { imagePanel.image = new ImageIcon(imageList[6]); imagePanel.repaint(); } else if(userChoice == 3 && compChoice == 2) { imagePanel.image = new ImageIcon(imageList[7]); imagePanel.repaint(); } else //if(userChoice == 3 && compChoice == 3) { imagePanel.image = new ImageIcon(imageList[8]); imagePanel.repaint(); } //Display result and play corresponding sound file if(result == 1) { printout.setText("Player chose: " + user + ", Computer chose: " + comp + ", The Player has won."); try { PlayClip.play(winurl); } catch(InterruptedException ie) { System.out.println(ie); } } else if(result == 2) { printout.setText("Player chose: " + user + ", Computer chose: " + comp + ", The Player has lost."); try { PlayClip.play(loseurl); } catch(InterruptedException ie) { System.out.println(ie); } } else //if(result == 3) { printout.setText("Player chose: " + user + ", Computer chose: " + comp + ", Tie Game."); try { PlayClip.play(tieurl); } catch(InterruptedException ie) { System.out.println(ie); } } } } public int mechanics() { double tempValue = (Math.random() * 3); compChoice = (int)Math.floor(tempValue)+1; if(compChoice == 1) comp = "Rock"; if(compChoice == 2) comp = "Paper"; if(compChoice == 3) comp = "Scissors"; if(userChoice == 1) { if(compChoice == 1) return 3; if(compChoice == 2) return 2; if(compChoice == 3) return 1; } else if(userChoice == 2) { if(compChoice == 1) return 1; if(compChoice == 2) return 3; if(compChoice == 3) return 2; } else //if(userChoice == 3) { if(compChoice == 1) return 2; if(compChoice == 2) return 1; if(compChoice == 3) return 3; } return 0; //Fixes Missing return statement error } }