Lab 10: Bank Account II Transaction List, error checking & aggregation.

Slides:



Advertisements
Similar presentations
More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.
Advertisements

Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Fields, Constructors, Methods
Project Part 3 Putting the pieces together. Modifications on previous parts In part 1 you created a : BankAccount class, with several fields. Increment.
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Improving structure with inheritance
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Understanding class definitions Looking inside classes 3.0.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
Grouping Objects 2 Collections and the for-each loop Collections and the while loop.
Grouping Objects 1 Introduction to Collections.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
1 Reflecting on the ticket machines Their behavior is inadequate in several ways: –No checks on the amounts entered. –No refunds. –No checks for a sensible.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
Understanding class definitions Looking inside classes.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
McGraw-Hill/Irwin Introduction to QuickBooks Pro, 2004 © 2005 The McGraw-Hill Companies, Inc., All Rights Reserved. Chapter 4 Bank Reconciliation.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Writing Methods Using if, loops, and variables to implement algorithms Copyright © 2012 Pearson Education, Inc.
Writing Classes (Chapter 4)
Glenn David Blank Computer Science & Engineering Department Lehigh University, Bethlehem, PA, USA With support from the National Science Foundation (Grants.
Object Oriented Design: Identifying Objects
Writing JavaDocs Mimi Opkins CECS 274 Copyright (c) Pearson All rights reserved.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Grouping objects Introduction to collections 5.0.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Copyright © 2012 Pearson Education, Inc. Lab 8: IF statement …. Conditionals and Loops.
Introduction to Java Java Translation Program Structure
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
Lab 7: Tree & Forest. Drawing a Forest Created by Emily Hill & Jerry Alan Fails.
Understanding class definitions
Chapter 4 Introduction to Classes, Objects, Methods and strings
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Chapter 13 ATM Case Study Part 2: Implementing an Object-Oriented Design Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Looking inside classes Conditional Statements Week 4.
Grouping objects Introduction to collections 5.0.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Objects First with Java CITS1001 week 4
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Objects First with Java Transaction List, error checking & aggregation
Understanding class definitions
Understanding class definitions
Conditions and Ifs BIS1523 – Lecture 8.
ITunes Lab Copyright © 2012 Pearson Education, Inc.
COS 260 DAY 18 Tony Gauvin.
COS 260 DAY 3 Tony Gauvin.
CS139 October 11, 2004.
Understanding class definitions
Collections and iterators
COS 260 DAY 4 Tony Gauvin.
Improving structure with inheritance
Collections and iterators
Presentation transcript:

Lab 10: Bank Account II Transaction List, error checking & aggregation

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

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

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

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.

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.

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”

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”

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

Aggregating Objects: Transaction Should already be implemented New additions

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.

Aggregating Objects: Transaction But we’ll need one more concept…

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.

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);

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.

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

Homework Finish lab CodingBat Look at Project 1 Read Chapter 5 Created by Emily Hill & Jerry Alan Fails