Commit 9929b01b by Patryk Czarnik

HTMLable

parent 05782136
package gotowe.p30_lambdy.html;
public record Adres(String ulica, String numer, String kodPocztowy, String miasto) implements HTMLable {
@Override
public String toHTML() {
return String.format("<div>ul. %s %s<br>%s %s</div>",
ulica, numer, kodPocztowy, miasto);
}
}
package gotowe.p30_lambdy.html;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
public class GeneratorStron {
public static String utworzStrone(String tytul, HTMLable... elementy) {
String polaczone = Stream.of(elementy)
.map(HTMLable::toHTML)
.collect(Collectors.joining("\n"));
return String.format("""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>%s</title>
</head>
<body>
<h1>%s</h1>
%s
</body>
</html>
""", tytul, tytul, polaczone);
}
public static void zapiszStrone(File plik, String tytul, HTMLable... elementy) throws IOException {
String html = utworzStrone(tytul, elementy);
Files.writeString(plik.toPath(), html, Charset.forName("UTF-8"));
}
public static void main(String[] args) {
Adres adres = new Adres("Jasna", "14/16a", "01-234", "Warszawa");
Osoba ala = new Osoba("Ala", "Kowalska", adres);
Osoba ola = new Osoba("Ola", "Malinowska", adres);
// do metody zapiszStrone mogę przekazać dowolne obiekty zgodne z interfejsem HTMLable,
// ale gdybym potrzebował dorzucić dowlny fragment HTML mogę też przekazać wyrażenie lambda, które w wyniku daje HTMLa
// dzięki temu, że interfejs HTMLable jest "interfejsem funkcyjnym", czyli ma tylko jedną metodę do zaimplementowania
try {
ola.showHTML();
adres.showHTML();
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("Pliki HTML", "html", "htm"));
int coSieStalo = chooser.showSaveDialog(null);
if(coSieStalo == JFileChooser.APPROVE_OPTION) {
zapiszStrone(chooser.getSelectedFile(), "Pracownicy ALX",
ala, ola, () -> "<h2>Adres firmy</h2>", adres);
JOptionPane.showMessageDialog(null, "Plik zpaisany");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package gotowe.p30_lambdy.html;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
/** Klasa zgodna z tym interfejsem posiada zdolność tworzenia HTML-a.
* Na obiekcie można wywołać metodę toHTML() i otrzymać fragment kodu HTML z danymi tego obiektu.
*/
public interface HTMLable {
String toHTML();
default void showHTML() {
JLabel label = new JLabel("<html>" + toHTML() + "</html>");
JOptionPane.showMessageDialog(null, label);
}
}
package gotowe.p30_lambdy.html;
public class Osoba implements HTMLable {
private String imie, nazwisko;
private Adres adres;
public Osoba(String imie, String nazwisko, Adres adres) {
this.imie = imie;
this.nazwisko = nazwisko;
this.adres = adres;
}
public String getImie() {
return imie;
}
public void setImie(String imie) {
this.imie = imie;
}
public String getNazwisko() {
return nazwisko;
}
public void setNazwisko(String nazwisko) {
this.nazwisko = nazwisko;
}
public Adres getAdres() {
return adres;
}
public void setAdres(Adres adres) {
this.adres = adres;
}
public String toString() {
return imie + " " + nazwisko + " " + ", adres: " + adres;
}
@Override
public String toHTML() {
String styl = "border: 2px solid blue; margin: 10px; padding:5px; background-color: #DDFFFF";
return String.format("<div style='%s'><div>%s %s</div>\n%s</div>", styl, imie, nazwisko, adres.toHTML());
}
}
......@@ -34,7 +34,6 @@ public class Strumienie2c {
strumien.forEach(s -> System.out.println(" * " + s));
// forEach nie ma obowiązku zachowania kolejności i w przypadku parallelStream nie zachowuje
System.out.println("KONIEC");
}
}
......@@ -32,7 +32,7 @@ public class C01_Generowanie {
Stream<LocalTime> czasy = Stream.generate(() -> LocalTime.now());
// to się zapętla:
//czasy.forEach(lt -> System.out.println(lt));
// czasy.forEach(lt -> System.out.println(lt));
czasy.limit(20).forEach(lt -> System.out.println(lt));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment