Commit 3181c979 authored by Vladislav Savchenkov's avatar Vladislav Savchenkov

Finished project.

parents
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
### Static files ###
resources/configuration.json
resources/logs.txt
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="postgres@localhost" uuid="f036859d-60a1-42b5-b9bb-c14fbb3b5a02">
<driver-ref>postgresql</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
<jdbc-url>jdbc:postgresql://localhost:5432/postgres</jdbc-url>
<jdbc-additional-properties>
<property name="com.intellij.clouds.kubernetes.db.host.port" />
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
<property name="com.intellij.clouds.kubernetes.db.resource.type" value="Deployment" />
<property name="com.intellij.clouds.kubernetes.db.container.port" />
</jdbc-additional-properties>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="ClassCanBeRecord" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
<inspection_tool class="Convert2Diamond" enabled="false" level="WARNING" enabled_by_default="false" />
</profile>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="gson-2.11.0">
<CLASSES>
<root url="jar://$PROJECT_DIR$/resources/lib/gson-2.11.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="postgresql-42.7.3">
<CLASSES>
<root url="jar://$PROJECT_DIR$/resources/lib/postgresql-42.7.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/application.iml" filepath="$PROJECT_DIR$/application.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/src/core/db/repositories/CarRepository.java" dialect="GenericSQL" />
<file url="PROJECT" dialect="PostgreSQL" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="postgresql-42.7.3" level="project" />
<orderEntry type="library" name="gson-2.11.0" level="project" />
</component>
</module>
\ No newline at end of file
{
"db.url": "~",
"db.username": "~",
"db.password": "~"
}
\ No newline at end of file
import core.Application;
import core.utils.logs.Logger;
import java.sql.SQLException;
public class Main
{
public static void main(String[] args)
{
try
{
Application application = new Application();
application.run();
}
catch (SQLException exception)
{
Logger.error("DB problem: " + exception.getMessage());
System.exit(exception.getErrorCode());
}
}
}
\ No newline at end of file
package core;
import core.db.repositories.CarRepository;
import core.db.repositories.Repository;
import core.models.Car;
import java.sql.SQLException;
import java.util.List;
public class Application
{
private final Repository<Car> repository;
public Application() throws SQLException
{
this.repository = new CarRepository();
}
public void run() throws SQLException
{
List<Car> cars = repository.get();
System.out.println("\nCars from DB:");
cars.forEach(System.out::println);
Car car = repository.get(1);
System.out.println("\nCar from DB by ID(1):");
System.out.println(car);
}
}
package core.configuration;
import com.google.gson.annotations.SerializedName;
public class ConfigurationMap
{
@SerializedName("db.url")
private final String url;
@SerializedName("db.username")
private final String username;
@SerializedName("db.password")
private final String password;
public ConfigurationMap(String url, String username, String password)
{
this.url = url;
this.username = username;
this.password = password;
}
public String getUrl()
{
return url;
}
public String getUsername()
{
return username;
}
public String getPassword()
{
return password;
}
}
package core.db;
import core.configuration.ConfigurationMap;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection
{
private volatile Connection instance;
public DBConnection(ConfigurationMap configuration) throws SQLException
{
this.instance = open(
configuration.getUrl(),
configuration.getUsername(),
configuration.getPassword()
);
}
private DBConnection() {}
public Connection getInstance()
{
return instance;
}
private Connection open(String url, String username, String password) throws SQLException
{
Connection local = instance;
if (local == null)
{
synchronized (Connection.class)
{
local = instance;
if (local == null)
{
instance = local = DriverManager.getConnection(url, username, password);
}
}
}
return local;
}
public void close() throws SQLException
{
instance.close();
instance = null;
}
}
package core.db.repositories;
import core.models.Car;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class CarRepository extends Repository<Car>
{
public CarRepository() throws SQLException
{
super();
}
@Override
public Car get(int id) throws SQLException
{
Statement statement = getStatement();
String query = "SELECT * FROM db.cars WHERE id = %d;".formatted(id);
ResultSet result = statement.executeQuery(query);
result.next();
Car car = map(result);
result.close();
return car;
}
@Override
public List<Car> get() throws SQLException
{
Statement statement = getStatement();
String query = "SELECT * FROM db.cars;";
ResultSet result = statement.executeQuery(query);
List<Car> cars = maps(result);
result.close();
return cars;
}
private Car map(ResultSet result) throws SQLException
{
return new Car(
result.getInt("id"),
result.getString("name"),
result.getString("type"),
result.getString("color"),
result.getFloat("volume")
);
}
private List<Car> maps(ResultSet result) throws SQLException
{
List<Car> cars = new ArrayList<Car>();
while (result.next())
{
cars.add(map(result));
}
return cars;
}
}
package core.db.repositories;
import core.db.DBConnection;
import core.utils.io.ConfigurationReader;
import java.sql.*;
import java.util.List;
public abstract class Repository<T>
{
private final Connection connection;
public Repository() throws SQLException
{
this.connection = new DBConnection(ConfigurationReader.get()).getInstance();
}
public abstract T get(int id) throws SQLException;
public abstract List<T> get() throws SQLException;
protected Statement getStatement() throws SQLException
{
return connection.createStatement();
}
}
package core.models;
public class Car
{
private final int id;
private final String name;
private final String type;
private final String color;
private final float volume;
public Car(int id, String name, String type, String color, float volume)
{
this.id = id;
this.name = name;
this.type = type;
this.color = color;
this.volume = volume;
}
@Override
public String toString()
{
String format = "{\"id\": %d, \"name\": %s, \"type\": %s, \"color\": %s, \"volume\": %.1f}";
return String.format(format, id, name, type, color, volume);
}
}
package core.utils.io;
import com.google.gson.Gson;
import core.configuration.ConfigurationMap;
public class ConfigurationReader
{
private static final Gson gson = new Gson();
private static final IOFileReader reader = new IOFileReader();
public static ConfigurationMap get()
{
String json = reader.read("resources/configuration.json");
return gson.fromJson(json, ConfigurationMap.class);
}
}
package core.utils.io;
import core.utils.logs.Logger;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class IOFileReader
{
public String read(String path)
{
try(BufferedReader reader = new BufferedReader(new FileReader(path)))
{
String line;
StringBuilder body = new StringBuilder();
while (true)
{
line = reader.readLine();
//null означает конец файла:
if (line == null)
{
break;
}
body.append(line);
}
return body.toString();
}
catch (IOException exception)
{
Logger.error("Exception occurred while reading file " + path + " " + exception.getMessage());
}
return "Empty";
}
}
package core.utils.io;
import core.utils.logs.Logger;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class IOFileWriter
{
//Перезаписывает...
public void write(String path, String body)
{
try(BufferedWriter writer = new BufferedWriter(new FileWriter(path)))
{
writer.write(body);
writer.newLine();
}
catch (IOException exception)
{
Logger.error("Exception occurred while writing file " + path + " " + exception.getMessage());
}
}
//Дозаписывает...
public void append(String path, String body)
{
try(BufferedWriter writer = new BufferedWriter(new FileWriter(path, true)))
{
writer.append(body);
writer.newLine();
}
catch (IOException exception)
{
Logger.error("Exception occurred while writing file " + path + " " + exception.getMessage());
}
}
}
\ No newline at end of file
package core.utils.logs;
import core.utils.io.IOFileWriter;
import java.time.LocalDateTime;
public class Logger
{
private static final String PATH = "resources/logs.txt";
private static final IOFileWriter writer = new IOFileWriter();
public static void info(String message)
{
writer.append(PATH, "INFO - " + LocalDateTime.now() + " - " + message);
}
public static void error(String message)
{
writer.append(PATH, "ERROR - " + LocalDateTime.now() + " - " + message);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment