HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"The Perils of Copy-Paste Coding"
Vol. 9, Issue 4, p. 43

	



Listing 1

public class PersonnelInfo {
  private int staffCount;
  private int sickDays;
  private int vacationDays;
  private int salaryCosts;
  private int pensionContributions;
  private int insuranceContributions;
  // getter and setter methods for
  // above instance variables not
  // shown
  // ...
}

public class PersonnelInfoExporter {
 
  public void exportPersonnelInfo(
    int startYear, int endYear) {
    PlanningTool planningTool = 
      PlanningTool.openConnection();
    planningTool.createChart(
      "Personnel Statististics for Period " + 
      startYear + "-" + endYear, 6);
    List personnelData = 
      new PersonnelController().
        getPersonnelInfo(startYear, endYear);
    Iterator personnelIterator = 
      personnelData.iterator();
    List dataSeries = new ArrayList();
    int year = startYear;
    while (personnelIterator.hasNext()){
      List dataItems = new ArrayList();
      PersonnelInfo info = 
        (PersonnelInfo) personnelIterator.next();
      dataItems.add(
        new PlanningTool.DataItem(
          info.getStaffCount(), "Staff count",
          Color.BLUE));
      dataItems.add(
        new PlanningTool.DataItem(
          info.getSickDays(), "Sick Days",
          Color.CYAN));
      dataItems.add(
        new PlanningTool.DataItem(
          info.getVacationDays(), "Vacation Days",
          Color.DARK_GRAY));
      dataItems.add(
        new PlanningTool.DataItem(
          info.getInsuranceContributions(), 
          "Insurance Contributions",
         Color.GREEN));
     dataItems.add(
       new PlanningTool.DataItem(
         info.getPensionContributions(),
         "Pension Contributions",
         Color.YELLOW));
     dataItems.add(
       new PlanningTool.DataItem(
         info.getSalaryCosts(),
         "Salary Costs",
         Color.MAGENTA));
     planningTool.addDataSeries(
       dataItems, Integer.toString(year));
     year++;
   }
   planningTool.setFooter(
     "Personnel Statistics Chart");
   planningTool.closeConnection();
 }
}
 

Listing 2

public class SalesInfo {
  private int salesStaffCount;
  private int totalSalesGenerated;
  private int 
    actualAdvertisingExpenditure;
  private int advertisingBudget;
  // getter and setter methods for
  // above instance variables not
  // shown
  // ...
}

public class SalesInfoExporter {
  public void exportSalesInfo(int startYear, 
                              int endYear){
    PlanningTool planningTool = 
      PlanningTool.openConnection();
    planningTool.createChart(
      "Sales Statistics for Period " + 
      startYear + "-" + endYear, 4);
    List salesData = new SalesController().
      getSalesInfo(startYear, endYear);
    Iterator salesIterator = 
      salesData.iterator();
    List dataSeries = new ArrayList();
    int year = startYear;
    while (salesIterator.hasNext()){
      List dataItems = new ArrayList();
      SalesInfo info = 
        (SalesInfo) salesIterator.next();
      dataItems.add(
        new PlanningTool.DataItem(
          info.getSalesStaffCount(),
          "Staff count", Color.BLUE));
      dataItems.add(
        new PlanningTool.DataItem(
          info.getTotalSalesGenerated(),
          "Total Sales", Color.RED));
      dataItems.add(
        new PlanningTool.DataItem(
          info.getAdvertisingBudget(),
          "Advertising Budget", 
          Color.DARK_GRAY));
      dataItems.add(
        new PlanningTool.DataItem(
          info.getActualAdvertisingExpenditure(),
          "Advertisting Expenditure", Color.GREEN));
      planningTool.addDataSeries(
        dataItems, Integer.toString(year));
      year++;
    }
    planningTool.setFooter("Sales Statistics Chart");
    planningTool.closeConnection();    
  }
}

Listing 3

public class ExporterHelper {
  public static PlanningTool 
    initializeTool(String title, 
                   int numColumns) {
    PlanningTool planningTool = 
      PlanningTool.openConnection();
    planningTool.createChart(title,
                             numColumns);
    return planningTool;
  }

  public static void terminateTool(
    PlanningTool planningTool, 
    String footer) {
    planningTool.setFooter(footer);
    planningTool.closeConnection();
  }
}

