感谢您的耐心等待 - 我将重新开始我的帖子并继续前进并提出我所拥有的一切。我很难解释我需要什么建议,因为我不太明白什么是错的。如果有人可以看看,那将是美好的。我遇到的最大问题是使用CHECK BALANCE按钮 - 即单击按钮时,getBalance()不会检索正确的数据,并且JOptionPane会显示“您在美国的支票总金额为0.00美元”,无论我是否试图“存入”钱。
TRAVELCHECK CLASS
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(); } }
FOR CHECKS CLASS
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; }
TRAVEL STRING CLASS
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"; }
谢谢!
答案 0 :(得分:0)
您正在尝试调用构造函数Sums()
,但您只声明了Sums(String s)
。
将String添加到方法调用中它应该可以正常工作
答案 1 :(得分:0)
有两种选择。
您可以为Sums
编写默认构造函数,也可以向构造函数发送String
参数。
默认构造函数看起来像这样(在Sums
类中):
public Sums() {
//do any initialization
}
或者将String
发送给构造函数,请更改:
Sums temp = new Sums();
您需要向其发送String
参数,例如:
Sums temp = new Sums("Hello World");
或者一些对初始化此对象有意义的字符串。
我必须看到实际的public Sums(String s)
代码才能猜出这里有什么意义。
正如对原始问题所评论的那样,为了确定你的程序没有“正确”运行的原因,我们需要更多的信息,从“你运行不正确”的意思开始,以及包括来自Sums
类的更多信息。
答案 2 :(得分:0)
该消息不言自明,您没有没有参数的构造函数。
一种可能的解决方案是修改你的Sum
类,添加一个no-args构造函数。
public class Sums {
//Add default constructor
public Sums(){
}
public Sums (String s)
{
...
}
public double getTotal() {
return total;
}
}
另一种可能的解决方案,更改您的客户端代码,将String作为参数传递。
Sums temp = new Sums("here put the string!!");
double number = temp.getTotal();