diff --git a/Agenda b/Agenda deleted file mode 100644 index 07ce3bd..0000000 --- a/Agenda +++ /dev/null @@ -1,188 +0,0 @@ -import java.io.EOFException; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; -import java.util.ArrayList; - -public class Agenda implements Serializable { - - private static final long serialVersionUID = 1; - - private ArrayList stages; - private ArrayList events; - private ArrayList artists; - - public Agenda() { - stages = new ArrayList(); - events = new ArrayList(); - artists = new ArrayList(); - } - - public void addStage(Stage stage) { - stages.add(stage); - } - - public void addEvent(Event event) { - events.add(event); - } - - public void addArtist(Artist artist) { - artists.add(artist); - } - - public void fillArtists() { - FileInputStream fis = null; - ObjectInputStream ois = null; - - try { - fis = new FileInputStream("artists.dat"); - ois = new ObjectInputStream(fis); - - Object object; - object = ois.readObject(); - try { - while (object != null) { - artists.add((Artist) object); - object = ois.readObject(); - } - } catch (EOFException e) { - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - public void fillEvents() { - FileInputStream fis = null; - ObjectInputStream ois = null; - - try { - fis = new FileInputStream("events.dat"); - ois = new ObjectInputStream(fis); - - Object object; - object = ois.readObject(); - try { - while (object != null) { - events.add((Event) object); - object = ois.readObject(); - } - } catch (EOFException e) { - } - } catch (Exception e) { - e.printStackTrace(); - } - - } - - public void fillStages() { - FileInputStream fis = null; - ObjectInputStream ois = null; - - try { - fis = new FileInputStream("stages.dat"); - ois = new ObjectInputStream(fis); - - Object object; - object = ois.readObject(); - try { - while (object != null) { - stages.add((Stage) object); - object = ois.readObject(); - } - } catch (EOFException e) { - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - public void saveStages() { - - try { - File file = new File("stages.dat"); - if (!file.exists()){ - file.createNewFile(); - } - FileOutputStream fos = new FileOutputStream(file); - ObjectOutputStream oos = new ObjectOutputStream(fos); - for (Stage stage : stages) { - oos.writeObject(stage); - } - oos.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public void saveEvents() { - - try { - File file = new File("events.dat"); - if (!file.exists()){ - file.createNewFile(); - } - FileOutputStream fos = new FileOutputStream(file); - ObjectOutputStream oos = new ObjectOutputStream(fos); - for (Event event : events) { - oos.writeObject(event); - } - oos.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public void saveArtists() { - - try { - File file = new File("artists.dat"); - if (!file.exists()){ - file.createNewFile(); - } - FileOutputStream fos = new FileOutputStream(file); - ObjectOutputStream oos = new ObjectOutputStream(fos); - for (Artist artist : artists) { - oos.writeObject(artist); - } - oos.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public void addEvents() - { - int ID = 0; - for (Stage stage : stages) - { - ID = stage.getID(); - for (Event event : events) - { - if ( ID == event.getID()) - { - stage.addEvent(event); - } - } - } - } - - public void saveAll() - { - saveArtists(); - saveEvents(); - saveStages(); - } - - public void fillAll() - { - fillArtists(); - fillEvents(); - fillStages(); - addEvents(); - } -} diff --git a/Agenda.java b/Agenda.java new file mode 100644 index 0000000..34a9e88 --- /dev/null +++ b/Agenda.java @@ -0,0 +1,211 @@ +import java.io.EOFException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.util.ArrayList; + +import javax.swing.JFileChooser; +import javax.swing.JOptionPane; +import javax.swing.filechooser.FileFilter; + +public class Agenda implements Serializable { + + private static final long serialVersionUID = 1; + + private ArrayList stages; + private ArrayList events; + private ArrayList artists; + + public Agenda() { + stages = new ArrayList(); + events = new ArrayList(); + artists = new ArrayList(); + } + + public void addStage(Stage stage){ + stages.add(stage); + } + + public void addEvent(Event event) { + events.add(event); + } + + public void addArtist(Artist artist) { + artists.add(artist); + } + + public ArrayList getEvents() + { + return events; + } + + public void saveAgenda() + { + JFileChooser fileChooser = new JFileChooser(); + + fileChooser.setFileFilter(new FileFilter() { + @Override + public boolean accept(File pathname) { + if(pathname.isFile() && pathname.getName().endsWith(".agn")) + { + return true; + }else if(pathname.isDirectory()){return true;}else{return false;} + } + + @Override + public String getDescription() { + return ".agn"; + } + + }); + fileChooser.setDialogTitle("Choose save location"); + int userSelection = fileChooser.showSaveDialog(null); + + if(userSelection == JFileChooser.APPROVE_OPTION) + { + File file = fileChooser.getSelectedFile(); + if(!file.getName().endsWith(".agn")) + { + file = new File(file.getAbsolutePath() + ".agn"); + } + + if(file.exists()) + { + if(JOptionPane.showConfirmDialog(null, "Are you sure you want to overwrite: " + file.getName(), "Overwrite", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) + { + saveAgenda(file); + } + } + else + { + saveAgenda(file); + } + } + } + + public void loadAgenda() + { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setFileFilter(new FileFilter() { + @Override + public boolean accept(File pathname) { + if(pathname.isFile() && pathname.getName().endsWith(".agn")) + { + return true; + }else if(pathname.isDirectory()){return true;}else{return false;} + } + + @Override + public String getDescription() { + return ".agn"; + } + + }); + + fileChooser.setDialogTitle("Choose file"); + int userSelection = fileChooser.showOpenDialog(null); + + if(userSelection == JFileChooser.APPROVE_OPTION) + { + File file = fileChooser.getSelectedFile(); + if(!file.exists()) + { + JOptionPane.showMessageDialog(null, "This file does not exist: " + file.getName()); + } + else + { + fillAgenda(file); + } + } + } + + public void fillAgenda(File file) { + FileInputStream fis = null; + ObjectInputStream ois = null; + + try { + + fis = new FileInputStream(file); + ois = new ObjectInputStream(fis); + + Object object; + object = ois.readObject(); + try { + while (object != null) { + events.add((Event) object); + object = ois.readObject(); + } + } catch (EOFException e) { + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + + public void fillArtists() + { + for (Event e : events) + { + boolean exists = false; + String name = e.getArtist().getName(); + for (Artist a : artists) + { + if ( a.getName().equals(name) ) { + exists = true; + } + } + if ( exists == false) { + addArtist(e.getArtist()); + } + } + } + + public void fillStages() + { + for (Event e : events) + { + boolean exists = false; + String name = e.getStage().getName(); + for (Stage s : stages) + { + if ( s.getName().equals(name) ) { + exists = true; + } + } + if ( exists == false) { + addStage(e.getStage()); + } + } + } + + public void saveAgenda(File file) { + + try { + FileOutputStream fos = new FileOutputStream(file); + ObjectOutputStream oos = new ObjectOutputStream(fos); + for (Event event : events) { + oos.writeObject(event); + } + oos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void clearAgenda() + { + events.clear(); + } + + public void fillAllLists() + { + loadAgenda(); + fillStages(); + fillArtists(); + } +}