- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个项目,允许用户执行您通常在银行可以执行的操作(创建、删除帐户、取款、存款、打印交易)。
我的问题是打印交易。我能够打印一些东西,但出现了错误的值。例如,当您创建一个银行账户时,您需要输入期初余额 (5.00)。当我打印交易时,什么也没有出现。所以我将钱存入同一帐户 (10.00),当我打印时,之前的 (5.00) 出现并且 (10.00) 出现在交易日期中。但是,当我查看帐户余额时,所有内容加起来就是显示的内容。
有人可以帮我排查和解决问题吗?
package mainsample;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
BankProcess bankProcess = new BankProcess();
TransactionProcess transactionProcess = new TransactionProcess();
Bank bank = new Bank();
BankAccount bankAccount = new BankAccount();
int input;
int selection;
while(true) {
System.out.println("");
System.out.println("######## MENU ########");
System.out.println("[1] Create an account");
System.out.println("[2] Print all existing accounts");
System.out.println("[3] Delete an account");
System.out.println("[4] Deposit");
System.out.println("[5] Withdraw");
System.out.println("[6] Print transactions");
System.out.println("[0] Exit");
System.out.println("######################");
System.out.println("Please choose one of the following: ");
selection = scan.nextInt();
switch (selection) {
case 0:
System.out.println("Exit Successful");
System.exit(0);
break;
case 1:
System.out.println("'[1] Create an account' has been selected.");
System.out.print("Account Id: ");
int accountId = scan.nextInt();
scan.nextLine();
System.out.print("Holder Name: ");
String holderName = scan.nextLine();
System.out.print("Holder Address: ");
String holderAddress = scan.nextLine();
System.out.print("Opening Balance: ");
double openingBalance = scan.nextDouble();
System.out.print("Open Date: ");
String openDate = scan.next();
bankAccount = new BankAccount(accountId, holderName, openingBalance, holderAddress, openDate);
bank.setAccounts(bankProcess.openNewAccount(bank.getAccounts(), bankAccount));
System.out.println("Successfully Added.");
break;
case 2:
System.out.println("'[2] Display all existing accounts' has been selected");
System.out.println("-----------------------------------------------------");
bank.getAccounts().forEach((i,b)->System.out.println(b));
System.out.println("-----------------------------------------------------");
break;
case 3:
System.out.println("[3] Delete an account has been selected");
System.out.println("Enter the account ID: ");
int accountNo = scan.nextInt();
bankAccount = bank.getAccount(accountNo); // get bankAccount from account id
bank.removeAccounts(bankProcess.removeAccount(bank.getAccounts(), bankAccount));
System.out.println("Account has been deleted.");
break;
case 4:
System.out.println("[4] Deposit has been selected");
System.out.println("Enter account ID: ");
int accountNumber = scan.nextInt();
System.out.println("Enter deposit amount: ");
double depositAmount = scan.nextDouble();
transactionProcess.deposit(bankAccount, depositAmount);
break;
case 5:
System.out.println("[5] Withdraw has been selected");
System.out.println("Enter account ID: ");
int accountNu = scan.nextInt();
System.out.println("Enter withdraw amount: ");
double withdrawAmount = scan.nextDouble();
transactionProcess.withdraw (bankAccount, withdrawAmount);
break;
case 6:
System.out.println("[6] Print Transaction has been selected");
System.out.println("Enter account ID: ");
int accountN = scan.nextInt();
bankAccount = bank.getAccount(accountN);
for (Transaction transaction: bankAccount.getTransactions()) {
// print transaction information ...
System.out.println(transaction.toString());
}
break;
default:
System.out.println("Your choice was not valid!");
}
}
}
}
package mainsample;
/**
*
* @author Khalid
*/
public class Transaction {
private String transactionType;
private double transactionAmount;
private int transactionDate;
public Transaction() {}
public Transaction( String transactionType, double transactionAmount, int transactionDate) {
this.transactionType = transactionType;
this.transactionAmount = transactionAmount;
this.transactionDate = transactionDate;
}
public int getTransactionDate() {
return transactionDate;
}
public void setTransactionDate (int transactionDate) {
this.transactionDate = transactionDate;
}
public String getTransactionType() {
return transactionType;
}
public void setTransactionType(String transactionType) {
this.transactionType = transactionType;
}
public double getTransactionAmount() {
return transactionAmount;
}
public void setTransactionAmount(double transactionAmount) {
this.transactionAmount = transactionAmount;
}
//Override the toString() method of String ?
public String toString() {
return "\nTransaction Amount : "+ this.transactionAmount +
"\nTransaction Type : " + this.transactionType +
"\nTransaction Date: " + this.transactionDate;
}
}
package mainsample;
/**
*
* @author Khalid
*/
public class TransactionProcess {
public void deposit(BankAccount bankAccount, double depositAmount) {
//Get the CurrentBalance
double currentBalance = bankAccount.getCurrentBalance();
//First Argument : set the Id of transaction
//Second Argument : set the Type of Transaction
//Third Argument : set the TransactionAmount
//Fourth Argument : set the Balance Before the transaction (for record purposes)
Transaction transaction = new Transaction("Deposit", currentBalance, (int) depositAmount);
if (depositAmount <= 0) {
System.out.println("Amount to be deposited should be positive");
} else {
//Set the updated or transacted balance of bankAccount.
bankAccount.setCurrentBalance(currentBalance + depositAmount);
//then set the MoneyAfterTransaction
bankAccount.addTransaction(transaction); // adds a transaction to the bank account
System.out.println(depositAmount + " has been deposited.");
}
}
// Explanation same as above
public void withdraw(BankAccount bankAccount, double withdrawAmount) {
double currentBalance = bankAccount.getCurrentBalance();
Transaction transaction = new Transaction("Withdraw", currentBalance, (int) withdrawAmount);
if (withdrawAmount <= 0) {
System.out.println("Amount to be withdrawn should be positive");
} else {
if (currentBalance < withdrawAmount) {
System.out.println("Insufficient balance");
} else {
bankAccount.setCurrentBalance(currentBalance - withdrawAmount);
bankAccount.addTransaction(transaction); // adds a transaction to the bank account
System.out.println(withdrawAmount + " has been withdrawed,");
}
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mainsample;
import java.util.*;
/**
*
* @author Khalid
*/
public class BankAccount {
private int accountId;
private String holderName;
private String holderAddress;
private String openDate;
private double currentBalance;
private List<Transaction> transactions = new ArrayList<Transaction>();
//Provide Blank Constructor
public BankAccount(){}
//Constructor with an arguments.
public BankAccount(int accountNum, String holderNam,double currentBalance, String holderAdd,String openDate) {
this.accountId = accountNum;
this.holderName = holderNam;
this.holderAddress = holderAdd;
this.openDate = openDate;
this.currentBalance = currentBalance;
}
// Always Provide Setter and Getters
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
public String getHolderName() {
return holderName;
}
public void setHolderName(String holderName) {
this.holderName = holderName;
}
public String getHolderAddress() {
return holderAddress;
}
public void setHolderAddress(String holderAddress) {
this.holderAddress = holderAddress;
}
public String getOpenDate() {
return openDate;
}
public void setOpenDate(String openDate) {
this.openDate = openDate;
}
public double getCurrentBalance() {
return currentBalance;
}
public void setCurrentBalance(double currentBalance) {
this.currentBalance = currentBalance;
}
public List<Transaction> getTransactions() {
return transactions;
}
public void setTransactions(List<Transaction> transactions) {
this.transactions = transactions;
}
public void addTransaction(Transaction transaction){
if(transactions.size() >= 6){ // test if the list has 6 or more transactions saved
transactions.remove(0); // if so, then remove the first (it's the oldest)
}
transactions.add(transaction); // the new transaction is always added, no matter how many other transactions there are already in the list
}
public String toString(){
return "\nAccount number: " + accountId +
"\nHolder's name: " + holderName +
"\nHolder's address: " + holderAddress +
"\nOpen Date: " + openDate +
"\nCurrent balance: " + currentBalance;
}
}
package mainsample;
import java.util.*;
/**
*
* @author Khalid
*/
public class Bank {
private TreeMap<Integer,BankAccount> bankAccounts = new TreeMap<Integer,BankAccount>();
public TreeMap<Integer, BankAccount> getAccounts() {
return bankAccounts;
}
public void setAccounts(TreeMap<Integer, BankAccount> accounts) {
this.bankAccounts = accounts;
}
public BankAccount getAccount(Integer accountNumber){
return bankAccounts.get(accountNumber);
}
public void removeAccounts(TreeMap<Integer, BankAccount> accounts) {
this.bankAccounts = accounts;
}
}
package mainsample;
import java.util.*;
/**
*
* @author Khalid
*/
public class BankProcess {
// return the Updated list of BankAccounts
public TreeMap<Integer,BankAccount> openNewAccount(TreeMap<Integer,BankAccount> bankAccounts,BankAccount bankAccount) {
//Get the List of existing bank Accounts then add the new BankAccount to it.
bankAccounts.put(bankAccount.getAccountId(), bankAccount);
return bankAccounts;
}
public TreeMap<Integer,BankAccount> removeAccount(TreeMap<Integer,BankAccount> bankAccounts,BankAccount bankAccount) {
bankAccounts.remove(bankAccount.getAccountId(), bankAccount);
return bankAccounts;
}
}
最佳答案
首先你在Transaction constrctor中犯了一个错误
public Transaction( String transactionType, double transactionAmount, int transactionDate) {
this.transactionType = transactionType;
this.transactionAmount = transactionAmount;
this.transactionDate = transactionDate;
}
但是你是这样分配的
Transaction transaction = new Transaction("Deposit", currentBalance, (int) depositAmount);
你的论点顺序在这里完全错误。你为什么指定存款金额而不是日期。应该是这样的
Transaction transaction = new Transaction("Deposit", (int) depositAmount,date );
此外,当您创建一个新帐户时,您还没有将该金额添加到交易中。您正在使用此代码创建新帐户
bankAccount = new BankAccount(accountId, holderName, openingBalance, holderAddress, openDate);
bank.setAccounts(bankProcess.openNewAccount(bank.getAccounts(), bankAccount));
但是您还没有将这个初始金额添加到交易中。它应该包含在 bankAccount 的构造函数中
Transaction transaction = new Transaction("Deposit", 0, (int) depositAmount);
if (depositAmount <= 0) {
System.out.println("Amount to be deposited should be positive");
} else {
//Set the updated or transacted balance of bankAccount.
bankAccount.setCurrentBalance(currentBalance + depositAmount);
//then set the MoneyAfterTransaction
bankAccount.addTransaction(transaction); // adds a transaction to the bank account
System.out.println(depositAmount + " has been deposited.");
}
现在我得到了正确的输出。当您创建您要求日期的帐户时。当您存款时,您没有询问日期。所以你不能为存款或取款指定交易日期
Transaction Amount : 5.0 Transaction Type : Deposit Transaction Date: 4
Transaction Amount : 10.0 Transaction Type : Deposit Transaction Date: 10
关于java - 变量赋值问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27521776/
你能解释一下这个作业是如何完成的吗, var fe, f = document.forms[0], h; 哪个等于哪个。 最佳答案 以上等同于 var fe; var f = document.for
据我测试,这两种方法都有效,但我不知道哪一种最好,也不知道它们之间的区别,这就是我想知道的。 以下是两种方法: window.location = 'http://www.google.com'; w
我正在处理用字符串填充的 numpy 数组。我的目标是分配给第一个数组 a 的切片,值包含在较小尺寸的第二个数组 b 中。 我想到的实现如下: import numpy as np a = np.em
在我使用过的其他语言(如 Erlang 和 Python)中,如果我正在拆分字符串并且不关心其中一个字段,我可以使用下划线占位符。我在 Perl 中试过这个: (_,$id) = split('
我认为这似乎很简单,但我对调用、应用、绑定(bind)感到困惑。等等 我有一个事件监听器 red.addEventListener("click", function() { j = 0;
这个问题在这里已经有了答案: What is the python "with" statement designed for? (11 个答案) 关闭 7 年前。 使用有什么区别: iFile =
这个问题在这里已经有了答案: What is the python "with" statement designed for? (11 个答案) 关闭 7 年前。 使用有什么区别: iFile =
几周前我们开始写一篇关于 Haskell 的论文,刚刚接到我们的第一个任务。我知道 SO 不喜欢家庭作业问题,所以我不会问怎么做。相反,如果有人能将我推向正确的方向,我将不胜感激。鉴于它可能不是一个特
我正在尝试为我的函数的变量根分配一个值,但似乎不起作用。我不明白这个问题。 hw7.c:155:7:警告:赋值使指针来自整数而不进行强制转换[默认启用] root = 负载(&fp, 大小); 此代码
我昨天花了大约 5 个小时来完成这个工作,并使用这个网站的帮助让代码可以工作,但我认为我这样做的方式是一种作弊方式,我使用了 scanf 命令。无论如何,我想以正确的方式解决这个问题。多谢你们!哦,代
我需要一些帮助来解决问题。 我有这个文本文件: 我将文本内容输入到字符串二维数组中,并将其转换为整数二维数组。当我转换为 int 数组时,nan 被替换为零。现在,我继续查找二维数组中每行的最大值和最
假设我有一个只能移动的类型。我们停止现有的默认提供的构造函数,但 Rvalue 引用引入了一种新的“ flavor ”,我们可以将其用于签名的移动版本: class CantCopyMe { priv
假设我有两个简单的对象,我想创建第三个对象来连接它们的属性。这非常有效: (()=>{ const a1 = {a: 2, b: 3} const b1 = {a: 100, c: 5}
我想知道我是否可以稍后在这样的代码中为 VAR 赋值 var myView: UIView func createView() { myView = UIView() { let _view =
我遇到了一些 Javascript/HTML/CSS 代码的问题。我对创建网站还很陌生,所以请多多包涵。 我最终想做的是从 javascript 中提取一个动态值并使用它对一些 div(在容器中)进行
#include class Box{ public: int x; Box(){ x=0; std::cout No move construction thanks to RV
我发现在javascript中&=运算符是按位赋值: var test=true; test&=true; //here test is an int variable javascript中是否存在
请帮助完成赋值重载函数的执行。 这是指令: 赋值运算符 (=),它将源字符串复制到目标字符串中。请注意,目标的大小需要调整为与源相同。 加法 (+) 和赋值 (=) 运算符都需要能够进行级联运算。这意
我有一个名为 SortedArrayList 的自定义结构它根据比较器对其元素进行排序,我想防止使用 operator[] 进行分配. 示例: 数组列表.h template class Array
我是 python 的新手,我看到了这种为列表赋值的形式 color= ['red' if v == 0 else 'green' for v in y] 但是如果我尝试用 3 个数字来做,例如 co
我是一名优秀的程序员,十分优秀!