CS 239 Designing a Class

In this lab, you will practice thinking through the decisions about creating a class. You are welcome to work with someone else and to submit only 1 copy of the lab for both of you (although both should keep a copy of it). If you do that, in the comment section for the lab submission, put both partner's names in the comment. Upload your completed stubs to receive credit for the lab.

Objectives

At the conclusion of this lab you will:

Background

You are part of a team of programmers working on an application for a small Mom and Pop convenience store. You have been assigned the task of developing the account class which will track regular customers and their accounts.

Instructions

Description of your class

An account represents one person or family's account with the store. The account will have an identifying name, and address, and a date that the account was opened. The account will also have a charge limit which represents the amount that the person can charge at the store. An account can be opened or closed. If closed, no charges can be made on that account. Initially all accounts have a 0 charge limit. We also want to track the total amount of purchases at the store. When an account exceeds $500.00 in total purchases, the store will extend them the courtesy of a charge. We must also keep track of the amount charged. So, we need to be able to do the following things:

  1. See if this account has a charge limit and how much that limit is.
  2. Post new purchases to the account. A purchase can either be positive (they purchased the item) or negative (they returned the item).
  3. Post new charges to the account. A charge can only be positive. If a charge exceeds the charge limit, we must throw a LimitExceededException. This will be built by another team. Charges do not add to purchases until they have been paid.
  4. Post payments to the account. A payment can only be positive, but will subtract from the charged amount and add to the purchased amount. If the payment exceeds the charged amount, the charges amount can be negative.
  5. See if an account with no charge limit has $500.00 in purchases. (This should be a boolean method)
  6. Display the current state of the account, with the person's name, address, date, charge limit, charge amount, and purchase amount. It is up to you how you want to format this data.
  7. At any time, we need to also be able to update a name and address. We must also be able to adjust the charge limit. Note, charge limits can never be negative.

Forming the initial encapsulation

Create a new .java file named Account.java. Build your class header and include the documentation of the authors, version, and description of the class.

  1. Looking at the description, what attributes will you need? What is their data type? These will be the important noun phrases in the description. Underline these in the description and decide which will be attributes(instance variables) and which may be derived from other attributes. Add the necessary ones to your class. Remember that all attributes should be private.

  2. What methods come to mind? These will be the verb phrases that tell you what we must be able to do with the account. Don't worry about constructors yet. Add them to your class, thinking carefully about the appropriate parameters and return types. Don't worry about all of the possible overloaded methods; that will come later. Include documentation to describe the method and its parameters, return, and any anticipated exceptions.

Refining the class

  1. One requirement is that if an account is closed, you can make no charges on that account. How will you implement this? Will a helper method be beneficial? Add it if you have not already done so.
  2. Are there any methods that may help out other methods. If so, have you designed them in such a way that they can be called from the other method.
  3. Are there any get methods that may be desireable.
  4. Add any of these methods (or alter existing methods) based on your thoughts. Be sure to document them. If they are helper methods, recall that they should be private.

Constructors

  1. Looking at the essential attributes of an account, what explicit value constructor would you add? Add it
  2. Is a default constructor beneficial (think about why or why not?). If you believe it will help your work, add it.
  3. Are there any other constructors that might be helpful, perhaps bringing in the data in a different form? If so add them.

Finally

  1. Are there any class constants or methods that we want to offer as part of this class? Are there class constants that could be helpful but should remain private? If so add these with any associated documentation.

Turning in your work.

Turn in your work using the method preferred by your instructor. Be prepared to talk about the decisions that you made in class on Tuesday.