1 package org.apache.ojb.broker.prevayler.demo; 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 29 public class Application 30 { 31 private Vector useCases; 32 private PersistenceBroker broker; 33 36 public Application() 37 { 38 broker = null; 39 try 40 { 41 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 42 } 43 catch (Throwable t) 44 { 45 t.printStackTrace(); 46 } 47 useCases = new Vector (); 48 useCases.add(new UCListAllProducts(broker)); 49 useCases.add(new UCEnterNewProduct(broker)); 50 useCases.add(new UCEditProduct(broker)); 51 useCases.add(new UCDeleteProduct(broker)); 52 useCases.add(new UCQuitApplication(broker)); 53 } 54 57 public void displayUseCases() 58 { 59 System.out.println(); 60 for (int i = 0; i < useCases.size(); i++) 61 { 62 System.out.println("[" + i + "] " + ((UseCase) useCases.get(i)).getDescription()); 63 } 64 } 65 70 public static void main(String [] args) 71 { 72 Application app = new Application(); 73 app.run(); 74 } 75 78 private String readLine() 79 { 80 try 81 { 82 BufferedReader rin = new BufferedReader (new InputStreamReader (System.in)); 83 return rin.readLine(); 84 } 85 catch (Exception e) 86 { 87 return ""; 88 } 89 } 90 93 public void run() 94 { 95 System.out.println(AsciiSplash.getSplashArt()); 96 System.out.println("Welcome to the OJB PB tutorial application"); 97 System.out.println(); 98 while (true) 100 { 101 try 102 { 103 UseCase uc = selectUseCase(); 105 uc.apply(); 106 } 107 catch (Throwable t) 108 { 109 broker.close(); 110 System.out.println(t.getMessage()); 111 } 112 } 113 } 114 117 public UseCase selectUseCase() 118 { 119 displayUseCases(); 120 System.out.println("type in number to select a use case"); 121 String in = readLine(); 122 int index = Integer.parseInt(in); 123 return (UseCase) useCases.get(index); 124 } 125 126 127 } 128 | Popular Tags |