Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.98 KB | None | 0 0
  1. package test;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.UnsupportedEncodingException;
  10. import java.sql.SQLException;
  11. import java.util.ArrayList;
  12. import java.util.Scanner;
  13.  
  14. import Clases.*;
  15.  
  16. public class main {
  17.  
  18. private static final String FIXER_CUSTOMERS = "Customers.txt";
  19. private static final String FITXER_PRODUCTS = "Products.txt";
  20. private static final String FITXER_ORDERS = "Orders.txt";
  21. private static final String FITXER_ORDER_DETAILS = "OrderDetails.txt";
  22. private static final int CAMPS_PRODUCTS = 9;
  23. private static final int CAMPS_CUSTOMERS = 13;
  24. private static final int CAMPS_ORDERS = 7;
  25. private static final int CAMPS_ORDERDETAILS = 5;
  26. static AccesMySQL accesBD;
  27. static String regularExpression = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
  28.  
  29. public static void main(String[] args) {
  30.  
  31. boolean error = true;
  32. boolean escriure = true;
  33. while(error)
  34. {
  35. try
  36. {
  37. accesBD = new AccesMySQL("cotxes","root","");
  38. error = false;
  39. }
  40. catch (Exception er)
  41. {
  42. System.out.println(er.getMessage());
  43. }
  44. }
  45. try
  46. {
  47. //Part2(escriure);
  48. //Part3(escriure);
  49. //Part4(escriure);
  50. Part5(escriure);
  51. }
  52. catch (Exception err)
  53. {
  54. System.out.println(err.getMessage());
  55. }
  56.  
  57.  
  58. }
  59.  
  60. private static void Part2(boolean escriure) throws IOException {
  61. boolean exitRotund=false;
  62. String linia;
  63. String[] camps;
  64. Customer actual;
  65. FileInputStream fis = new FileInputStream(FIXER_CUSTOMERS);
  66. BufferedReader brEntrada = new BufferedReader(new InputStreamReader(fis,"UTF-16"));
  67.  
  68. if (escriure) ProcessantInfo(FIXER_CUSTOMERS);
  69. while((linia = brEntrada.readLine())!=null)
  70. {
  71. exitRotund=true;
  72. try
  73. {
  74. camps = LlegirCamps(linia);
  75. if (camps.length<CAMPS_CUSTOMERS)
  76. camps = AfegirCamps(camps,linia);
  77.  
  78. actual = new Customer(
  79. Integer.parseInt(camps[0]), camps[1],
  80. camps[2],camps[3],camps[4], camps[5],
  81. camps[6],camps[7],camps[8],camps[9],camps[10],
  82. (camps[11].equals("NULL")?-1:Integer.parseInt(camps[11])),
  83. (camps[12].equals("NULL")?-1:Double.parseDouble(camps[12])));
  84.  
  85. if (!accesBD.InsertCustomer(actual))
  86. {
  87. exitRotund=false;
  88. if (escriure)ErrorInsertInfo(String.valueOf(actual.getNumber()));
  89. }
  90. }
  91. catch (Exception err)
  92. {
  93. exitRotund=false;
  94. System.out.println(err.getMessage());
  95. }
  96. }
  97.  
  98. brEntrada.close();
  99. fis.close();
  100.  
  101. if (escriure)FinalitzarInfo(exitRotund);
  102. }
  103. private static void Part3(boolean escriure) throws IOException {
  104. boolean exitRotund=false;
  105. String linia;
  106. String[] camps;
  107. Product actual;
  108. FileInputStream fis = new FileInputStream(FITXER_PRODUCTS);
  109. BufferedReader brEntrada = new BufferedReader(new InputStreamReader(fis,"UTF-16"));
  110. if (escriure)ProcessantInfo(FITXER_PRODUCTS);
  111. while((linia = brEntrada.readLine())!=null)
  112. {
  113. exitRotund=true;
  114. try
  115. {
  116. camps = LlegirCamps(linia);
  117. if (camps.length<CAMPS_PRODUCTS)
  118. camps = AfegirCamps(camps,linia);
  119.  
  120. actual = new Product(camps[0], camps[1], Integer.parseInt(camps[6]),
  121. Double.parseDouble(camps[7]),Double.parseDouble(camps[8]));
  122.  
  123. if (!accesBD.InsertModel(actual))
  124. {
  125. exitRotund=false;
  126. ErrorInsertInfo(actual.getProductCode());
  127. }
  128. }
  129. catch (Exception err)
  130. {
  131. exitRotund=false;
  132. System.out.println(err.getMessage());
  133. }
  134. }
  135. brEntrada.close();
  136. fis.close();
  137.  
  138. if(escriure)FinalitzarInfo(exitRotund);
  139. }
  140.  
  141. private static void Part4(boolean escriure) throws IOException {
  142. boolean exitRotund=false;
  143. String liniaOrder, liniaOrderDetails;
  144. String[] camps;
  145. String[] campsOD;
  146. Order orderActual=null;
  147. OrderDetail odActual=null;
  148. LineOrder loActual=null;
  149. ArrayList<LineOrder> detallLiniesComanda;
  150. FileInputStream fisOrders = new FileInputStream(FITXER_ORDERS);
  151. FileInputStream fisOrderDetails = new FileInputStream(FITXER_ORDER_DETAILS);
  152. BufferedReader brOrders = new BufferedReader(new InputStreamReader(fisOrders,"UTF-16"));
  153. BufferedReader brOrderDetails = new BufferedReader(new InputStreamReader(fisOrderDetails,"UTF-16"));
  154.  
  155. if (escriure)ProcessantInfo(FITXER_ORDERS+" i "+FITXER_ORDER_DETAILS);
  156. liniaOrderDetails = brOrderDetails.readLine();
  157. loActual=CrearLineOrder(liniaOrderDetails);
  158.  
  159. while((liniaOrder = brOrders.readLine())!=null)
  160. {
  161. exitRotund=true;
  162. try
  163. {
  164.  
  165. //Llegir Order
  166. camps = LlegirCamps(liniaOrder);
  167. if (camps.length<CAMPS_ORDERS)
  168. camps = AfegirCamps(camps,(liniaOrder = brOrders.readLine()));
  169.  
  170. orderActual = new Order(Integer.parseInt(camps[0]),
  171. camps[1],camps[2],camps[3],camps[4],
  172. camps[5],
  173. Integer.parseInt(camps[6]));
  174.  
  175. //Llegir OrderDetails
  176. detallLiniesComanda = new ArrayList();
  177. while(loActual != null && loActual.getOrderNumber() <= orderActual.getNumber())
  178. {
  179. //Afegir si coincideix
  180. if (loActual.getOrderNumber() == orderActual.getNumber())
  181. detallLiniesComanda.add(loActual);
  182.  
  183. //Llegir Seguent
  184. liniaOrderDetails = brOrderDetails.readLine();
  185. loActual=CrearLineOrder(liniaOrderDetails);
  186.  
  187.  
  188. }
  189.  
  190. odActual = new OrderDetail(orderActual.getNumber(),detallLiniesComanda);
  191. if (!accesBD.InsertComanda(orderActual, odActual))
  192. {
  193. exitRotund=false;
  194. if (escriure)
  195. ErrorInsertInfo(String.valueOf(orderActual.getNumber()));
  196. }
  197. }
  198. catch (Exception err)
  199. {
  200. exitRotund=false;
  201. System.out.println(err.getMessage());
  202. }
  203. }
  204.  
  205. brOrderDetails.close();
  206. brOrders.close();
  207. fisOrderDetails.close();
  208. fisOrders.close();
  209.  
  210. FinalitzarInfo(exitRotund);
  211. }
  212.  
  213. private static void Part5(boolean escriure) throws IOException, SQLException {
  214. Scanner scanner = new Scanner(System.in);
  215. double importTotal;
  216. int numComanda = -1;
  217. Order ordre;
  218. OrderDetail detallOrdre;
  219. //while(numComanda != 0)
  220. //{
  221. System.out.println("Entra el codi de comanda(0 per sortir): ");
  222. numComanda = Integer.parseInt(scanner.nextLine());
  223. ordre = accesBD.GetOrder(numComanda);
  224. System.out.println(ordre.toString());
  225. detallOrdre = accesBD.GetOrderDetail(numComanda);
  226. System.out.println(detallOrdre.toString());
  227. importTotal = accesBD.GetTotalOrder(numComanda);
  228. System.out.println("Total order: " + importTotal);
  229. //}
  230. }
  231.  
  232. private static LineOrder CrearLineOrder(String linia) {
  233. //Retorna null si linia null
  234. String[] camps;
  235. LineOrder lo = null;
  236. if (linia != null)
  237. {
  238. camps = LlegirCamps(linia);
  239. lo = new LineOrder(camps[1],
  240. Integer.parseInt(camps[2]),
  241. Double.parseDouble(camps[3]),
  242. Integer.parseInt(camps[4]),
  243. Integer.parseInt(camps[0]));
  244. }
  245. return lo;
  246. }
  247.  
  248. //METODES PRIVATS
  249. private static String[] LlegirCamps(String linia)
  250. {
  251. String[] camps=linia.replace("'", "´").split(regularExpression);
  252. for(int i=0;i<camps.length;i++)
  253. camps[i] = camps[i].replace("\"", "");
  254.  
  255. return camps;
  256. }
  257.  
  258. private static String[] AfegirCamps(String[] camps, String linia) {
  259. String[] afegir = linia.split(regularExpression);
  260. String[] nouCamps = new String[camps.length+afegir.length];
  261. int i=0;
  262. for (i=0;i<camps.length;i++)
  263. nouCamps[i]=camps[i];
  264.  
  265. for(int j=0;j<afegir.length;j++,i++)
  266. {
  267. nouCamps[i] = afegir[j].replace("\"", "");
  268. }
  269. return nouCamps;
  270. }
  271.  
  272. private static void ErrorInsertInfo(String id) {
  273. System.out.println(id+", no s'ha pogut insertar");
  274. }
  275.  
  276. private static void ProcessantInfo(String fitxer) {
  277. System.out.println("Processant fitxer "+fitxer+"...");
  278. }
  279.  
  280. private static void FinalitzarInfo(boolean exitRotund) {
  281.  
  282. System.out.print("S'han insertat");
  283. if(exitRotund)
  284. System.out.println(" tots els registres");
  285. else
  286. System.out.println(" els registres, amb algún inconvenient.");
  287. }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement