From 5141ce8dc14fde9f3d630352d7fddb32e8309609 Mon Sep 17 00:00:00 2001 From: anon Date: Fri, 7 Jun 2024 11:46:13 +0200 Subject: [PATCH] progtech exam --- Java/jumpin/.unittest/my-app/pom.xml | 18 ++ .../src/main/java/com/mycompany/app/App.java | 13 ++ .../test/java/com/mycompany/app/AppTest.java | 38 ++++ Java/jumpin/.unittest/pom.xml | 24 +++ .../.unittest/src/main/java/MyClass.java | 5 + .../.unittest/src/test/java/MyClassTest.java | 13 ++ .../compile/default-compile/createdFiles.lst | 1 + .../compile/default-compile/inputFiles.lst | 1 + .../default-testCompile/createdFiles.lst | 1 + .../default-testCompile/inputFiles.lst | 1 + .../target/surefire-reports/MyClassTest.txt | 4 + .../surefire-reports/TEST-MyClassTest.xml | 58 ++++++ Java/jumpin/clean | 3 + Java/jumpin/my-app/pom.xml | 18 ++ .../src/main/java/com/mycompany/app/App.java | 13 ++ .../test/java/com/mycompany/app/AppTest.java | 38 ++++ Java/jumpin/pom.xml | 23 +++ Java/jumpin/src/main/java/Main.java | 131 ++++++++++++ Java/jumpin/src/main/java/Market.java | 195 ++++++++++++++++++ Java/jumpin/src/main/java/Seller.java | 141 +++++++++++++ Java/jumpin/src/main/java/SellerFactory.java | 52 +++++ Java/jumpin/src/main/java/SellerStrategy.java | 9 + .../src/main/java/StrategyCompromizing.java | 6 + Java/jumpin/src/main/java/StrategyGreedy.java | 13 ++ Java/jumpin/src/main/java/StrategySimple.java | 6 + Java/jumpin/src/test/java/MarketTests.java | 108 ++++++++++ .../compile/default-compile/createdFiles.lst | 8 + .../compile/default-compile/inputFiles.lst | 8 + .../default-testCompile/createdFiles.lst | 1 + .../default-testCompile/inputFiles.lst | 1 + .../target/surefire-reports/MarketTests.txt | 4 + .../surefire-reports/TEST-MarketTests.xml | 67 ++++++ Java/jumpin/txt.txt | 58 ++++++ 33 files changed, 1080 insertions(+) create mode 100644 Java/jumpin/.unittest/my-app/pom.xml create mode 100644 Java/jumpin/.unittest/my-app/src/main/java/com/mycompany/app/App.java create mode 100644 Java/jumpin/.unittest/my-app/src/test/java/com/mycompany/app/AppTest.java create mode 100644 Java/jumpin/.unittest/pom.xml create mode 100644 Java/jumpin/.unittest/src/main/java/MyClass.java create mode 100644 Java/jumpin/.unittest/src/test/java/MyClassTest.java create mode 100644 Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst create mode 100644 Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst create mode 100644 Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst create mode 100644 Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst create mode 100644 Java/jumpin/.unittest/target/surefire-reports/MyClassTest.txt create mode 100644 Java/jumpin/.unittest/target/surefire-reports/TEST-MyClassTest.xml create mode 100755 Java/jumpin/clean create mode 100644 Java/jumpin/my-app/pom.xml create mode 100644 Java/jumpin/my-app/src/main/java/com/mycompany/app/App.java create mode 100644 Java/jumpin/my-app/src/test/java/com/mycompany/app/AppTest.java create mode 100644 Java/jumpin/pom.xml create mode 100644 Java/jumpin/src/main/java/Main.java create mode 100644 Java/jumpin/src/main/java/Market.java create mode 100644 Java/jumpin/src/main/java/Seller.java create mode 100644 Java/jumpin/src/main/java/SellerFactory.java create mode 100644 Java/jumpin/src/main/java/SellerStrategy.java create mode 100644 Java/jumpin/src/main/java/StrategyCompromizing.java create mode 100644 Java/jumpin/src/main/java/StrategyGreedy.java create mode 100644 Java/jumpin/src/main/java/StrategySimple.java create mode 100644 Java/jumpin/src/test/java/MarketTests.java create mode 100644 Java/jumpin/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst create mode 100644 Java/jumpin/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst create mode 100644 Java/jumpin/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst create mode 100644 Java/jumpin/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst create mode 100644 Java/jumpin/target/surefire-reports/MarketTests.txt create mode 100644 Java/jumpin/target/surefire-reports/TEST-MarketTests.xml create mode 100644 Java/jumpin/txt.txt diff --git a/Java/jumpin/.unittest/my-app/pom.xml b/Java/jumpin/.unittest/my-app/pom.xml new file mode 100644 index 0000000..88b3fe2 --- /dev/null +++ b/Java/jumpin/.unittest/my-app/pom.xml @@ -0,0 +1,18 @@ + + 4.0.0 + com.mycompany.app + my-app + jar + 1.0-SNAPSHOT + my-app + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + diff --git a/Java/jumpin/.unittest/my-app/src/main/java/com/mycompany/app/App.java b/Java/jumpin/.unittest/my-app/src/main/java/com/mycompany/app/App.java new file mode 100644 index 0000000..77cf3e0 --- /dev/null +++ b/Java/jumpin/.unittest/my-app/src/main/java/com/mycompany/app/App.java @@ -0,0 +1,13 @@ +package com.mycompany.app; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/Java/jumpin/.unittest/my-app/src/test/java/com/mycompany/app/AppTest.java b/Java/jumpin/.unittest/my-app/src/test/java/com/mycompany/app/AppTest.java new file mode 100644 index 0000000..3355990 --- /dev/null +++ b/Java/jumpin/.unittest/my-app/src/test/java/com/mycompany/app/AppTest.java @@ -0,0 +1,38 @@ +package com.mycompany.app; + +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 ); + } +} diff --git a/Java/jumpin/.unittest/pom.xml b/Java/jumpin/.unittest/pom.xml new file mode 100644 index 0000000..022b1d0 --- /dev/null +++ b/Java/jumpin/.unittest/pom.xml @@ -0,0 +1,24 @@ + + 4.0.0 + + com.example + my-maven-project + 1.0-SNAPSHOT + jar + + My Maven Project + http://maven.apache.org + + + + + junit + junit + 4.13.2 + test + + + diff --git a/Java/jumpin/.unittest/src/main/java/MyClass.java b/Java/jumpin/.unittest/src/main/java/MyClass.java new file mode 100644 index 0000000..f3ac522 --- /dev/null +++ b/Java/jumpin/.unittest/src/main/java/MyClass.java @@ -0,0 +1,5 @@ +public class MyClass { + public int add(int a, int b) { + return a + b; + } +} diff --git a/Java/jumpin/.unittest/src/test/java/MyClassTest.java b/Java/jumpin/.unittest/src/test/java/MyClassTest.java new file mode 100644 index 0000000..4fdb169 --- /dev/null +++ b/Java/jumpin/.unittest/src/test/java/MyClassTest.java @@ -0,0 +1,13 @@ +// @BAKE java MyClassTest.java +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class MyClassTest { + + @Test + public void testAdd() { + MyClass myClass = new MyClass(); + int result = myClass.add(2, 3); + assertEquals("Sum should be 5", 5, result); + } +} diff --git a/Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..27d78e7 --- /dev/null +++ b/Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1 @@ +MyClass.class diff --git a/Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..08bf89f --- /dev/null +++ b/Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1 @@ +/home/anon/Swap/school/patterns/jumpin/unittest/src/main/java/MyClass.java diff --git a/Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst b/Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst new file mode 100644 index 0000000..15411f0 --- /dev/null +++ b/Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst @@ -0,0 +1 @@ +MyClassTest.class diff --git a/Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst b/Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst new file mode 100644 index 0000000..3e7c4ab --- /dev/null +++ b/Java/jumpin/.unittest/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst @@ -0,0 +1 @@ +/home/anon/Swap/school/patterns/jumpin/unittest/src/test/java/MyClassTest.java diff --git a/Java/jumpin/.unittest/target/surefire-reports/MyClassTest.txt b/Java/jumpin/.unittest/target/surefire-reports/MyClassTest.txt new file mode 100644 index 0000000..afe0746 --- /dev/null +++ b/Java/jumpin/.unittest/target/surefire-reports/MyClassTest.txt @@ -0,0 +1,4 @@ +------------------------------------------------------------------------------- +Test set: MyClassTest +------------------------------------------------------------------------------- +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.046 s -- in MyClassTest diff --git a/Java/jumpin/.unittest/target/surefire-reports/TEST-MyClassTest.xml b/Java/jumpin/.unittest/target/surefire-reports/TEST-MyClassTest.xml new file mode 100644 index 0000000..5f827c8 --- /dev/null +++ b/Java/jumpin/.unittest/target/surefire-reports/TEST-MyClassTest.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Java/jumpin/clean b/Java/jumpin/clean new file mode 100755 index 0000000..4f850d5 --- /dev/null +++ b/Java/jumpin/clean @@ -0,0 +1,3 @@ +#!/bin/bash + +rm *.class diff --git a/Java/jumpin/my-app/pom.xml b/Java/jumpin/my-app/pom.xml new file mode 100644 index 0000000..88b3fe2 --- /dev/null +++ b/Java/jumpin/my-app/pom.xml @@ -0,0 +1,18 @@ + + 4.0.0 + com.mycompany.app + my-app + jar + 1.0-SNAPSHOT + my-app + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + diff --git a/Java/jumpin/my-app/src/main/java/com/mycompany/app/App.java b/Java/jumpin/my-app/src/main/java/com/mycompany/app/App.java new file mode 100644 index 0000000..77cf3e0 --- /dev/null +++ b/Java/jumpin/my-app/src/main/java/com/mycompany/app/App.java @@ -0,0 +1,13 @@ +package com.mycompany.app; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/Java/jumpin/my-app/src/test/java/com/mycompany/app/AppTest.java b/Java/jumpin/my-app/src/test/java/com/mycompany/app/AppTest.java new file mode 100644 index 0000000..3355990 --- /dev/null +++ b/Java/jumpin/my-app/src/test/java/com/mycompany/app/AppTest.java @@ -0,0 +1,38 @@ +package com.mycompany.app; + +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 ); + } +} diff --git a/Java/jumpin/pom.xml b/Java/jumpin/pom.xml new file mode 100644 index 0000000..3388147 --- /dev/null +++ b/Java/jumpin/pom.xml @@ -0,0 +1,23 @@ + + 4.0.0 + + com.example + my-maven-project + 1.0-SNAPSHOT + jar + + My Maven Project + http://maven.apache.org + + + + + junit + junit + 4.13.2 + test + + + diff --git a/Java/jumpin/src/main/java/Main.java b/Java/jumpin/src/main/java/Main.java new file mode 100644 index 0000000..08200a4 --- /dev/null +++ b/Java/jumpin/src/main/java/Main.java @@ -0,0 +1,131 @@ +// @BAKE javac -Xlint *.java + +import java.util.*; + +//public class Main { +// public static void main(String[] args) { +// return; +// } +//} + +//public class Main { +// public static void main(String[] args) { +// Market m = new Market(); +// } +//} + +//public class Main { +// public static void main(String[] args) { +// Market m = new Market(); +// boolean r = m.buyFrom(0, "apple", 100, 1.0); +// } +//} + +//public class Main { +// public static void main(String[] args) { +// int prefered_buy_price = 100; +// double prefered_buy_amount = 1.0; +// String prefered_buy_subject = "apple"; +// +// Market m = new Market(); +// boolean buy_success = m.buyFrom(0, prefered_buy_subject, prefered_buy_price, prefered_buy_amount); +// if (buy_success) { +// System.out.println("Successfully bought " +// + prefered_buy_amount + " kg " +// + prefered_buy_subject + "s for " +// + prefered_buy_price); +// } else { +// System.out.println("Buying failed."); +// } +// } +//} + +//public class Main { +// public static void main(String[] args) { +// int prefered_buy_price = 100; +// double prefered_buy_amount = 10.5; +// String prefered_buy_subject = "apple"; +// +// Market m = new Market(); +// boolean buy_success = m.buyFrom(0, prefered_buy_subject, prefered_buy_price, prefered_buy_amount); +// if (buy_success) { +// System.out.println("Successfully bought " +// + prefered_buy_amount + " kg " +// + prefered_buy_subject + "s for " +// + prefered_buy_price); +// } else { +// System.out.println("Buying failed."); +// } +// } +//} + +//public class Main { +// public static void main(String[] args) { +// int prefered_buy_price = 100; +// double prefered_buy_amount = 5.2; +// String prefered_buy_subject = "apple"; +// +// Market m = new Market(); +// boolean buy_success = m.buyFrom(0, prefered_buy_subject, prefered_buy_price, prefered_buy_amount); +// if (buy_success) { +// System.out.println("Successfully bought " +// + prefered_buy_amount + " kg " +// + prefered_buy_subject + "s for " +// + prefered_buy_price); +// } else { +// System.out.println("Buying failed."); +// } +// } +//} + +//public class Main { +// public static void main(String[] args) { +// int prefered_buy_price = 100; +// double prefered_buy_amount = 5.2; +// String prefered_buy_subject = "apple"; +// +// Market nagybaniPiac = new Market(); +// boolean buy_success = nagybaniPiac.buyFrom(0, prefered_buy_subject, prefered_buy_price, prefered_buy_amount); +// if (buy_success) { +// System.out.println("Successfully bought " +// + prefered_buy_amount + " kg " +// + prefered_buy_subject + "s for " +// + prefered_buy_price); +// } else { +// System.out.println("Buying failed."); +// } +// } +//} + +public class Main { + public static void main(String[] args) { + Random randy = new Random(); + + int prefered_buy_price = 100; + double prefered_buy_amount = 5.2; + String prefered_buy_subject = "apple"; + + Market nagybaniPiac = new Market(); + int c = nagybaniPiac.getSellerCount(); + if (c == 0) { + System.out.println("The market is empty."); + return; + } + + int sellerIndex = randy.nextInt(c); + + boolean buy_success = nagybaniPiac.buyFrom(sellerIndex, + prefered_buy_subject, + prefered_buy_price, + prefered_buy_amount + ); + if (buy_success) { + System.out.println("Successfully bought " + + prefered_buy_amount + " kg " + + prefered_buy_subject + "s for " + + prefered_buy_price); + } else { + System.out.println("Buying failed."); + } + } +} diff --git a/Java/jumpin/src/main/java/Market.java b/Java/jumpin/src/main/java/Market.java new file mode 100644 index 0000000..3a21d51 --- /dev/null +++ b/Java/jumpin/src/main/java/Market.java @@ -0,0 +1,195 @@ +import java.util.*; + +//public class Market { +// List sellers = new ArrayList<>(); +// +// public Market() { +// sellers.add(new Seller()); +// } +//} + +//public class Market { +// List sellers = new ArrayList<>(); +// +// public Market() { +// sellers.add(new Seller()); +// } +// +// public boolean buyFrom(int seller, String what, int for_price, double amount) { +// int i = sellers.get(seller).buy(what, for_price, amount); +// return true; +// } +//} + +//public class Market { +// List sellers = new ArrayList<>(); +// +// public Market() { +// sellers.add(new Seller()); +// } +// +// public boolean buyFrom(int seller, String what, int for_price, double amount) { +// int i = sellers.get(seller).buy(what, for_price, amount); +// if (for_price >= i) { +// return true; +// } else { +// return false; +// } +// } +//} + +//public class Market { +// List sellers = new ArrayList<>(); +// +// public Market() { +// SellerFactory f = new SellerFactory(); +// HashMap m = new HashMap(); +// +// m.put("apple", 15); +// m.put("pear", 20); +// +// for (int i = 0; i < 10; i++) { +// try { +// sellers.add(f.makeSeller(m)); +// } catch (Exception e) { +// System.out.println("Making a seller went wrong"); +// } +// } +// } +// +// public boolean buyFrom(int seller, String what, int for_price, double amount) { +// int i = sellers.get(seller).buy(what, for_price, amount); +// if (for_price >= i) { +// return true; +// } else { +// return false; +// } +// } +//} + +//public class Market { +// List sellers = new ArrayList<>(); +// +// public Market() { +// SellerFactory f = new SellerFactory(); +// Random rand = new Random(); +// +// try { +// for (int i = 0; i < 3; i++) { +// HashMap m = new HashMap(); +// m.put("apple", rand.nextInt(21) + 10); +// m.put("pear", rand.nextInt(21) + 10); +// sellers.add(f.makeSeller(m)); +// } +// +// if (true) { +// HashMap m = new HashMap(); +// +// m.put("apple", rand.nextInt(11) + 10); +// m.put("pear", rand.nextInt(11) + 10); +// sellers.add(f.makeSeller(m, "greedy")); +// +// m.put("apple", rand.nextInt(25) + 10); +// m.put("pear", rand.nextInt(25) + 10); +// sellers.add(f.makeSeller(m, "compromizing")); +// } +// } catch (Exception e) { +// System.out.println("Making a seller went wrong"); +// } +// } +// +// public boolean buyFrom(int seller, String what, int for_price, double amount) { +// int i = sellers.get(seller).buy(what, for_price, amount); +// if (for_price >= i) { +// return true; +// } else { +// return false; +// } +// } +//} + +//public class Market { +// private List sellers = new ArrayList<>(); +// +// public Market() { +// SellerFactory f = new SellerFactory(); +// Random rand = new Random(); +// +// try { +// for (int i = 0; i < 3; i++) { +// HashMap m = new HashMap(); +// m.put("apple", rand.nextInt(21) + 10); +// m.put("pear", rand.nextInt(21) + 10); +// sellers.add(f.makeSeller(m)); +// } +// +// if (true) { +// HashMap m = new HashMap(); +// +// m.put("apple", rand.nextInt(11) + 10); +// m.put("pear", rand.nextInt(11) + 10); +// sellers.add(f.makeSeller(m, "greedy")); +// +// m.put("apple", rand.nextInt(25) + 10); +// m.put("pear", rand.nextInt(25) + 10); +// sellers.add(f.makeSeller(m, "compromizing")); +// } +// } catch (Exception e) { +// System.out.println("Making a seller went wrong"); +// } +// } +// +// public int getSellerCount() { +// return 0; +// } +// +// public boolean buyFrom(int seller, String what, int for_price, double amount) { +// int i = sellers.get(seller).buy(what, for_price, amount); +// if (for_price >= i) { +// return true; +// } else { +// return false; +// } +// } +//} + +public class Market { + private List sellers = new ArrayList<>(); + + public Market() { + SellerFactory f = new SellerFactory(); + Random randy = new Random(); + + try { + for (int i = 0; i < 3; i++) { + HashMap m = new HashMap(); + m.put("apple", randy.nextInt(21) + 10); + m.put("pear", randy.nextInt(21) + 10); + sellers.add(f.makeSeller(m)); + } + + if (true) { + HashMap m = new HashMap(); + + m.put("apple", randy.nextInt(11) + 10); + m.put("pear", randy.nextInt(11) + 10); + sellers.add(f.makeSeller(m, "greedy")); + + m.put("apple", randy.nextInt(25) + 10); + m.put("pear", randy.nextInt(25) + 10); + sellers.add(f.makeSeller(m, "compromizing")); + } + } catch (Exception e) { + System.out.println("Making a seller went wrong"); + } + } + + public int getSellerCount() { + return sellers.size(); + } + + public boolean buyFrom(int seller, String what, int for_price, double amount) { + int i = sellers.get(seller).buy(what, for_price, amount); + return for_price >= i; + } +} diff --git a/Java/jumpin/src/main/java/Seller.java b/Java/jumpin/src/main/java/Seller.java new file mode 100644 index 0000000..6cc7b1b --- /dev/null +++ b/Java/jumpin/src/main/java/Seller.java @@ -0,0 +1,141 @@ +import java.util.*; + +//public class Seller { +// public int buy(String what, int for_price, float amount) { +// return 0; +// } +//} + +//public class Seller { +// public int buy(String what, int for_price, double amount) { +// return 0; +// } +//} + +//public class Seller { +// HashMap price_table = new HashMap<>(); +// +// public Seller() { +// price_table.put("apple", 15); +// price_table.put("pear", 20); +// } +// +// public int buy(String what, int for_price, double amount) { +// int p = price_table.get(what); +// return (int)(p * amount); +// } +//} + +//public class Seller { +// HashMap price_table = new HashMap<>(); +// SellerStrategy strategy = new SellerStrategy(); +// +// public Seller() { +// price_table.put("apple", 15); +// price_table.put("pear", 20); +// } +// +// public int buy(String what, int for_price, double amount) { +// int p = price_table.get(what); +// return (int)(p * amount); +// } +//} + +//public class Seller { +// HashMap price_table = new HashMap<>(); +// SellerStrategy strategy = new SellerStrategy(); +// +// public Seller() { +// price_table.put("apple", 15); +// price_table.put("pear", 20); +// } +// +// public int buy(String what, int for_price, double amount) { +// int p = price_table.get(what); +// return strategy((int)(p * amount), for_price); +// } +//} + +//public class Seller { +// HashMap price_table = new HashMap<>(); +// SellerStrategy strategy = new Strategy1(); +// +// public Seller() { +// price_table.put("apple", 15); +// price_table.put("pear", 20); +// } +// +// public int buy(String what, int for_price, double amount) { +// int p = price_table.get(what); +// return strategy((int)(p * amount), for_price); +// } +//} + +//public class Seller { +// HashMap price_table = new HashMap<>(); +// SellerStrategy strategy; +// +// public Seller(SellerStrategy strategy_) { +// strategy = strategy_; +// price_table.put("apple", 15); +// price_table.put("pear", 20); +// } +// +// public int buy(String what, int for_price, double amount) { +// int p = price_table.get(what); +// return strategy.strategize((int)(p * amount), for_price); +// } +//} + +//public class Seller { +// HashMap priceTable = new HashMap<>(); +// SellerStrategy strategy; +// +// public Seller(SellerStrategy strategy_, HashMap priceTable_) throws IllegalArgumentException { +// if (priceTable == null) { +// throw new IllegalArgumentException("Table cannot be null."); +// } +// strategy = strategy_; +// priceTable = priceTable_; +// } +// +// public int buy(String what, int for_price, double amount) throws IllegalArgumentException { +// Integer p = priceTable.get(what); +// if (p == null) { +// throw new IllegalArgumentException("No such item for sale."); +// } +// return strategy.strategize((int)(p * amount), for_price); +// } +//} + +public class Seller { + HashMap priceTable; + SellerStrategy strategy; + + public Seller() { + strategy = new StrategySimple(); + priceTable = new HashMap(); + priceTable.put("apple", 15); + priceTable.put("pear", 20); + } + + public Seller(SellerStrategy strategy_, HashMap priceTable_) throws IllegalArgumentException { + if (strategy_ == null) { + throw new IllegalArgumentException("Strategy must not be null"); + } + strategy = strategy_; + + if (priceTable_ == null) { + throw new IllegalArgumentException("Table cannot be null."); + } + priceTable = priceTable_; + } + + public int buy(String what, int for_price, double amount) throws IllegalArgumentException { + Integer p = priceTable.get(what); + if (p == null) { + throw new IllegalArgumentException("No such item for sale."); + } + return strategy.strategize((int)(p * amount), for_price); + } +} diff --git a/Java/jumpin/src/main/java/SellerFactory.java b/Java/jumpin/src/main/java/SellerFactory.java new file mode 100644 index 0000000..f714da9 --- /dev/null +++ b/Java/jumpin/src/main/java/SellerFactory.java @@ -0,0 +1,52 @@ +import java.util.*; + +//public class SellerFactory { +// Seller makeSeller() { +// return new Seller(); +// } +//} + +//public class SellerFactory { +// Seller makeSeller() { +// return new Seller(new Strategy1()); +// } +// +// Seller makeSeller(String type) { +// return new Seller(new Strategy1()); +// } +//} + +//public class SellerFactory { +// Seller makeSeller() { +// return new Seller(new Strategy1()); +// } +// +// Seller makeSeller(String type) { +// if (type == "compromizing") { +// return new Seller(new Strategy2()); +// } else if (type == "greedy") { +// return new Seller(new Strategy3()); +// } +// return new Seller(new Strategy1()); +// } +//} + +public class SellerFactory { + Seller makeSeller(HashMap priceTable_) { + return new Seller(new StrategySimple(), priceTable_); + } + + Seller makeSeller(HashMap priceTable_, String type) { + if (priceTable_ == null) { + priceTable_ = new HashMap(); + } + + if (type == "compromizing") { + return new Seller(new StrategyCompromizing(), priceTable_); + } else if (type == "greedy") { + return new Seller(new StrategyGreedy(), priceTable_); + } + return new Seller(new StrategySimple(), priceTable_); + } +} + diff --git a/Java/jumpin/src/main/java/SellerStrategy.java b/Java/jumpin/src/main/java/SellerStrategy.java new file mode 100644 index 0000000..f8b6215 --- /dev/null +++ b/Java/jumpin/src/main/java/SellerStrategy.java @@ -0,0 +1,9 @@ +//public class SellerStrategy { +// public int strategize(int prefered, int actual) { +// return prefered; +// } +//} + +public abstract class SellerStrategy { + public abstract int strategize(int prefered, int actual); +} diff --git a/Java/jumpin/src/main/java/StrategyCompromizing.java b/Java/jumpin/src/main/java/StrategyCompromizing.java new file mode 100644 index 0000000..35a632a --- /dev/null +++ b/Java/jumpin/src/main/java/StrategyCompromizing.java @@ -0,0 +1,6 @@ +public class StrategyCompromizing extends SellerStrategy { + @Override + public int strategize(int prefered, int actual) { + return (prefered + actual) / 2; + } +} diff --git a/Java/jumpin/src/main/java/StrategyGreedy.java b/Java/jumpin/src/main/java/StrategyGreedy.java new file mode 100644 index 0000000..cd7c184 --- /dev/null +++ b/Java/jumpin/src/main/java/StrategyGreedy.java @@ -0,0 +1,13 @@ +//public class Strategy3 extends SellerStrategy { +// @Override +// public int strategize(int prefered, int actual) { +// return prefered * 2; +// } +//} + +public class StrategyGreedy extends SellerStrategy { + @Override + public int strategize(int prefered, int actual) { + return prefered * 2; + } +} diff --git a/Java/jumpin/src/main/java/StrategySimple.java b/Java/jumpin/src/main/java/StrategySimple.java new file mode 100644 index 0000000..38350e0 --- /dev/null +++ b/Java/jumpin/src/main/java/StrategySimple.java @@ -0,0 +1,6 @@ +public class StrategySimple extends SellerStrategy { + @Override + public int strategize(int prefered, int actual) { + return prefered; + } +} diff --git a/Java/jumpin/src/test/java/MarketTests.java b/Java/jumpin/src/test/java/MarketTests.java new file mode 100644 index 0000000..e69e15a --- /dev/null +++ b/Java/jumpin/src/test/java/MarketTests.java @@ -0,0 +1,108 @@ +import java.util.*; +import org.junit.Test; +import static org.junit.Assert.*; + +public class MarketTests { + @Test + public void testTest() { + return; + } + + @Test + public void newSellers() throws Exception { + Seller s; + + s = new Seller(); + + HashMap m = new HashMap(); + m.put("apple", 15); + m.put("pear", 20); + + s = new Seller(new StrategyGreedy(), m); + s = new Seller(new StrategyCompromizing(), m); + } + + @Test + public void invalidSeller() { + HashMap m = new HashMap(); + + try { + Seller s = new Seller(null, m); + fail("Invalid seller can be created."); + } catch (Exception e) { + // ; + } + + try { + Seller s = new Seller(new StrategySimple(), null); + fail("Invalid seller can be created."); + } catch (Exception e) { + // ; + } + + try { + Seller s = new Seller(null, null); + fail("Invalid seller can be created."); + } catch (Exception e) { + // ; + } + } + + @Test + public void newMarget() { + Market m = new Market(); + assertNotNull(m); + } + + @Test + public void newBasicBuy() throws Exception { + Market m = new Market(); + boolean r = m.buyFrom(0, "apple", 100, 1.0); + } + + @Test + public void invalidSellerSelected() { + Market m = new Market(); + try { + boolean r = m.buyFrom(-1, "apple", 100, 1.0); + fail("Did not throw on invalid index!"); + } catch (Exception e) { + return; + } + } + + @Test + public void mustSellSimple() { + HashMap m = new HashMap(); + m.put("apple", 10); + + Seller s = new Seller(new StrategySimple(), m); + + assertTrue(s.buy("apple", 100, 1.0) <= 100); + } + + @Test + public void mustNotSellGreedy() { + HashMap m = new HashMap(); + m.put("apple", 10); + + Seller s = new Seller(new StrategyGreedy(), m); + assertFalse(s.buy("apple", 10, 1.0) <= 10); + } + + @Test + public void mustNotSellCompromizing() { + HashMap m = new HashMap(); + m.put("apple", 10); + + Seller s = new Seller(new StrategyCompromizing(), m); + assertFalse(s.buy("apple", 10, 1.0) < 10); + } + + @Test + public void SellerFactoryMethod() { + SellerFactory f = new SellerFactory(); + Seller s = f.makeSeller(null, ""); + assertTrue(s instanceof Seller); + } +} diff --git a/Java/jumpin/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/Java/jumpin/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..43d239f --- /dev/null +++ b/Java/jumpin/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1,8 @@ +Main.class +StrategyCompromizing.class +Market.class +SellerStrategy.class +Seller.class +StrategySimple.class +StrategyGreedy.class +SellerFactory.class diff --git a/Java/jumpin/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/Java/jumpin/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..6e5ad11 --- /dev/null +++ b/Java/jumpin/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,8 @@ +/home/anon/Swap/school/patterns/jumpin/src/main/java/SellerStrategy.java +/home/anon/Swap/school/patterns/jumpin/src/main/java/StrategyCompromizing.java +/home/anon/Swap/school/patterns/jumpin/src/main/java/StrategyGreedy.java +/home/anon/Swap/school/patterns/jumpin/src/main/java/StrategySimple.java +/home/anon/Swap/school/patterns/jumpin/src/main/java/SellerFactory.java +/home/anon/Swap/school/patterns/jumpin/src/main/java/Seller.java +/home/anon/Swap/school/patterns/jumpin/src/main/java/Market.java +/home/anon/Swap/school/patterns/jumpin/src/main/java/Main.java diff --git a/Java/jumpin/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst b/Java/jumpin/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst new file mode 100644 index 0000000..0d955ff --- /dev/null +++ b/Java/jumpin/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst @@ -0,0 +1 @@ +MarketTests.class diff --git a/Java/jumpin/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst b/Java/jumpin/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst new file mode 100644 index 0000000..bc201fc --- /dev/null +++ b/Java/jumpin/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst @@ -0,0 +1 @@ +/home/anon/Swap/school/patterns/jumpin/src/test/java/MarketTests.java diff --git a/Java/jumpin/target/surefire-reports/MarketTests.txt b/Java/jumpin/target/surefire-reports/MarketTests.txt new file mode 100644 index 0000000..f82571e --- /dev/null +++ b/Java/jumpin/target/surefire-reports/MarketTests.txt @@ -0,0 +1,4 @@ +------------------------------------------------------------------------------- +Test set: MarketTests +------------------------------------------------------------------------------- +Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.071 s -- in MarketTests diff --git a/Java/jumpin/target/surefire-reports/TEST-MarketTests.xml b/Java/jumpin/target/surefire-reports/TEST-MarketTests.xml new file mode 100644 index 0000000..39338ec --- /dev/null +++ b/Java/jumpin/target/surefire-reports/TEST-MarketTests.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Java/jumpin/txt.txt b/Java/jumpin/txt.txt new file mode 100644 index 0000000..54e377f --- /dev/null +++ b/Java/jumpin/txt.txt @@ -0,0 +1,58 @@ +>TTD + +### nagybani piac program: ### + >kontainer osztály (eladó lista) + >alma & körte árulás + >mást is lehessen árulni + -eladók: + >más árak + >alma & körte + >alku + >miből hány kiló + >mennyiért + -vásárlás(mi, mennyi, mennyiért) -> visszatérési ár: kereskedő válasza + if mennyi == r -> siker + >más alku stratégiák + >előző verziók megjegyzésbe + +### Minták kidolgozásra ### + >stratégia + >prototype + >szabad + +Stratégia: + >a stratégia viselkedés mint adat + >OOP-ben mint osztály + >alakalmazásával elkerülthető az alfaj robbanás + >segithet elkerültni mocskos if-else blockokat + >használata triviális, az új osztáj hierarchia szerepe néhány metódus + implementágása + >intuitív, álltalában akkor nem kerül alkalmazásra a viselkedés + complexitás mögött megbújik + >további előnye, hogy a viselkedés módosíthatóvá tehető a példány élet + ideje alatt + { + kacsák + van egy a beugróban + self.(menü példa) + } + +Prototype: + >(túl egyszerű) + >ha egy példány létrehozás kölcséges akkor lehet, + hogy a clónozása olcsóbb + >álltalában egy cloneable interface-el és egy clone() metódussal + implementálják + >új példányokat létezőkből hozunk létre + >létező példányok le "cache"-elése lehetséges + +Factory: + >polimorfikus alosztály generálás központja + >elhárít töbször ismételt if-else logikát + >a factory felhasználója decoupling-re kerül az alosztályoktól + >paraméterezéssel történi a megfelelő alosztály kiválasztása + >lehet metódus alalapon implementálva ahol a class *Factory egy metódusa + adja vissza a megfelelő osztályt (ez lehet statukus /* vagy szabad*/) + >segít a "ne használjuk new kulcsszó" tanács alkalmazását + >lehet több párhuzamos class hierarchiát egy abstract Factory osztályal + egy interface-re hozni { chicago/(new york) pizza gyáral}