【问题标题】:Why isn't my running total working?为什么我的跑步总数不起作用?
【发布时间】:2014-11-15 03:35:44
【问题描述】:

我的控制器中有一个运行总计,它将跟踪最终用户通过单击按钮插入的资金。当最终用户单击“购买”按钮时,将运行总计分配给将发送到模型中的字符串变量;但是,字符串变量的值与运行总变量的值不同。我已经倾注了 Eclipse 的调试并浏览了代码,但这超出了我的范围。我会很感激任何帮助:)

“购买”按钮的类位于代码 sn-p 的最底部附近。保存累计值的字符串称为“fundsDouble”。

package edu.witc.controller;

//Importing necessary packages
//Intangible packages
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//Other classes
import edu.witc.view.VendingMachine;
import edu.witc.model.Products;


//Parent class
public class Controller
{
    // Declare all variables here: the Declare Monster be lurkin' in these parts
    private VendingMachine view = null;         // A reference to our view
    private Products model = new Products();    // A reference to our model
    private double funds = 0.00;                // See the method 'calculateFunds'


    // Default constructor
    public Controller()
    {

    }


    // Overloaded constructor for the parent class
    public Controller(VendingMachine view)
    {
        // Here, we are simply connecting to the listeners in the view
        // Making sure Java knows what our view is because Java is not very smart
        this.view = view;


        // Instantiating button listener classes
            // Money Buttons
                // Nickel Button
        NickelButtonListener nickelListen = new NickelButtonListener();
                // Dime Button
        DimeButtonListener dimeListen = new DimeButtonListener();
                // Quarter Button
        QuarterButtonListener quarterListen = new QuarterButtonListener();
                // One Dollar Button
        OneDollarButtonListener oneDollarListen = new OneDollarButtonListener();
                // Five Dollars Buttons
        FiveDollarsButtonListener fiveDollarsListen = new FiveDollarsButtonListener();
            // Other Buttons
                // Purchase Button
        PurchaseButtonListener purchaseListen = new PurchaseButtonListener();
                // Cancel Button
        CancelButtonListener cancelListen = new CancelButtonListener();

        // Then pinning our 'VendingMachine' buttons to their listeners
            // Money Buttons
                // Nickel Button
        this.view.addNickelListener(nickelListen);
                // Dime Button
        this.view.addDimeListener(dimeListen);
                // Quarter Button
        this.view.addQuarterListener(quarterListen);
                // One Dollar Button
        this.view.addOneDollarListener(oneDollarListen);
                // Five Dollar Button
        this.view.addFiveDollarsListener(fiveDollarsListen);
            // Other Buttons
                // Purchase Button
        this.view.addPurchaseListener(purchaseListen);
                // Cancel Button
        this.view.addCancelListener(cancelListen);
    }


    // This function handles the funds the end user inserts into the vending machine
    private void calculateFunds(double money)
    {
        // We're going to add funds for the end user, depending on which button they clicked, which is why 'money' doesn't represent a specified coin/bill
        funds = funds + money;

        // Send 'funds' to the view
        view.setFunds(funds);
    }


    // This method handles the displaying of the end user's change
    public void displayChange()
    {
        String change = model.getChange();

        // Display the end user's change
        view.displayMessage(change);
    }


    // What follows are our various classes for our many buttons on the form
        // Money Buttons
            // Nickel Button
    class NickelButtonListener implements ActionListener
    {
        // This variable represents a nickel
        private double nickel = 0.05;

        // "Who woke me up?" asked the 'actionPerformed' method, and we're telling it that 'e' did, which is an 'ActionEvent' object
        public void actionPerformed(ActionEvent e)
        {
            // Call the 'funds' function to add funds to the vending machine
            calculateFunds(nickel);
        }
    }
            // Dime Button
    class DimeButtonListener implements ActionListener
    {
        // This variable represents a dime
        private double dime = 0.10;

