Advertisement
Guest User

Java

a guest
May 10th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.08 KB | None | 0 0
  1. import java.sql.*;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.time.LocalDate;
  5. import java.time.ZoneOffset;
  6. //import java.util.Date;
  7. import java.time.format.DateTimeFormatter;
  8. import java.util.Random;
  9.  
  10. public class Main {
  11.  
  12.     private Connection connection;
  13.  
  14.     private Connection getConnection() {
  15.         if (connection == null) {
  16.             try {
  17.                 String PASSWORD = "olap";
  18.                 String URL = "jdbc:postgresql://localhost:5432/shop";
  19.                 String LOGIN = "olap";
  20.                 String DRIVER = "org.postgresql.Driver";
  21.  
  22.                 Class.forName(DRIVER);
  23.  
  24.                 connection = DriverManager.getConnection(URL, LOGIN, PASSWORD);
  25.             } catch (ClassNotFoundException | SQLException e) {
  26.                 e.printStackTrace();
  27.             }
  28.         }
  29.         return connection;
  30.     }
  31.  
  32.     private int getRandomCustomerId() {
  33.         return new Random().nextInt(10) + 1;
  34.     }
  35.  
  36.     private int getRandomEmployeeId() {
  37.         return new Random().nextInt(10) + 1;
  38.     }
  39.  
  40.     private int getRandomCommodityId() {
  41.         return new Random().nextInt(31) + 1;
  42.     }
  43.  
  44.     private int insertInvoice(LocalDate date) {
  45.         try {
  46.             String query = "INSERT INTO invoices(employee_id, customer_id, date) VALUES (?, ?, ?)";
  47.             PreparedStatement statement = getConnection().prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
  48.  
  49.             statement.setInt(1, getRandomEmployeeId());
  50.             statement.setInt(2, getRandomCustomerId());
  51.             statement.setDate(3, Date.valueOf(date));
  52.  
  53.             statement.executeUpdate();
  54.  
  55.             ResultSet rs = statement.getGeneratedKeys();
  56.             if (rs.next()) {
  57.                 return rs.getInt(1);
  58.             }
  59.         } catch (SQLException e) {
  60.             e.printStackTrace();
  61.         }
  62.  
  63.         return -1;
  64.     }
  65.  
  66.     private void addItem(int invoiceId) {
  67.         double priceFluctuation = 0.1;
  68.         int maxNumberOfItems = 5;
  69.  
  70.         try {
  71.             int commodityId = getRandomCommodityId();
  72.  
  73.             String selectQuery = "SELECT name, price FROM commodity WHERE id=?";
  74.             PreparedStatement selectStatement = getConnection().prepareStatement(selectQuery);
  75.  
  76.             selectStatement.setInt(1, commodityId);
  77.  
  78.             ResultSet rs = selectStatement.executeQuery();
  79.             if (rs.next()) {
  80.                 System.out.println("\tAdding item " + rs.getString("name"));
  81.  
  82.                 int finalPrice = (int) (rs.getInt("price") * (1 + (Math.random() - 0.5) * 2 * priceFluctuation));
  83.  
  84.                 String insertQuery = "INSERT INTO sold_item(invoice_id, commodity_id, quantity, price) VALUES (?, ?, ?, ?)";
  85.                 PreparedStatement insertStatement = getConnection().prepareStatement(insertQuery);
  86.  
  87.                 insertStatement.setInt(1, invoiceId);
  88.                 insertStatement.setInt(2, commodityId);
  89.                 insertStatement.setInt(3, new Random().nextInt(maxNumberOfItems) + 1);
  90.                 insertStatement.setInt(4, finalPrice);
  91.  
  92.                 insertStatement.execute();
  93.             }
  94.  
  95.         } catch (SQLException e) {
  96.             System.err.println("Duplicate item in invoice " + invoiceId);
  97.         }
  98.     }
  99.  
  100.     private void generateSales() {
  101.         int salesNumber = 1000;
  102.         int maxNumberOfItems = 5;
  103.  
  104.         LocalDate startDate = LocalDate.of(2010, 1, 1);
  105.         double incFail = 0.5;
  106.  
  107.         for (int index = 0; index < salesNumber; index++) {
  108.             if (Math.random() < incFail) {
  109.                 startDate = startDate.plusDays(1);
  110.                 index--;
  111.             } else {
  112.                 System.out.println("New invoice at " + DateTimeFormatter.ofPattern("dd.MM.yyyy").format(startDate));
  113.                 int invoiceId = insertInvoice(startDate);
  114.                 int itemsCount = new Random().nextInt(maxNumberOfItems) + 1;
  115.  
  116.                 for (int it = 0; it < itemsCount; it++) {
  117.                     addItem(invoiceId);
  118.                 }
  119.             }
  120.         }
  121.     }
  122.  
  123.     private void generateItems() {
  124.         int shelfCount = 20;
  125.         int maxQuantity = 10;
  126.         int operationsCount = 50;
  127.  
  128.  
  129.         for (int i = 0; i < operationsCount; i++) {
  130.             try {
  131.                 String sql = "INSERT INTO items(commodity_id, shelf_number, quantity) VALUES (?, ?, ?)";
  132.                 PreparedStatement statement = getConnection().prepareStatement(sql);
  133.  
  134.                 statement.setInt(1, getRandomCommodityId());
  135.                 statement.setInt(2, new Random().nextInt(shelfCount) + 1);
  136.                 statement.setInt(3, new Random().nextInt(maxQuantity) + 1);
  137.  
  138.                 statement.execute();
  139.                 System.out.println("Added");
  140.             } catch (SQLException e) {
  141.                 System.err.println("Duplicate key");
  142.                 operationsCount--;
  143.             }
  144.         }
  145.  
  146.     }
  147.  
  148.     private void generate() {
  149.         generateItems();
  150.         generateSales();
  151.     }
  152.  
  153.     public static void main(String[] args) {
  154.         new Main().generate();
  155.     }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement