ご辛抱いただきありがとうございます - 私は投稿を最初からやり直し、先に進み、私が持っているすべてのものを掲載しています。何が問題なのかよくわからないので、どのようなアドバイスが必要かを説明するのは難しい. 誰かが見ることができれば、それは素晴らしいことです。私が抱えている最大の問題は、CHECK BALANCE ボタンに関するものです。つまり、ボタンをクリックすると、getBalance() が適切なデータを取得せず、JOptionPane が「米国での小切手の合計額は $0.00 です」と表示されます。お金を「入金」しようとしたかどうかに関係なく。
トラベルチェッククラス
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TravelCheck extends JFrame {
private static final int WIDTH = 400, HEIGHT = 300;
ButtonHandler bHandler = new ButtonHandler();
JLabel cash = new JLabel("Cash Your Check", SwingConstants.CENTER);
JLabel click = new JLabel("Click the Button that matches your currency type", SwingConstants.CENTER);
JButton pesoB = new JButton(TravelString.PESO_S);
JButton francB = new JButton(TravelString.FRANC_S);
JButton euroB = new JButton(TravelString.EURO_S);
JButton usdB = new JButton(TravelString.US_S);
JButton checkB = new JButton(TravelString.CHECK_S);
JButton exitB = new JButton(TravelString.EXIT_S);
public TravelCheck() {
Container pane = getContentPane();
pane.add(cash);
pane.add(click);
pane.add(pesoB);
pane.add(francB);
pane.add(usdB);
pane.add(checkB);
pane.add(exitB);
pesoB.addActionListener(bHandler);
francB.addActionListener(bHandler);
euroB.addActionListener(bHandler);
usdB.addActionListener(bHandler);
checkB.addActionListener(bHandler);
exitB.addActionListener(bHandler);
setSize(WIDTH, HEIGHT);
setTitle("Check Machine");
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE); }
public class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
Checks mexican = new Checks(TravelString.PESO_S);
Checks swiss = new Checks(TravelString.FRANC_S);
Checks european = new Checks(TravelString.EURO_S);
Checks us = new Checks(TravelString.US_S);
if (e.getActionCommand().equals(TravelString.EXIT_S))
System.exit(0);
else if (e.getActionCommand().equals(TravelString.PESO_S))
transaction(mexican);
else if (e.getActionCommand().equals(TravelString.FRANC_S))
transaction(swiss);
else if (e.getActionCommand().equals(TravelString.EURO_S))
transaction(european);
else if (e.getActionCommand().equals(TravelString.US_S))
transaction(us);
else if (e.getActionCommand().equals(TravelString.CHECK_S)) {
//this is where I'm having the problem, I think
String str;
Checks test = new Checks("hello");
double temp = test.getBalance();
str = String.format("The amount of your checks total in US is $%.2f", temp);
JOptionPane.showMessageDialog(null, str, "Traveler's check balance", JOptionPane.INFORMATION_MESSAGE);}
public void transaction(Checks C) {
int temp = intPrompt(C);
if (temp == 1)
if (C.getFace() == 0) {
String denomS = JOptionPane.showInputDialog("Please enter the denomination of checks that you want.");
int denomination = Integer.parseInt(denomS);
String numberS = JOptionPane.showInputDialog("Please enter the number of checks that you want");
int number = Integer.parseInt(numberS);
C.deposit(number, denomination); }
else {
int denomination = C.getFace();
String numS = JOptionPane.showInputDialog("Please enter the number of checks that you want");
int number = Integer.parseInt(numS);
C.deposit(number, denomination); }
//else if...
//The code keeps on going for other options (withdrawal and quit), but the above is the most relevant.
public int intPrompt(Checks C) {
String choice = JOptionPane.showInputDialog("For the country of " + C.getCountry() + " you have " + C.getNumber() + " checks with face value denomination of $" + C.getFace() + "\nPlease enter:\n1 - for a deposit\n2 - for a withdrawal\n3 - to quit");
int choiceI = Integer.parseInt(choice);
return choiceI; } }
public static void main(Strin[] args) {
TravelCheck tc = new TravelCheck(); } }
チェッククラスの場合
public class Checks {
private static final double
PESO = 2.0, FRANC = 3.0, EURO = 4.0;
private int numberOfChecks;
private int faceAmount;
private String country;
private static double balance;
public Checks (String s) {
numberOfChecks = 0;
faceAmount = 0;
balance = 0.0;
if (s.equals(TravelString.PESO S) || s.equals(TravelString.FRANC S) || s.equals(TravelString.EURO S) || s.equals(TravelString.US S))
country = s;
else {
country = TravelString.US S;
System.err.println("ERROR: Bad Checks"); }}
public int getNumber() {
return numberOfChecks; }
public String getCountry() {
return country; }
public int getFace() {
return faceAmount; }
public double getBalance() {
return balance; }
public void deposit(int number, int denomination) {
if (number > 0 && denomination > 0 ) {
if (faceAmount == 0) {
faceAmount = denomination;
numberOfChecks = number;
balance += faceAmount * number * conversion(); }
else if (faceAmount == denomination) {
numberOfChecks += number;
balance += faceAmount * number * conversion(); }
else
System.err.println("### Non-existent denomination error in deposit() ###"); }
else
System.err.println("### Non-positive parameter error in deposit() ###"); }
public void withdrawal(int number) {
if (number < 0) // bad invocation scenarios
System.err.println("### Negative parameter error in withdrawal() ###");
else if (balance < number * faceAmount * conversion())
System.err.println("### Overdraw error in withdrawal() ###");
else {
numberOfChecks -= number;
balance -= faceAmount * number * conversion();
if (numberOfChecks == 0)
faceAmount = 0; } }
private double conversion() {
// converts monetary amounts to US Dollars
double retval = 0.0;
if (country.equals(TravelString.US S))
retval = 1.0;
else if (country.equals(TravelString.PESO S))
retval = 1.0/PESO;
else if (country.equals(TravelString.FRANC S))
retval = 1.0/FRANC;
else if (country.equals(TravelString.EURO S))
retval = 1.0/EURO;
return retval; }
トラベルストリングクラス
public class TravelString {
public static final String
PESO_S = "Mexican peso" ,
FRANC_S = "Swiss franc" ,
EURO_S = "Euro dollar" ,
US_S = "US dollar" ,
CHECK_S = "Check Balance" ,
EXIT_S = "Exit"; }
ありがとうございました!