1 27 28 29 package cmporderclient; 30 31 import java.rmi.RemoteException ; 32 import java.text.MessageFormat ; 33 import java.util.Collection ; 34 import java.util.Iterator ; 35 import javax.naming.*; 36 import javax.rmi.PortableRemoteObject ; 37 import request.*; 38 39 40 public class Main { 41 private static OrderRequest orderRequest; 42 private static MessageFormat mf = 43 new MessageFormat (": {0, number, $#,##0.##}"); 44 45 public static void main(String [] args) { 46 try { 47 Context initial = new InitialContext(); 48 49 Object objref = initial.lookup("ejb/RequestBean"); 50 RequestRemoteHome home = 51 (RequestRemoteHome) PortableRemoteObject.narrow(objref, 52 RequestRemoteHome.class); 53 54 RequestRemote request = home.create(); 55 56 createData(request); 57 printData(request); 58 } catch (Exception ex) { 59 System.err.println("Caught an exception:"); 60 ex.printStackTrace(); 61 } 62 } 63 64 private static void createData(RequestRemote request) { 65 try { 66 request.createPart(new PartRequest("1234-5678-01", 1, "ABC PART", 67 new java.util.Date (), "PARTQWERTYUIOPASXDCFVGBHNJMKL", null)); 68 request.createPart(new PartRequest("9876-4321-02", 2, "DEF PART", 69 new java.util.Date (), "PARTQWERTYUIOPASXDCFVGBHNJMKL", null)); 70 request.createPart(new PartRequest("5456-6789-03", 3, "GHI PART", 71 new java.util.Date (), "PARTQWERTYUIOPASXDCFVGBHNJMKL", null)); 72 request.createPart(new PartRequest("ABCD-XYZW-FF", 5, "XYZ PART", 73 new java.util.Date (), "PARTQWERTYUIOPASXDCFVGBHNJMKL", null)); 74 request.createPart(new PartRequest("SDFG-ERTY-BN", 7, "BOM PART", 75 new java.util.Date (), "PARTQWERTYUIOPASXDCFVGBHNJMKL", null)); 76 77 request.addPartToBillOfMaterial(new BomRequest("SDFG-ERTY-BN", 7, 78 "1234-5678-01", 1)); 79 request.addPartToBillOfMaterial(new BomRequest("SDFG-ERTY-BN", 7, 80 "9876-4321-02", 2)); 81 request.addPartToBillOfMaterial(new BomRequest("SDFG-ERTY-BN", 7, 82 "5456-6789-03", 3)); 83 request.addPartToBillOfMaterial(new BomRequest("SDFG-ERTY-BN", 7, 84 "ABCD-XYZW-FF", 5)); 85 86 request.createVendor(new VendorRequest(100, "WidgetCorp", 87 "111 Main St., Anytown, KY 99999", "Mr. Jones", 88 "888-777-9999")); 89 request.createVendor(new VendorRequest(200, "Gadget, Inc.", 90 "123 State St., Sometown, MI 88888", "Mrs. Smith", 91 "866-345-6789")); 92 93 request.createVendorPart(new VendorPartRequest("1234-5678-01", 1, 94 "PART1", 100.00, 100)); 95 request.createVendorPart(new VendorPartRequest("9876-4321-02", 2, 96 "PART2", 10.44, 200)); 97 request.createVendorPart(new VendorPartRequest("5456-6789-03", 3, 98 "PART3", 76.23, 200)); 99 request.createVendorPart(new VendorPartRequest("ABCD-XYZW-FF", 5, 100 "PART4", 55.19, 100)); 101 request.createVendorPart(new VendorPartRequest("SDFG-ERTY-BN", 7, 102 "PART5", 345.87, 100)); 103 104 Integer orderId = new Integer (1111); 105 request.createOrder(new OrderRequest(orderId, 'N', 10, 106 "333 New Court, NewCity, CA 90000")); 107 request.addLineItem(new LineItemRequest(orderId, "1234-5678-01", 1, 108 3)); 109 request.addLineItem(new LineItemRequest(orderId, "9876-4321-02", 2, 110 5)); 111 request.addLineItem(new LineItemRequest(orderId, "ABCD-XYZW-FF", 5, 112 7)); 113 114 orderId = new Integer (4312); 115 request.createOrder(new OrderRequest(orderId, 'N', 0, 116 "333 New Court, NewCity, CA 90000")); 117 request.addLineItem(new LineItemRequest(orderId, "SDFG-ERTY-BN", 7, 118 1)); 119 request.addLineItem(new LineItemRequest(orderId, "ABCD-XYZW-FF", 5, 120 3)); 121 request.addLineItem(new LineItemRequest(orderId, "1234-5678-01", 1, 122 15)); 123 } catch (Exception ex) { 124 System.err.println("Caught an exception:"); 125 ex.printStackTrace(); 126 } 127 } 128 129 private static void printData(RequestRemote request) { 130 try { 131 BomRequest bomRequest = new BomRequest("SDFG-ERTY-BN", 7, null, 0); 132 double price = request.getBillOfMaterialPrice(bomRequest); 133 System.out.println("Cost of Bill of Material for PN " + 134 bomRequest.bomPartNumber + " Rev: " + bomRequest.bomRevision + 135 mf.format(new Object [] { new Double (price) })); 136 137 printCostOfOrders(request); 138 139 System.out.println("\nAdding 5% discount"); 140 request.adjustOrderDiscount(5); 141 printCostOfOrders(request); 142 143 System.out.println("\nRemoving 7% discount"); 144 request.adjustOrderDiscount(-7); 145 printCostOfOrders(request); 146 147 java.lang.Double price0 = request.getAvgPrice(); 148 149 if (price0 == null) { 150 System.out.println("\nNo parts found"); 151 } else { 152 System.out.println("\nAverage price of all parts" + 153 mf.format(new Object [] { price0 })); 154 } 155 156 VendorRequest vendorRequest = 157 new VendorRequest(100, null, null, null, null); 158 price0 = request.getTotalPricePerVendor(vendorRequest); 159 160 if (price0 == null) { 161 System.out.println("\nNo parts found for Vendor " + 162 vendorRequest.vendorId); 163 } else { 164 System.out.println("\nTotal price of parts for Vendor " + 165 vendorRequest.vendorId + "" + 166 mf.format(new Object [] { price0 })); 167 } 168 169 System.out.println("\nOrdered list of vendors for order 1111"); 170 System.out.println(request.reportVendorsByOrder(new Integer (1111))); 171 172 System.out.println("Counting all line items"); 173 174 int count = request.countAllItems(); 175 System.out.println("Found " + count + " line items"); 176 177 System.out.println("\nRemoving Order"); 178 request.removeOrder(new Integer (4312)); 179 count = request.countAllItems(); 180 System.out.println("Found " + count + " line items"); 181 182 Collection names = request.locateVendorsByPartialName("I"); 183 System.out.println("\nFound " + names.size() + 184 " out of 2 vendors with 'I' in the name:"); 185 186 for (Iterator it = names.iterator(); it.hasNext();) { 187 System.out.println(it.next()); 188 } 189 } catch (Exception ex) { 190 System.err.println("Caught an exception:"); 191 ex.printStackTrace(); 192 } 193 } 194 195 private static void printCostOfOrders(RequestRemote request) 196 throws RemoteException { 197 Integer orderId = new Integer (1111); 198 double price = request.getOrderPrice(orderId); 199 System.out.println("Cost of Order " + orderId + 200 mf.format(new Object [] { new Double (price) })); 201 202 orderId = new Integer (4312); 203 price = request.getOrderPrice(orderId); 204 System.out.println("Cost of Order " + orderId + 205 mf.format(new Object [] { new Double (price) })); 206 } 207 208 } 209 210
| Popular Tags
|