- package helpisneeded;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Postbox {
- private ArrayList<Letter> postbox;
- private ArrayList<Letter> postbag;
- /**
- * Gibt eine Beschreibung aller Briefe im Briefkasten aus
- */
- public void printAll(){
- for(Letter letter: postbox){
- System.out.println("Absender " + letter.getSenderInfo() + "Empfaenger: " + letter.getRecipientInfo());
- }
- }
- /**
- * Fuegt einen Brief zum Briefkasten hinzu
- */
- public void addLetterInPostbox(Letter letter) {
- postbox.add(letter);
- }
- /**
- * Wirft alle Briefe aus dem Briefkasten in den Postsack
- */
- public void emptyPostboxToPostbag(){
- postbox.addAll(postbox);
- postbox.clear();
- }
- public void savePostbox(){
- try {
- ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("speicher"));
- for(Letter letter:postbox) {
- outputStream.writeObject(letter);
- }
- outputStream.close();
- } catch(IOException e) {
- System.out.println("Datei konnte nicht gespeichert werden.");
- }
- }
- public void readPostbox() {
- Letter letter;
- try {
- ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("speicher"));
- while(true) {
- letter = (Letter) inputStream.readObject();
- addLetterInPostbox(letter);
- }
- } catch(EOFException e) {
- System.out.println("Datei wurde zu Ende gelesen.");
- } catch(ClassNotFoundException e) {
- System.out.println("Falscher Dateityp");
- } catch(IOException e) {
- System.out.println("Datei konnte nicht gelesen werden.");
- }
- }
- public static void mainMenu() {
- Postbox pb = new Postbox();
- String auswahl;
- Scanner sc = new Scanner(System.in);
- do {
- System.out.println("Please choose one of the following options: "
- + "\n1:Add a new letter in postbox"
- + "\n2:Save a postbox to file"
- + "\n3:Read a postbox from file"
- + "\n4:Print all letters in postbox"
- + "\n5:Empty postbox to postbag"
- + "\n0:Exit ");
- auswahl = sc.next();
- if(auswahl.equals("1")){
- System.out.println("Bitte Sender angeben: ");
- String sender = sc.next();
- System.out.println("Bitte Empfaenger angeben: ");
- String empfaenger = sc.next();
- pb.addLetterInPostbox(new Letter(sender, empfaenger));
- } else if (auswahl.equals("2")){
- pb.savePostbox();
- } else if (auswahl.equals("3")) {
- pb.readPostbox();
- } else if (auswahl.equals("4")) {
- pb.printAll();
- } else if (auswahl.equals("5")) {
- pb.emptyPostboxToPostbag();
- }
- } while (!auswahl.equals("0"));
- sc.close();
- System.out.println("Das Programm wird beendet.");
- }
- public static void main(String[] args) {
- mainMenu();
- }
- }
Undefined
By: Guest | Date: Nov 28 2014 22:10 | Format: None | Expires: never | Size: 3.78 KB | Hits: 998
Latest pastes
1 hours ago
11 hours ago
1 days ago
2 days ago
2 days ago