我编写了一个代码,它以单独的方法读取两个文本文件。
这些是文件:
帐号和余额
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
帐号、存款 (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
我从这些文件中创建了不同的变量,也是用两种不同的方法。现在,我想创建一个新方法,在其中使用其他两个方法中的变量。
这是我的带有额外类的代码。
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;
}
}
我想创建一个名为 Summary() 的新方法,其中使用 readAccountline() 和(帐户、类型、突变)readMutationLine() 中的所有变量(account1 和余额)。有什么办法可以做到这一点吗?
最佳答案
首先将您的类命名为Account
,并使用accountName
作为您的字符串,模型应反射(reflect)实际用途
您的银行
应该有一个帐户
列表来存储和使用它们
class Bank {
PrintStream out;
List<Account> accounts;
Bank() {
out = new PrintStream(System.out);
accounts = new ArrayList<>();
}
}
然后在创建Account
实例时,保存它们
void readAccountLine(String account) {
//...
Account acc = new Account(account1, balance);
accounts.add(acc);
}
然后您可以编写 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();
}
并使用类似:
public static void main(String[] args) {
Bank b = new Bank();
b.start();
System.out.println(b); // will call toString();
}
然后对突变
执行相同的操作
关于java - 如何在新方法中使用两种不同方法中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58696831/