public class SalesInfoExporter {
  public void exportSalesInfo(int startYear, 
                              int endYear){
    PlanningTool planningTool = 
      ExporterHelper.initializeTool(
      "Sales Statistics for Period " + 
      startYear + "-" + endYear, 4);
    List salesData = new SalesController().
      getSalesInfo(startYear, endYear);
    Iterator salesIterator = 
      salesData.iterator();
    List dataSeries = new ArrayList();
     int year = startYear;
    while (salesIterator.hasNext()){
      List dataItems = new ArrayList();
      SalesInfo info = 
        (SalesInfo) salesIterator.next();
      dataItems.add(
        new PlanningTool.DataItem(
          info.getSalesStaffCount(),
          "Staff count", Color.BLUE));
      dataItems.add(
        new PlanningTool.DataItem(
          info.getTotalSalesGenerated(),
          "Total Sales", Color.RED));
      dataItems.add(
        new PlanningTool.DataItem(
          info.getAdvertisingBudget(),
          "Advertising Budget", 
          Color.DARK_GRAY));
      dataItems.add(
        new PlanningTool.DataItem(
          info.getActualAdvertisingExpenditure(),
          "Advertisting Expenditure", Color.GREEN));
      planningTool.addDataSeries(
        dataItems, Integer.toString(year));
      year++;
    }
    ExporterHelper.terminateTool(
      planningTool, "Sales Statistics Chart");
  }
}

 
Listing 4

public class SalesInfo 
  implements IExportableData {
  
  private static final String TITLES[] = 
    new String[]{
      "Staff count", "Total Sales", 
      "Advertising Budget", 
      "Advertisting Expenditure"
    };
  
  private static final Color COLORS[] = 
    new Color[]{
      Color.BLUE, Color.RED, 
      Color.DARK_GRAY, Color.GREEN
    };
  
  private int salesStaffCount;
  private int totalSalesGenerated;
  private int actualAdvertisingExpenditure;
  private int advertisingBudget;
  
  // Getters and setters for instance 
  // variables not shown
  // ...

  public int getColumnCount(int column) {
    switch (column){
      case 0: 
        return getSalesStaffCount();
      case 1: 
        return getTotalSalesGenerated();
      case 2: 
        return getAdvertisingBudget();
      case 3: 
        return getActualAdvertisingExpenditure();
      default: // Should never happen
        throw new IllegalArgumentException(
          "Invalid column index " + column);
    }
  }

  public String getTitle(int column) {
    return TITLES[column];
  }
  public Color getColor(int column) {
    return COLORS[column];
  }
}

Listing 5

public abstract class AbstractExporter {
  public void exportInfo(int startYear, 
                         int endYear) {
    PlanningTool planningTool = 
      ExporterHelper.initializeTool(
        getTitle() + startYear + "-" + 
        endYear, getNumberOfColumns());
    List dataForPeriod = getDataForPeriod(
                           startYear, endYear);
    Iterator dataIterator = 
      dataForPeriod.iterator();
    List dataSeries = new ArrayList();
    int year = startYear;
    while (dataIterator.hasNext()){
      List dataItems = new ArrayList();
      IExportableData info = 
        (IExportableData) 
          dataIterator.next();
      for (int i = 0; 
           i < getNumberOfColumns(); i++){
        dataItems.add(
          new PlanningTool.DataItem(
            info.getColumnCount(i),
            info.getTitle(i),
            info.getColor(i)));
      }
      planningTool.addDataSeries(
        dataItems, 
        Integer.toString(year));
      year++;
    }
    ExporterHelper.terminateTool(
      planningTool, getFooterText());
  }

  protected abstract String getFooterText();
  protected abstract List 
    getDataForPeriod(int startYear, 
                     int endYear);
  protected abstract String getTitle();
  protected abstract int getNumberOfColumns();
}

public class SalesInfoExporter 
  extends AbstractExporter {
  protected List getDataForPeriod(
    int startYear, int endYear) {
    return new SalesController().
     getSalesInfo(startYear, endYear);
  }
  protected String getTitle() {
    return "Sales Statistics for Period ";
  }
  protected int getNumberOfColumns() {
    return 4;
  }
  protected String getFooterText() {
    return "Sales Statistics Chart";
  }
}

 
Listing 6

public class AbstractExporter {
  private IExporter exporter;
  
  public AbstractExporter(
    IExporter exporter){
    this.exporter = exporter;
  }

  public void exportInfo(int startYear, 
                         int endYear) {
    PlanningTool planningTool = 
      ExporterHelper.initializeTool(
        exporter.getTitle() + 
        startYear + "-" + 
        endYear, 
        exporter.getNumberOfColumns());
    List dataForPeriod = 
      exporter.getDataForPeriod(
                 startYear, endYear);
    Iterator dataIterator = 
      dataForPeriod.iterator();
    List dataSeries = new ArrayList();
    int year = startYear;
    while (dataIterator.hasNext()){
      List dataItems = new ArrayList();
      IExportableData info = 
        (IExportableData) 
          dataIterator.next();
      for (int i = 0; 
           i < exporter.getNumberOfColumns(); 
           i++){
        dataItems.add(
          new PlanningTool.DataItem(
            info.getColumnCount(i),
            info.getTitle(i),
            info.getColor(i)));
      }
      planningTool.addDataSeries(
        dataItems, 
        Integer.toString(year));
      year++;
    }
    
    ExporterHelper.terminateTool(
      planningTool, 
      exporter.getFooterText());
    
  }
}

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.