Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions JavaJPA.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$" dumb="true">
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
</content>
</component>
<component name="FacetManager">
<facet type="jpa" name="JPA">
<configuration>
<setting name="validation-enabled" value="true" />
<datasource-mapping />
<naming-strategy-map />
<deploymentDescriptor name="persistence.xml" url="file://$MODULE_DIR$/src/main/resources/META-INF/persistence.xml" />
</configuration>
</facet>
</component>
</module>
106 changes: 81 additions & 25 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,54 +1,110 @@
<?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">
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>JavaJPA</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.release>25</maven.compiler.release>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.jupiter.version>6.0.1</junit.jupiter.version>
<assertj.core.version>3.27.6</assertj.core.version>
<mockito.version>5.21.0</mockito.version>
<javafx.version>21.0.1</javafx.version>
<hibernate.version>6.4.1.Final</hibernate.version>
</properties>

<dependencies>
<!-- JavaFX -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>

<!-- JPA och Hibernate -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>7.2.0.Final</version>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.1.0</version>
</dependency>

<!-- MySQL Driver -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.5.0</version>
<scope>runtime</scope>
<version>9.1.0</version>
</dependency>

<!-- Test (Förenklat) -->
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.184</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>17</release>
</configuration>
</plugin>

<!-- Detta plugin gör att du kan köra 'mvn javafx:run' -->
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>org.example.Main</mainClass>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.example.Main</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/persistence.xml</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>




95 changes: 46 additions & 49 deletions src/main/java/org/example/App.java
Original file line number Diff line number Diff line change
@@ -1,57 +1,54 @@
package org.example;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.example.util.DataInitializer;
import org.example.util.JPAUtil;

public class App extends Application {
// ... existing code ...

@Override
public void start(Stage stage) {
try {
// 1. Initiera databasen och ladda testdata (seed)
initDatabase();

// 2. Ladda gränssnittet
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource("/fxml/main_view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 1000, 600);

// Koppla på CSS för "Hertz-looken"
// scene.getStylesheets().add(App.class.getResource("/css/main.css").toExternalForm());

stage.setTitle("Hertz Biluthyrning - Grupp 7");
stage.setScene(scene);
stage.show();
} catch (Exception e) {
e.printStackTrace();
System.err.println("Kunde inte starta applikationen: " + e.getMessage());
}
}

public class App {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("carRentalPU");
EntityManager em = emf.createEntityManager();

em.getTransaction().begin();

// Rensa tabellen innan vi lägger in nya bilar
em.createQuery("DELETE FROM Car").executeUpdate();

// Skapa och persist 20 bilar med rimliga dagspriser
em.persist(createCar("Volvo", "XC90", 2025, "Lyxbil", 800_000));
em.persist(createCar("BMW", "7 Series", 2024, "Lyxbil", 1_000_000));
em.persist(createCar("Audi", "A8", 2023, "Lyxbil", 900_000));
em.persist(createCar("Toyota", "Corolla", 2022, "Personbil", 200_000));
em.persist(createCar("Honda", "Civic", 2021, "Personbil", 190_000));
em.persist(createCar("Ford", "Focus", 2022, "Personbil", 210_000));
em.persist(createCar("Mercedes", "Sprinter", 2023, "Minibuss", 350_000));
em.persist(createCar("Volkswagen", "Transporter", 2023, "Minibuss", 330_000));
em.persist(createCar("Renault", "Kangoo", 2022, "Minibuss", 300_000));
em.persist(createCar("Fiat", "500", 2023, "Småbil", 120_000));
em.persist(createCar("Mini", "Cooper", 2022, "Småbil", 150_000));
em.persist(createCar("Volkswagen", "Up!", 2022, "Småbil", 110_000));
em.persist(createCar("Porsche", "911", 2025, "Sportbil", 1_500_000));
em.persist(createCar("Ferrari", "Roma", 2024, "Sportbil", 2_000_000));
em.persist(createCar("Lamborghini", "Huracan", 2025, "Sportbil", 2_500_000));
em.persist(createCar("Tesla", "Model S", 2023, "Lyxbil", 900_000));
em.persist(createCar("Kia", "Rio", 2022, "Småbil", 130_000));
em.persist(createCar("Hyundai", "i30", 2023, "Personbil", 195_000));
em.persist(createCar("Opel", "Vivaro", 2022, "Minibuss", 320_000));
em.persist(createCar("Jaguar", "F-Type", 2024, "Sportbil", 1_600_000));

em.getTransaction().commit();
em.close();
emf.close();

System.out.println("20 cars saved successfully!");
private void initDatabase() {
EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
try {
DataInitializer.seed(em);
} finally {
em.close();
}
}

// Skapar bil och sätter dagspris till 0,5% av priset
private static Car createCar(String brand, String model, int year, String type, double price) {
Car car = new Car;
car.setBrand(brand);
car.setModel(model);
car.setYear(year);
car.setType(type);
car.setPrice(price);
car.setDailyPrice(price * 0.005); // 0,5% av totalpris
return car;
@Override
public void stop() {
// Stäng ner JPA snyggt när appen stängs
JPAUtil.shutdown();
}

public static void main(String[] args) {
launch();
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example;

public class Main {
public static void main(String[] args) {
App.main(args);
}
}
Loading
Loading