Skip to content

Commit

Permalink
task solved
Browse files Browse the repository at this point in the history
  • Loading branch information
banderan committed Jul 1, 2024
1 parent d960e6b commit f054197
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 71 deletions.
40 changes: 17 additions & 23 deletions src/main/java/mate/academy/StartClass.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,53 @@
package mate.academy;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import mate.academy.dao.BookDao;
import mate.academy.dao.BookDaoImpl;
import mate.academy.model.Book;
import mate.academy.service.BookService;
import mate.academy.service.BookServiceImpl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class StartClass {
private final static BookDao bookDao = new BookDaoImpl();
private final static BookService bookService = new BookServiceImpl(bookDao);
private final static int AMOUNT_OF_BOOKS = 5;
private final static Long FIRST_BOOK_ID = 1L;
private final static Long SECOND_BOOK_ID = 2L;
private final static Long THIRD_BOOK_ID = 3L;
private final static Long FOURTH_BOOK_ID = 4L;
private final static Long FIFTH_BOOK_ID = 5L;


public static void main(String[] args) {
final Long secondBookId = 2L;
final Long thirdBookId = 3L;

final BookDao bookDao = new BookDaoImpl();
final BookService bookService = new BookServiceImpl(bookDao);
List<Book> books = createBook();
Book newBook = new Book("okok", BigDecimal.valueOf(420));
Book newBook = new Book(3L,"okok", BigDecimal.valueOf(420));

bookService.create(books.get(0));//C
bookService.create(books.get(1));
bookService.create(books.get(2));
bookService.create(books.get(3));
bookService.create(books.get(4));

Optional<Book> book = bookService.readID(SECOND_BOOK_ID);//R
Optional<Book> book = bookService.readID(secondBookId);//R

boolean delete = bookService.delete(THIRD_BOOK_ID);//D
boolean delete = bookService.delete(thirdBookId);//D

Book update = bookService.update(newBook);//U

List<Book> listOfBooks = bookService.readAll();

System.out.println("We have " + book +
", we delete third book? -> " + delete +
" and we updated this book -> " + update);
System.out.println("We have " + book
+ ", we delete third book? -> " + delete
+ " and we updated this book -> " + update);
for (int i = 0; i < listOfBooks.size(); i++) {
System.out.println("Our " + i + "th book: " + listOfBooks.get(i));
}
}

private static List<Book> createBook() {
final int amountOfBooks = 5;
String title = "Java";
BigDecimal price = BigDecimal.valueOf(12.32);
List<Book> books = new ArrayList<>();
for (int i = 0; i < AMOUNT_OF_BOOKS; i++) {
for (int i = 0; i < amountOfBooks; i++) {
books.add(new Book(title + i,
price.add(BigDecimal.valueOf(i))));
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/mate/academy/dao/BookDao.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package mate.academy.dao;

import mate.academy.model.Book;
import java.util.List;
import java.util.Optional;
import mate.academy.model.Book;

public interface BookDao {
void createTable();
Book save(Book book);
Book create(Book book);

Optional<Book> findById(Long id);

List<Book> findAll();

Book update(Book book);

boolean deleteById(Long id);
}
62 changes: 32 additions & 30 deletions src/main/java/mate/academy/dao/BookDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package mate.academy.dao;

import mate.academy.lib.Dao;
import mate.academy.model.Book;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import mate.academy.lib.Dao;
import mate.academy.model.Book;
import mate.academy.model.DataProcessingException;

@Dao
public class BookDaoImpl implements BookDao {
Expand All @@ -22,13 +26,12 @@ public class BookDaoImpl implements BookDao {
private static final String PRICE = "price";
private static final String ADD_LINE = " ";
private static final String INIT_DB_SQL = "src/main/resources/init_db.sql";
private static final String TAKE_BOOK_FROM_DB = "SELECT * FROM book WHERE id = ?";
private static final String DELETE_BOOK_FROM_DB = "DELETE * FROM book WHERE id = ?";
private static final String TAKE_BOOKS_FROM_DB = "SELECT * FROM book";
private static final String SAVE_BOOK_IN_DB = "INSERT INTO book (title, price) VALUES (?, ?)";
private static final String TAKE_BOOK_FROM_DB = "SELECT * FROM books WHERE id = ?";
private static final String DELETE_BOOK_FROM_DB = "DELETE FROM books WHERE id = ?";
private static final String TAKE_BOOKS_FROM_DB = "SELECT * FROM books";
private static final String SAVE_BOOK_IN_DB = "INSERT INTO books (title, price) VALUES (?, ?)";

@Override
public void createTable() {
public BookDaoImpl() {
try (Connection connection = ConnectionUtil.getConnection()) {
String sqlCodeForTable = Files.readAllLines(Path.of(INIT_DB_SQL))
.stream()
Expand All @@ -37,23 +40,23 @@ public void createTable() {
PreparedStatement statement = connection.prepareStatement(sqlCodeForTable);
statement.executeUpdate();
} catch (SQLException | IOException e) {
throw new RuntimeException(e);
throw new DataProcessingException(e);
}
}

@Override
public Book save(Book book) {
public Book create(Book book) {
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(SAVE_BOOK_IN_DB,
Statement.RETURN_GENERATED_KEYS)) {
PreparedStatement statement = connection.prepareStatement(SAVE_BOOK_IN_DB,
Statement.RETURN_GENERATED_KEYS)) {

statement.setString(FIRST_QUESTION_MARK_INDEX,
book.getTitle());
statement.setBigDecimal(SECOND_QUESTION_MARK_INDEX,
book.getPrice());
int end = statement.executeUpdate();
if (end < 1) {
throw new RuntimeException("Failed to save book");
throw new DataProcessingException("Failed to save book");
}
ResultSet generatedKeys = statement.getGeneratedKeys();
if (generatedKeys.next()) {
Expand All @@ -62,15 +65,15 @@ public Book save(Book book) {
}

} catch (SQLException e) {
throw new RuntimeException(e);
throw new DataProcessingException(e);
}
return book;
}

@Override
public Optional<Book> findById(Long id) {
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(TAKE_BOOK_FROM_DB)) {
PreparedStatement statement = connection.prepareStatement(TAKE_BOOK_FROM_DB)) {

statement.setLong(FIRST_QUESTION_MARK_INDEX, id);
ResultSet resultSet = statement.executeQuery();
Expand All @@ -87,15 +90,15 @@ public Optional<Book> findById(Long id) {
return Optional.of(book);
}
} catch (SQLException e) {
throw new RuntimeException(e);
throw new DataProcessingException(e);
}
return null;
return Optional.empty();
}

@Override
public List<Book> findAll() {
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(TAKE_BOOKS_FROM_DB)) {
PreparedStatement statement = connection.prepareStatement(TAKE_BOOKS_FROM_DB)) {
ResultSet resultSet = statement.executeQuery();
List<Book> books = new ArrayList<>();
while (resultSet.next()) {
Expand All @@ -111,33 +114,32 @@ public List<Book> findAll() {
}
return books;
} catch (SQLException e) {
throw new RuntimeException(e);
throw new DataProcessingException(e);
}
}

@Override
public boolean deleteById(Long id) {
try (Connection connection = ConnectionUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(DELETE_BOOK_FROM_DB)) {
PreparedStatement statement = connection.prepareStatement(DELETE_BOOK_FROM_DB)) {

statement.setLong(FIRST_QUESTION_MARK_INDEX, id);
ResultSet resultSet = statement.executeQuery();

if (resultSet.next()) {
return true;
}
statement.executeUpdate();
return true;
} catch (SQLException e) {
throw new RuntimeException(e);
throw new DataProcessingException(e);
}
return false;
}

@Override
public Book update(Book book) {
if (book.getId() == null) {
throw new DataProcessingException();
}
long bookID = book.getId();
boolean deleteById = deleteById(bookID);
if (deleteById) {
save(book);
create(book);
}
return book;
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/mate/academy/dao/ConnectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import mate.academy.model.DataProcessingException;

public class ConnectionUtil {
private static final String DB_URL = "jdbc:mysql://localhost:3306/test";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
private static final String CLASS_NAME = "com.mysql.cj.jdbc.Driver";

static {
try {
Class.forName(CLASS_NAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
throw new DataProcessingException(e);
}
}

public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/mate/academy/model/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public Book setPrice(BigDecimal price) {

@Override
public String toString() {
return "Book{" +
"id=" + id +
", title='" + title + '\'' +
", price=" + price +
'}';
return "Book{"
+ "id=" + id
+ ", title='" + title + '\''
+ ", price=" + price
+ '}';
}
}
18 changes: 18 additions & 0 deletions src/main/java/mate/academy/model/DataProcessingException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package mate.academy.model;

public class DataProcessingException extends RuntimeException {
public DataProcessingException() {
}

public DataProcessingException(String message) {
super(message);
}

public DataProcessingException(String message, Throwable cause) {
super(message, cause);
}

public DataProcessingException(Throwable cause) {
super(cause);
}
}
6 changes: 5 additions & 1 deletion src/main/java/mate/academy/service/BookService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package mate.academy.service;

import mate.academy.model.Book;
import java.util.List;
import java.util.Optional;
import mate.academy.model.Book;

public interface BookService {
Book create(Book book);

Optional<Book> readID(Long id);

List<Book> readAll();

Book update(Book book);

boolean delete(Long id);
}
11 changes: 4 additions & 7 deletions src/main/java/mate/academy/service/BookServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package mate.academy.service;

import mate.academy.dao.BookDao;
import mate.academy.model.Book;

import java.util.List;
import java.util.Optional;
import mate.academy.dao.BookDao;
import mate.academy.model.Book;

public class BookServiceImpl implements BookService{
public class BookServiceImpl implements BookService {
private BookDao bookDao;

public BookServiceImpl(BookDao bookDao) {
this.bookDao = bookDao;
bookDao.createTable();
}


@Override
public Book create(Book book) {
return bookDao.save(book);
return bookDao.create(book);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/init_db.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE books (
CREATE TABLE IF NOT EXISTS books (
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(255),
price INT,
Expand Down

0 comments on commit f054197

Please sign in to comment.