题目要求创建一个类:Bank,和一个类BankAccount,BankAccount中包含属性accountnumber和balance,在类Bank中写意个函数ReadFile,调用BankAccount中的函数Read,读取一个文件中的内容,内容格式为:
accountNumber1 balance1
accountNumber2 balance2
. . .然后显示balance最高的那个account。如果输入文件的格式不对,提供机会重新选择文件,这道题目的部分代码如下,有点I长,但很简单。关键是不知道ReadFile函数和Read函数如何互相调用,又如何传递参数,这两个函数分属两个不同的类啊。哪位能给出这两个函数的代码吗import java.util.Scanner;
import java.io.IOException;
/**
This program prompts the user to enter a file name
with account information. A bank object is filled with
the account numbers and balances specified in the file.
In case of an exception, the user can choose another file.
*/
public class BankReader
{
public static void main(String[] args)
{
boolean done = false;
Scanner in = new Scanner(System.in);
Bank bank = new Bank();
while (!done)
{
System.out.print("Filename: ");
String filename = in.next();
try
{
bank. readFile(filename);
BankAccount highest = bank.getHighestBalance();
System.out.println(highest.getAccountNumber()
+ " " + highest.getBalance());
done = true;
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
Use the following classes in your solution:
import java.util.Scanner;
import java.io.IOException;
import java.util.InputMismatchException;
/**
A bank account has a balance that can be changed by deposits and
withdrawals.
*/
public class BankAccount
{
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
accountNumber = 0;
balance = 0;
}
/**
Constructs a bank account with a given balance.
@param initial Balance the initial balance
*/
public BankAccount(int anAccountNumber, double initial Balance)
{
accountNumber = anAccountNumber;
balance = initial Balance;
}
/**
Reads an account number and balance.
@param in the scanner
@return true if the data was read
false if the end of the stream was reached
*/
public void read(Scanner in) throws IOException
{
. . .
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
balance = balance + amount;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
balance = balance - amount;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
/**
Gets the account number of the bank account.
@return the account number
*/
public int getAccountNumber()
{
return accountNumber;
}
private double balance;
private int accountNumber;
}
_______________________________________
import java.util.ArrayList;
import java.io.IOException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.NoSuchElementException;
/**
A bank contains account numbers and balances of each customer.
*/
public class Bank
{
/**
Constructs a Bank object.
*/
public Bank()
{
accountList = new ArrayList<BankAccount>();
}
/**
Read a file with account numbers and balances and adds the
accounts
to the bank.
@param filename the name of the file
*/
public void readFile(String filename) throws IOException
{
. . .
}
/**
Adds an account to the bank.
@param b the BankAccount reference
*/
public void addAccount(BankAccount b)
{
accountList.add(b);
}
/**
Gets the account with the highest balance.
@return the account with the highest balance
*/
public BankAccount getHighestBalance()
{
BankAccount max = accountList.get(0);
for (BankAccount account : accountList)
{
if (account.getBalance() > max.getBalance())
max = account;
}
return max;
}
private ArrayList<BankAccount> accountList;
}
accountNumber1 balance1
accountNumber2 balance2
. . .然后显示balance最高的那个account。如果输入文件的格式不对,提供机会重新选择文件,这道题目的部分代码如下,有点I长,但很简单。关键是不知道ReadFile函数和Read函数如何互相调用,又如何传递参数,这两个函数分属两个不同的类啊。哪位能给出这两个函数的代码吗import java.util.Scanner;
import java.io.IOException;
/**
This program prompts the user to enter a file name
with account information. A bank object is filled with
the account numbers and balances specified in the file.
In case of an exception, the user can choose another file.
*/
public class BankReader
{
public static void main(String[] args)
{
boolean done = false;
Scanner in = new Scanner(System.in);
Bank bank = new Bank();
while (!done)
{
System.out.print("Filename: ");
String filename = in.next();
try
{
bank. readFile(filename);
BankAccount highest = bank.getHighestBalance();
System.out.println(highest.getAccountNumber()
+ " " + highest.getBalance());
done = true;
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
Use the following classes in your solution:
import java.util.Scanner;
import java.io.IOException;
import java.util.InputMismatchException;
/**
A bank account has a balance that can be changed by deposits and
withdrawals.
*/
public class BankAccount
{
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
accountNumber = 0;
balance = 0;
}
/**
Constructs a bank account with a given balance.
@param initial Balance the initial balance
*/
public BankAccount(int anAccountNumber, double initial Balance)
{
accountNumber = anAccountNumber;
balance = initial Balance;
}
/**
Reads an account number and balance.
@param in the scanner
@return true if the data was read
false if the end of the stream was reached
*/
public void read(Scanner in) throws IOException
{
. . .
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
balance = balance + amount;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
balance = balance - amount;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
/**
Gets the account number of the bank account.
@return the account number
*/
public int getAccountNumber()
{
return accountNumber;
}
private double balance;
private int accountNumber;
}
_______________________________________
import java.util.ArrayList;
import java.io.IOException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.NoSuchElementException;
/**
A bank contains account numbers and balances of each customer.
*/
public class Bank
{
/**
Constructs a Bank object.
*/
public Bank()
{
accountList = new ArrayList<BankAccount>();
}
/**
Read a file with account numbers and balances and adds the
accounts
to the bank.
@param filename the name of the file
*/
public void readFile(String filename) throws IOException
{
. . .
}
/**
Adds an account to the bank.
@param b the BankAccount reference
*/
public void addAccount(BankAccount b)
{
accountList.add(b);
}
/**
Gets the account with the highest balance.
@return the account with the highest balance
*/
public BankAccount getHighestBalance()
{
BankAccount max = accountList.get(0);
for (BankAccount account : accountList)
{
if (account.getBalance() > max.getBalance())
max = account;
}
return max;
}
private ArrayList<BankAccount> accountList;
}
public void readFile(String filename) throws IOException
{
File file = new File(filename);
LineNumberReader lnr = new LineNumberReader(new FileReader(file));
do
{
String line = lnr.readLine();
// Parse line
if (null != line) {
int p = line.indexOf(" ");
String a = line.substring(0, p);
String b = line.substring(p + 1);
}
}
while(true);
}
{ String line = in.nextLine();
// Next
// Next
}1楼有代码可以copy过来了。