0

我是 Java 的初学者,希望能更多地了解这门语言。我知道这需要练习,我希望尽我所能探索这个问题,但是我需要更多地理解这个问题。我已经完成了一些代码,但是无法发布它,因为我班上的其他人将能够看到它并复制代码。

以下是问题::


假设您正在为自动柜员机设计一个程序。自动柜员机生成交易,然后发送到银行中央计算机进行处理。

在本作业中,您将创建两个类,“Account”和“Transaction”。一个 Account 对象应该有一个唯一的帐号,可以用一个整数和一个余额来表示。最初余额为零。一个 Transaction 对象应该有一个被交易的金额和一个对与交易关联的 Account 类的引用。

下面是调用两个类的示例:

/**
 * This class contains a main method that calls methods in the classes you will write.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class RunTransactions
{
    static void main()
    {
       // Create two new accounts with the given account numbers
       Account fred = new Account(1234);
       Account jim = new Account(6778);

       // Provide accessor methods for account information.
       int accountNumber = fred.getAccountNumber();
       float balance = fred.getBalance();

       // Transactions consist of an account reference and an amount
       Transaction t1 = new Transaction(fred, 20);
       Transaction t2 = new Transaction(jim, 10);
       Transaction t3 = new Transaction(jim, -20);

       // Transactions must contain a "process" method that is called to
       //  actually perform the transaction.
       // A transaction should not be allowed if it results in a negative balance.
       t1.process();
       t2.process();
       t3.process();

       // Print out a report of the account balance.
       // The format should be like this: Account 6778 has balance $20.0
       fred.report();
       jim.report();
    }
}

下载此文件。它包含一个只有 RunTransactions 类的 BlueJ 项目。您必须创建自己的 Account 和 Transaction 类。您可以创建您认为合适的任何字段和方法,但您必须提供可以完全按照上述方式调用的方法。

报告方法应该打印出一条简单的消息,给出帐号和余额。格式应该是

   Account <account number> has balance $<account balance>

例如,上述示例代码的报告输出应为:

   Account 1234 has balance $20.0
   Account 6778 has balance $10.0

您将在以后的讲座中了解主要方法和静态限定符。现在,您只需要知道,当您右键单击 RunTransactions 类时,您将在菜单 void main() 中看到一个项目。单击此按钮将运行 main 方法,该方法将调用您的代码。

您可以更改主要方法以使用不同的帐户和交易组合测试您的代码。只记得你必须使用完全相同的类和方法名称,并且参数类型和返回值必须相同。

当您提交作业时,我们会使用不同的主要方法测试您的代码。

4

1 回答 1

0

他们要求您编写两个类 - AccountTransaction,以便在提供的 RunTransactions 中的代码执行时(运行 main 方法),应用程序将产生预期的输出。祝你好运!

于 2013-09-19T09:38:53.763 回答