Thursday, March 29, 2012

ITC162 Assignment Three

Refractoring

Below are three classes for a banking application. There is a lot of repeated code and other redundancies.

The goal of the assignment is to restructure the classes to eliminate as much redundancy as possible. This can involve encapsulating elements into a new class. It can involve making super classes or interfaces.


To Do

Restructure the code in the classes so that they conform to good OOP principles.

Create a Class diagram to show the new structure of the classes and their relationships to each other


To turn in (what I am looking for)

The code for the classes 10 pts

The UML diagram 10 points


Code for Classes

Checking.java

import java.util.*;
public class Checking {
 /*
  * Class for refactoring:
  * This class tracks transactions in a 
  * checking account. At this point they 
  * are not stored to file, but just stored
  * in an ArrayList
  * 
  */
 
 //private fields
 private String customerName;
 private String accountNumber;
 private String customerAddress;
 private String customerPhone;
 private double balance;
 private final double OVERDRAFTFEE=25;
 ArrayList transactions;
 
 
 //constructors
 public Checking(){
  balance = 0;
  transactions = new ArrayList<Double>();
  
 }
 
 public Checking(double initialBalance){
  balance=initialBalance;
  transactions.add(initialBalance);
 }
 
 //assessors and mutators
 public void setCustomerName(String customerName) {
  this.customerName = customerName;
 }
 public String getCustomerName() {
  return customerName;
 }
 public void setAccountNumber(String accountNumber) {
  this.accountNumber = accountNumber;
 }
 public String getAccountNumber() {
  return accountNumber;
 }
 private void setCustomerAddress(String customerAddress) {
  this.customerAddress = customerAddress;
 }
 private String getCustomerAddress() {
  return customerAddress;
 }
 public void setCustomerPhone(String customerPhone) {
  this.customerPhone = customerPhone;
 }
 public String getCustomerPhone() {
  return customerPhone;
 }
 //balance has only a public get
 public double getBalance(){
  return balance;
 }
 
 //public methods
 public void credit(double amount){
  balance += amount;
  transactions.add(amount);
 }
 
 public void debit(double amount){
  balance -= amount;
  transactions.add(amount*-1);
  if (balance < 0)
  { 
   balance -= OVERDRAFTFEE;
   transactions.add(OVERDRAFTFEE * -1);
  }
 }
 
 public ArrayList<Double> getTransactions(){
  return transactions;
 }
 

}


Savings.java

import java.util.*;

public class Savings {
 
 /*
  * Class for refractoring
  * This class handles the savings account
  * the interest calculations are 
  * extremely simplistic
  */
 
 //private fields
 private String customerName;
 private String accountNumber;
 private String customerAddress;
 private String customerPhone;
 private double balance;
 ArrayList transactions;
 private final double IRATE = .03;
 
 //constructors
 public Savings(){
  balance=0;
 }
 
 public Savings(double initialBal){
  balance=initialBal;
 }
 
 public void setCustomerName(String customerName) {
  this.customerName = customerName;
 }
 public String getCustomerName() {
  return customerName;
 }
 public void setAccountNumber(String accountNumber) {
  this.accountNumber = accountNumber;
 }
 public String getAccountNumber() {
  return accountNumber;
 }
 private void setCustomerAddress(String customerAddress) {
  this.customerAddress = customerAddress;
 }
 private String getCustomerAddress() {
  return customerAddress;
 }
 public void setCustomerPhone(String customerPhone) {
  this.customerPhone = customerPhone;
 }
 public String getCustomerPhone() {
  return customerPhone;
 }
 //balance has only a public get
 public double getBalance(){
  return balance;
 }
 
 public void credit(double amount){
  balance += amount;
  transactions.add(amount);
 }
 
 public void debit(double amount){
  balance -= amount;
  transactions.add(amount*-1);
  
 }
 
 public ArrayList<Double> getTransactions(){
  return transactions;
 }
 
 public double CalculateInterest()
 {
  //much too simple but will do 
  //for now
  return balance * IRATE;
  
 
 }

}


CreditCard.java

import java.util.ArrayList;


public class CreditCard {
 /*
  * Class for Refractoring
  * This class handles credit card
  * transactions
  * again many of the calculations
  * are very simple
  */
 
 private String customerName;
 private String accountNumber;
 private String customerAddress;
 private String customerPhone;
 private double creditLimit;
 private double balance;
 ArrayList<Double> transactions;
 private final double IRATE = .22;
 
 public CreditCard(){
  creditLimit=1000;
 }
 
 public CreditCard(double creditLimit){
  this.creditLimit=creditLimit;
 }
 
 public void setCustomerName(String customerName) {
  this.customerName = customerName;
 }
 public String getCustomerName() {
  return customerName;
 }
 public void setAccountNumber(String accountNumber) {
  this.accountNumber = accountNumber;
 }
 public String getAccountNumber() {
  return accountNumber;
 }
 private void setCustomerAddress(String customerAddress) {
  this.customerAddress = customerAddress;
 }
 private String getCustomerAddress() {
  return customerAddress;
 }
 public void setCustomerPhone(String customerPhone) {
  this.customerPhone = customerPhone;
 }
 public String getCustomerPhone() {
  return customerPhone;
 }
 
 //credit limit and balance read only
 public double getCreditLimit(){
  return creditLimit;
 }

 public double getBalance(){
  return balance;
 }
 
 public void credit(double amount){
  //simplistic way to calculate interest
  //doing it on a daily scale
  balance += amount - (amount *(IRATE/365));
  transactions.add(amount);
 }
 
 public void debit(double amount){
  balance -= amount + (amount *(IRATE/365));
  transactions.add(amount*-1);
  
 }
 
 public ArrayList<Double> getTransactions(){
  return transactions;
 }
 
 public double CalculateInterest()
 {
  //much too simple but will do 
  //for now
  return balance * IRATE;
  
 
 }

}

No comments:

Post a Comment