Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Scanner;
- import Clases.*;
- public class main {
- private static final String FIXER_CUSTOMERS = "Customers.txt";
- private static final String FITXER_PRODUCTS = "Products.txt";
- private static final String FITXER_ORDERS = "Orders.txt";
- private static final String FITXER_ORDER_DETAILS = "OrderDetails.txt";
- private static final int CAMPS_PRODUCTS = 9;
- private static final int CAMPS_CUSTOMERS = 13;
- private static final int CAMPS_ORDERS = 7;
- private static final int CAMPS_ORDERDETAILS = 5;
- static AccesMySQL accesBD;
- static String regularExpression = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
- public static void main(String[] args) {
- boolean error = true;
- boolean escriure = true;
- while(error)
- {
- try
- {
- accesBD = new AccesMySQL("cotxes","root","");
- error = false;
- }
- catch (Exception er)
- {
- System.out.println(er.getMessage());
- }
- }
- try
- {
- //Part2(escriure);
- //Part3(escriure);
- //Part4(escriure);
- Part5(escriure);
- }
- catch (Exception err)
- {
- System.out.println(err.getMessage());
- }
- }
- private static void Part2(boolean escriure) throws IOException {
- boolean exitRotund=false;
- String linia;
- String[] camps;
- Customer actual;
- FileInputStream fis = new FileInputStream(FIXER_CUSTOMERS);
- BufferedReader brEntrada = new BufferedReader(new InputStreamReader(fis,"UTF-16"));
- if (escriure) ProcessantInfo(FIXER_CUSTOMERS);
- while((linia = brEntrada.readLine())!=null)
- {
- exitRotund=true;
- try
- {
- camps = LlegirCamps(linia);
- if (camps.length<CAMPS_CUSTOMERS)
- camps = AfegirCamps(camps,linia);
- actual = new Customer(
- Integer.parseInt(camps[0]), camps[1],
- camps[2],camps[3],camps[4], camps[5],
- camps[6],camps[7],camps[8],camps[9],camps[10],
- (camps[11].equals("NULL")?-1:Integer.parseInt(camps[11])),
- (camps[12].equals("NULL")?-1:Double.parseDouble(camps[12])));
- if (!accesBD.InsertCustomer(actual))
- {
- exitRotund=false;
- if (escriure)ErrorInsertInfo(String.valueOf(actual.getNumber()));
- }
- }
- catch (Exception err)
- {
- exitRotund=false;
- System.out.println(err.getMessage());
- }
- }
- brEntrada.close();
- fis.close();
- if (escriure)FinalitzarInfo(exitRotund);
- }
- private static void Part3(boolean escriure) throws IOException {
- boolean exitRotund=false;
- String linia;
- String[] camps;
- Product actual;
- FileInputStream fis = new FileInputStream(FITXER_PRODUCTS);
- BufferedReader brEntrada = new BufferedReader(new InputStreamReader(fis,"UTF-16"));
- if (escriure)ProcessantInfo(FITXER_PRODUCTS);
- while((linia = brEntrada.readLine())!=null)
- {
- exitRotund=true;
- try
- {
- camps = LlegirCamps(linia);
- if (camps.length<CAMPS_PRODUCTS)
- camps = AfegirCamps(camps,linia);
- actual = new Product(camps[0], camps[1], Integer.parseInt(camps[6]),
- Double.parseDouble(camps[7]),Double.parseDouble(camps[8]));
- if (!accesBD.InsertModel(actual))
- {
- exitRotund=false;
- ErrorInsertInfo(actual.getProductCode());
- }
- }
- catch (Exception err)
- {
- exitRotund=false;
- System.out.println(err.getMessage());
- }
- }
- brEntrada.close();
- fis.close();
- if(escriure)FinalitzarInfo(exitRotund);
- }
- private static void Part4(boolean escriure) throws IOException {
- boolean exitRotund=false;
- String liniaOrder, liniaOrderDetails;
- String[] camps;
- String[] campsOD;
- Order orderActual=null;
- OrderDetail odActual=null;
- LineOrder loActual=null;
- ArrayList<LineOrder> detallLiniesComanda;
- FileInputStream fisOrders = new FileInputStream(FITXER_ORDERS);
- FileInputStream fisOrderDetails = new FileInputStream(FITXER_ORDER_DETAILS);
- BufferedReader brOrders = new BufferedReader(new InputStreamReader(fisOrders,"UTF-16"));
- BufferedReader brOrderDetails = new BufferedReader(new InputStreamReader(fisOrderDetails,"UTF-16"));
- if (escriure)ProcessantInfo(FITXER_ORDERS+" i "+FITXER_ORDER_DETAILS);
- liniaOrderDetails = brOrderDetails.readLine();
- loActual=CrearLineOrder(liniaOrderDetails);
- while((liniaOrder = brOrders.readLine())!=null)
- {
- exitRotund=true;
- try
- {
- //Llegir Order
- camps = LlegirCamps(liniaOrder);
- if (camps.length<CAMPS_ORDERS)
- camps = AfegirCamps(camps,(liniaOrder = brOrders.readLine()));
- orderActual = new Order(Integer.parseInt(camps[0]),
- camps[1],camps[2],camps[3],camps[4],
- camps[5],
- Integer.parseInt(camps[6]));
- //Llegir OrderDetails
- detallLiniesComanda = new ArrayList();
- while(loActual != null && loActual.getOrderNumber() <= orderActual.getNumber())
- {
- //Afegir si coincideix
- if (loActual.getOrderNumber() == orderActual.getNumber())
- detallLiniesComanda.add(loActual);
- //Llegir Seguent
- liniaOrderDetails = brOrderDetails.readLine();
- loActual=CrearLineOrder(liniaOrderDetails);
- }
- odActual = new OrderDetail(orderActual.getNumber(),detallLiniesComanda);
- if (!accesBD.InsertComanda(orderActual, odActual))
- {
- exitRotund=false;
- if (escriure)
- ErrorInsertInfo(String.valueOf(orderActual.getNumber()));
- }
- }
- catch (Exception err)
- {
- exitRotund=false;
- System.out.println(err.getMessage());
- }
- }
- brOrderDetails.close();
- brOrders.close();
- fisOrderDetails.close();
- fisOrders.close();
- FinalitzarInfo(exitRotund);
- }
- private static void Part5(boolean escriure) throws IOException, SQLException {
- Scanner scanner = new Scanner(System.in);
- double importTotal;
- int numComanda = -1;
- Order ordre;
- OrderDetail detallOrdre;
- //while(numComanda != 0)
- //{
- System.out.println("Entra el codi de comanda(0 per sortir): ");
- numComanda = Integer.parseInt(scanner.nextLine());
- ordre = accesBD.GetOrder(numComanda);
- System.out.println(ordre.toString());
- detallOrdre = accesBD.GetOrderDetail(numComanda);
- System.out.println(detallOrdre.toString());
- importTotal = accesBD.GetTotalOrder(numComanda);
- System.out.println("Total order: " + importTotal);
- //}
- }
- private static LineOrder CrearLineOrder(String linia) {
- //Retorna null si linia null
- String[] camps;
- LineOrder lo = null;
- if (linia != null)
- {
- camps = LlegirCamps(linia);
- lo = new LineOrder(camps[1],
- Integer.parseInt(camps[2]),
- Double.parseDouble(camps[3]),
- Integer.parseInt(camps[4]),
- Integer.parseInt(camps[0]));
- }
- return lo;
- }
- //METODES PRIVATS
- private static String[] LlegirCamps(String linia)
- {
- String[] camps=linia.replace("'", "´").split(regularExpression);
- for(int i=0;i<camps.length;i++)
- camps[i] = camps[i].replace("\"", "");
- return camps;
- }
- private static String[] AfegirCamps(String[] camps, String linia) {
- String[] afegir = linia.split(regularExpression);
- String[] nouCamps = new String[camps.length+afegir.length];
- int i=0;
- for (i=0;i<camps.length;i++)
- nouCamps[i]=camps[i];
- for(int j=0;j<afegir.length;j++,i++)
- {
- nouCamps[i] = afegir[j].replace("\"", "");
- }
- return nouCamps;
- }
- private static void ErrorInsertInfo(String id) {
- System.out.println(id+", no s'ha pogut insertar");
- }
- private static void ProcessantInfo(String fitxer) {
- System.out.println("Processant fitxer "+fitxer+"...");
- }
- private static void FinalitzarInfo(boolean exitRotund) {
- System.out.print("S'han insertat");
- if(exitRotund)
- System.out.println(" tots els registres");
- else
- System.out.println(" els registres, amb algún inconvenient.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement