Markou is absolutely insane
This commit is contained in:
18
Semester 4/Assignments/DataStructures/nb-configuration.xml
Normal file
18
Semester 4/Assignments/DataStructures/nb-configuration.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project-shared-configuration>
|
||||
<!--
|
||||
This file contains additional configuration written by modules in the NetBeans IDE.
|
||||
The configuration is intended to be shared among all the users of project and
|
||||
therefore it is assumed to be part of version control checkout.
|
||||
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
|
||||
-->
|
||||
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
|
||||
<!--
|
||||
Properties that influence various parts of the IDE, especially code formatting and the like.
|
||||
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
|
||||
That way multiple projects can share the same settings (useful for formatting rules for example).
|
||||
Any value defined here will override the pom.xml file value but is only applicable to the current project.
|
||||
-->
|
||||
<netbeans.hint.jdkPlatform>JDK_1.8</netbeans.hint.jdkPlatform>
|
||||
</properties>
|
||||
</project-shared-configuration>
|
14
Semester 4/Assignments/DataStructures/pom.xml
Normal file
14
Semester 4/Assignments/DataStructures/pom.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?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>edu.slcc.asdv.caleb</groupId>
|
||||
<artifactId>DataStructures</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<exec.mainClass>edu.slcc.asdv.caleb.datastructures.DataStructures</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package edu.slcc.asdv.caleb.datastructures;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class DataStructures {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.datastructures;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public interface OneToMany<One, Many>
|
||||
{
|
||||
/**
|
||||
* Initialization of Ones. The method should be used first before any other
|
||||
* methods
|
||||
*
|
||||
* @param one - the ones ( i.e, countries, or SelectItem or any Object) to
|
||||
* use for initialization
|
||||
* @return true if the initialization succeeded by using the method once,
|
||||
* false when the method is used more than once.
|
||||
*/
|
||||
boolean initializeOne(One... one);
|
||||
|
||||
/**
|
||||
* Initialization of the many for a given one. The method can be used
|
||||
* multiple times after the method initializeOne has succeeded.
|
||||
*
|
||||
* @param one - the one that has the many
|
||||
* @param many - the many which belong to th eone
|
||||
* @throws IllegalArgumentException when the one does not exist (i.e. user's
|
||||
* typing error for the name of one) or when the initialization of the one
|
||||
* has not occurred.
|
||||
*
|
||||
*/
|
||||
void initializeMany(One one, Many... many)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
|
||||
/**
|
||||
* Gets the many of a specific one.
|
||||
*
|
||||
* @param one the one to get its many
|
||||
* @return the many of the parameter one or null if the one does not exist.
|
||||
*/
|
||||
Collection<Many> getMany(One one);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Given a value of the many it gets the one that the many belongs to.
|
||||
*
|
||||
* @param many one of the values of the many
|
||||
* @return the one
|
||||
*/
|
||||
One getOne(Many many);
|
||||
|
||||
/**
|
||||
* Gets a set with all the ones
|
||||
*
|
||||
* @return the set of ones.
|
||||
*/
|
||||
Set<One> getAllOnes();
|
||||
}
|
||||
|
@@ -0,0 +1,185 @@
|
||||
package edu.slcc.asdv.caleb.datastructures;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
public class OneToManyFactory {
|
||||
|
||||
/**
|
||||
* Creates an object of type OneToMany
|
||||
*
|
||||
* @param <One> a generic parameter can be any object that denotes a One.
|
||||
* @param <Many> a generic parameter can be any object that denotes a city that belongs to the one generic type.
|
||||
* @return a OneCity object.
|
||||
*/
|
||||
public static <One, Many> //generic types to be used in the method
|
||||
OneToMany<One, Many> //rturn type
|
||||
createOneToMany()
|
||||
{
|
||||
return new OneToMany<One, Many>() {
|
||||
private Map<One, Many> oneToMany = new HashMap();
|
||||
boolean oneInitialized = false;
|
||||
boolean manyInitialized = false;
|
||||
|
||||
@Override
|
||||
public boolean initializeOne(One... one)
|
||||
{
|
||||
if (oneInitialized == false && manyInitialized == false) {
|
||||
for (int i = 0; i < one.length; ++i) {
|
||||
oneToMany.put(one[i], (Many) new Boolean(true));
|
||||
}
|
||||
oneInitialized = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initializeMany(One one, Many... many)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
if (oneToMany.get(one) == null) { // of the key of the one is null
|
||||
// the method initializekey has not been used
|
||||
throw new IllegalArgumentException(one + " is not valid.");
|
||||
}
|
||||
oneToMany.put(one, (Many) new ArrayList<Many>(Arrays.asList(many)));
|
||||
manyInitialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Many> getMany(One one)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
if (oneInitialized == true && manyInitialized == true) {
|
||||
if (oneToMany.get(one) == null) {
|
||||
throw new IllegalArgumentException(one + " is not a valid One");
|
||||
}
|
||||
Collection c1 = (Collection) oneToMany.get(one);
|
||||
return c1;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public One getOne(Many many)
|
||||
{
|
||||
Set< Entry<One, Many>> set = oneToMany.entrySet();
|
||||
for (Map.Entry<One, Many> entry : oneToMany.entrySet()) {
|
||||
One key = (One) entry.getKey();
|
||||
Collection<Many> value = (Collection<Many>) oneToMany.get(key);
|
||||
if (value.contains(many)) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<One> getAllOnes()
|
||||
{
|
||||
return (Set<One>) oneToMany.keySet();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
|
||||
OneToMany cc = OneToManyFactory.createOneToMany();
|
||||
|
||||
try {
|
||||
cc.initializeMany("France", "Paris");
|
||||
} catch (Exception e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
boolean b1 = cc.initializeOne("USA", "Greece");
|
||||
System.out.println(b1);
|
||||
boolean b2 = cc.initializeOne("USA", "Greece");
|
||||
System.out.println(b2);
|
||||
cc.initializeMany("USA", "Lafayette", "New Orleans");
|
||||
cc.initializeMany("Greece", "Athens", "Sparta");
|
||||
Collection<String> cities1 = cc.getMany("USA");
|
||||
System.out.println(cities1);
|
||||
Collection<String> cities2 = cc.getMany("Greece");
|
||||
System.out.println(cities2);
|
||||
System.out.println(cc.getOne("Athens"));
|
||||
System.out.println(cc.getOne("Lafayette"));
|
||||
System.out.println(cc.getOne("France"));
|
||||
try {
|
||||
System.out.println(cc.getMany("Germany"));
|
||||
} catch (Exception e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
|
||||
System.out.println(cc.getAllOnes());
|
||||
System.out.println("----------------------------------------------------------");
|
||||
OneToMany supplierParts = OneToManyFactory.createOneToMany();
|
||||
supplierParts.initializeOne(new Supplier("s1"), new Supplier("s2"));
|
||||
Supplier s1 = new Supplier("s1");
|
||||
Supplier s2 = new Supplier("s2");
|
||||
supplierParts.initializeOne(s1, s2);
|
||||
Part p1 = new Part("p1");
|
||||
Part p2 = new Part("p2");
|
||||
Part p3 = new Part("p3");
|
||||
Part p4 = new Part("p4");
|
||||
supplierParts.initializeMany(s1, p1, p2);
|
||||
supplierParts.initializeMany(s2, p3, p4);
|
||||
}
|
||||
}
|
||||
class Supplier {
|
||||
|
||||
public Supplier(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Supplier{" + "name=" + name + '}';
|
||||
}
|
||||
}
|
||||
|
||||
class Part {
|
||||
|
||||
public Part(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Part{" + "name=" + name + '}';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user