From 4fc3b7661fa37f23cdf161bd3b255bce321a70a5 Mon Sep 17 00:00:00 2001 From: marcin Date: Sun, 14 Apr 2019 13:42:06 +0200 Subject: [PATCH] added java2 --- Java/zad2_1/Koszyk.java | 29 +++++++++++++++++++++++++++++ Java/zad2_1/Produkt.java | 17 +++++++++++++++++ Java/zad2_1/zad2_1.java | 11 +++++++++++ Java/zad3_1/FileLogger.java | 18 ++++++++++++++++++ Java/zad3_1/Logger.java | 7 +++++++ Java/zad3_1/StdoutLogger.java | 8 ++++++++ Java/zad3_1/zad3_1.java | 20 ++++++++++++++++++++ 7 files changed, 110 insertions(+) create mode 100644 Java/zad2_1/Koszyk.java create mode 100644 Java/zad2_1/Produkt.java create mode 100644 Java/zad2_1/zad2_1.java create mode 100644 Java/zad3_1/FileLogger.java create mode 100644 Java/zad3_1/Logger.java create mode 100644 Java/zad3_1/StdoutLogger.java create mode 100644 Java/zad3_1/zad3_1.java diff --git a/Java/zad2_1/Koszyk.java b/Java/zad2_1/Koszyk.java new file mode 100644 index 0000000..da2216b --- /dev/null +++ b/Java/zad2_1/Koszyk.java @@ -0,0 +1,29 @@ +package POB_2019.Java.zad2_1; + +import java.util.List; +import java.util.ArrayList; + +public class Koszyk { + private List products = new ArrayList(); + private String name; + + public Koszyk(String _name){ + this.name=_name; + } + public void addProduct(Produkt product){ + products.add(product); + } + private float totalCost(){ + float sum=0; + for(Produkt p : products) + sum+=p.getPrice(); + return sum; + } + public String toString(){ + String fullDesc="Koszyk " + this.name + ":\n"; + for(Produkt p : products) + fullDesc = fullDesc + p.toString(); + fullDesc = fullDesc + "Total: " + totalCost() + "\n"; + return fullDesc; + } +} \ No newline at end of file diff --git a/Java/zad2_1/Produkt.java b/Java/zad2_1/Produkt.java new file mode 100644 index 0000000..8df6e7e --- /dev/null +++ b/Java/zad2_1/Produkt.java @@ -0,0 +1,17 @@ +package POB_2019.Java.zad2_1; + +public class Produkt { + private String name; + private float price; + + public Produkt(String _name, float _price){ + this.name=_name; + this.price=_price; + } + public String toString(){ + return String.format("Nazwa: %s\nCena: %.2f\n\n", this.name, this.price); + } + public float getPrice(){ + return this.price; + } +} \ No newline at end of file diff --git a/Java/zad2_1/zad2_1.java b/Java/zad2_1/zad2_1.java new file mode 100644 index 0000000..6b6c065 --- /dev/null +++ b/Java/zad2_1/zad2_1.java @@ -0,0 +1,11 @@ +package POB_2019.Java.zad2_1; + +public class zad2_1 { + public static void main(String[] args){ + Koszyk cart1 = new Koszyk("Marcin"); + cart1.addProduct(new Produkt("Banan", 3.19f)); + cart1.addProduct(new Produkt("Ananas", 5.69f)); + cart1.addProduct(new Produkt("PomaraƄcza", 2.99f)); + System.out.println(cart1); + } +} \ No newline at end of file diff --git a/Java/zad3_1/FileLogger.java b/Java/zad3_1/FileLogger.java new file mode 100644 index 0000000..e036881 --- /dev/null +++ b/Java/zad3_1/FileLogger.java @@ -0,0 +1,18 @@ +package POB_2019.Java.zad3_1; + +import java.io.FileWriter; +import java.io.IOException; + +public class FileLogger extends Logger { + private FileWriter fileWriter; + public FileLogger() throws IOException { + String fileName = "./log.txt"; + this.fileWriter = new FileWriter(fileName); + } + @Override + public void log(String textToLog) throws IOException { + this.fileWriter.append(textToLog); + this.fileWriter.append("\n"); + fileWriter.flush(); + } +} \ No newline at end of file diff --git a/Java/zad3_1/Logger.java b/Java/zad3_1/Logger.java new file mode 100644 index 0000000..9a79860 --- /dev/null +++ b/Java/zad3_1/Logger.java @@ -0,0 +1,7 @@ +package POB_2019.Java.zad3_1; + +import java.io.IOException; + +public class Logger { + public void log(String textToLog) throws IOException{}; +} \ No newline at end of file diff --git a/Java/zad3_1/StdoutLogger.java b/Java/zad3_1/StdoutLogger.java new file mode 100644 index 0000000..3f839d6 --- /dev/null +++ b/Java/zad3_1/StdoutLogger.java @@ -0,0 +1,8 @@ +package POB_2019.Java.zad3_1; + +public class StdoutLogger extends Logger { + @Override + public void log(String textToLog){ + System.out.println(textToLog); + } +} \ No newline at end of file diff --git a/Java/zad3_1/zad3_1.java b/Java/zad3_1/zad3_1.java new file mode 100644 index 0000000..0414318 --- /dev/null +++ b/Java/zad3_1/zad3_1.java @@ -0,0 +1,20 @@ +package POB_2019.Java.zad3_1; + +import java.io.IOException; +import java.util.List; +import java.util.ArrayList; + +public class zad3_1 { + public static void main(String[] args) throws IOException { + Logger log1 = new StdoutLogger(); + Logger log2 = new FileLogger(); + List texts = new ArrayList(); + texts.add("Jechane"); + texts.add("ASDSAD"); + texts.add("Jechane1"); + for(String text : texts){ + log1.log(text); + log2.log(text); + } + } +} \ No newline at end of file