Advertisement
BorislavaYordanova

Clothing Order

May 1st, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.97 KB | None | 0 0
  1. package ClothingItemShop;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9.  
  10. public class ClothingOrder {
  11.     static ArrayList<ClothingOrder> orders = new ArrayList<ClothingOrder>();
  12.     ArrayList<ClothingItemShop> items = new ArrayList<ClothingItemShop>();
  13.  
  14.     public static void main(String[] args) {
  15.         load_data();
  16.  
  17.         for (ClothingOrder order : orders) {
  18.  
  19.             System.out.println("Тhe total price for this order is: " + order.calculateTotalPrice());
  20.         }
  21.     }
  22.  
  23.     private int id;
  24.     private String customer_name;
  25.  
  26.     public ClothingOrder(int id_param, String name_param) {
  27.         id = id_param;
  28.         customer_name = name_param;
  29.         createOrderObjects();
  30.     }
  31.  
  32.     private static void load_data() {
  33.         Connection connection = null;
  34.         Statement statement = null;
  35.  
  36.         try {
  37.             Class.forName("com.mysql.jdbc.Driver");
  38.             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClothingItem", "root", "123456");
  39.             statement = connection.createStatement();
  40.             ResultSet result = statement.executeQuery(" SELECT * FROM orders  ");
  41.  
  42.             while (result.next()) {
  43.                 int id = result.getInt("id");
  44.                 String customer_name = result.getString("customer_name");
  45.                 System.out.println("Id Order = " + id);
  46.                 System.out.println("Customer Name = " + customer_name);
  47.  
  48.                 orders.add(new ClothingOrder(id, customer_name));
  49.             }
  50.             result.close();
  51.             statement.close();
  52.             connection.close();
  53.         } catch (SQLException ex) {
  54.             System.out.println("No successful connection");
  55.             System.out.println("SQLException: " + ex.getMessage());
  56.             System.out.println("SQLState: " + ex.getSQLState());
  57.         }
  58.  
  59.         catch (ClassNotFoundException x_not_found) {
  60.             System.out.println("Class not found");
  61.         }
  62.     }
  63.  
  64.     private int createString() {
  65.         int x;
  66.         for (x = 0; x < 40; x++) {
  67.             System.out.print("~");
  68.         }
  69.         System.out.println("");
  70.         return x;
  71.     }
  72.  
  73.     private void createOrderObjects() {
  74.         Connection connection = null;
  75.         Statement statement = null;
  76.  
  77.         try {
  78.             Class.forName("com.mysql.jdbc.Driver");
  79.             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClothingItem", "root", "123456");
  80.             statement = connection.createStatement();
  81.             ResultSet result = statement.executeQuery(
  82.                     " SELECT clothingitem.*, orderitems.item_count FROM clothingitem JOIN orderitems on clothingitem.id = orderitems.item_id ");
  83.  
  84.             while (result.next()) {
  85.                 int id = result.getInt("id");
  86.                 String type = result.getString("type");
  87.                 float price = result.getFloat("price");
  88.                 float weight = result.getFloat("weight");
  89.                 String size = result.getString("size");
  90.                 String color = result.getString("color");
  91.                 String colorPattern = result.getString("color_pattern");
  92.                 String fabric = result.getString("fabric");
  93.                 int item_count = result.getInt("item_count");
  94.                 System.out.println("Item count = " + item_count);
  95.                 System.out.println("ID = " + id);
  96.                 System.out.println("Type = " + type);
  97.                 System.out.println("Price = " + price);
  98.                 System.out.println("Weight = " + weight);
  99.                 System.out.println("Size = " + size);
  100.                 System.out.println("Color = " + color);
  101.                 System.out.println("Color Pattern = " + colorPattern);
  102.                 System.out.println("Fabric = " + fabric);
  103.                 createString();
  104.  
  105.                 for (int i = 1; i <= item_count; i++) {
  106.                     if (type.equals("Hat")) {
  107.                         Hat new_hat = new Hat(price, weight, size, color, colorPattern, fabric);
  108.                         items.add(new_hat);
  109.                     } else if (type.equals("Socks")) {
  110.                         Socks new_socks = new Socks(price, weight, size, color, colorPattern, fabric);
  111.                         items.add(new_socks);
  112.                     }
  113.                     if (type.equals("Dress")) {
  114.                         EveningDress new_eveningDress = new EveningDress(price, weight, size, color, colorPattern,
  115.                                 fabric);
  116.                         items.add(new_eveningDress);
  117.                     }
  118.                     if (type.equals("Tie")) {
  119.                         Tie new_tie = new Tie(price, weight, size, color, colorPattern, fabric);
  120.                         items.add(new_tie);
  121.                     }
  122.                     if (type.equals("Suite")) {
  123.                         Suite new_suite = new Suite(price, weight, size, color, colorPattern, fabric);
  124.                         items.add(new_suite);
  125.                     } else {
  126.                         System.out.println("Sorry, there is no such item");
  127.                     }
  128.                 }
  129.                 result.close();
  130.                 statement.close();
  131.                 connection.close();
  132.             }
  133.         }
  134.  
  135.         catch (SQLException ex) {
  136.             System.out.println("No successful connection");
  137.             System.out.println("SQLState: " + ex.getSQLState());
  138.         }
  139.  
  140.         catch (ClassNotFoundException x_not_found) {
  141.             System.out.println("Class not found");
  142.         }
  143.     }
  144.  
  145.     private float calculateTotalPrice() {
  146.         float total_price = 0;
  147.         float total_weight = 0;
  148.         float container_price = 0;
  149.         float i;
  150.  
  151.         for (ClothingItemShop item : items) {
  152.             total_price += item.getItemPrice();
  153.             total_weight += item.getWeight();
  154.             container_price += item.getContainerPrice();
  155.         }
  156.  
  157.         if (total_weight >= 1000) {
  158.             total_price += 5;
  159.         }
  160.  
  161.         i = total_price + container_price;
  162.         createString();
  163.         return i;
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement