Download presentation
Presentation is loading. Please wait.
Published byGarry Merritt Modified over 9 years ago
1
Lab 10: Bank Account II Transaction List, error checking & aggregation
2
Last time we created a BankAccount class: 1.Fields to store: –Current balance –Account number –Customer name –Customer address * Think about what types these should be. For example, should the account number actually be stored as a number? What about leading 0’s? 2.Write a constructor that takes the customer’s name, address, and account number, and initializes the balance to 0. 3.Write getter and setter methods for the name and address fields. 4.Write a deposit and a debit method.The method signatures are similar in that there is no return value and one parameter. Created by Emily Hill & Jerry Alan Fails
3
And we tested BankAccount in main: 4.Write a print method that prints out the account information, including the current balance. Make sure to use proper formatting so both dollars and cents are displayed correctly. This method should take no parameters and return nothing. 5.Write a main method that creates a new, empty BankAccount stored in local variable myBankAccount. –Deposit $5.00 into your BankAccount & print the balance –Debit $1.50 into your BankAccount & print the balance 6.Before submitting make sure your BankAccount has the following methods: –1 constructor only (not 2) that takes 3 parameters –getCustomerName, getCustomerAddress, getAccountNumber –setCustomerName, setCustomerAddress –deposit, debit, print, main Created by Emily Hill & Jerry Alan Fails
4
Today we will Update debit to check the balance before deducting Update debit & deposit to only work with positive values Add a Transaction class too keep track of all deposits and debits Create new deposit and debit methods that also take a String description as a parameter Create a printSummary method that prints all the transactions Created by Emily Hill & Jerry Alan Fails
5
The if Statement if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped. Copyright © 2012 Pearson Education, Inc.
6
The if-else Statement if ( condition ) statement1; else statement2; If the condition is true, statement1 is executed; if the condition is false, statement2 is executed One or the other will be executed, but not both Copyright © 2012 Pearson Education, Inc.
7
Logic of an if-else statement condition evaluated statement1 true false statement2 Copyright © 2012 Pearson Education, Inc. Can use if to code things like: if total > 100 print “$” + total / 100 else print total + “ cents”
8
Logic of an if-else statement total > 100? print “$” + total / 100 truefalse print total + “ cents” Copyright © 2012 Pearson Education, Inc. Can use if to code things like: if total > 100 print “$” + total / 100 else print total + “ cents”
9
Extending BankAccount with if 7.Modify the debit method to make sure there are sufficient funds before deducting from the balance. Print an error message if there are insufficient funds. Test this in main. 8.Modify the debit & deposit methods so that negative amounts have no effect. For example, myBankAccount.deposit(5) will add $5, but myBankAccount.deposit(-5) will do nothing. Test this in main. Created by Emily Hill & Jerry Alan Fails
10
Aggregating Objects: Transaction Should already be implemented New additions
11
Adding a Transaction class 9.Create a Transaction class & enumerated type: * You can use Eclipse’s source code generation to create getters and setters for the fields 10.Create an ArrayList field in BankAccount to store a list of Transactions, & initialize it in the constructor.
12
Aggregating Objects: Transaction But we’ll need one more concept…
13
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Grouping (i.e., collecting) objects Many applications involve collections of objects: –Personal organizers. –Library catalogs. –Student-record system. The number of items to be stored varies. –Items added. –Items deleted.
14
For-each Loops & ArrayLists Simplifies repetitive processing of items in a collection Can be applied to a collection of items, such as ArrayList: ArrayList list = new ArrayList (); We can add items to the list: list.add(new Book(“Ender’s Game”, “Orson Scott Card”)); The following loop will print each book: for (Book myBook : list) System.out.println (myBook);
15
Adding a Transaction class 9.Create a Transaction class & enumerated type: * You can use Eclipse’s source code generation to create getters and setters for the fields 10.Create an ArrayList field in BankAccount to store a list of Transactions, & initialize it in the constructor.
16
Adding a Transaction class, cont’d 11.Update deposit and debit so that they also take a String description as a parameter, this will temporarily cause errors in main. 12.Update debit and deposit to to add a new Transaction whenever balance is changed. 13.Create new deposit and debit methods that only take amount as a parameter, and simply calls the existing corresponding debit/deposit method with an empty string (“”). 13.Create a printSummary method that calls the print() method and then prints all the transactions using a for each loop. Test this in main. Created by Emily Hill & Jerry Alan Fails
17
Homework Finish lab CodingBat Look at Project 1 Read Chapter 5 Created by Emily Hill & Jerry Alan Fails
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.