I needed to take this programming class in Java for my CIS curriculum and I am having the toughest time with it. Some folks say its the easiest language to learn but i am having the worse experience. Any how can some take a look at my crappy code and point into the right direction? for one the calculation isnt working.
Main class:
import javax.swing.JFrame;
public class Main
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Checking Account GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
CheckingAcctTransactions panel = new CheckingAcctTransactions();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
Transaction class
public class Transaction
{
private int transNumber;
private int transId;
private double transAmt;
public Transaction(int number, int id, double amount)
{
transNumber = number;
transId = id;
transAmt = amount;
}
public int getTransNumber()
{
return transNumber;
}
public int getTransId()
{
return transId;
}
public double getTransAmount()
{
return transAmt;
}
}
Checking Acct Transactions class:
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.*;
// Checking Account Transaction CLASS
public class CheckingAcctTransactions extends JPanel
{
// instance variables
public static double initialBalance, transactionAmount;
public static int firstTime = 0;
public static CheckingAccount cash;
public static DecimalFormat dollar;
public static Transaction t;
public static int check = 1, deposit = 2, serviceCharge = 3;
private JLabel message; // GUI message
private JRadioButton transaction, listTrans, checks, deposits; // GUI buttons
ArrayList transList; // ARRAY LIST
public CheckingAcctTransactions()
{
initialBalance = initialBalance();
cash= new CheckingAccount(initialBalance);
dollar = new DecimalFormat("#,###.00");
message = new JLabel ("Choose Action: ");
message.setFont (new Font ("Helvitica", Font.BOLD, 24));
transaction = new JRadioButton("Entering a Transaction");
transaction.setBackground (Color.green);
listTrans = new JRadioButton("Listing All Transactions");
listTrans.setBackground(Color.green);
checks = new JRadioButton("Listing All Checks");
checks.setBackground(Color.green);
deposits = new JRadioButton("Listing All Deposits");
deposits.setBackground(Color.green);
ButtonGroup group = new ButtonGroup();
group.add(transaction);
group.add(listTrans);
group.add(checks);
group.add(deposits);
CheckingAcctTransactionsListener listener = new CheckingAcctTransactionsListener();
transaction.addActionListener(listener);
listTrans.addActionListener(listener);
checks.addActionListener(listener);
deposits.addActionListener(listener);
add(message);
add(transaction);
add(listTrans);
add(checks);
add(deposits);
setBackground(Color.green);
setPreferredSize (new Dimension(240, 170));
}
private class CheckingAcctTransactionsListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
int transactionCode;
double transAmt = 0;
int i = 0;
if(source==transaction)
{
transactionCode = getTransCode();
t = new Transaction(cash.getTransCount(),transactionCode,t ransAmt);
cash.addTrans(t);
switch(transactionCode)
{
case 1:
transAmt = getTransAmt();
cash.setBalance(transAmt, transactionCode);
processCheck(transAmt);
JOptionPane.showMessageDialog(null, transList.get(i));
break;
case 2:
transAmt = getTransAmt();
cash.setBalance(transAmt, transactionCode);
processDeposit(transAmt);
JOptionPane.showMessageDialog(null, transList.get(i));
break;
case 0:
JOptionPane.showMessageDialog(null, "Transaction: End\n"
+ "Current Balance: $" + cash.getBalance() + "\n"
+ "Total Service Charge: $" + dollar.format(cash.getServiceCharge()) + "\n"
+ "Final Balance: $"
+ dollar.format(cash.getBalance() - cash.getServiceCharge()));
break;
default:
JOptionPane.showMessageDialog(null, "Invalid Choice. Enter Again.");
}
}
else if(source==listTrans)
{
int types = 0;
int nums = 0;
double amount = 0;
String line;
line = String.format("List All Transactions" + "\n"
+ "ID Type Amount" + "\n");
for(int index = 0; index < cash.getSize(); index++)
{
types = cash.getTrans(index).getTransId();
nums = cash.getTrans(index).getTransNumber();
amount = cash.getTrans(index).getTransAmount();
line += String.format("%-10s %3d %10s", nums,types,amount) + "\n";
}
JTextArea text = new JTextArea(line);
text.setBorder(null);
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14));
JOptionPane.showMessageDialog(null, text);
}
else if(source==checks)
{
String line = String.format("Checks made: \n");
JTextArea text = new JTextArea(line);
text.setBorder(null);
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
JOptionPane.showMessageDialog(null, text);
}
else if(source==deposits)
{
String line = String.format("Deposits made: \n");
JTextArea text = new JTextArea(line);
text.setBorder(null);
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
JOptionPane.showMessageDialog(null, text);
}
} // END OF ACTION PERFORMED
} // END OF CheckingAcctTransactionsListener
public static int getTransCode()
{
int code;
String userInput;
userInput=JOptionPane.showInputDialog("Enter the transaction code:\n"+
"1) Check \n2) Deposit \n0) Exit the program");
code=Integer.parseInt(userInput);
return code;
}
public static double initialBalance()
{
double initialBalance;
String userInput;
userInput = JOptionPane.showInputDialog("Enter initial account balance :");
initialBalance = Double.parseDouble(userInput);
return initialBalance;
}
public static double getTransAmt()
{
double amount;
String userInput;
userInput=JOptionPane.showInputDialog("Enter the transaction amount: ");
amount=Double.parseDouble(userInput);
return amount;
}
public static double processCheck(double transAmt)
{
cash.setBalance(transAmt, 1);
cash.setServiceCharge(0.15);
if(cash.getBalance() < 500)
{
if(firstTime==0)
{
cash.setServiceCharge(5.00);
JOptionPane.showMessageDialog(null,"Transaction Amount: Check in Amount of $"
+ transactionAmount + "\n" +"Current Balance: $" + dollar.format(cash.getBalance())
+ "\n"+"Service Charge: Check --- charge $0.15 \n"
+ "Service Charge: Below $500 --- charge $5.00\n"
+ "Total Service Charge: $" + dollar.format(cash.getServiceCharge()) );
firstTime++;
}
JOptionPane.showMessageDialog(null,"Transaction Amount: Check in Amount of $"
+ transactionAmount + "\n" +"Current Balance: $" + dollar.format(cash.getBalance())
+ "\n"+"Service Charge: Check --- charge $0.15 \n"
+ "Total Service Charge: $" + dollar.format(cash.getServiceCharge()) );
}
else
{
JOptionPane.showMessageDialog(null,"Transaction Amount: Check in Amount of $"
+ transactionAmount + "\n" + "Current Balance: $"
+ dollar.format(cash.getBalance())
+ "\n"+"Service Charge: Check --- charge $0.15 \n"
+ "Service Charge: None\n" + "Total Service Charge: $"
+ dollar.format(cash.getServiceCharge()) );
}
if(cash.getBalance()<0)
{
cash.setServiceCharge(10.00);
JOptionPane.showMessageDialog(null, "Your balance is under $0.");
}
if(cash.getBalance()<50)
{
JOptionPane.showMessageDialog(null, "Your balance is under $50.");
}
return transAmt;
}
public static double processDeposit(double transAmt)
{
cash.setBalance(transAmt, 2);
cash.setServiceCharge(0.10);
if(cash.getBalance()<500)
{
if(firstTime==0)
{
cash.setServiceCharge(5.00);
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + "\n" +"Current Balance: "
+ dollar.format(cash.getBalance())
+ "\n"+"Service Charge: Deposit --- charge $0.10 \n"
+ "Service Charge: Below $500 --- charge $5.00\n"
+ "Total Service Charge: " + dollar.format(cash.getServiceCharge()));
}
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + "\n" +"Current Balance: "
+ dollar.format(cash.getBalance())
+ "\n"+"Service Charge: Deposit --- charge $0.10 \n"
+ "Total Service Charge: " + dollar.format(cash.getServiceCharge()));
}
else
{
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + "\n" +"Current Balance: "
+ dollar.format(cash.getBalance())
+ "\n"+"Service Charge: Deposit --- charge $0.10 \n"
+ "Service Charge: None\n" + "Total Service Charge: "
+ dollar.format(cash.getServiceCharge()));
}
if(cash.getBalance()<0)
{
cash.setServiceCharge(10.00);
JOptionPane.showMessageDialog(null, "Your balance is under $0.");
}
if(cash.getBalance()<50)
{
JOptionPane.showMessageDialog(null, "Your balance is under $50.");
}
return transAmt;
}
}
Checking acct class:
import java.util.ArrayList;
public class CheckingAccount
{
private double balance;
private double totalServiceCharge;
private ArrayList<Transaction> transList; // array
private int transCount = 0;
private int transSize = 0;
public CheckingAccount(double initialBalance)
{
balance = initialBalance;
totalServiceCharge = 0;
}
public double getBalance()
{
return balance;
}
public void setBalance(double transAmt, int tCode)
{
if(tCode==1)
balance-=transAmt;
else if(tCode==2)
balance+=transAmt;
}
public double getServiceCharge()
{
return totalServiceCharge;
}
public void setServiceCharge(double currentServiceCharge)
{
totalServiceCharge += currentServiceCharge;
}
public void addTrans(Transaction newTrans)
{
transList = new ArrayList<Transaction>();
transList.add(newTrans);
}
public int getTransCount()
{
transCount++;
return transCount;
}
public Transaction getTrans(int i)
{
return transList.get(i);
}
public int getSize()
{
transSize = transList.size();
return transSize;
}
}