Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian committed Oct 20, 2022
1 parent 18ac5b3 commit a2294f3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 38 deletions.
65 changes: 40 additions & 25 deletions src/main/java/it/books/MicrosoftDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.*;

public class MicrosoftDB {
/** Connect and retrieve a list of books from a given database
Expand Down Expand Up @@ -184,10 +178,12 @@ public static ArrayList<EvBook> connectAndGetNew(File dbFile, int offset, int li
while(res.next()){
EvBook book = new EvBook(res.getInt("ID"), res.getString("Codice"), res.getString("Titolo"), res.getString("Autore"));
book.addDetails(res.getString("Titolo_Originale"), res.getString("Genere"), res.getString("Anno_Pubblicazione"), res.getString("Edizione"), res.getString("Editore"), res.getString("Collezione"), res.getInt("Pagine"), res.getString("Formato_Pagine"), res.getString("Nazione"), res.getString("Scaffale"), res.getString("Posseduto_Dal"));
ResultSet pre = query.executeQuery("SELECT * FROM PRESTITI WHERE Codice=\""+book.getCode()+"\"");
if(pre.next()){
book.setLease(pre.getString("Data_Inizio_Prestito"), pre.getString("Data_Fine_Prestito"), pre.getString("Prestato_A").split(";")[0], pre.getString("Prestato_A").split(";")[1], pre.getString("Prestato_A").split(";")[2]);
}
try{
ResultSet pre = query.executeQuery("SELECT * FROM PRESTITI WHERE Codice=\""+book.getCode()+"\"");
if(pre.next()){
book.setLease(pre.getString("Data_Inizio_Prestito"), pre.getString("Data_Fine_Prestito"), pre.getString("Prestato_A").split(";")[0], pre.getString("Prestato_A").split(";")[1], pre.getString("Prestato_A").split(";")[2]);
}
}catch (Exception ignored){ }

books.add(book);
}
Expand Down Expand Up @@ -303,22 +299,41 @@ public static EvBook connectAndSearch(File dbFile, String code){

/** Connect to the database and add a new lease entry
* @param dbFile specify the database which work on;
* @param code specify the book code
* @param beginDate specify the beginning of leasing;
* @param expectedEnd specify the expected end of leasing;
* @param name specify the name of user;
* @param surname specify the surname of user;
* @param tel specify the phone number of user;
* @param book the book with specified leasing (book must already exist)
* **/
public static void connectAndAddLease(File dbFile, EvBook book){
try(Database db = DatabaseBuilder.open(dbFile)){
EvBook data = MicrosoftDB.connectAndSearch(dbFile, book.getCode());
//Exit immediately if the book does not exist.
if(Objects.isNull(data)) return;
//Procedure
String[] personData = book.leasedTo().split(";");
db.getTable("PRESTITI").addRow(book.getCode(), book.isLeasing(), book.getBeginDate(), book.getEndDate(), personData[0]+";"+personData[1]+";"+personData[2]);
//Reset here
}catch (IOException | NoSuchElementException e){
return;
}
}

/** Connect to the database and delete an existing lease information
* @param dbFile specify the database which work on;
* @param book the book with specified leasing (book must already exist)
* **/
public static void connectAndAddLease(File dbFile, String code, LocalTime beginDate, LocalTime expectedEnd, String name, String surname, String tel){
ArrayList<EvBook> books = connectAndGetNew(dbFile);
try{
EvBook data = books.stream().filter(e -> e.getCode().equals(code)).findFirst().orElse(null);
public static void connectAndDeleteLease(File dbFile, EvBook book){
try(Database db = DatabaseBuilder.open(dbFile)){
EvBook data = MicrosoftDB.connectAndSearch(dbFile, book.getCode());
book.endLease(); //Assume exiting
if(data == null) return;
data.setLease(beginDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")), expectedEnd.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")), name, surname, tel);
DeleteEntry(dbFile, -1, null, data.getTitle());
connectAndUploadANewBook(dbFile, data);
}catch (NoSuchElementException e){

//Find in the leasing table
Iterator<Row> row = db.getTable("PRESTITI").iterator();
Row row1;
while((row1 = row.next()) != null){
if(row1.getString("Codice").equals(book.getCode())){
db.getTable("PRESTITI").deleteRow(row1);
}
}
}catch (IOException | NoSuchElementException e){
return;
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/it/books/gcon/LeasingDetailedG.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ public void Submit(){ //Add leasing
try {
EvBook book = Objects.requireNonNull(MicrosoftDB.connectAndSearch(dbFile, CodeAndTitle.getText()));
book.setLease(BeginLease.getValue().format(DateTimeFormatter.ofPattern("dd-MM-yyyy")), EndLease.getValue().format(DateTimeFormatter.ofPattern("dd-MM-yyyy")), Name.getText(), Surname.getText(), TelBox.getText());
MicrosoftDB.DeleteEntry(dbFile, -1, book.getCode(), book.getTitle());
MicrosoftDB.connectAndUploadANewBook(dbFile, book);
MicrosoftDB.connectAndAddLease(dbFile, book);
//MicrosoftDB.DeleteEntry(dbFile, -1, book.getCode(), book.getTitle());
//MicrosoftDB.connectAndUploadANewBook(dbFile, book);
new Alert(Alert.AlertType.INFORMATION, "Prestito aggiunto", ButtonType.OK).showAndWait().ifPresentOrElse(i -> {MainPane.getScene().getWindow().hide();}, () -> {MainPane.getScene().getWindow().hide();});
} catch (NullPointerException e) {
new Alert(Alert.AlertType.ERROR, "Errore: Impossibile aggiornare il database.", ButtonType.OK).showAndWait();
Expand All @@ -97,9 +98,7 @@ public void Submit(){ //Add leasing
}

public void Unlock(){ //Remove leasing
selected.endLease();
MicrosoftDB.DeleteEntry(dbFile, -1, selected.getCode() , selected.getTitle());
MicrosoftDB.connectAndUploadANewBook(dbFile, selected);
MicrosoftDB.connectAndDeleteLease(dbFile, selected);
MainPane.getScene().getWindow().hide();
}
}
15 changes: 7 additions & 8 deletions src/main/resources/it/books/gcon/leasingDetailed.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>


<AnchorPane fx:id="MainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="285.0" prefWidth="451.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="it.books.gcon.LeasingDetailedG">
<children>
<Label layoutX="163.0" layoutY="14.0" text="Gestione prestiti">
Expand All @@ -17,18 +16,18 @@
</Label>
<Label layoutX="60.0" layoutY="112.0" text="Nome" />
<Label layoutX="60.0" layoutY="172.0" text="Cognome" />
<TextField fx:id="Surname" layoutX="58.0" layoutY="190.0" />
<TextField fx:id="Name" layoutX="58.0" layoutY="130.0" />
<TextField fx:id="CodeAndTitle" editable="false" layoutX="58.0" layoutY="60.0" prefHeight="26.0" prefWidth="334.0" />
<Label layoutX="215.0" layoutY="41.0" text="Codice" />
<TextField fx:id="Name" layoutX="58.0" layoutY="130.0" />
<TextField fx:id="Surname" layoutX="58.0" layoutY="190.0" />
<TextField fx:id="TelBox" layoutX="58.0" layoutY="246.0" />
<Label layoutX="60.0" layoutY="228.0" text="Recapito telefonico" />
<DatePicker fx:id="BeginLease" layoutX="216.0" layoutY="130.0" promptText="selezionare la data" />
<Label layoutX="218.0" layoutY="112.0" text="Data inzio prestito" />
<Label layoutX="218.0" layoutY="172.0" text="Data prevista fine prestito" />
<DatePicker fx:id="EndLease" layoutX="216.0" layoutY="190.0" promptText="selezionare la data" />
<Button fx:id="SubmitBtn" layoutX="216.0" layoutY="245.0" mnemonicParsing="false" onAction="#Submit" prefHeight="26.0" prefWidth="84.0" text="Conferma" />
<Button fx:id="CloseBtn" layoutX="386.0" layoutY="15.0" mnemonicParsing="false" onAction="#Close" text="Chiudi" />
<Button fx:id="UnlockBtn" layoutX="309.0" layoutY="245.0" mnemonicParsing="false" onAction="#Unlock" prefHeight="26.0" prefWidth="84.0" text="Sblocca" />
<Button fx:id="CloseBtn" layoutX="386.0" layoutY="15.0" mnemonicParsing="false" onAction="#Close" text="Chiudi" />
<Label layoutX="215.0" layoutY="41.0" text="Codice" />
<Label layoutX="60.0" layoutY="228.0" text="Cell / Email" />
<Label layoutX="218.0" layoutY="112.0" text="Data inzio prestito" />
<Label layoutX="218.0" layoutY="172.0" text="Data prevista fine prestito" />
</children>
</AnchorPane>

0 comments on commit a2294f3

Please sign in to comment.