调用类的输出是重复的结果(JavaFX)

问题描述 投票:-1回答:1

我是一个新的Java和工作的任务,我不得不做同样的应用程序在CLI和GUI。 我创建了2个类 BankingAccount(摘要、项目要求)和 SavingAccount 计算所有的利息和所有的银行业务。我在CLI中使用输入做了一个菜单(选择1,2或3),CLI工作得很好,但是当我试图在GUI中做 "同样的事情 "时,结果是重复的。 例如,如果我在 Deposit 字段,它给我的第一笔存款余额为200美元(见截图) 这也是我正在做的代码。

controller.java

package bank;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.stage.Stage;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.stage.Stage;
import javafx.event.*;
import javafx.scene.control.*;

public class Controller  {
    @FXML
    TextField txt_dAmount;
    @FXML
    TextField txt_aRate;
    @FXML
    TextArea txt_EventView;

public void createAccountBtn(){
    double amount = Double.parseDouble(txt_dAmount.getText());

    double aRate = Double.parseDouble(txt_aRate.getText());

    SavingsAccount GUIsavings = new SavingsAccount(amount,aRate);
    GUIsavings.deposit(amount);

    double currentAmount = GUIsavings.balance;
    System.out.println(currentAmount);

    txt_EventView.appendText("Your deposited: $" + amount + "\n");
    txt_EventView.appendText("Your balance is: $" + currentAmount + "\n");
}

public void CloseButtonAction(){
    System.out.println("whatever");
}

}

sample.fmxl:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" style="-fx-background-color: #0c1446;" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="bank.Controller">
   <children>
      <AnchorPane layoutX="217.0" layoutY="9.0" prefHeight="583.0" prefWidth="574.0" style="-fx-background-color: #87ACA3;">
         <effect>
            <DropShadow height="1.0" radius="0.0" width="1.0" />
         </effect>
         <children>
            <TextField layoutX="166.0" layoutY="65.0" prefHeight="30.0" prefWidth="102.0">
               <font>
                  <Font name="Century Gothic" size="14.0" />
               </font>
            </TextField>
            <Label layoutX="19.0" layoutY="21.0" text="Deposit Amount">
               <font>
                  <Font name="Century Gothic" size="14.0" />
               </font>
            </Label>
            <Button blendMode="MULTIPLY" layoutX="166.0" layoutY="105.0" mnemonicParsing="false" onAction="#createAccountBtn" prefHeight="30.0" prefWidth="102.0" style="-fx-background-color: #87ACA3;" text="Deposit" textFill="#11101b">
               <font>
                  <Font name="Century Gothic" size="14.0" />
               </font>
               <cursor>
                  <Cursor fx:constant="HAND" />
               </cursor>
            </Button>
            <Label layoutX="19.0" layoutY="70.0" text="Withdrawl">
               <font>
                  <Font name="Century Gothic" size="14.0" />
               </font>
            </Label>
            <TextField fx:id="txt_dAmount" layoutX="166.0" layoutY="16.0" prefHeight="30.0" prefWidth="102.0">
               <font>
                  <Font name="Century Gothic" size="14.0" />
               </font>
            </TextField>
            <Button blendMode="MULTIPLY" layoutX="511.0" layoutY="467.0" mnemonicParsing="false" style="-fx-background-color: #87ACA3;" text="Save" textFill="#0d0840">
               <font>
                  <Font name="Century Gothic" size="14.0" />
               </font>
            </Button>
            <TextArea fx:id="txt_EventView" editable="false" layoutX="12.0" layoutY="147.0" prefHeight="306.0" prefWidth="550.0" />
            <Label layoutX="301.0" layoutY="21.0" text="Annual Interest">
               <font>
                  <Font name="Century Gothic" size="14.0" />
               </font>
            </Label>
            <TextField fx:id="txt_aRate" layoutX="413.0" layoutY="16.0" prefHeight="30.0" prefWidth="102.0">
               <font>
                  <Font name="Century Gothic" size="14.0" />
               </font>
            </TextField>
            <Button blendMode="MULTIPLY" layoutX="311.0" layoutY="105.0" mnemonicParsing="false" onAction="#createAccountBtn" prefHeight="30.0" prefWidth="102.0" style="-fx-background-color: #87ACA3;" text="Withdraw" textFill="#11101b">
               <font>
                  <Font name="Century Gothic" size="14.0" />
               </font>
               <cursor>
                  <Cursor fx:constant="HAND" />
               </cursor>
            </Button>
         </children>
      </AnchorPane>
      <AnchorPane layoutY="8.0" prefHeight="583.0" prefWidth="217.0" style="-fx-background-color: #175873;">
         <children>
            <Button fx:id="closeButton" layoutX="40.0" layoutY="510.0" mnemonicParsing="false" onAction="#CloseButtonAction" prefHeight="58.0" prefWidth="177.0" style="-fx-background-color: #175873;" text="Exit" textFill="WHITE">
               <font>
                  <Font name="Century Gothic" size="18.0" />
               </font>
            </Button>
            <Button layoutX="40.0" layoutY="123.0" mnemonicParsing="false" prefHeight="77.0" prefWidth="177.0" style="-fx-background-color: #175873;" text="Accounts" textFill="WHITE">
               <font>
                  <Font name="Century Gothic" size="18.0" />
               </font>
            </Button>
            <AnchorPane prefHeight="583.0" prefWidth="40.0" style="-fx-background-color: #0C1446;" />
            <Label layoutX="61.0" layoutY="14.0" text="My Banking" textFill="#b2caf8">
               <font>
                  <Font name="Century Gothic" size="24.0" />
               </font>
            </Label>
            <Label layoutX="103.0" layoutY="51.0" text="App" textFill="#cac5f8">
               <font>
                  <Font name="Century Gothic" size="24.0" />
               </font>
            </Label>
            <Separator layoutX="40.0" layoutY="88.0" prefHeight="3.0" prefWidth="177.0" />
         </children>
         <effect>
            <DropShadow />
         </effect>
      </AnchorPane>
   </children>
