Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ClothingItemShop;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- public class ClothingOrder {
- static ArrayList<ClothingOrder> orders = new ArrayList<ClothingOrder>();
- ArrayList<ClothingItemShop> items = new ArrayList<ClothingItemShop>();
- public static void main(String[] args) {
- load_data();
- for (ClothingOrder order : orders) {
- System.out.println("Тhe total price for this order is: " + order.calculateTotalPrice());
- }
- }
- private int id;
- private String customer_name;
- public ClothingOrder(int id_param, String name_param) {
- id = id_param;
- customer_name = name_param;
- createOrderObjects();
- }
- private static void load_data() {
- Connection connection = null;
- Statement statement = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClothingItem", "root", "123456");
- statement = connection.createStatement();
- ResultSet result = statement.executeQuery(" SELECT * FROM orders ");
- while (result.next()) {
- int id = result.getInt("id");
- String customer_name = result.getString("customer_name");
- System.out.println("Id Order = " + id);
- System.out.println("Customer Name = " + customer_name);
- orders.add(new ClothingOrder(id, customer_name));
- }
- result.close();
- statement.close();
- connection.close();
- } catch (SQLException ex) {
- System.out.println("No successful connection");
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- }
- catch (ClassNotFoundException x_not_found) {
- System.out.println("Class not found");
- }
- }
- private int createString() {
- int x;
- for (x = 0; x < 40; x++) {
- System.out.print("~");
- }
- System.out.println("");
- return x;
- }
- private void createOrderObjects() {
- Connection connection = null;
- Statement statement = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClothingItem", "root", "123456");
- statement = connection.createStatement();
- ResultSet result = statement.executeQuery(
- " SELECT clothingitem.*, orderitems.item_count FROM clothingitem JOIN orderitems on clothingitem.id = orderitems.item_id ");
- while (result.next()) {
- int id = result.getInt("id");
- String type = result.getString("type");
- float price = result.getFloat("price");
- float weight = result.getFloat("weight");
- String size = result.getString("size");
- String color = result.getString("color");
- String colorPattern = result.getString("color_pattern");
- String fabric = result.getString("fabric");
- int item_count = result.getInt("item_count");
- System.out.println("Item count = " + item_count);
- System.out.println("ID = " + id);
- System.out.println("Type = " + type);
- System.out.println("Price = " + price);
- System.out.println("Weight = " + weight);
- System.out.println("Size = " + size);
- System.out.println("Color = " + color);
- System.out.println("Color Pattern = " + colorPattern);
- System.out.println("Fabric = " + fabric);
- createString();
- for (int i = 1; i <= item_count; i++) {
- if (type.equals("Hat")) {
- Hat new_hat = new Hat(price, weight, size, color, colorPattern, fabric);
- items.add(new_hat);
- } else if (type.equals("Socks")) {
- Socks new_socks = new Socks(price, weight, size, color, colorPattern, fabric);
- items.add(new_socks);
- }
- if (type.equals("Dress")) {
- EveningDress new_eveningDress = new EveningDress(price, weight, size, color, colorPattern,
- fabric);
- items.add(new_eveningDress);
- }
- if (type.equals("Tie")) {
- Tie new_tie = new Tie(price, weight, size, color, colorPattern, fabric);
- items.add(new_tie);
- }
- if (type.equals("Suite")) {
- Suite new_suite = new Suite(price, weight, size, color, colorPattern, fabric);
- items.add(new_suite);
- } else {
- System.out.println("Sorry, there is no such item");
- }
- }
- result.close();
- statement.close();
- connection.close();
- }
- }
- catch (SQLException ex) {
- System.out.println("No successful connection");
- System.out.println("SQLState: " + ex.getSQLState());
- }
- catch (ClassNotFoundException x_not_found) {
- System.out.println("Class not found");
- }
- }
- private float calculateTotalPrice() {
- float total_price = 0;
- float total_weight = 0;
- float container_price = 0;
- float i;
- for (ClothingItemShop item : items) {
- total_price += item.getItemPrice();
- total_weight += item.getWeight();
- container_price += item.getContainerPrice();
- }
- if (total_weight >= 1000) {
- total_price += 5;
- }
- i = total_price + container_price;
- createString();
- return i;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement