1
0
forked from pabulaner/vivi

Compare commits

...

6 Commits

Author SHA1 Message Date
Paul H
5d5ceedfa8 Fixed tiny error 2025-03-25 21:11:28 +01:00
Paul H
8aa3bf1b6c Added merge sort 2025-03-25 21:05:46 +01:00
Paul H
2c88f9596a Implemented iterator 2025-03-25 20:49:46 +01:00
Paul H
0eb671dddf Implemented BubbleSort 2025-03-25 20:45:08 +01:00
Paul H
cc55f2703d Added bank 2025-03-24 11:31:31 +01:00
Paul H
3a45050fa6 Added jmarket project 2025-03-17 15:54:46 +01:00
37 changed files with 1400 additions and 32 deletions

3
.idea/workspace.xml generated
View File

@ -5,8 +5,7 @@
</component>
<component name="ChangeListManager">
<list default="true" id="395d95f7-d752-4ca0-b261-78679f611e7e" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/vcs.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/list/src/main/java/de/vivi/list/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/list/src/main/java/de/vivi/list/Main.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />

3
bank/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

13
bank/.idea/compiler.xml generated Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="bank" />
</profile>
</annotationProcessing>
</component>
</project>

7
bank/.idea/encodings.xml generated Normal file
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>

20
bank/.idea/jarRepositories.xml generated Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

12
bank/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,12 @@
<?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="temurin-21" project-jdk-type="JavaSDK" />
</project>

124
bank/.idea/uiDesigner.xml generated Normal file
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>

6
bank/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

0
bank/.mvn/jvm.config Normal file
View File

0
bank/.mvn/maven.config Normal file
View File

41
bank/example.txt Normal file
View File

@ -0,0 +1,41 @@
$> create Paul
Created account with id: '0' and name 'Paul'
$> create Peter
Created account with id: '1' and name 'Peter'
$> create Mic
Created account with id: '2' and name 'Mic'
$> info 1
Info for account '1': [Name: 'Peter', Balance: 0$]
$> add 1 100
Added 100$ to account '1'
$> info 1
Info for account '1': [Name: 'Paul', Balance: 0$]
$> transfer 1 0 40
Transferred 40$ from account '1' to account '0'
$> info 0
Info for account '0': [Name: 'Paul', Balance: 40$]
$> info 1
Info for account '1': [Name: 'Peter', Balance: 60$]
$> transfer 1 2 30
Transferred 30$ from account '1' to account '2'
$> transfer 2 1 10
Transferred 10$ from account '2' to account '1'
$> transactions 1
Transactions for account '1':
Send 40$ to account '0'
Send 30$ to account '2'
Received 10$ from account '2'
$> remove 0 120
Removed 120$ from account '0'

81
bank/pom.xml Normal file
View File

@ -0,0 +1,81 @@
<?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>de.vivi</groupId>
<artifactId>bank</artifactId>
<version>1.0-SNAPSHOT</version>
<name>bank</name>
<description>A simple bank.</description>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.6.1</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</reporting>
</project>

View File

@ -0,0 +1,27 @@
package de.vivi;
import de.vivi.bank.Bank;
import java.util.Scanner;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
Bank bank = null;
Scanner scanner = new Scanner(System.in);
while (true) {
String[] values = scanner.nextLine().split(" ");
if (values.length > 0 && values[0].equals("exit")) {
return;
}
Command.exec(bank, values);
}
}
}

View File

@ -0,0 +1,50 @@
package de.vivi;
import de.vivi.bank.Bank;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
public class Command {
private static final Map<String, Command> COMMANDS = new HashMap<String, Command>() {{
put("create", new Command(1, (bank, args) -> bank.create(args[1])));
put("info", new Command(1, (bank, args) -> bank.info(Integer.parseInt(args[1]))));
put("add", new Command(1, (bank, args) -> bank.add(Integer.parseInt(args[1]), Integer.parseInt(args[2]))));
put("remove", new Command(1, (bank, args) -> bank.remove(Integer.parseInt(args[1]), Integer.parseInt(args[2]))));
put("transfer", new Command(1, (bank, args) -> bank.transfer(Integer.parseInt(args[1]), Integer.parseInt(args[2]), Integer.parseInt(args[3]))));
put("transactions", new Command(1, (bank, args) -> bank.transactions(Integer.parseInt(args[1]))));
}};
private final int args;
private final BiConsumer<Bank, String[]> consumer;
private Command(int args, BiConsumer<Bank, String[]> consumer) {
this.args = args;
this.consumer = consumer;
}
public static void exec(Bank bank, String[] args) {
if (args.length > 0) {
Command command = COMMANDS.get(args[0]);
if (command == null) {
System.out.println("Command not found");
return;
}
if (command.args != args.length) {
System.out.println("Invalid argument count");
return;
}
try {
command.consumer.accept(bank, args);
} catch (NumberFormatException e) {
System.out.println("Invalid argument");
}
}
}
}

View File

@ -0,0 +1,50 @@
package de.vivi.bank;
public interface Bank {
/**
* Creates an account with the given name and generates the id.
* Prints:
* Created account with id '#id' and name '#name'
*/
void create(String name);
/**
* Prints the info of the account with the given id.
* Prints:
* Account info: [Name: '#name', Balance: '#balance']
*/
void info(int id);
/**
* Adds the given amount to the account with the given id.
* Prints:
* Added #amount$ to account '#id'
*/
void add(int id, int amount);
/**
* Removes the given amount from the account with the given id.
* Prints:
* Removed #amount$ from account '#id'
*/
void remove(int id, int amount);
/**
* Transfers the given amount from the account with the given
* fromId to the account with the given toId.
* Prints:
* Transferred #amount$ from account '#fromId' to account '#toId'
*/
void transfer(int fromId, int toId, int amount);
/**
* Lists all transactions of the account with the given id.
* Prints:
* Transactions for account with id '#id':
* Send #amount$ to account '#id'
* Received #amount$ from account '#id'
* ...
*/
void transactions(int id);
}

26
bank/src/site/site.xml Normal file
View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="bank" xmlns="http://maven.apache.org/DECORATION/1.8.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.8.0 http://maven.apache.org/xsd/decoration-1.8.0.xsd">
<bannerLeft>
<name>bank</name>
<src>https://maven.apache.org/images/apache-maven-project.png</src>
<href>https://www.apache.org/</href>
</bannerLeft>
<bannerRight>
<src>https://maven.apache.org/images/maven-logo-black-on-white.png</src>
<href>https://maven.apache.org/</href>
</bannerRight>
<skin>
<groupId>org.apache.maven.skins</groupId>
<artifactId>maven-fluido-skin</artifactId>
<version>1.7</version>
</skin>
<body>
<menu ref="parent" />
<menu ref="reports" />
</body>
</project>

View File

@ -0,0 +1,38 @@
package de.vivi;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

38
jmarket/.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

3
jmarket/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

7
jmarket/.idea/encodings.xml generated Normal file
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
jmarket/.idea/misc.xml generated 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_17" project-jdk-name="temurin-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

124
jmarket/.idea/uiDesigner.xml generated Normal file
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>

7
jmarket/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

16
jmarket/pom.xml Normal file
View File

@ -0,0 +1,16 @@
<?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>jmarket-09-10</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,28 @@
package de.vimo.jmarket;
import de.vimo.jmarket.api.ProductDatabase;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
}
/*
private static void usage() {
ProductDatabase db = null;
db.addProduct(new PaulMarketProduct("banana", LocalDate.of(2025, 8, 1), 10.4, 4.5));
db.addProduct(new PaulMarketProduct("banana", LocalDate.of(2025, 4, 3), 6.4, 2.5));
db.addProduct(new PaulMarketProduct("banana", LocalDate.of(2026, 8, 1), 12.7, 6.5));
db.addProduct(new PaulMarketProduct("orange", LocalDate.of(2024, 1, 14), 2.4, 1.5));
db.addProduct(new PaulMarketProduct("orange", LocalDate.of(2023, 3, 20), 3.3, 2.5));
db.addProduct(new PaulMarketProduct("candy", LocalDate.of(2028, 8, 1), 30.4, 10.4));
db.addProduct(new PaulMarketProduct("candy", LocalDate.of(2026, 8, 23), 24.5, 12.6));
db.addProduct(new PaulMarketProduct("beans", LocalDate.of(2025, 6, 12), 2.3, 120.5));
db.addProduct(new PaulMarketProduct("doughnut", LocalDate.of(2020, 3, 1), 1.4, 6.5));
}
*/
}

View File

@ -0,0 +1,17 @@
package de.vimo.jmarket.api;
import java.time.LocalDate;
/**
* TODO: implement all methods
*/
public interface Product {
String getName();
LocalDate getExpirationDate();
double getWeight();
double getPrice();
}

View File

@ -0,0 +1,93 @@
package de.vimo.jmarket.api;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Stream;
/**
* TODO: implement all methods
*/
public interface ProductDatabase {
/**
* Adds a product to the database and generates a random id for it.
*
* @param product The product to add
* @return The randomly generated id
*/
int addProduct(Product product);
/**
* Returns the product with the specified id. If there is no
* such product, it returns null.
*
* @param id The id of the product
* @return The product with the specified id
*/
Product getProduct(int id);
/**
* Returns all products with the specified name.
*
* @param name The name to search for
* @return The products that have the specified name
*/
List<Product> getProductsByName(String name);
/**
* Gets all products that match the given predicate.
*
* @param predicate The condition the products have to fulfill
* @return The filtered products
*/
List<Product> getProducts(Predicate<Product> predicate);
/**
* Returns all products that are expired at or after the specified
* date.
*
* @param date The date to check for
* @return The products that are expired at or after the date
*/
List<Product> getExpiredProducts(LocalDate date);
/**
* Returns a list of all distinct product names inside the database.
*
* @return The product names.
*/
List<String> getDistinctProductNames();
/**
* Returns a map where the key is the product name and the
* value is the total weight of all products with that name.
*
* @return The map with product names and weights
*/
Map<String, Double> getTotalProductWeights();
/**
* Returns the count of products with the specified name.
*
* @param name The name of the product to count
* @return The count of products
*/
int getProductCount(String name);
/**
* Returns the total price of all products with the specified name.
*
* @param name The name of the product
* @return The total price
*/
double getTotalProductPrice(String name);
/**
* Adds all values from the specified databases to this database.
*
* @param databases The databases to add
*/
void addAll(Stream<Stream<ProductDatabase>> databases);
}

View File

@ -0,0 +1,34 @@
package de.vimo.jmarket.api;
import java.util.function.BiConsumer;
/**
* TODO: implement all methods
*/
public interface TreeList {
/**
* Adds the product with the id into the list.
*
* @param id The unique identifier
* @param product The product
*/
void add(int id, Product product);
/**
* Returns the product with the specified id or null if
* the id can't be found.
*
* @param id The unique identifier
* @return The product
*/
Product get(int id);
/**
* Calls the accept method for each entry inside the tree
* for the provided consumer.
*
* @param consumer The consumer to consume all entries in the list
*/
void forEach(BiConsumer<Integer, Product> consumer);
}

124
list/.idea/uiDesigner.xml generated Normal file
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>

View File

@ -1,19 +1,26 @@
package de.vivi.list;
import de.vivi.list.list.ArrayByteList;
import de.vivi.list.list.ByteList;
import de.vivi.list.list.LinkedByteList;
import de.vivi.list.sort.BubbleSort;
import de.vivi.list.sort.ByteSort;
import de.vivi.list.sort.MergeSort;
public class Main {
public static void main(String[] args) {
ByteList array = new ArrayByteList();
ByteList linked = new LinkedByteList();
ByteList combined = new CombinedByteList(16);
// ByteList combined = new CombinedByteList(16);
testList(array);
// testList(array);
testList(linked);
testList(combined);
// testList(combined);
testSort(array);
// testSort(array);
testSort(linked);
testSort(combined);
// testSort(combined);
}
private static void testList(ByteList list) {
@ -22,6 +29,11 @@ public class Main {
list.add((byte) 5);
list.add((byte) -86);
list.add((byte) 3);
list.add((byte) 100);
list.add((byte) 20);
list.add((byte) 40);
list.add((byte) -100);
list.add((byte) 40);
// list: [10, 5, -86, 3]
@ -51,35 +63,35 @@ public class Main {
list.size(); // returns: 4
// does the list contain the specified element?
list.contains(43); // returns: true
list.contains(4); // returns: false
// return a string of the form: [1, 84, 53]
list.toString(); // returns: "[8, 43, -86, 3]"
// return a copy of the list
list.copy(); // returns: [8, 43, -86, 3]
// remove all elements inside the list
list.clear();
// list: []
// allow to use for each loop on list
for (byte value : list) {
// do some stuff with value...
}
// list.contains(43); // returns: true
// list.contains(4); // returns: false
//
// // return a string of the form: [1, 84, 53]
// list.toString(); // returns: "[8, 43, -86, 3]"
//
// // return a copy of the list
// list.copy(); // returns: [8, 43, -86, 3]
//
//
// // remove all elements inside the list
// list.clear();
//
// // list: []
//
// // allow to use for each loop on list
// for (byte value : list) {
// // do some stuff with value...
// }
}
private static void testSort(ByteList list) {
ByteSort bubble = new BubbleSort();
ByteSort merge = new MergeSort();
ByteSort bogo = new BogoSort();
// sort the lists with the sorting algorithm
bubble.sort(list.copy());
merge.sort(list.copy());
bogo.sort(list.copy());
// ByteSort bogo = new BogoSort();
//
// // sort the lists with the sorting algorithm
bubble.sort(list);
merge.sort(list);
// bogo.sort(list.copy());
}
}

View File

@ -0,0 +1,69 @@
package de.vivi.list.list;
import java.util.Arrays;
import java.util.Iterator;
public class ArrayByteList implements ByteList {
private byte[] values;
public ArrayByteList() {
values = new byte[0];
}
@Override
public void add(byte value) {
add(size(), value);
}
@Override
public void add(int index, byte value) {
byte[] newValues = new byte[size() + 1];
System.arraycopy(values, 0, newValues, 0, index);
System.arraycopy(values, index, newValues, index + 1, size() - index);
newValues[index] = value;
values = newValues;
}
@Override
public void remove(int index) {
byte[] newValues = new byte[size() - 1];
System.arraycopy(values, 0, newValues, 0, index);
System.arraycopy(values, index + 1, newValues, index, size() - index - 1);
values = newValues;
}
@Override
public byte get(int index) {
return values[index];
}
@Override
public void set(int index, byte value) {
values[index] = value;
}
@Override
public void clear() {
values = new byte[0];
}
@Override
public int size() {
return values.length;
}
@Override
public Iterator<Byte> iterator() {
return new ByteListIterator(this);
}
@Override
public String toString() {
return Arrays.toString(values);
}
}

View File

@ -0,0 +1,18 @@
package de.vivi.list.list;
public interface ByteList extends Iterable<Byte> {
void add(byte value);
void add(int index, byte value);
void remove(int index);
byte get(int index);
void set(int index, byte value);
void clear();
int size();
}

View File

@ -0,0 +1,25 @@
package de.vivi.list.list;
import java.util.Iterator;
public class ByteListIterator implements Iterator<Byte> {
private int index;
private final ByteList list;
public ByteListIterator(ByteList list) {
this.index = 0;
this.list = list;
}
@Override
public boolean hasNext() {
return index < list.size();
}
@Override
public Byte next() {
return list.get(index++);
}
}

View File

@ -0,0 +1,149 @@
package de.vivi.list.list;
import java.util.Iterator;
public class LinkedByteList implements ByteList {
private static class Entry {
public byte value;
public Entry next;
}
private Entry root;
private int size;
public LinkedByteList() {
root = null;
size = 0;
}
@Override
public void add(byte value) {
add(size, value);
}
@Override
public void add(int index, byte value) {
if (index < 0) {
throw new IndexOutOfBoundsException();
}
size += 1;
// create the new entry
Entry entry = new Entry();
entry.value = value;
// check if we need to set the root
if (index == 0) {
entry.next = root;
root = entry;
return;
}
// the entry before the new entry
Entry prev = root;
// find the spot to insert the entry
while (--index > 0) {
prev = prev.next;
}
// insert the entry
entry.next = prev.next;
prev.next = entry;
}
@Override
public void remove(int index) {
if (index < 0) {
throw new IndexOutOfBoundsException();
}
size -= 1;
// check if we need to set the root
if (index == 0) {
root = root.next;
return;
}
// the entry before the removed entry
Entry prev = root;
// find the spot to remove the entry
while (--index > 0) {
prev = prev.next;
}
// remove the entry
prev.next = prev.next.next;
}
@Override
public byte get(int index) {
if (index < 0) {
throw new IndexOutOfBoundsException();
}
Entry entry = root;
while (index-- > 0) {
entry = entry.next;
}
return entry.value;
}
@Override
public void set(int index, byte value) {
if (index < 0) {
throw new IndexOutOfBoundsException();
}
Entry entry = root;
while (index-- > 0) {
entry = entry.next;
}
entry.value = value;
}
@Override
public void clear() {
root = null;
size = 0;
}
@Override
public int size() {
return size;
}
@Override
public Iterator<Byte> iterator() {
return new ByteListIterator(this);
}
@Override
public String toString() {
StringBuilder result = new StringBuilder("[");
Entry entry = root;
while (entry != null) {
if (entry != root) {
result.append(", ");
}
result.append(entry.value);
entry = entry.next;
}
return result.append("]").toString();
}
}

View File

@ -0,0 +1,25 @@
package de.vivi.list.sort;
import de.vivi.list.list.ByteList;
public class BubbleSort implements ByteSort {
@Override
public void sort(ByteList list) {
boolean swapped = true;
while (swapped) {
swapped = false;
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i) > list.get(i + 1)) {
swapped = true;
byte temp = list.get(i);
list.set(i, list.get(i + 1));
list.set(i + 1, temp);
}
}
}
}
}

View File

@ -0,0 +1,8 @@
package de.vivi.list.sort;
import de.vivi.list.list.ByteList;
public interface ByteSort {
void sort(ByteList list);
}

View File

@ -0,0 +1,60 @@
package de.vivi.list.sort;
import de.vivi.list.list.ArrayByteList;
import de.vivi.list.list.ByteList;
public class MergeSort implements ByteSort {
@Override
public void sort(ByteList list) {
if (list.size() <= 1) {
return;
}
ByteList left = new ArrayByteList();
ByteList right = new ArrayByteList();
split(list, left, right);
if (left.size() > 1) {
sort(left);
}
if (right.size() > 1) {
sort(right);
}
int leftIndex = 0;
int rightIndex = 0;
while (leftIndex < left.size() && rightIndex < right.size()) {
if (left.get(leftIndex) < right.get(rightIndex)) {
list.add(left.get(leftIndex++));
} else {
list.add(right.get(rightIndex++));
}
}
while (leftIndex < left.size()) {
list.add(left.get(leftIndex++));
}
while (rightIndex < right.size()) {
list.add(right.get(rightIndex++));
}
}
private void split(ByteList list, ByteList left, ByteList right) {
int half = list.size() >> 1; // divide by two
for (int i = 0; i < list.size(); i++) {
if (i < half) {
left.add(list.get(i));
} else {
right.add(list.get(i));
}
}
list.clear();
}
}