diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..bd9a639 --- /dev/null +++ b/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + org.java-elementary-0804 + Space-Game + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + mysql + mysql-connector-java + 8.0.20 + + + junit + junit + 4.12 + test + + + org.hamcrest + hamcrest-all + 1.3 + test + + + diff --git a/src/main/java/com/game/model/dao/BaysDAO.java b/src/main/java/com/game/model/dao/BaysDAO.java new file mode 100644 index 0000000..c6f4280 --- /dev/null +++ b/src/main/java/com/game/model/dao/BaysDAO.java @@ -0,0 +1,68 @@ +package com.game.model.dao; + +import com.game.model.entity.BayEntity; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +public class BaysDAO { + /* + Метод должен делать запрос в базу данных и возвращать соответстующий список Entity + */ + + public List getBayList(String jdbcDriver, String dbUrl, + String userName, String userPass) { + final String DB_SELECT_STATEMENT = + "SELECT Bays.resource_id, Resources.name, Resources.description, " + + "Resources.group_id, ResourcesGroups.name, ResourcesGroups.description\n" + + "FROM Bays LEFT JOIN Resources ON Bays.resource_id=Resources.id\n" + + "LEFT JOIN ResourcesGroups ON Resources.group_id = ResourcesGroups.id;"; + + List bayEntityList = new ArrayList(); + downloadJdbcDriver(jdbcDriver); + Connection connection = null; + try { + connection = DriverManager.getConnection(dbUrl, userName, userPass); + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(DB_SELECT_STATEMENT); + prepareEntityProperties(bayEntityList, resultSet); + resultSet.close(); + statement.close(); + connection.close(); + } catch (SQLException e) { + System.err.println(e.getMessage()); + } finally { + try { + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + System.out.println("Connection close failed."); + System.err.println(e.getMessage()); + } + } + return bayEntityList; + } + + private void downloadJdbcDriver(String jdbcDriver) { + try { + Class.forName(jdbcDriver); + } catch (ClassNotFoundException e) { + System.err.println(e.getMessage()); + } + } + + private void prepareEntityProperties(List list, ResultSet resultSet) throws SQLException { + while (resultSet.next()) { + BayEntity bayEntity = new BayEntity(); + bayEntity.setResourceId(resultSet.getInt("resource_id")); + bayEntity.setResourceName(resultSet.getString("Resources.name")); + bayEntity.setResourceDescription(resultSet.getString("Resources.description")); + bayEntity.setGroupId(resultSet.getInt("group_id")); + bayEntity.setGroupName(resultSet.getString("ResourcesGroups.name")); + bayEntity.setGroupDescription(resultSet.getString("ResourcesGroups.description")); + list.add(bayEntity); + } + } +} diff --git a/src/main/java/com/game/model/entity/BayEntity.java b/src/main/java/com/game/model/entity/BayEntity.java new file mode 100644 index 0000000..53c44ab --- /dev/null +++ b/src/main/java/com/game/model/entity/BayEntity.java @@ -0,0 +1,90 @@ +package com.game.model.entity; + +import java.util.Objects; + +public class BayEntity { + private int groupId; + private String groupName; + private String groupDescription; + private int resourceId; + private String resourceName; + private String resourceDescription; + + public int getGroupId() { + return this.groupId; + } + + public void setGroupId(int groupId) { + this.groupId = groupId; + } + + public String getGroupName() { + return this.groupName; + } + + public void setGroupName(String groupName) { + this.groupName = groupName; + } + + public String getGroupDescription() { + return this.groupDescription; + } + + public void setGroupDescription(String groupDescription) { + this.groupDescription = groupDescription; + } + + public int getResourceId() { + return this.resourceId; + } + + public void setResourceId(int resourceId) { + this.resourceId = resourceId; + } + + public String getResourceName() { + return this.resourceName; + } + + public void setResourceName(String resourceName) { + this.resourceName = resourceName; + } + + public String getResourceDescription() { + return this.resourceDescription; + } + + public void setResourceDescription(String resourceDescription) { + this.resourceDescription = resourceDescription; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + BayEntity bayEntity = (BayEntity) o; + return groupId == bayEntity.groupId && + resourceId == bayEntity.resourceId && + groupName.equals(bayEntity.groupName) && + groupDescription.equals(bayEntity.groupDescription) && + resourceName.equals(bayEntity.resourceName) && + resourceDescription.equals(bayEntity.resourceDescription); + } + + @Override + public int hashCode() { + return Objects.hash(groupId, groupName, groupDescription, resourceId, resourceName, resourceDescription); + } + + @Override + public String toString() { + return "BayEntity{" + + "groupId=" + groupId + + ", groupName='" + groupName + '\'' + + ", groupDescription='" + groupDescription + '\'' + + ", resourceId=" + resourceId + + ", resourceName='" + resourceName + '\'' + + ", resourceDescription='" + resourceDescription + '\'' + + '}'; + } +} diff --git a/src/main/resources/db/2_bays.sql b/src/main/resources/db/2_bays.sql new file mode 100644 index 0000000..aec3335 --- /dev/null +++ b/src/main/resources/db/2_bays.sql @@ -0,0 +1,104 @@ +/* +Создать таблицу Bays с полями: + id int + resource_id int +*/ +CREATE TABLE Bays +( + id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, + resource_id INT NOT NULL +); +/* +Если не создана то создать таблицу ResourcesGroups с полями: + id int + name varchar + description varchar +*/ +CREATE TABLE ResourcesGroups +( + id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, + name varchar(50) NOT NULL UNIQUE, + description varchar(120) NOT NULL + +); +/* +Если не создана то создать таблицу Resources с полями: + id int + name varchar + description varchar + group_id int + permanent boolean +*/ +CREATE TABLE Resources +( + id INT PRIMARY KEY NOT NULL, + name varchar(50) NOT NULL UNIQUE, + description varchar(120) NOT NULL, + group_id INT NOT NULL, + permanent BOOLEAN +); + +ALTER TABLE Bays + ADD FOREIGN KEY (resource_id) REFERENCES Resources(id); + +ALTER TABLE Resources + ADD FOREIGN KEY (group_id) REFERENCES ResourcesGroups(id); + +/* +Добавить в этот файл SQL скрипты для заполнения таблиц данными из GoogleDocs. +*/ +INSERT INTO Bays + (resource_id) +VALUES (11), + (12), + (13), + (14), + (15), + (16), + (17), + (18), + (19), + (20), + (21), + (22), + (23), + (24), + (25), + (26), + (27); + + +INSERT INTO ResourcesGroups + (name, description) +VALUES ('Ships', 'Has max slots'), + ('Bays', 'Control lifecycle'), + ('Resources', 'For move and fight'), + ('Augmentations', 'Prokachka'), + ('Weapons', 'Oruzhie'), + ('People', 'Ludi'), + ('Drones', 'Drony'), + ('Fighters', 'Boyci'), + ('Upgrades', 'Usovershenstvovaniya'), + ('Achievements', 'Dostizheniya'), + ('Notification', 'Uvedomleniya'), + ('Dialogs', 'Razgovory'); + +INSERT INTO Resources +(id, name, description, group_id, permanent) +VALUES (11, 'Shields', 'Schity', 2, true), + (12, 'Engines', 'Dvigately', 2, true), + (13, 'Oxygen', 'Kislorod', 2, true), + (14, 'Weapon Control', 'Upravlenie oruzhiem', 2, true), + (15, 'Drone Control', 'Upravlenie dronami', 2, true), + (16, 'Medbay', 'Sredniy otsek', 2, true), + (17, 'Crew Teleporter', 'Teleport comandy', 2, true), + (18, 'Cloaking', 'Maskirovka', 2, true), + (19, 'Artillery Beam', 'Artileristskiy luch', 2, true), + (20, 'Artillery Flak', 'Artileristskaya zenitka', 2, true), + (21, 'Clone Bay', 'Otsek klonirovaniya', 2, true), + (22, 'Mind Control', 'Control soznaniya', 2, true), + (23, 'Hacking', 'Vzlom', 2, true), + (24, 'Piloting', 'Pilotirovanie', 2, true), + (25, 'Sensors', 'Sensory', 2, true), + (26, 'Door System', 'Dvernaya systema', 2, true), + (27, 'Backup Batter', 'Rezervnaya batareya', 2, true); diff --git a/src/test/java/com/game/model/dao/BaysDAOTest.java b/src/test/java/com/game/model/dao/BaysDAOTest.java new file mode 100644 index 0000000..59cafe8 --- /dev/null +++ b/src/test/java/com/game/model/dao/BaysDAOTest.java @@ -0,0 +1,24 @@ +package com.game.model.dao; + +import org.junit.Test; + +import java.util.Collection; + +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +public class BaysDAOTest { + private final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; + private final String DATABASE_URL = "jdbc:mysql://localhost:3306/tempProj?serverTimezone=UTC"; + private final String USER = "root"; + private final String PASSWORD = "root"; + private BaysDAO baysDAO = new BaysDAO(); + + @Test + public void printBayList() { + Collection collection = baysDAO.getBayList(JDBC_DRIVER, DATABASE_URL, USER, PASSWORD); + for (int i = 0; i < collection.size(); i++) { + System.out.println(collection.toArray()[i].toString()); + } + } +}