Я новичок в Java и работаю на задание, мне нужно было сделать то же самое приложение в CLI и GUI. Я создал 2 класса BankingAccount
(аннотация, требование проекта) и SavingAccount
, которые рассчитывают все проценты и все хорошие банковские вещи. Я делаю меню в CLI, используя входы (выберите 1,2 или 3), CLI работает ИДЕАЛЬНО, но затем, когда я пытаюсь сделать «то же самое» для GUI, результаты дублируются. Например, если я напишу 100 $ в поле Deposit
, это даст мне баланс в 200 $ при первом депозите (см. Скриншот) Вот также код, над которым я работаю:
контроллер. 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;
}
}
BankingAccount. 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;
}
}
Дайте мне знать, если потребуется больше кода