Паттерн: Команда (Command)
Исходник: SalesReports.java, язык: java [code #489, hits: 6408]
автор: this [добавлен: 04.10.2007]
  1. package command;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7.  
  8. import org.jdom.Document;
  9. import org.jdom.Element;
  10. import org.jdom.input.SAXBuilder;
  11.  
  12. public class SalesReports implements Module {
  13. private String xmlReportDataPath;
  14.  
  15. public SalesReports(String xmlReportDataPath) {
  16. super();
  17. this.xmlReportDataPath = xmlReportDataPath;
  18. }
  19.  
  20. public List<String> GetMenu() {
  21. List<String> res = new ArrayList<String>(2);
  22. res.add("Вывести данные по всем продажам");
  23. return res;
  24. }
  25.  
  26. public void Run() {
  27. // Инициируем sax-парсер
  28. SAXBuilder builder = new SAXBuilder();
  29. Document doc = null;
  30. try {
  31. doc = builder.build(new File(xmlReportDataPath));
  32. } catch (Exception e) {
  33. System.out.println("ошибка!");
  34. }
  35.  
  36. Element root = doc.getRootElement();
  37.  
  38. // Выбираем все списки продаж
  39. List sales = root.getChildren("sale");
  40. Iterator i = sales.iterator();
  41. while (i.hasNext()) {
  42. Element sale = (Element) i.next();
  43.  
  44. // Выбираем все товары в каждой продаже
  45. List products = sale.getChildren("product");
  46. // ...
  47. }
  48. }
  49.  
  50. }
Сущность ConcreteCommand

Реализация модуля отчетов.
Тестировалось на: java 1.5.0_04

+добавить реализацию