</Pane>

Main.java

package bank;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);    
    }  

    //initialize the GUI library with a window 800x600
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("My Banking App");
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }    
}

SavingsAccount.java /在后台进行计算。

package bank;

import java.io.FileWriter;
import java.text.DecimalFormat;

//Define the class SavingsAccount.
public class SavingsAccount extends BankAccount {

    //Declare the required data members.
    private boolean status;

    DecimalFormat df = new DecimalFormat("#.00");

    //Define the constructor.
    public SavingsAccount(double bal, double iRate) {
        super(bal, iRate);
        status = bal > 25;
    }

    //Define the method deposit().
    public void deposit(double amount) {
        if(!status) {
            if(balance + amount >= 25) {
                status = true;
            }
        }
        super.deposit(amount);
    }

    //Define the method withdraw().
    public void withdraw(double amount) {
        if(status) {
            super.withdraw(amount);
            if(balance < 25) {
                status = false;
            }
        }
    }

    //Define the method monthlyProvess().
    public void monthlyProcess() {
        if(num_withdrawls > 4)
        {
            month_charge +=1;
        }
        balance -= month_charge;
        this.calcInterest();
        if(balance < 25) {
            status = false;
        }
        num_deposits = 0;
        num_withdrawls = 0;
    }

    //Define the method get_status().
    public boolean get_status() {
        return status;
    }    
}  

银行账户.java /也在后台进行计算。

package bank;

import java.text.DecimalFormat;

//Define the class BankAccount.
public abstract class BankAccount {

    DecimalFormat df = new DecimalFormat("#.00");

    //Declare the required data members.
    protected String Name;
    protected String Number;
    protected double balance;
    protected int num_deposits;
    protected int num_withdrawls;
    protected double ann_int_rate;
    protected double month_charge;

    //Define the constructor.
    public BankAccount(double bal, double rate) {
        bal = Double.valueOf(df.format(bal));
        this.balance = bal;
        this.ann_int_rate = rate;
        num_deposits = 0;
        num_withdrawls = 0;
        month_charge = 0;
    }

    //Define the method deposit().
    public void deposit(double amount) {
        this.balance += amount;
        num_deposits++;
    }

    //Define the method withdrawl().
    public void withdraw(double amount) {

        /*If the required amount can be
        withdrawn from the account then,
        perform the withdrawal operation.*/
        if(amount <= balance) {
            this.balance -= amount;
            num_withdrawls++;
        }
    }

    //Define the method calcInterest().
    public void calcInterest() {
        double mon_int_rate = (ann_int_rate/12);
        mon_int_rate = Double.valueOf(df.format(mon_int_rate));
        double interest = balance * mon_int_rate;
        interest = Double.valueOf(df.format(interest));
        balance = balance + interest;
        balance = Double.valueOf(df.format(balance));
    }

    //Define the method monthlyProcess().
    public void monthlyProcess()
    {
        balance -= month_charge;
        this.calcInterest();
        num_deposits = 0;
        num_withdrawls = 0;
        month_charge = 0;
    }

    /*Define the required
    getter and setter methods.*/
    public void set_name(String name) {
        this.Name = name;
    }

    public void set_account(String account) {
        this.Number = account;
    }

    public double get_bal() {
        return balance;
    }

    public int get_no_deposit() {
        return num_deposits;
    }

    public int get_no_withdrawls() {
        return num_withdrawls;
    }
}

如果需要更多的代码,请告诉我

enter image description here

java javafx
1个回答
3
投票

问题是由你发布的这行代码引起的。(参考方法 createAccountBtn() 课堂上 Controller)

SavingsAccount GUIsavings = new SavingsAccount(amount,aRate);

根据你的说法,问题是第一次存款显示的余额不正确,所以我推测储蓄账户一开始的余额是0(零)。因此,当你创建一个 SavingsAccount 例如,给它一个零的起始余额。因此,您应该将 amount 在呼吁 SavingsAccount 构造函数的值为零,如。

SavingsAccount guiSavings = new SavingsAccount(0, aRate);

请注意,根据... 评论克洛帕特拉我改变了变量的名称 (GUIsavings),使其附着在 命名惯例. 另外要注意的是,你的代码至少还有一个我发现的问题,但现在我决定只回答你提出的问题。

热门问题
推荐问题
最新问题