added java2

This commit is contained in:
marcin 2019-04-14 13:42:06 +02:00
parent 14a1b7186c
commit 4fc3b7661f
7 changed files with 110 additions and 0 deletions

29
Java/zad2_1/Koszyk.java Normal file
View File

@ -0,0 +1,29 @@
package POB_2019.Java.zad2_1;
import java.util.List;
import java.util.ArrayList;
public class Koszyk {
private List<Produkt> products = new ArrayList<Produkt>();
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;
}
}

17
Java/zad2_1/Produkt.java Normal file
View File

@ -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;
}
}

11
Java/zad2_1/zad2_1.java Normal file
View File

@ -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);
}
}

View File

@ -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();
}
}

7
Java/zad3_1/Logger.java Normal file
View File

@ -0,0 +1,7 @@
package POB_2019.Java.zad3_1;
import java.io.IOException;
public class Logger {
public void log(String textToLog) throws IOException{};
}

View File

@ -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);
}
}

20
Java/zad3_1/zad3_1.java Normal file
View File

@ -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<String> texts = new ArrayList<String>();
texts.add("Jechane");
texts.add("ASDSAD");
texts.add("Jechane1");
for(String text : texts){
log1.log(text);
log2.log(text);
}
}
}