final ver commit

This commit is contained in:
Maksym Sierszen 2024-01-13 21:13:31 +01:00
commit 24d8efc74b
15 changed files with 706 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

38
Biblioteka/.gitignore vendored Normal file
View File

@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
Biblioteka/.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

14
Biblioteka/.idea/misc.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,124 @@
<?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>

31
Biblioteka/pom.xml Normal file
View File

@ -0,0 +1,31 @@
<?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.example</groupId>
<artifactId>Library</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Hibernate core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.9.Final</version>
</dependency>
<!-- PostgreSQL Driver -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.4.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,67 @@
package org.example;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name = "\"Authors\"")
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "author_id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "nationality")
private String nationality;
@Column(name = "birth_year")
private Integer birthYear;
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL)
private List<Book> books;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
public Integer getBirthYear() {
return birthYear;
}
public void setBirthYear(Integer birthYear) {
this.birthYear = birthYear;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
}

View File

@ -0,0 +1,70 @@
package org.example;
import org.hibernate.annotations.Type;
import javax.persistence.*;
import java.time.ZonedDateTime;
@Entity
@Table(name = "\"Books\"")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_id")
private Long id;
@Column(name = "title")
private String title;
@Column(name = "publication_date")
@Type(type = "org.hibernate.type.ZonedDateTimeType")
private ZonedDateTime publicationDate;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
private Author author;
@Column(name = "availability_status")
private boolean availabilityStatus;
// Getters and setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public ZonedDateTime getPublicationDate() {
return publicationDate;
}
public void setPublicationDate(ZonedDateTime publicationDate) {
this.publicationDate = publicationDate;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public boolean isAvailabilityStatus() {
return availabilityStatus;
}
public void setAvailabilityStatus(boolean availabilityStatus) {
this.availabilityStatus = availabilityStatus;
}
}

View File

@ -0,0 +1,71 @@
package org.example;
import org.hibernate.annotations.Type;
import javax.persistence.*;
import java.time.ZonedDateTime;
@Entity
@Table(name = "\"Borrows\"")
public class Borrow {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "borrow_id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "book_id")
private Book book;
@Column(name = "borrow_date")
@Type(type = "org.hibernate.type.ZonedDateTimeType")
private ZonedDateTime borrowDate;
@Column(name = "return_date")
@Type(type = "org.hibernate.type.ZonedDateTimeType")
private ZonedDateTime returnDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public ZonedDateTime getBorrowDate() {
return borrowDate;
}
public void setBorrowDate(ZonedDateTime borrowDate) {
this.borrowDate = borrowDate;
}
public ZonedDateTime getReturnDate() {
return returnDate;
}
public void setReturnDate(ZonedDateTime returnDate) {
this.returnDate = returnDate;
}
}

View File

@ -0,0 +1,100 @@
package org.example;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import java.util.List;
public class LibraryService {
private SessionFactory sessionFactory;
public LibraryService(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void printAllAuthors() {
try (Session session = sessionFactory.openSession()) {
Query<Author> query = session.createQuery("FROM Author", Author.class);
List<Author> authors = query.getResultList();
System.out.println("Dostępni autorzy:");
for (Author author : authors) {
System.out.println("ID: " + author.getId() + author.getName());
}
}
}
public List<Book> findAllBooksByAuthorId(Long authorId) {
List<Book> books;
try (Session session = sessionFactory.openSession()) {
Query<Book> query = session.createQuery("SELECT b FROM Book b JOIN FETCH b.author WHERE b.author.id = :authorId", Book.class);
query.setParameter("authorId", authorId);
books = query.getResultList();
}
return books;
}
public void printBooksByAuthor(long authorId) {
List<Book> booksByAuthor = findAllBooksByAuthorId(authorId);
if (!booksByAuthor.isEmpty()) {
Author author = booksByAuthor.get(0).getAuthor();
System.out.println("Książki autora:\n " + author.getName());
for (Book book : booksByAuthor) {
System.out.println("Tytuł: " + book.getTitle());
System.out.println("Data publikacji: " + book.getPublicationDate());
System.out.println("--------");
}
} else {
System.out.println("Nie znaleziono książek dla autora o ID: " + authorId);
}
}
public void printBorrowsByUserId(long userId, int pageNumber) {
int pageSize = 3;
try (Session session = sessionFactory.openSession()) {
Query<Borrow> query = session.createQuery("FROM Borrow b WHERE b.user.id = :userId", Borrow.class);
query.setParameter("userId", userId);
query.setFirstResult((pageNumber - 1) * pageSize);
query.setMaxResults(pageSize);
List<Borrow> borrows = query.getResultList();
if (borrows.isEmpty()) {
System.out.println("Użytkownik o ID " + userId + " nie ma żadnych wypożyczeń.");
} else {
for (Borrow borrow : borrows) {
System.out.println("Wypożyczenie ID: " + borrow.getId());
System.out.println("Książka: " + borrow.getBook().getTitle());
System.out.println("Data wypożyczenia: " + borrow.getBorrowDate());
System.out.println("Data zwrotu: " + borrow.getReturnDate());
System.out.println("--------");
}
}
}
}
public boolean hasBorrows(long userId) {
try (Session session = sessionFactory.openSession()) {
Query<Long> query = session.createQuery("SELECT COUNT(b.id) FROM Borrow b WHERE b.user.id = :userId", Long.class);
query.setParameter("userId", userId);
return query.getSingleResult() > 0;
}
}
public void printAvailableBooks() {
try (Session session = sessionFactory.openSession()) {
Query<Book> query = session.createQuery("FROM Book b WHERE b.availabilityStatus = true", Book.class);
List<Book> availableBooks = query.getResultList();
if (availableBooks.isEmpty()) {
System.out.println("Brak dostępnych książek.");
} else {
System.out.println("Dostępne książki:");
for (Book book : availableBooks) {
System.out.println("ID: " + book.getId() + ", Tytuł: " + book.getTitle());
}
}
}
}
}

View File

@ -0,0 +1,81 @@
package org.example;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Konfiguracja Hibernate z pliku hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
// Utworzenie sesji Hibernate
SessionFactory sessionFactory = configuration.buildSessionFactory();
// Utworzenie instancji LibraryService
LibraryService libraryService = new LibraryService(sessionFactory);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("-- BIBLIOTEKA --");
System.out.println("1. Wypisz książki danego autora");
System.out.println("2. Wypisz wypożyczenia użytkownika");
System.out.println("3. Wypisz dostępne książki");
System.out.println("4. Wyjście");
System.out.print("Wybierz opcję: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
libraryService.printAllAuthors();
System.out.print("Podaj ID autora: ");
long authorId = scanner.nextLong();
libraryService.printBooksByAuthor(authorId);
break;
case 2:
System.out.print("Podaj ID użytkownika: ");
long userId = scanner.nextLong();
if (!libraryService.hasBorrows(userId)) {
System.out.println("Użytkownik o ID " + userId + " nie ma żadnych wypożyczeń.");
} else {
int currentPage = 1;
boolean browsing = true;
while (browsing) {
libraryService.printBorrowsByUserId(userId, currentPage);
System.out.println("Aktualna strona: " + currentPage);
System.out.println("1. Następna strona");
System.out.println("2. Poprzednia strona");
System.out.println("3. Wyjście ze stronicowania");
System.out.print("Wybierz opcję: ");
int pageChoice = scanner.nextInt();
switch (pageChoice) {
case 1:
currentPage++;
break;
case 2:
currentPage = Math.max(1, currentPage - 1);
break;
case 3:
browsing = false;
break;
}
}
}
break;
case 3:
libraryService.printAvailableBooks();
break;
case 4:
System.out.println("> EXIT <");
sessionFactory.close();
System.exit(0);
break;
default:
System.out.println("Nieprawidłowa opcja, spróbuj ponownie.");
}
}
}
}

View File

@ -0,0 +1,70 @@
package org.example;
import org.hibernate.annotations.Type;
import javax.persistence.*;
import java.time.ZonedDateTime;
import java.util.List;
@Entity
@Table(name = "\"Users\"")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "birth_date")
@Type(type = "org.hibernate.type.ZonedDateTimeType")
private ZonedDateTime birthDate;
@Column(name = "email", unique = true)
private String email;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Borrow> borrows;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ZonedDateTime getBirthDate() {
return birthDate;
}
public void setBirthDate(ZonedDateTime birthDate) {
this.birthDate = birthDate;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Borrow> getBorrows() {
return borrows;
}
public void setBorrows(List<Borrow> borrows) {
this.borrows = borrows;
}
}

View File

@ -0,0 +1,25 @@
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Library</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">true</property>
<mapping class="org.example.Book"/>
<mapping class="org.example.Author"/>
<mapping class="org.example.User"/>
<mapping class="org.example.Borrow"/>
</session-factory>
</hibernate-configuration>

BIN
library_postgre_backup.sql Normal file

Binary file not shown.