DCS 235 Software Engineering Laboratory Sheet 1 Programming in the Large: Building a Bank version 1.2, October 2004 Prerequisites • You should be able to run TogetherEC, the Together plugin for Eclipse. Check the SE web page for instructions. • Download the the zip file containing 2 java projects from the SE web page. Unpack it in a suitable place in your filestore. Aims The aim of this exercise is develop a program, making use of classes and also to become familiar with TogtherEC. The skills you will learn and practise are: • Writing Java programs with classes. • Understanding the different ways classes are related. • Identifying suitable classes from information about a problem. • Using TogetherEC. The program – modelling a bank account – is developed in steps. To begin with detailed instructions are given; for the later steps the instructions are less detailed. Two partially implemented Java projects are provided with this exercise – ensure that they are downloaded onto your machine. Note: you could achieve the first 3 aims above using a simpler tools such as BlueJ. TogetherEC is a real state-of-the-art development not just for a teaching tool and it will be used throughout this course unit. However, it might be sensible to revert to BlueJ if you get stuck with TogetherEC. Preparatory Reading Revise class declarations in Java. Many textbooks are suitable, including: • Horstmann’s ‘Computing Concepts with Java Essentials ’ • Jia’s book ‘Object-Oriented Software using Java’, sections 4.1-4.4, 5.1 and 5.2 (Java classes). 1.The Account Class – Bank0 Open the Bank0 project1 which contains a single class ‘Account’. The class is partially implemented: a complete template is provided, with comments explaining how each method should behave. There are 5 methods which you need to complete. 1. Use the ‘Togther Modeling Perspective’ to show the class diagram and the Java source code. Figure 1 shows what you should see. Note: read the comments carefully to see how an annual interest rate of 1.5% is represented: in the parameter list of the constructor it's a double, but it is stored as an integer. The number types in Java are covered in section 4.1 in Jia’s book. 1 See notes on projects in TogetherEC at the end of this sheet. DCS 235 Lab Sheet 1 (version 1.2_0, 4 Oct 2004) Page 1 2. Implement and compile the methods. 3. Write a main method to test the program. Put the main method in a new class: Bank. Figure 1: Interface of the Account Class Some simple features of TogtherEC you should try out: 1. Editor features: the declaration of an identifier is displayed when the cursor hovers over an identifier (also press F2); method calls can be selected from a list. 2. Syntax and type errors are highlighted (like spelling mistakes in Word). 3. There are multiple ‘perspectives’: switch between the Java Perspective and the Together Modeling Perspective. 4. Generate HTML documentation from JavaDoc comments in the source file (see Jia section 6.1.4). Browse the documentation in a web browser – Figure 2. Figure 2: Browing Doumentation in Java DCS 235 Lab Sheet 1 (version 1.2_0, 4 Oct 2004) Page 2 2.Transactions – Project1 The bank account is simplistic: there is no record of the deposits and withdrawals made by the account owner. Here is a UML class diagram2 for a possible design for a more realistic bank account. The diagram shows 4 classes: Account, Transaction, Deposit and Withdraw. Deposit and Withdraw extend Transaction. Open Project 1; this contains a partial implementation of the ‘Account’ class and the abstract class ‘Transaction’ in full. The transactions of the account are held in a linked list; this has been provided for you. Read the code and check you understand how it works. 1. Look at the interface of the classes, for example by generating the documentation. Note that there are some changes to the interface of this version of the Account: • the methods ‘getBalancePounds’ and ‘getBalancePence’ have been dropped • we now specify the day on which a withdrawal or deposit is made • there is no calculation of interest (we return to this later!). 2. Add the classes ‘Withdrawal’ and ‘Deposit’. This can be done in the disgram window. 3. Complete the implementation of the classes ‘Withdraw’ and ‘Deposit’. 4. The ‘Account’ class contains the unimplemented method ‘getCurrentBalance’. Complete the implementation of this method. 5. The Account class contains an unimplemented method ‘showStatement’. This is intended to print a statement to the console. For example: Opening balance £100.00 Deposited £13.50 on day 1 Withdrew £25.80 on day 2 Current balance £87.70 Complete the implementation of the ‘showStatement’ method. Note that a method ‘poundsAndPence’ is provided to ensure that the pounds and pence are properly formatted: private static String poundsAndPence(int amount) { int aa = Math.abs(amount) ; String leadingZero = aa % 100 < 10 ? "0" : "" ; String poundSign = amount < 0 ? "-£" : "£" ; return (poundSign + aa/100 + "." + leadingZero + aa%100) } ; 2 UML class diagrams will be covered in lectures. To get ahead, read Jia section 2.2. However, you do not need to understand beyond the explanation given in the this sheet for this exercise. DCS 235 Lab Sheet 1 (version 1.2_0, 4 Oct 2004) Page 3 Account Transaction Deposit Withdraw *1 3.Reorganising the Code – Project2 1. Did you make use of the ‘poundsAndPence’ method? You may have thought that this code isn’t well organised: • you needed the same routine in other classes – did you copy it or make it public? • rather than being static, it should operate on the some object, but it isn’t an operation on the Account class. 2. The ‘balance’ field of the Account class is being used to hold the ‘opening balance’. This means that every time the current balance is requested, all the transactions have to be added up. A better approach might be to make Account.balance the current balance. It would need to be updated for each transaction. How would the opening balance be held? Could it be considered to be another kind of transaction? Reorganise your code to take account of these points. 4.Monthly Statements – Project3 Real banks do not send you a statement listing all the transactions on an account – each statement would include the previous one. Lets enhance the bank to have monthly statements. The bank’s requirements are: • the transactions occur in a month (give each month a number starting at 1, ignore years) • the day of a transaction is relative to the start of the month • a new method ‘endMonth’ is needed, which completes the current month and starts the next one • a statement covers all the transactions in a month, starting with the opening balance and finishing with the balance at the end of the month • the statement for any month can be printed. Enhance the software to meet these requirements: 1. Decide which new class (or classes) is (are) needed. Identify the attributes and methods of the new class(es). Write this down before you begin to code. 2. Use an array to hold the months, allowing up to twelve months. 5.Challenge Problem – Interest Calculation We can now implement a more realistic calculation of interest. The bank uses the following rules to calculate the interest: • Interest payment/charges are added to the account at the end of each month (assume all months have 30 days). • A payment/charge is calculated for each day, using the annual appropriate rate (assume 365 days in every year). • If the account is overdrawn on any day in the month, no interest is paid for the days when the account is in credit. DCS 235 Lab Sheet 1 (version 1.2_0, 4 Oct 2004) Page 4 6.Notes on Creating Projects in TogetherEC First download and unpack the zip file. To create a new project in Together, use the File/New/Project command. 1. At the ‘New Project, Select Wizard’ tab, choose ‘Java Project’. 2. At the ‘New Java Project’ tab, give the project a name and choose the ‘Create Project at External Location’ option: navigate to the Bank0 directory you have saved. See Figure 3. 3. Do nothing (hopefully) on the ‘Java Settings’ tab. Figure 3: Creating a New Project in TogetherEC You can open and close a project: in both states the project is shown in the tree view of the workspace. You can delete a project to remove it from TogetherEC: you are asked whether or not you want the files deleted from the disk when you do this. DCS 235 Lab Sheet 1 (version 1.2_0, 4 Oct 2004) Page 5