        // "Who woke me up?" asked the 'actionPerformed' method, and we're telling it that 'e' did, which is an 'ActionEvent' object
        public void actionPerformed(ActionEvent e)
        {
            // Call the 'funds' function to add funds to the vending machine
            calculateFunds(dime);
        }
    }
            // Quarter Button
    class QuarterButtonListener implements ActionListener
    {
        // This variable represents a quarter
        private double quarter = 0.25;

        // "Who woke me up?" asked the 'actionPerformed' method, and we're telling it that 'e' did, which is an 'ActionEvent' object
        public void actionPerformed(ActionEvent e)
        {
            // Call the 'funds' function to add funds to the vending machine
            calculateFunds(quarter);
        }
    }
            // One Dollar Button
    class OneDollarButtonListener implements ActionListener
    {
        // This variable represents one dollar
        private double oneDollar = 1.00;

        // "Who woke me up?" asked the 'actionPerformed' method, and we're telling it that 'e' did, which is an 'ActionEvent' object
        public void actionPerformed(ActionEvent e)
        {
            // Call the 'funds' function to add funds to the vending machine
            calculateFunds(oneDollar);
        }
    }
            // Five Dollars Button
    class FiveDollarsButtonListener implements ActionListener
    {
        // This variable represents five dollars
        private double fiveDollars = 5.00;

        // "Who woke me up?" asked the 'actionPerformed' method, and we're telling it that 'e' did, which is an 'ActionEvent' object
        public void actionPerformed(ActionEvent e)
        {
            // Call the 'funds' function to add funds to the vending machine
            calculateFunds(fiveDollars);
        }
    }
        // Other Buttons
            // Purchase Button
    class PurchaseButtonListener implements ActionListener
    {
        // Declare your variables here
        String itemNumber = "";                         // When the end user submits an item number, this variable will capture that item number
        String fundsDouble = String.valueOf(funds);     // This is the end user's funds that they're going to be purchasing with
        String message = "";


        // "Who woke me up?" asked the 'actionPerformed' method, and we're telling it that 'e' did, which is an 'ActionEvent' object
        @Override
        public void actionPerformed(ActionEvent e)
        {
            // Get the item number
            itemNumber = view.getItemNumber();

            // Send 'itemNumber' to a method that will handle its purpose
            model.calculatePrice(itemNumber, fundsDouble);

            // POSSIBLY, WE CAN SEND OUR MESSAGE TO THE VIEW FROM HERE
            displayChange();
        }
    }
    // Cancel Button
    class CancelButtonListener implements ActionListener
    {
        // "Who woke me up?" asked the 'actionPerformed' method, and we're telling it that 'e' did, which is an 'ActionEvent' object
        public void actionPerformed(ActionEvent e)
        {
            // Send a good bye message to the end user
            view.displayMessage("Your money has been returned to you. Goodbye!");

            // Now quit the application
            System.exit(0);
        }
    }
}

【问题讨论】:

  • 您说fundsDouble 是您的运行总数,但您没有在代码中的任何地方更新它。计算出商品的价格后,您需要将 fundsDouble 更新为新的总价。
  • 不,'fundsDouble' 将保存字符串版本的 'funds',我的运行总变量。 'funds' 是运行总变量,它工作正常。罪魁祸首是“fundsDouble”,我不知道为什么它不起作用。
  • 您可能没有将值传递给模型?
  • String fundDouble = String.valueOf(funds); //这只初始化一次。您需要更新其 actionPerformed() 中的值
  • 您只在第一次创建动作监听器时设置fundsDouble 的值。你永远不会更新它。您的评论暗示您认为更改 funds 的值会以某种方式神奇地更改 fundsDouble 的值,但事实并非如此。

标签: java model-view-controller controller


【解决方案1】:

azurefrog 解决了这个问题。 (他们的答案在问题的 cmets 中。)我需要在 calculateFunds 方法中将 fundsDouble 设置为运行总变量 funds

【讨论】:

    猜你喜欢
    • 2018-07-22
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 2015-07-27
    • 2015-11-14
    • 2022-01-13
    • 2020-11-11
    • 1970-01-01
    相关资源
    最近更新 更多