Skip to content

Commit

Permalink
Merge pull request rubfan#1 from chevyden/master
Browse files Browse the repository at this point in the history
Empty Maven project has been created.
  • Loading branch information
rubfan authored Jul 12, 2020
2 parents ae09650 + 5554510 commit 45ca442
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.java-elementary-0804</groupId>
<artifactId>Space-Game</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
68 changes: 68 additions & 0 deletions src/main/java/com/game/model/dao/BaysDAO.java
Original file line number Diff line number Diff line change
@@ -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<BayEntity> 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<BayEntity> bayEntityList = new ArrayList<BayEntity>();
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<BayEntity> 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);
}
}
}
90 changes: 90 additions & 0 deletions src/main/java/com/game/model/entity/BayEntity.java
Original file line number Diff line number Diff line change
@@ -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 + '\'' +
'}';
}
}
104 changes: 104 additions & 0 deletions src/main/resources/db/2_bays.sql
Original file line number Diff line number Diff line change
@@ -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);
24 changes: 24 additions & 0 deletions src/test/java/com/game/model/dao/BaysDAOTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit 45ca442

Please sign in to comment.