Java-TreeMap用户输入
我正在创建一个银行系统项目 我在将用户输入/扫描仪与treemap合并时遇到问题 我想创建一个帐号为帐号的帐号,帐号为帐号的帐号名为密钥 当帐户被存储后,我将为事务创建一个链表,该链表只存储最后6个事务 有人能给我一些关于如何从用户输入添加帐户到树形图的指导吗 命令盒 多谢各位Java-TreeMap用户输入,java,Java,我正在创建一个银行系统项目 我在将用户输入/扫描仪与treemap合并时遇到问题 我想创建一个帐号为帐号的帐号,帐号为帐号的帐号名为密钥 当帐户被存储后,我将为事务创建一个链表,该链表只存储最后6个事务 有人能给我一些关于如何从用户输入添加帐户到树形图的指导吗 命令盒 多谢各位 import java.util.*; public class Bank { TreeMap < Integer, BankAccount > accounts = new TreeMap < I
import java.util.*;
public class Bank {
TreeMap < Integer, BankAccount > accounts = new TreeMap < Integer, BankAccount > ();
public int openNewAccount(int accountNumber, String holderName, double openingBalance, String holderAddress, String openDate) {
}
//End of Class
}
import java.util.*;
公营银行{
TreeMapaccounts=新的TreeMap();
public int-openNewAccount(int-accountNumber,String-holderName,double-openingBalance,String-holderAddress,String-openDate){
}
//下课
}
import java.util.*;
公营银行帐户{
私人国际帐户号码;
私有字符串持有者名称;
私有字符串持有者地址;
私有字符串openDate;
私人双电流平衡;
私有列表latestTransactions=newarraylist();
公共银行账户(int accountNum、String holderNam、String holderAdd、,
字符串(openDat){
accountNumber=accountNum;
holderName=holderNam;
holderAddress=holderAdd;
openDate=openDat;
}
公共字符串getAccountInfo(){
返回“\n账号:“+accountNumber+”\n股东姓名:“+holderName+”\n股东地址:“+holderAddress+”\n开放日期:“+openDate+”\n当前余额:“+currentBalance;
}
public int getAccountNum(){
返回帐号;
}
//下课
}
import java.util.*;
公共类事务{
私有列表transactions=newarraylist();
公开交易(国际账户号,双倍当前余额){
}
公共无效添加事务(事务t){
if(transactions.size()+1>6)
transactions.remove(0);//首先删除列表元素
增加(t);
}
公共无效存款(双倍活期余额,双倍金额){
如果(数量我希望这将有助于你了解该怎么做
注意:我在里面提供了一些评论,以便您理解。如果对此仍不清楚,请发表评论
银行类
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 String toString(){
return "\nAccount number: " + accountId + "\nHolder's name: " + holderName + "\nHolder's address: " + holderAddress + "\nOpen Date: " + openDate + "\nCurrent balance: " + currentBalance;
}
}
public class Transaction {
private int transactionId;
private String transactionType;
private double transactionAmount;
private double moneyBeforeTransaction;
private double moneyAfterTransaction;
public Transaction(){}
public Transaction(int transactionId,String transactionType,double transactionAmount, double moneyBeforeTransaction){
this.transactionId = transactionId;
this.transactionType = transactionType;
this.transactionAmount = transactionAmount;
this.moneyBeforeTransaction = moneyBeforeTransaction;
}
public int getTransactionId() {
return transactionId;
}
public void setTransactionId(int transactionId) {
this.transactionId = transactionId;
}
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;
}
public double getMoneyBeforeTransaction() {
return moneyBeforeTransaction;
}
public void setMoneyBeforeTransaction(double moneyBeforeTransaction) {
this.moneyBeforeTransaction = moneyBeforeTransaction;
}
public double getMoneyAfterTransaction() {
return moneyAfterTransaction;
}
public void setMoneyAfterTransaction(double moneyAfterTransaction) {
this.moneyAfterTransaction = moneyAfterTransaction;
}
//Override the toString() method of String ?
public String toString(){
return "Transaction ID : " + this.transactionId +
" Transaction Type : " + this.transactionType +
" Transaction Amount : " + this.transactionAmount +
" Money Before Transaction : " + this.moneyBeforeTransaction +
" Money After Transaction : " + this.moneyAfterTransaction;
}
}
public class TransactionProcess {
//Always Provide another class for process.
//Pass the bankAccount of the user
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(bankAccount.getTransactions().size(),"Deposit",depositAmount,currentBalance);
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
transaction.setMoneyAfterTransaction(bankAccount.getCurrentBalance());
//after the transaction add it to the list of Transaction of bankAccount
bankAccount.getTransactions().add(transaction);
}
}
// Explanation same as above
public void withdraw(BankAccount bankAccount,double withdrawAmount) {
double currentBalance = bankAccount.getCurrentBalance();
Transaction transaction = new Transaction(bankAccount.getTransactions().size(),"Withdraw",withdrawAmount,currentBalance);
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);
transaction.setMoneyAfterTransaction(bankAccount.getCurrentBalance());
bankAccount.getTransactions().add(transaction);
}
}
}
}
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 the add the new BankAccount to it.
bankAccounts.put(bankAccount.getAccountId(), bankAccount);
return bankAccounts;
}
}
为Pojo(普通旧Java对象)提供Setter和Getter
始终将您的事务流程或POJO内的任何其他流程分开
看这个
交易流程类
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 String toString(){
return "\nAccount number: " + accountId + "\nHolder's name: " + holderName + "\nHolder's address: " + holderAddress + "\nOpen Date: " + openDate + "\nCurrent balance: " + currentBalance;
}
}
public class Transaction {
private int transactionId;
private String transactionType;
private double transactionAmount;
private double moneyBeforeTransaction;
private double moneyAfterTransaction;
public Transaction(){}
public Transaction(int transactionId,String transactionType,double transactionAmount, double moneyBeforeTransaction){
this.transactionId = transactionId;
this.transactionType = transactionType;
this.transactionAmount = transactionAmount;
this.moneyBeforeTransaction = moneyBeforeTransaction;
}
public int getTransactionId() {
return transactionId;
}
public void setTransactionId(int transactionId) {
this.transactionId = transactionId;
}
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;
}
public double getMoneyBeforeTransaction() {
return moneyBeforeTransaction;
}
public void setMoneyBeforeTransaction(double moneyBeforeTransaction) {
this.moneyBeforeTransaction = moneyBeforeTransaction;
}
public double getMoneyAfterTransaction() {
return moneyAfterTransaction;
}
public void setMoneyAfterTransaction(double moneyAfterTransaction) {
this.moneyAfterTransaction = moneyAfterTransaction;
}
//Override the toString() method of String ?
public String toString(){
return "Transaction ID : " + this.transactionId +
" Transaction Type : " + this.transactionType +
" Transaction Amount : " + this.transactionAmount +
" Money Before Transaction : " + this.moneyBeforeTransaction +
" Money After Transaction : " + this.moneyAfterTransaction;
}
}
public class TransactionProcess {
//Always Provide another class for process.
//Pass the bankAccount of the user
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(bankAccount.getTransactions().size(),"Deposit",depositAmount,currentBalance);
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
transaction.setMoneyAfterTransaction(bankAccount.getCurrentBalance());
//after the transaction add it to the list of Transaction of bankAccount
bankAccount.getTransactions().add(transaction);
}
}
// Explanation same as above
public void withdraw(BankAccount bankAccount,double withdrawAmount) {
double currentBalance = bankAccount.getCurrentBalance();
Transaction transaction = new Transaction(bankAccount.getTransactions().size(),"Withdraw",withdrawAmount,currentBalance);
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);
transaction.setMoneyAfterTransaction(bankAccount.getCurrentBalance());
bankAccount.getTransactions().add(transaction);
}
}
}
}
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 the add the new BankAccount to it.
bankAccounts.put(bankAccount.getAccountId(), bankAccount);
return bankAccounts;
}
}
都德..你至少要给我们看一些代码。谷歌搜索扫描器和树形图。你会得到很多教程。你甚至是如何创建银行帐户的?
?你在任何地方都没有实例化一个。@Makoto你是什么意思?我的意思是,你在这里有很多结构和定义,但我看不到你的对象彼此交互。你没有创建一个新的银行账户
。这可能是你被卡住的地方,所以让我换一句话:在你的银行
类中,你实际希望从openNewAccount
方法中得到什么?非常感谢!问题。如果我只想要一个银行账户,我将如何进行?例如,当程序运行时,我会t已经在一家银行中启动,用户所做的只是通过添加帐户等方式与该银行进行交互…可以对银行类做什么?ahmm你的意思是你想要获得特定的银行帐户?如果是,只需调用bank.getBankAccounts().get(accountId);
我只希望用户与银行帐户交互,而不是创建一个id和名称为的新银行。应该创建一个银行帐户,该帐户应保存到代码中已提供的银行中。aaaa。您想拥有一个现有银行吗?查看我的更新:)顺便说一句。从什么时候开始使用这种语言?
Bank Id: 1
Bank Name: SampleBank
Account Id: 1
Holder Name: SampleName
Opening Balance: 10000
Holder Address: SampleAddress
Open Date: SampleDate
Enter Deposit Amount
1000
Enter Withdraw Amount
2000
Account number: 1
Holder's name: SampleName
Holder's address: SampleAddress
Open Date: SampleDate
Current balance: 9000.0
Transaction ID : 0 Transaction Type : Deposit Transaction Amount : 1000.0 Money Before Transaction : 10000.0 Money After Transaction : 11000.0
Transaction ID : 1 Transaction Type : Withdraw Transaction Amount : 2000.0 Money Before Transaction : 11000.0 Money After Transaction : 9000.0