added java2
This commit is contained in:
parent
14a1b7186c
commit
4fc3b7661f
29
Java/zad2_1/Koszyk.java
Normal file
29
Java/zad2_1/Koszyk.java
Normal 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
17
Java/zad2_1/Produkt.java
Normal 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
11
Java/zad2_1/zad2_1.java
Normal 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);
|
||||
}
|
||||
}
|
18
Java/zad3_1/FileLogger.java
Normal file
18
Java/zad3_1/FileLogger.java
Normal 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
7
Java/zad3_1/Logger.java
Normal 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{};
|
||||
}
|
8
Java/zad3_1/StdoutLogger.java
Normal file
8
Java/zad3_1/StdoutLogger.java
Normal 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
20
Java/zad3_1/zad3_1.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user