0

感谢您的耐心等待 - 我将重新开始我的帖子并继续前进并提出我所拥有的一切。我很难解释我需要什么建议,因为我不太明白哪里出了问题。如果有人能看一看,那就太好了。我遇到的最大问题是使用 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"; }

谢谢!

4

3 回答 3

0

您正在尝试调用构造函数Sums(),但您只声明了Sums(String s)
将字符串添加到您的方法调用中,它应该可以工作

于 2013-11-14T23:58:32.110 回答
0

有两种选择。

您可以为 编写默认构造函数Sums,也可以向构造函数发送String参数。


默认构造函数看起来像这样(在Sums类中):

public Sums() {
    //do any initialization
}

或者要将 a 发送String到构造函数,请更改以下内容:

Sums temp = new Sums();

您需要向它发送一个String参数,例如:

Sums temp = new Sums("Hello World");

或者一些对初始化这个对象有意义的字符串。

我必须查看实际public Sums(String s)代码才能猜测这里的意义。


正如对原始问题所评论的那样,要确定为什么您的程序未“正确”运行,我们需要更多信息,从您所说的“未正确运行”开始,并包括更多信息从Sums课堂上。

于 2013-11-14T23:58:45.643 回答
0

该消息是不言自明的,您没有没有参数的构造函数。

一个可能的解决方案是修改您的Sum类,添加一个无参数构造函数。

public class Sums {

//Add default constructor
public Sums(){

}

public Sums (String s)
{
...
}
public double getTotal() {
return total; 
}
}

另一种可能的解决方案是更改将字符串作为参数传递的客户端代码。

Sums temp = new Sums("here put the string!!");
double number = temp.getTotal();
于 2013-11-14T23:58:45.860 回答