분류 | 게시판 |
베스트 |
|
유머 |
|
이야기 |
|
이슈 |
|
생활 |
|
취미 |
|
학술 |
|
방송연예 |
|
방송프로그램 |
|
디지털 |
|
스포츠 |
|
야구팀 |
|
게임1 |
|
게임2 |
|
기타 |
|
운영 |
|
임시게시판 |
|
프로그램이 너무 길고 4가지 파일로 나뉘어져 있어서 도와주실 분들 찾기 어려울거라고 알지만
정말 생명의 은인찾는 심정으로 한번 올려봅니다....ㅠㅠ
패키지 하나 안에 A2, CheckingAccount, Transaction, TransPanel이라는 .class파일들이 있습니다.
A2가 메인인데 그안에서 TransPanel을 이용해서 Actionlistener이런것들을 사용한 윈도우창을 하나 띄우고
1. 거래하기, 2. 모든 거래내역 보기, 3. 모든 출금 거래내역보기, 4. 모든 입금 거래내역보기 중에 선택해서
사용자로부터 거래하기를 클릭받앗을경우는 1. 입금하기, 2. 출금하기, 0. 끝내기 라고 따로 또 번호를 받습니다.
이런식으로 윈도우 버튼창은 계속 떠있으니까 계속 입금할수도 있고 출금할수도있고 마지막에 2. 모든거래내역보기 혹은 모든 출금거래내역 보기 등
을 눌렀을 경우는 모든거래내역, 혹은 딱 출금내역들만 이렇게 출력해내야 하는데
모든 거래내역을 어떻게 컴퓨터가 기억하고 있게끔해서 출력해 내고, 출금내역들만은 또 어떻게 출력해 내는지 모르겠습니다.
A2안에서 출력하라고 명령해야 하는것 같고 문제는 지금 CheckingAccount안에 있는것 같습니다.
for 을 사용해서 CheckingAccount안에 있는 정보들을 이용해야 하는것 같은데 아직도 초보라.. 프로그램들 올려봅니다....
----------------------------------------------A2----------------------------------------------
import java.text.DecimalFormat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class A2
{
// global variables:
public static CheckingAccount CA; //define a CheckingAccount object to keep track of the account information.
public static boolean under500 = true;
public static boolean under50 = true;
public static boolean under0 = true;
public static JFrame frame;
public static void main (String[] args)
{
double initialBalance;
String numbal; // defines local variables
numbal = JOptionPane.showInputDialog ("Enter the initial balance: ");
initialBalance = Double.parseDouble(numbal); // get initial balance from the user
CA = new CheckingAccount(initialBalance);
frame = new JFrame ("Checking Account actions");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
TransPanel panel = new TransPanel();
frame.getContentPane().add (panel);
frame.pack();
frame.setVisible(true);
}
public static int getTransCode()
{
int transCode;
String numtrans;
numtrans = JOptionPane.showInputDialog ("Enter trans code: ");
transCode = Integer.parseInt(numtrans);
return transCode;
}
public static void inputTransCode()
{
int transCode;
double transAmt;
transCode = getTransCode();
if (transCode == 1)
{
transAmt = getTransAmt();
CA.setBalance(transAmt, transCode);
processCheck(transAmt);
}
else if (transCode == 2)
{
transAmt = getTransAmt();
CA.setBalance(transAmt, transCode);
processDeposit(transAmt);
}
else if (transCode == 0)
{
if (CA.getBalance() < 0)
{
double currentbal;
String message2;
DecimalFormat formatter = new DecimalFormat("#0.00");
currentbal = CA.getBalance();
currentbal = -1*currentbal;
message2 = "Transaction: End\n" + "Current Balance:($" + formatter.format(currentbal) + ")\n" +
"Total Service Charge:$" + formatter.format(CA.getServiceCharge()) + "\n" +
"Final Balance: ($" + formatter.format((currentbal+CA.getServiceCharge())) + ")\n";
JOptionPane.showMessageDialog(null, message2);
}
else if (CA.getBalance() > 0)
{
double currentbal;
String message2;
DecimalFormat formatter = new DecimalFormat("#0.00");
currentbal = CA.getBalance();
if (currentbal > CA.getServiceCharge())
{
currentbal = -1*currentbal;
message2 = "Transaction: End\n" + "Current Balance: $" + formatter.format(-1*currentbal) + "\n" +
"Total Service Charge: $" + formatter.format(CA.getServiceCharge()) + "\n" +
"Final Balance: $" + formatter.format(currentbal - CA.getServiceCharge()) + "\n";
JOptionPane.showMessageDialog(null, message2);
}
else if (currentbal < CA.getServiceCharge())
{
currentbal = -1*currentbal;
message2 = "Transaction: End\n" +
"Current Balance: $" + formatter.format(currentbal) + "\n" +
"Total Service Charge: $" + formatter.format(CA.getServiceCharge()) + "\n" +
"Final Balance: ($" + formatter.format(currentbal + CA.getServiceCharge()) + ")\n";
JOptionPane.showMessageDialog(null, message2);
}
}
System.exit(0);
}
}
public static double getTransAmt()
{
double transAmt;
String numamt;
numamt = JOptionPane.showInputDialog ("Enter transaction amount: ");
transAmt = Double.parseDouble(numamt);
return transAmt;
}
public static void listTrans()
{
JTextArea text = new JTextArea();
String message = "";
int num = CA.gettransCount() - 1;
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
text.setBorder(null);
message+="List all transactions:\n\n" + "ID Type Amount\n";
for (num = 0; num < CA.gettransCount(); num++)
{
Transaction trans = CA.getTrans(num);
message += String.format("%3d %-10s %3d\n",
num, trans.getTransId(), trans.getTransAmount());
}
text.setText(message);
JOptionPane.showMessageDialog(null, text);
}
public static void listChecks()
{
JTextArea text = new JTextArea();
String message = "";
int num = CA.gettransCount() - 1;
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
text.setBorder(null);
message+="Checks cashed:\n\n" + "ID Amount\n";
for (num = 0; num < CA.gettransCount(); num++)
{
Transaction trans = CA.getTrans(num);
if (trans.getTransId() == 1)
{
message += String.format("%3d %3d\n", num, trans.getTransAmount());
}
}
text.setText(message);
JOptionPane.showMessageDialog(null, text);
}
public static void listDeposits()
{
JTextArea text = new JTextArea();
String message = "";
int num = CA.gettransCount() - 1;
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
text.setBorder(null);
message+="Deposits made:\n\n" + "ID Amount\n";
for (num = 0; num < CA.gettransCount(); num++)
{
Transaction trans = CA.getTrans(num);
if (trans.getTransId() == 2)
{
message += String.format("%3d %3d\n", num, trans.getTransAmount());
}
}
text.setText(message);
JOptionPane.showMessageDialog(null, text);
}
public static void processCheck(double transAmt)
{
String trans = "Check", message1;
double currentbal;
currentbal = CA.getBalance();
DecimalFormat formatter = new DecimalFormat("#0.00");
if (currentbal > 0 && currentbal < 500 && under500)
{
CA.setServiceCharge(5.15);
message1 = "Transaction: " + trans + " in amount of $" + formatter.format(transAmt) + "\n" +
"Current Balance: $" + formatter.format(currentbal) + "\n" +
"Service Charge: " + trans + " --- charge $0.15\n" +
"Service Charge: " + "Below $500 --- charge $5.00\n" +
"Total Service Charge: $" + formatter.format(CA.getServiceCharge());
JOptionPane.showMessageDialog (null, message1);
under500 = false;
}
else if (currentbal > 0 && currentbal < 50 && under50)
{
CA.setServiceCharge(0.15);
message1 = "Transaction: " + trans + " in amount of $" + formatter.format(transAmt) + "\n" +
"Current Balance: $" + formatter.format(currentbal) + "\n" +
"Service Charge: " + trans + " --- charge $0.15\n" +
"Warning: Balance below $50\n" +
"Total Service Charge: $" + formatter.format(CA.getServiceCharge());
JOptionPane.showMessageDialog (null, message1);
under50 = false;
}
else if (currentbal < 0 && under0)
{
CA.setServiceCharge(10.15);
currentbal = -(currentbal);
message1 = "Transaction: " + trans + " in amount of $" + formatter.format(transAmt) + "\n" +
"Current Balance: ($" + formatter.format(currentbal) + ")\n" +
"Service Charge: " + trans + " --- charge $0.15\n" +
"Warning: Balance below $50\n" +
"Service Charge: below $0 --- charge $10.00\n" +
"Total Service Charge: $" + formatter.format(CA.getServiceCharge());
JOptionPane.showMessageDialog (null, message1);
under0 = false;
}
else if (currentbal < 0)
{
CA.setServiceCharge(0.15);
currentbal = -1*currentbal;
message1 = "Transaction: " + trans + " in the amount of $" + formatter.format(transAmt) + "\n" +
"Current Balance: ($" + formatter.format(currentbal) + ")\n" +
"Service Charge: " + trans + " --- charge $0.15\n" +
"Warning: Balance below $50\n" +
"Total Service Charge: $" + formatter.format(CA.getServiceCharge());
JOptionPane.showMessageDialog(null, message1);
}
else
{
CA.setServiceCharge(0.15);
message1 = "Transaction: " + trans + " in amount of $" + formatter.format(transAmt) + "\n" +
"Current Balance: $" + formatter.format(currentbal) + "\n" +
"Service Charge: " + trans + " --- charge $0.15\n" +
"Total Service Charge: $" + formatter.format(CA.getServiceCharge());
JOptionPane.showMessageDialog (null, message1);
}
}
public static void processDeposit(double transAmt)
{
String trans = "Deposit", message1;
double currentbal;
currentbal = CA.getBalance();
DecimalFormat formatter = new DecimalFormat("#0.00");
if (currentbal > 0 && currentbal < 500 && under500)
{
CA.setServiceCharge(5.10);
message1 = "Transaction: " + trans + " in amount of $" + formatter.format(transAmt) + "\n" +
"Current Balance: $" + formatter.format(currentbal) + "\n" +
"Service Charge: " + trans + " --- charge $0.10\n" +
"Service Charge: " + "Below $500 --- charge $5.00\n" +
"Total Service Charge: $" + formatter.format(CA.getServiceCharge());
JOptionPane.showMessageDialog (null, message1);
under500 = false;
}
else if (currentbal > 0 && currentbal < 50 && under50)
{
CA.setServiceCharge(0.10);
message1 = "Transaction: " + trans + " in amount of $" + formatter.format(transAmt) + "\n" +
"Current Balance: $" + formatter.format(currentbal) + "\n" +
"Service Charge: " + trans + " --- charge $0.10\n" +
"Warning: Balance below $50\n" +
"Total Service Charge: $" + formatter.format(CA.getServiceCharge());
JOptionPane.showMessageDialog (null, message1);
under50 = false;
}
else if (currentbal < 0 && under0)
{
CA.setServiceCharge(10.10);
currentbal = -(currentbal);
message1 = "Transaction: " + trans + " in amount of $" + formatter.format(transAmt) + "\n" +
"Current Balance: ($" + formatter.format(currentbal) + ")\n" +
"Service Charge: " + trans + " --- charge $0.10\n" +
"Warning: Balance below $50\n" +
"Service Charge: below $0 --- charge $10.00\n" +
"Total Service Charge: $" + formatter.format(CA.getServiceCharge());
JOptionPane.showMessageDialog (null, message1);
under0 = false;
}
else
{
CA.setServiceCharge(0.10);
message1 = "Transaction: " + trans + " in amount of $" + formatter.format(transAmt) + "\n" +
"Current Balance: $" + formatter.format(currentbal) + "\n" +
"Service Charge: " + trans + " --- charge $0.10\n" +
"Total Service Charge: $" + formatter.format(CA.getServiceCharge());
JOptionPane.showMessageDialog (null, message1);
}
}
}
---------------------------------------CheckingAccount-----------------------------------------------
public class CheckingAccount
{
private double balance;
private double totalServiceCharge;
private int transNumber;
private int transId;
private double transAmt;
private ArrayList<Transaction> transList; // keeps a list of Transaction objects for the account
private int transCount;// the count of Transaction objects and used as the ID for each transaction
public void addTrans(Transaction newTrans)
{
transList.add(newTrans); // adds a transaction object to the transList
}
public int gettransCount()
{
return transCount; //returns the current value of transCount;
}
public Transaction getTrans(int i)
{
return transList.get(i); // returns the i-th Transaction object in the list
}
public CheckingAccount(double initialBalance)
{
balance = initialBalance;
totalServiceCharge = 0;
}
public double getBalance()
{
return balance;
}
public void setBalance(double transAmt, int tCode)
{
if(tCode == 1)
{
balance = balance - transAmt;
//1. Check
}
else//if(tCode == 2)
{
balance = balance + transAmt;
//2. Deposit
}
}
public double getServiceCharge()
{
return totalServiceCharge;
}
public void setServiceCharge(double currentServiceCharge)
{
totalServiceCharge += currentServiceCharge;
//3. Service charge
}
}
---------------------------------------------Transaction-----------------------------------------------
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;
}
}
--------------------------------------------TransPanel----------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TransPanel extends JPanel
{
private JLabel prompt;
private JRadioButton one, two, three, four;
public TransPanel()
{
prompt = new JLabel ("Choose action:");
prompt.setFont (new Font ("Helvetica", Font.BOLD, 24));
one = new JRadioButton ("enter transaction");
one.setBackground (Color.yellow);
two = new JRadioButton ("list all transactions");
two.setBackground (Color.yellow);
three = new JRadioButton ("list all checks");
three.setBackground (Color.yellow);
four = new JRadioButton ("list all deposits");
four.setBackground (Color.yellow);
ButtonGroup group = new ButtonGroup();
group.add (one);
group.add (two);
group.add (three);
group.add (four);
TransPanel.TransListener listener = new TransPanel.TransListener();
one.addActionListener (listener);
two.addActionListener (listener);
three.addActionListener (listener);
four.addActionListener (listener);
add (prompt);
add (one);
add (two);
add (three);
add (four);
setBackground (Color.yellow);
setPreferredSize (new Dimension(300, 100));
}
private class TransListener implements ActionListener
{
//--------------------------------------------------------------
// Calls the method to process the option for which radio
// button was pressed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
Object source = event.getSource();
if (source == one)
A2.inputTransCode();
else if (source == two)
A2.listTrans();
else if (source == three)
A2.listChecks();
else if (source == four)
A2.listDeposits();
}
}
}
죄송합니다. 댓글 작성은 회원만 가능합니다.