1 package org.apache.ojb.tutorial1; 2 3 17 18 import org.apache.ojb.broker.PersistenceBroker; 19 import org.apache.ojb.broker.PersistenceBrokerFactory; 20 import org.apache.ojb.broker.util.ui.AsciiSplash; 21 22 import java.io.BufferedReader ; 23 import java.io.InputStreamReader ; 24 import java.util.Vector ; 25 26 29 public class Application 30 { 31 32 private Vector useCases; 33 34 private PersistenceBroker broker; 35 36 39 public Application() 40 { 41 broker = null; 42 try 43 { 44 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 45 } 46 catch (Throwable t) 47 { 48 t.printStackTrace(); 49 } 50 51 useCases = new Vector (); 52 useCases.add(new UCListAllProducts(broker)); 53 useCases.add(new UCEnterNewProduct(broker)); 54 useCases.add(new UCEditProduct(broker)); 55 useCases.add(new UCDeleteProduct(broker)); 56 useCases.add(new UCQuitApplication(broker)); 57 } 58 59 62 public void displayUseCases() 63 { 64 System.out.println(); 65 for (int idx = 0; idx < useCases.size(); idx++) 66 { 67 System.out.println("[" + idx + "] " + ((UseCase)useCases.get(idx)).getDescription()); 68 } 69 } 70 71 76 public static void main(String [] args) 77 { 78 Application app = new Application(); 79 80 app.run(); 81 } 82 83 88 private String readLine() 89 { 90 try 91 { 92 BufferedReader rin = new BufferedReader (new InputStreamReader (System.in)); 93 94 return rin.readLine(); 95 } 96 catch (Exception e) 97 { 98 return ""; 99 } 100 } 101 102 105 public void run() 106 { 107 System.out.println(AsciiSplash.getSplashArt()); 108 System.out.println("Welcome to the OJB PB tutorial application"); 109 System.out.println(); 110 111 while (true) 113 { 114 try 115 { 116 UseCase uc = selectUseCase(); 118 119 uc.apply(); 120 } 121 catch (Throwable t) 122 { 123 broker.close(); 124 System.out.println(t.getMessage()); 125 } 126 } 127 } 128 129 132 public UseCase selectUseCase() 133 { 134 displayUseCases(); 135 System.out.println("type in number to select a use case"); 136 137 String in = readLine(); 138 int idx = Integer.parseInt(in); 139 140 return (UseCase)useCases.get(idx); 141 } 142 143 144 } 145 | Popular Tags |