Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JFB 7: Build jar from homework 6.5 using Maven and Gradle #8

Merged
merged 8 commits into from
Jul 4, 2024
Merged
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 .github/workflows/build-gradle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Build with Gradle and run
on: [push, workflow_dispatch]

jobs:
build_with_gradle:
runs-on: ubuntu-latest
steps:
- name: Get Repo
uses: actions/[email protected]
- name: Setup Java
uses: actions/[email protected]
with:
java-version: 21
distribution: 'temurin'
- name: Build Project with Gradle
run: gradle clean build
- name: Run Application
run: java -jar build/libs/java-trainee-edu-1.0-SNAPSHOT.jar
19 changes: 19 additions & 0 deletions .github/workflows/build-maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Build with Maven and run
on: [push, workflow_dispatch]

jobs:
build_with_maven:
runs-on: ubuntu-latest
steps:
- name: Get Repo
uses: actions/[email protected]
- name: Setup Java
uses: actions/[email protected]
with:
java-version: 21
distribution: 'temurin'
cache: 'maven'
- name: Build Project with Maven
run: mvn clean package
- name: Run Application
run: java -jar java-trainee-edu/target/java-trainee-edu-1.0-SNAPSHOT.jar
35 changes: 35 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
plugins {
id 'java'
id 'application'
}

group 'com.rtemi'
version '1.0-SNAPSHOT'

java{
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

repositories {
mavenCentral()
}

dependencies {
implementation 'com.fasterxml.jackson.core:jackson-core:2.17.1'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.17.1'
}

application {
mainClassName = 'com.rtemi.services.CustomDataStructures'
}

jar {
manifest {
attributes(
'Main-Class': 'com.rtemi.services.CustomDataStructures'
)
}

}
30 changes: 29 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,42 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.rtemi</groupId>
<artifactId>trainee-edu-hw2</artifactId>
<artifactId>java-trainee-edu</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>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.rtemi.services.CustomDataStructures</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
Binary file removed screenshot/Screenshot_4.png
Binary file not shown.
Binary file removed screenshot/busticketvalidator.png
Binary file not shown.
5 changes: 2 additions & 3 deletions src/main/java/com/rtemi/model/ConcertTicket.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ConcertTicket extends TicketUID implements Printable, Shareable {
private static final int WEIGHT_PRECISION = 3;

private String concertTicketId;
@NullableWarning(key = "Variable [concertHall] is null in [Ticket]!")
@NullableWarning(key = "Variable [concertHall] is null in [Ticket]!")
private String concertHall;
private int eventCode;
private long ticketPurchaseTime;
Expand Down Expand Up @@ -88,8 +88,7 @@ public String toString() {
", isPromo - " + isPromo +
", sector - " + sector +
", maxWeight - " + maxWeight.setScale(WEIGHT_PRECISION,RoundingMode.HALF_UP) +
", ticketPrice - " + ticketPrice.setScale(PRICE_PRECISION,RoundingMode.HALF_UP) +
'}';
", ticketPrice - " + ticketPrice.setScale(PRICE_PRECISION,RoundingMode.HALF_UP);
}

@Override
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/com/rtemi/model/CustomArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.rtemi.model;
import java.util.Arrays;
import java.util.Arrays;

/**
* Custom implementation of an ArrayList.
*/
public class CustomArrayList<T> {
private static final int DEFAULT_CAPACITY = 10;
private Object[] elements;
private int size = 0;

public CustomArrayList() {
elements = new Object[DEFAULT_CAPACITY];
}

/**
* Adds an element to the list.
* @param element The element to add.
*/
public void put(T element) {
if (size == elements.length) {
resize();
}
elements[size++] = element;
}

/**
* Gets an element by its index.
* @param index The index of the element.
* @return The element at the specified index.
* @throws IndexOutOfBoundsException if the index is out of range.
*/
@SuppressWarnings("unchecked")
public T get(int index) {
if (index >= size || index < 0) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
return (T) elements[index];
}

/**
* Removes an element by its index.
* @param index The index of the element to remove.
* @return The removed element.
* @throws IndexOutOfBoundsException if the index is out of range.
*/
@SuppressWarnings("unchecked")
public T delete(int index) {
if (index >= size || index < 0) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
T element = (T) elements[index];
int numMoved = size - index - 1;
if (numMoved > 0) {
System.arraycopy(elements, index + 1, elements, index, numMoved);
}
elements[--size] = null; // clear to let GC do its work
return element;
}

/**
* Returns the size of the list.
* @return The size of the list.
*/
public int size() {
return size;
}

/**
* Resizes the internal array to accommodate more elements.
*/
private void resize() {
int newSize = elements.length * 2;
elements = Arrays.copyOf(elements, newSize);
}
}
121 changes: 121 additions & 0 deletions src/main/java/com/rtemi/model/CustomHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.rtemi.model;
import java.util.Iterator;
import java.util.LinkedList;

/**
* Custom implementation of a HashSet.
*/
public class CustomHashSet<T> implements Iterable<T> {
private static final int DEFAULT_CAPACITY = 16;
private static final float LOAD_FACTOR = 0.75f;
private LinkedList<T>[] buckets;
private int size = 0;

public CustomHashSet() {
buckets = new LinkedList[DEFAULT_CAPACITY];
}

/**
* Adds an element to the set.
* @param element The element to add.
*/
public void put(T element) {
if (contains(element)) {
return;
}
if (size + 1 > LOAD_FACTOR * buckets.length) {
resize();
}
int index = getIndex(element);
if (buckets[index] == null) {
buckets[index] = new LinkedList<>();
}
buckets[index].add(element);
size++;
}

/**
* Checks if the set contains the specified element.
* @param element The element to check.
* @return True if the set contains the element, false otherwise.
*/
public boolean contains(T element) {
int index = getIndex(element);
LinkedList<T> bucket = buckets[index];
return bucket != null && bucket.contains(element);
}

/**
* Removes an element from the set.
* @param element The element to remove.
* @return True if the element was removed, false otherwise.
*/
public boolean remove(T element) {
int index = getIndex(element);
LinkedList<T> bucket = buckets[index];
if (bucket != null && bucket.remove(element)) {
size--;
return true;
}
return false;
}

/**
* Returns the size of the set.
* @return The size of the set.
*/
public int size() {
return size;
}

/**
* Returns an iterator over the elements in the set.
* @return An iterator.
*/
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int bucketIndex = 0;
private Iterator<T> bucketIterator = buckets[bucketIndex] == null ? null : buckets[bucketIndex].iterator();

@Override
public boolean hasNext() {
while ((bucketIterator == null || !bucketIterator.hasNext()) && bucketIndex < buckets.length - 1) {
bucketIndex++;
bucketIterator = buckets[bucketIndex] == null ? null : buckets[bucketIndex].iterator();
}
return bucketIterator != null && bucketIterator.hasNext();
}

@Override
public T next() {
return bucketIterator.next();
}
};
}

/**
* Gets the index of the bucket for the specified element.
* @param element The element.
* @return The index of the bucket.
*/
private int getIndex(T element) {
return element == null ? 0 : Math.abs(element.hashCode() % buckets.length);
}

/**
* Resizes the internal array to accommodate more elements.
*/
private void resize() {
LinkedList<T>[] oldBuckets = buckets;
buckets = new LinkedList[oldBuckets.length * 2];
size = 0;
for (LinkedList<T> bucket : oldBuckets) {
if (bucket != null) {
for (T element : bucket) {
put(element);
}
}
}
}
}
Loading