跪求JAVA高手帮我完善下程序,写好的+30分

红框以上的我都写好了,我不会的是红框里面的,我读不懂什么意思,麻烦JAVA高手帮我写程序的时候帮忙解释下红框里面的要求是什么意思
这个是我写的程序:
//*******************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************
import java.util.Random;
public class Account
{
private double balance;
private String name;
private long acctNum;
private static int numAccount = 0;
public static Random generator = new Random();
//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
public Account(double initBal, String owner)
{
balance = initBal;
name = owner;
acctNum = generator.nextInt(10000) + 10000;
numAccount++;
}
//----------------------------------------------
// Checks to see if balance is sufficient for withdrawal.
// If so, decrements balance by amount; if not, prints message.
//----------------------------------------------
public void withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}
//----------------------------------------------
// Adds deposit amount to balance.
//----------------------------------------------
public void deposit(double amount)
{
balance += amount;
}
//----------------------------------------------
// Returns balance.
//----------------------------------------------
public double getBalance()
{
return balance;
}
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString()
{
return "Name:" + name +"\nAccount Number: " + acctNum + "\nBalance: " + balance;
}
public static int numAccounts()
{
return numAccount;
}
}

//***********************************************************
// TestAccounts1
// A simple program to test the numAccts method of the
// Account class.
//***********************************************************
import java.util.Scanner;
public class TestAccounts1
{
public static void main(String[] args)
{
Account testAcct;
Scanner scan = new Scanner(System.in);
System.out.println("How many accounts would you like to create?");
int num = scan.nextInt();
for (int i=1; i<=num; i++)
{
testAcct = new Account(100, "Name" + i);
System.out.println("\nCreated account " + testAcct);
System.out.println("Now there are " + Account.numAccounts() +
" accounts");
}
}
}

第1个回答  2012-04-06
--consolidate
public static Account consolidate(Account a1 ,Account a2)
{
if(!a1.getName().equls(a2.getName())){
return null;
}
//close 方法里面 加 closed的目的是区分一下是否有效
if(a1.getName().endsWith("CLOSED") or a2.getName().endsWith("CLOSED")){
return null;
}
if(a1.getAcctNum().equls(a2.getAcctNum())){
return null;
}

Account aNew= new Account(100, "Name" + (Account.numAccount +1));
aNew.setBalance(a1.getBalance()+a1.getBalance());
this.colose(a1);
this.colose(a2);
return aNew;
}
public class TestAccounts1
{
public static void main(String[] args)
{
Account testAcct;
List accList= new ArrayList();
Scanner scan = new Scanner(System.in);
System.out.println("How many accounts would you like to create?");
int num = scan.nextInt();
for (int i=1; i<=num; i++)
{
testAcct = new Account(100, "Name" + i);
accList.add(testAcct );
System.out.println("\nCreated account " + testAcct.getAcctNum());
System.out.println("\nCreated account " + testAcct.getName());
" accounts");
}
for (int i=0; i<accList.seze(); i++)
{
testAcct = (Account)accList.get(i);
System.out.println("AccNum: " + testAcct.get);
System.out.println("AccNumName: " + testAcct.getName());
}
testAcct = (Account)accList.get(0);
testAcct.close();
testAcct = Account.consolidate((Account)accList.get(1),(Account)accList.get(2));
System.out.println("NewAcc: " + testAcct.getAcctNum());

}
}
----Account.close()
这个方法就是 在name属性后面连接一个串“CLOSED” 用来区分是无效的账户。
同时把balance的值=0;

很简单,不写了。 以上都是现写的没有测试过。应该没有问题。可以自己调试一下,加深理解
第2个回答  2012-04-06
//***********************************************************
// TestAccounts1
// A simple program to test the numAccts method of the
// Account class.
//***********************************************************
import java.util.Scanner;

public class TestAccounts1 {
public static void main(String[] args) {
Account testAcct;
Scanner scan = new Scanner(System.in);
System.out.println("How many accounts would you like to create?");
int num = scan.nextInt();
for (int i = 1; i <= num; i++) {
testAcct = new Account(100, "Name" + i);
System.out.println("\nCreated account " + testAcct);
System.out.println("Now there are " + Account.numAccounts()
+ " accounts");
}
System.out.println("Which account would you like to close?");
num = scan.nextInt();
Account.close(num);
System.out.println(Account.getAccountMap());
System.out.println("Which two accounts would you like to consolidate?");
long acct1 = scan.nextLong();
long acct2 = scan.nextLong();
Account.consolidate(acct1,acct2);
System.out.println(Account.getAccountMap());
}
}
//*******************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
public class Account
{
private double balance;
private String name;
private long acctNum;
public long getAcctNum() {
return acctNum;
}
public void setAcctNum(long acctNum) {
this.acctNum = acctNum;
}
private static int numAccount = 0;
private static Map accountMap = new TreeMap();
public static Map getAccountMap() {
return accountMap;
}
public static void setAccountMap(Map accountMap) {
Account.accountMap = accountMap;
}
public static Random generator = new Random();
//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
public Account(double initBal, String owner)
{
balance = initBal;
name = owner;
acctNum = generator.nextInt(10000) + 10000;
accountMap.put(acctNum, this);
numAccount++;
}
//----------------------------------------------
// Checks to see if balance is sufficient for withdrawal.
// If so, decrements balance by amount; if not, prints message.
//----------------------------------------------
public void withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}
//----------------------------------------------
// Adds deposit amount to balance.
//----------------------------------------------
public void deposit(double amount)
{
balance += amount;
}
//----------------------------------------------
// Returns balance.
//----------------------------------------------
public double getBalance()
{
return balance;
}
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString()
{
return "Name:" + name +"\nAccount Number: " + acctNum + "\nBalance: " + balance;
}
public static int numAccounts()
{
return numAccount;
}
public static void close(long acctNum){
Account account = (Account)accountMap.get(acctNum);
account.setName(account.getName()+"_CLOSED");
account.setBalance(0);
numAccount--;
}
public static Account consolidate(long acct1,long acct2){
Account acct11 = (Account)accountMap.get(acct1);
Account acct22 = (Account)accountMap.get(acct2);
Account account = null;
if(acct11.getName().equals(acct22.getName()) && acct11.getAcctNum()!=acct22.getAcctNum()){
account = new Account(100,acct22.getName());
account.setBalance(acct11.balance+acct22.balance);
close(acct11.getAcctNum());
close(acct22.getAcctNum());
}else{
System.out.println("两个账号不是同一人所有,不能合并!");
}
return account;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setBalance(double balance) {
this.balance = balance;
}
}追问

兄弟,我想问下Map的作用

追答

可以很方便的用账号找到对应的 对象
不然你怎么去修改之前创建的对象,更别说去合并了

本回答被提问者采纳
第3个回答  2012-04-06
不好弄吧、他们都很忙

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网