Wednesday, April 18, 2012

Refactoring Example Class

Refactoring

Refactoring refers to the process of rearranging code to make it more elegant and efficient. These rearrangements do not affect the function of the code, but make it more readable and maintainable

Refactoring can involve any of the following activities

1. Renaming variables
2. Encapsulating Fields in sets and gets
3. Extracting methods (taking repeating code putting it in its own method)
4. Extracting an interface
5. Extracting classes (breaking code into new separate classes)
6. Extracting super classes<

Here is a picture of the class diagram for the class



here is the code for a class we will practice refactoring in class



public class Tip {
/*
 * Assume the programmer wanted to expand the tip class
 * to handle tips for things other than a meal 
 * such as taxis and valet parking
 * For taxis the suggested tip is a percent of the
 * meter cost plus a dollar per bag
 * For the valet parking it is a little looser
 * based on the expense or quality of the venue and the
 * expensiveness of your car
 * The programmer has adapted the one class to handle the 
 * different types of tips
 */
 
 private double mealAmount;
 private double tipPercent;
 private double taxPercent;
 private double cabFare;
 private int bags;
 private int eventRating; //1 to 5 five most expensive
 private int vehicleValue; //(1 to 5);
 private final double CABPERCENT =.07;
 //*************************************************
 //Constructors for the various uses
 //**************************************************
 //empty constructor
 public Tip(){
  
 }
 //Meal tip constructor
 public Tip(double meal, double tipPerc, double tax){
  setMealAmount(meal);
  setTipPercent(tipPerc);
  setTaxPercent(tax);
 }
 
 //constructor for taxis
 public Tip(double fare, int bags){
  setCabFare(fare);
  setBags(bags);
 }
 
 //constructor for Valet
 public Tip(int event, int vehicle){
  setEventRating(event);
  setVehicleValue(vehicle);
 }
 
 //*********************************************
 //gets and sets
 //*********************************************
 public double getMealAmount() {
  return mealAmount;
 }
 public void setMealAmount(double mealAmount) {
  this.mealAmount = mealAmount;
 }
 public double getTipPercent() {
  return tipPercent;
 }
 public void setTipPercent(double tipPercent) {
  if(tipPercent >=1){
   tipPercent /= 100;
  }
  this.tipPercent = tipPercent;
 }
 public double getTaxPercent() {
  
  return taxPercent;
 }
 public void setTaxPercent(double taxPercent) {
  if(taxPercent >=1){
   taxPercent /= 100;
  }
  this.taxPercent = taxPercent;
 }
 public double getCabFare() {
  return cabFare;
 }
 public void setCabFare(double cabFare) {
  this.cabFare = cabFare;
 }
 public int getBags() {
  return bags;
 }
 public void setBags(int bags) {
  this.bags = bags;
 }
 private int getEventRating() {
  return eventRating;
 }
 private void setEventRating(int eventRating) {
  if (eventRating < 1 )
   eventRating = 1;
  else if (eventRating > 5)
   eventRating=5;
  
  this.eventRating = eventRating;
 }
 public int getVehicleValue() {
  if (vehicleValue< 1 )
   vehicleValue = 1;
  else if (vehicleValue > 5)
   vehicleValue=5;
  return vehicleValue;
 }
 public void setVehicleValue(int vehicleValue) {
  this.vehicleValue = vehicleValue;
 }
 
 //**************************************
 //public methods
 //*************************************
 //calculate tip percent
 
 public double calculateMealTip(){
  return getMealAmount() * getTipPercent();
 }

 //calculate Taxi Tip
 public double calculateTaxiTip(){
  return getCabFare() * CABPERCENT + getBags();
 }
 
 public double calculateValetTip(){
  int weight=getVehicleValue() + getEventRating();
  double tipAmount=0;
  switch (weight){
  case 1: 
  case 2:
  case 3:
   tipAmount=5;
   break;
  case 4:
  case 5:
  case 6:
   tipAmount=10;
   break;
  case 7:
  case 8:
   tipAmount=15;
  case 9:
  case 10:
   tipAmount=20;
   break;
  default:
   tipAmount=10;
   break;
  }
  
  return tipAmount;
    
 
 }
 
 public double calculateTax(){
  
  return getMealAmount()*getTaxPercent();
 }
 
 public double CalculateMealTotal(){
  return getMealAmount() + calculateTax() + calculateMealTip();
 }
 
 public double CalculateTaxiTotal(){
  return getCabFare()+calculateTaxiTip();
 }
 
 public double CalculateValetTotal(){
  return calculateValetTip();
 }
}

No comments:

Post a Comment