refactoring
play

Refactoring Lecture 7 January 02, 2009 O b j e c t O r i e n t e - PowerPoint PPT Presentation

366 Refactoring Lecture 7 January 02, 2009 O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g Refactoring: problem statement O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g Add a htmlStatment method


  1. 366 Refactoring Lecture 7 January 02, 2009 O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g

  2. Refactoring: problem statement O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g Add a htmlStatment method which returns a customer statement string containing html tags. ...and there will be some changes to the way movies are classified ...affecting frequent renter points and charging. January 02, 2009 377

  3. Refactoring: step 2 public String statement() { double totalAmount = 0; O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g int frequentRenterPoints = 0; Enumeration rentals = _rental.elements(); String result = ÒRental Record for Ò + getName() + Ò\nÓ; while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental) rentals.nextElement(); thisAmount = amountFor(each); frequentRenterPoints ++; if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE)&& each.getDaysRented() > 1) frequentRenterPoints++; result += Ò\tÓ + each.getMovie().getTitle()+ Ò\tÓ + String.valueOf(thisAmount) + Ò\nÓ; totalAmount += thisAmount; } result += ÒAmount owed is Ò+Sting.valueOf(totalAmount) + Ò\nÓ; result += ÒYou earned Ò+Sting.valueOf(frequentRenterPoints) + Òfrequent renter points\nÓ; January 02, 2009 380 return result;

  4. Refactoring: step 7 class Customer ... public String statement() { O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rental.elements(); String result = ÒRental Record for Ò + getName() + Ò\nÓ; while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental) rentals.nextElement(); thisAmount = amountFor(each); // add frequent renter points frequentRenterPoints ++; // add bonus for a two day new release rental if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE)&& each.getDaysRented() > 1) frequentRenterPoints++; //show figures for this rental result += Ò\tÓ + each.getMovie().getTitle()+ Ò\tÓ + String.valueOf(thisAmount) + Ò\nÓ; totalAmount += thisAmount; É January 02, 2009 389

  5. Refactoring: step 7 class Customer ... public String statement() { O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rental.elements(); String result = ÒRental Record for Ò + getName() + Ò\nÓ; while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental) rentals.nextElement(); thisAmount = each.getCharge(); // add frequent renter points frequentRenterPoints ++; // add bonus for a two day new release rental if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE)&& each.getDaysRented() > 1) frequentRenterPoints++; //show figures for this rental result += Ò\tÓ + each.getMovie().getTitle()+ Ò\tÓ + String.valueOf(thisAmount) + Ò\nÓ; totalAmount += thisAmount; January 02, 2009 390

  6. Refactoring: step 14 abstract class Price { abstract int getPriceCode(); O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g } class ChildrenPrice extends Price { int getPriceCode(){ return MOVIE.CHILDREN; } } class NewReleasePrice extends Price { int getPriceCode(){ return MOVIE.NEW_RELEASE; } } class RegularPrice extends Price { int getPriceCode(){ return MOVIE.REGULAR; } } January 02, 2009 420

  7. Bad Smells in Code Comments (stench 2) O b j e c t O r i e n t e d S o f t w a r e E n g i n e e r i n g Comments are often a sign of unclear code... consider refactoring January 02, 2009 447

Recommend


More recommend