importjava.util.Random;
importjava.text.NumberFormat;
import static java.lang.System.out;
classBetterAccount {
String lastName;
int id;
double balance;
<strong> </strong>
<strong>void fillWithData() {</strong>
<strong>Random myRandom = new Random();</strong>
<strong>lastName = "" +</strong>
<strong> </strong> <strong>(char) (myRandom.nextInt(26) + 'A') +</strong>
<strong> </strong> <strong>(char) (myRandom.nextInt(26) + 'a') +</strong>
<strong> </strong> <strong>(char) (myRandom.nextInt(26) + 'a');</strong>
<strong> </strong>
<strong>id = myRandom.nextInt(10000);</strong>
<strong>balance = myRandom.nextInt(10000);</strong>
<strong>}</strong>
void display() {
NumberFormat currency = NumberFormat.getCurrencyInstance();
out.print("The account with last name ");
out.print(lastName);
out.print(" and ID number ");
out.print(id);
out.print(" has balance ");
out.println(currency.format(balance));
}
}
Here’s a way to use the class in the code above. Check out the new code.
classProcessBetterAccounts {
public static void main(String args[]) {
<strong>BetterAccount</strong>anAccount;
for (int i = 0; i < 3; i++) {
anAccount = new <strong>BetterAccount</strong>();
<strong>anAccount.fillWithData();</strong>
anAccount.display();
}
}
}
The second set of code is pretty slick. Because the code in the first listing is so darn smart, the new code has very little work to do. This new code just creates a BetterAccount
object and then calls the methods in the first listing of code.