[英]How do I get to use variables from two different methods, in a new method?
I have written a code which reads in two text files in seperate methods.我编写了一个代码,它以单独的方法读取两个文本文件。
These are the files:这些是文件:
Accountnumbers and balances帐号和余额
0015 112.34
0020 4000.00
0034 9345.89
0069 723.50
0085 1500.00
0091 8237.31
0128 29.75
0129 -483.14
0135 2908.83
0189 10045.90
0251 5700.10
0396 29.75
0404 3299.99
0563 1000.02
2678 -5679.23
4561 12.00
8888 0.00
9999 9876.78
Accountnumbers, a deposit (1) or withdrawal (2), and the amount帐号、存款 (1) 或取款 (2) 以及金额
0015 1 2000.00
2678 1 5000.00
0189 1 250.00
0034 2 500.00
0085 1 375.00
0404 2 72.49
0128 2 30.00
0189 1 250.00
4561 1 10.00
0020 2 189.70
0015 2 1000.00
0010 1 500.00
From these files I created different variables, also in two different methods.从这些文件中,我创建了不同的变量,也是用两种不同的方法。 Now, I want to create a new method, in which I work with variables from the other two methods.
现在,我想创建一个新方法,在其中我使用其他两种方法中的变量。
Here is my code with an extra class.这是我的代码,带有额外的 class。
package Bank;
import java.util.Scanner;
import java.io.PrintStream;
import ui.UIAuxiliaryMethods;
class Bank {
PrintStream out;
Bank() {
out = new PrintStream(System.out);
}
void readAccountsIn() {
Scanner fileScanner = UIAuxiliaryMethods.askUserForInput().getScanner();
while(fileScanner.hasNext()) {
String Account = fileScanner.nextLine();
readAccountLine(Account);
}
}
void readMutationsIn() {
Scanner fileScanner1 = UIAuxiliaryMethods.askUserForInput().getScanner();
while(fileScanner1.hasNext()) {
String Mutation = fileScanner1.nextLine();
readMutationLine(Mutation);
}
}
void readAccountLine(String Account) {
String[] parts = Account.split("\\s+");
String account1 = parts[0];
String balance1 = parts[1];
double balance = Double.parseDouble(balance1);
Accounts line = new Accounts(account1, balance);
out.printf("%s %.2f \n",line.account1, line.balance);
}
void readMutationLine(String Mutation) {
String[] parts = Mutation.split("\\s+");
String account = parts[0];
String type1 = parts[1];
String mutation1 = parts[2];
int type = Integer.parseInt(type1);
double mutation = Double.parseDouble(mutation1);
Mutations line1 = new Mutations(account, type, mutation);
out.printf("%s %d %.2f \n",line1.account, line1.type, line1.mutation);
}
void start() {
readAccountsIn();
readMutationsIn();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Bank().start();
}
}
package Bank;
class Accounts {
String account1;
double balance;
Accounts(String account1, double balance){
this.account1 = account1;
this.balance = balance;
}
}
I want to create a new method called Summary() in which I use all the variables (account1 and balance) from readAccountline() and (account, type, mutation) readMutationLine().我想创建一个名为 Summary() 的新方法,其中我使用 readAccountline() 和(帐户、类型、突变)readMutationLine() 中的所有变量(account1 和 balance)。 Is there any way to do this?
有没有办法做到这一点?
First name your class Account
and use accountName
for your string, the model should reflect the real use首先命名您的 class
Account
并使用accountName
作为您的字符串,model 应该反映真实用途
Your Bank
should hve a list of Account
to store and use them您的
Bank
应该有一个Account
列表来存储和使用它们
class Bank {
PrintStream out;
List<Account> accounts;
Bank() {
out = new PrintStream(System.out);
accounts = new ArrayList<>();
}
}
Then when creating Account
instanes, save them然后在创建
Account
实例时,保存它们
void readAccountLine(String account) {
//...
Account acc = new Account(account1, balance);
accounts.add(acc);
}
Then you could write the toString
method that would summarize the Bank's content然后你可以编写
toString
方法来总结银行的内容
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Bank");
for (Account a : accounts)
sb.append(a); // you may write the toString() for Account too
return sb.toString();
}
And use like:并使用如下:
public static void main(String[] args) {
Bank b = new Bank();
b.start();
System.out.println(b); // will call toString();
}
Then do the same for Mutations
然后对
Mutations
做同样的事情
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.