-
-
Save anonymous/97c810c45b54d0dd3841 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javaapplication8; | |
import java.util.Collections; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Optional; | |
public class InMemoryTrollRepository { | |
private final List<Troll> trolls = new LinkedList<>(); | |
public InMemoryTrollRepository() { | |
Troll perceptroll = new Troll(); | |
perceptroll.setId(1L); | |
perceptroll.setNick("perceptroll"); | |
trolls.add(perceptroll); | |
Troll kyt = new Troll(); | |
kyt.setId(2L); | |
kyt.setNick("kyt"); | |
trolls.add(kyt); | |
} | |
public List<Troll> list() { | |
return Collections.unmodifiableList(trolls); | |
} | |
public int count() { | |
return trolls.size(); | |
} | |
public Optional<Troll> findById(Long id) { | |
return trolls | |
.stream() | |
.filter((troll) -> troll.getId().equals(id)) | |
.findFirst(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javaapplication8; | |
public class Troll { | |
private Long id; | |
private String nick; | |
private boolean hungry; | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
public String getNick() { | |
return nick; | |
} | |
public void setNick(String nick) { | |
this.nick = nick; | |
} | |
public boolean isHungry() { | |
return hungry; | |
} | |
public void setHungry(boolean hungry) { | |
this.hungry = hungry; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javaapplication8; | |
import javax.swing.BorderFactory; | |
import javax.swing.BoxLayout; | |
import static javax.swing.BoxLayout.LINE_AXIS; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.SwingUtilities; | |
public class TrollDetailForm extends JFrame { | |
private final InMemoryTrollRepository trollRepository = new InMemoryTrollRepository(); | |
private final JLabel trollLabel = new JLabel(); | |
public TrollDetailForm() { | |
setLayout(new BoxLayout(getContentPane(), LINE_AXIS)); | |
initializeModel(); | |
trollLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); | |
add(trollLabel); | |
setSize(800, 600); | |
pack(); | |
setLocationRelativeTo(null); | |
setVisible(true); | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
} | |
private void initializeModel() { | |
Troll troll = trollRepository.findById(2L).get(); | |
StringBuilder trollStatus = new StringBuilder("Troll "); | |
trollStatus.append(troll.getNick()); | |
if(troll.isHungry()) { | |
trollStatus.append(" needs food"); | |
} else { | |
trollStatus.append(" needs food"); | |
} | |
trollLabel.setText(trollStatus.toString()); | |
} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(() -> new TrollDetailForm()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment