Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.time.LocalDate;
- import java.time.ZoneOffset;
- //import java.util.Date;
- import java.time.format.DateTimeFormatter;
- import java.util.Random;
- public class Main {
- private Connection connection;
- private Connection getConnection() {
- if (connection == null) {
- try {
- String PASSWORD = "olap";
- String URL = "jdbc:postgresql://localhost:5432/shop";
- String LOGIN = "olap";
- String DRIVER = "org.postgresql.Driver";
- Class.forName(DRIVER);
- connection = DriverManager.getConnection(URL, LOGIN, PASSWORD);
- } catch (ClassNotFoundException | SQLException e) {
- e.printStackTrace();
- }
- }
- return connection;
- }
- private int getRandomCustomerId() {
- return new Random().nextInt(10) + 1;
- }
- private int getRandomEmployeeId() {
- return new Random().nextInt(10) + 1;
- }
- private int getRandomCommodityId() {
- return new Random().nextInt(31) + 1;
- }
- private int insertInvoice(LocalDate date) {
- try {
- String query = "INSERT INTO invoices(employee_id, customer_id, date) VALUES (?, ?, ?)";
- PreparedStatement statement = getConnection().prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
- statement.setInt(1, getRandomEmployeeId());
- statement.setInt(2, getRandomCustomerId());
- statement.setDate(3, Date.valueOf(date));
- statement.executeUpdate();
- ResultSet rs = statement.getGeneratedKeys();
- if (rs.next()) {
- return rs.getInt(1);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return -1;
- }
- private void addItem(int invoiceId) {
- double priceFluctuation = 0.1;
- int maxNumberOfItems = 5;
- try {
- int commodityId = getRandomCommodityId();
- String selectQuery = "SELECT name, price FROM commodity WHERE id=?";
- PreparedStatement selectStatement = getConnection().prepareStatement(selectQuery);
- selectStatement.setInt(1, commodityId);
- ResultSet rs = selectStatement.executeQuery();
- if (rs.next()) {
- System.out.println("\tAdding item " + rs.getString("name"));
- int finalPrice = (int) (rs.getInt("price") * (1 + (Math.random() - 0.5) * 2 * priceFluctuation));
- String insertQuery = "INSERT INTO sold_item(invoice_id, commodity_id, quantity, price) VALUES (?, ?, ?, ?)";
- PreparedStatement insertStatement = getConnection().prepareStatement(insertQuery);
- insertStatement.setInt(1, invoiceId);
- insertStatement.setInt(2, commodityId);
- insertStatement.setInt(3, new Random().nextInt(maxNumberOfItems) + 1);
- insertStatement.setInt(4, finalPrice);
- insertStatement.execute();
- }
- } catch (SQLException e) {
- System.err.println("Duplicate item in invoice " + invoiceId);
- }
- }
- private void generateSales() {
- int salesNumber = 1000;
- int maxNumberOfItems = 5;
- LocalDate startDate = LocalDate.of(2010, 1, 1);
- double incFail = 0.5;
- for (int index = 0; index < salesNumber; index++) {
- if (Math.random() < incFail) {
- startDate = startDate.plusDays(1);
- index--;
- } else {
- System.out.println("New invoice at " + DateTimeFormatter.ofPattern("dd.MM.yyyy").format(startDate));
- int invoiceId = insertInvoice(startDate);
- int itemsCount = new Random().nextInt(maxNumberOfItems) + 1;
- for (int it = 0; it < itemsCount; it++) {
- addItem(invoiceId);
- }
- }
- }
- }
- private void generateItems() {
- int shelfCount = 20;
- int maxQuantity = 10;
- int operationsCount = 50;
- for (int i = 0; i < operationsCount; i++) {
- try {
- String sql = "INSERT INTO items(commodity_id, shelf_number, quantity) VALUES (?, ?, ?)";
- PreparedStatement statement = getConnection().prepareStatement(sql);
- statement.setInt(1, getRandomCommodityId());
- statement.setInt(2, new Random().nextInt(shelfCount) + 1);
- statement.setInt(3, new Random().nextInt(maxQuantity) + 1);
- statement.execute();
- System.out.println("Added");
- } catch (SQLException e) {
- System.err.println("Duplicate key");
- operationsCount--;
- }
- }
- }
- private void generate() {
- generateItems();
- generateSales();
- }
- public static void main(String[] args) {
- new Main().generate();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement