Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- import java.util.Scanner;
- import POJOS.*;
- public class Main {
- public final static String LOGIN_NAME = "root";
- public final static String LOGIN_PASS = "";
- public final static String URL_MYSQL = "jdbc:mysql://localhost:3306/cotxes";
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- BufferedReader brProducts = new BufferedReader(new InputStreamReader(new FileInputStream("Products.txt"),"unicode"));
- BufferedReader brOrders = new BufferedReader(new InputStreamReader(new FileInputStream("Orders.txt"),"unicode"));
- BufferedReader brOrdersDetails = new BufferedReader(new InputStreamReader(new FileInputStream("OrdresDetails.txt"),"unicode"));
- BufferedReader brCustomers = new BufferedReader(new InputStreamReader(new FileInputStream("Customers.txt"),"unicode"));
- ArrayList<Order> orders = getOrders(brOrders);
- ArrayList<Product> products = getProducts(brProducts);
- ArrayList<Customer> customers = getCustomers(brCustomers);
- try
- {
- AccesMySQL bdAccess = new AccesMySQL(URL_MYSQL,LOGIN_NAME,LOGIN_PASS);
- for (Customer customer:customers)
- {
- bdAccess.InsertCustomer(customer);
- }
- for (Product product:products)
- {
- bdAccess.InsertModel(product);
- }
- int i=0;
- boolean correct = true;
- while (i<orders.size() && correct)
- {
- OrderDetail orderDetail=new OrderDetail(getLineOrders(orders.get(i),brOrdersDetails),orders.get(i).getOrderNumber());
- correct = bdAccess.InsertComanda(orders.get(i),orderDetail);
- i++;
- }
- boolean complete = false;
- Scanner sc = new Scanner(System.in);
- do {
- System.out.print("Introdueix el codi de la comanda (0 per sortir): ");
- String s = sc.next();
- if (!s.equals("0"))
- {
- Order order = bdAccess.GetOrder(Integer.parseInt(s));
- if (order!= null)
- {
- OrderDetail oDetail = bdAccess.GetOrderDetail(Integer.parseInt(s));
- double impTotal = bdAccess.GetTotalOrder(Integer.parseInt(s));
- System.out.println("\n");
- System.out.println("COMANDA");
- System.out.println("Ordre: " +order.getOrderNumber());
- System.out.println("Estat: " +order.getStatus());
- System.out.println("Data de comanda nº: " +order.getOrderDate());
- System.out.println("Data d'enviamentº: " +order.getShippedDate());
- System.out.println("Data prevista: "+ order.getRequiredDate());
- System.out.println("Número de client: " +order.getCustomerNumber());
- System.out.println("Comentaris: " +order.getComments());
- System.out.println("\n");
- for(LineOrder lOrder:oDetail.getLineOrders())
- {
- System.out.println("Informació: ");
- System.out.println("Codi del producte: " +lOrder.getProductCode());
- System.out.println("Quantitat: " +lOrder.getQuantityOrdered());
- System.out.println("€/unitat: " +lOrder.getPriceEach());
- System.out.println("Ordre nº: " +lOrder.getOrderLineNumber());
- System.out.println("\n");
- }
- System.out.println("Total: " + impTotal+"€");
- System.out.println("\n");
- }
- else
- System.out.println("Codi Incorrecte o Inexistent.");
- }
- else complete = true;
- }while (!complete);
- System.out.println("Adeu.");
- }
- catch (Exception e)
- {
- System.out.println(e.getMessage());
- }
- finally{
- brCustomers.close();
- brOrdersDetails.close();
- brOrders.close();
- brProducts.close();
- }
- }
- private static String agafarNumeros (String cadena)
- {
- String resultat = "";
- for (char caracter : cadena.toCharArray())
- {
- if (Character.isDigit(caracter))
- resultat += caracter;
- }
- return resultat;
- }
- private static String agafarCaracters (String cadena)
- {
- String resultat = "";
- for (char caracter : cadena.toCharArray())
- {
- if (caracter != 0)
- resultat += caracter;
- }
- return resultat;
- }
- private static String agafarData(String cadena)
- {
- String[] data;
- String resultat = "";
- if (cadena.equals("NULL")) resultat = cadena;
- else
- {
- data = cadena.split(" ");
- resultat = data[0] + "/" + data[1] + "/" + data[2];
- }
- return resultat;
- }
- private static ArrayList<Order> getOrders(BufferedReader brOrders)
- {
- ArrayList<Order> orders = new ArrayList<>();
- String linia;
- String[] camps;
- try
- {
- linia=brOrders.readLine();
- while(linia!=null)
- {
- camps=linia.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
- orders.add(new Order(
- camps[5],
- agafarData(camps[1]),
- agafarData(camps[2]),
- agafarData(camps[3]),
- camps[4],
- Integer.parseInt(camps[0]),
- Integer.parseInt(camps[6])));
- linia=brOrders.readLine();
- }
- }
- catch(Exception ex)
- {
- System.out.println(ex.getMessage());
- }
- return orders;
- }
- private static ArrayList<LineOrder> getLineOrders(Order o, BufferedReader brODetails)throws Exception{
- int orderCode=o.getOrderNumber();
- ArrayList<LineOrder> orders=new ArrayList<>();
- String linia;
- String[] camps;
- boolean trobat = false;
- linia=brODetails.readLine();
- try{
- while(linia!=null&&!trobat){
- camps=linia.split(",");
- trobat=Integer.parseInt(camps[0])>orderCode;
- if(orderCode==Integer.parseInt(camps[0])&&!trobat){
- orders.add(new LineOrder(
- camps[1],
- Double.parseDouble(camps[2]),
- Integer.parseInt(camps[3]),
- Integer.parseInt(camps[4])));
- }
- linia=brODetails.readLine();
- }
- }
- catch(Exception ex){
- System.out.println(ex.getMessage());
- }
- return orders;
- }
- private static ArrayList<Product> getProducts(BufferedReader brProducts) throws Exception
- {
- ArrayList<Product> products = new ArrayList<>();
- String linia;
- String segonaLinia;
- String[] camps;
- int i = 1;
- boolean endOfFile= false;
- try
- {
- linia = brProducts.readLine();
- while (linia != null)
- {
- StringBuffer sBuffer = new StringBuffer(linia);
- segonaLinia = brProducts.readLine();
- while (segonaLinia != null && !endOfFile)
- {
- if (segonaLinia != null && segonaLinia.charAt(0) != '"')
- {
- sBuffer.append(segonaLinia);
- segonaLinia = brProducts.readLine();
- }
- else endOfFile = true;
- }
- linia = sBuffer.toString();
- endOfFile = false;
- camps = linia.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
- products.add(new Product(
- agafarCaracters(camps[0]),
- agafarCaracters(camps[1]),
- Integer.parseInt(agafarCaracters(camps[6])),
- Double.parseDouble(agafarNumeros(camps[7])),
- Double.parseDouble(agafarNumeros(camps[8]))));
- linia = segonaLinia;
- }
- }
- catch (Exception ex)
- {
- System.out.println("Exception: " + ex.getMessage());
- }
- return products;
- }
- private static ArrayList<Customer> getCustomers (BufferedReader brCustomers)
- {
- ArrayList<Customer> customers = new ArrayList<>();
- Customer customer;
- String camps[];
- String linia;
- int i = 0;
- int vendes;
- try
- {
- linia = brCustomers.readLine();
- while (linia != null)
- {
- camps = linia.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
- if (agafarCaracters(camps[11]).equals("NULL"))
- vendes = 0;
- else
- vendes = Integer.parseInt(agafarNumeros(camps[11]));
- if (agafarCaracters(camps[9]).equals(""))
- camps[9] = "NULL";
- customer = new Customer(
- Integer.parseInt(agafarNumeros(camps[0])),
- camps[1],
- camps[2],
- camps[3],
- camps[4],
- camps[5],
- camps[6],
- camps[7],
- camps[8],
- camps[9],
- camps[10],
- vendes,
- Double.parseDouble(agafarNumeros(camps[12])));
- customers.add(customer);
- linia = brCustomers.readLine();
- }
- }
- catch (Exception ex)
- {
- System.out.println(ex.getMessage());
- }
- return customers;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement