Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mineavtaler;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.swing.JOptionPane;
- /**
- * Denne klassen inneholder all data til hver individuelle avtale. Den tilbyr
- * også funksjoner for endring av data.
- *
- * @author Tomas Sandven
- */
- public class Avtale implements Comparable<Avtale>
- {
- /*
- * Fields
- */
- private String beskrivelse;
- private String innhold;
- private Date tidspunkt;
- /*
- * Constructors
- */
- public Avtale()
- {
- this("Ingen beskrivelse", "Intet innhold", new Date());
- }
- public Avtale(String beskrivelse, String innhold, Date tidspunkt)
- {
- this.setBeskrivelse(beskrivelse);
- this.setInnhold(innhold);
- this.setTidspunkt(tidspunkt);
- }
- /*
- * Static functions
- */
- /**
- * Asks the user for input for each field in the Avtale class
- *
- * @param existing_beskrivelse
- * The default value for beskrivelse
- * @param existing_innhold
- * The default value for innhold
- * @param existing_tidspunkt
- * The default value for tidspunkt
- *
- * @return An Avtale object with the user input information
- */
- public static Avtale requestFromUser(String existing_beskrivelse, String existing_innhold, String existing_tidspunkt)
- {
- String input_beskrivelse, input_innhold, input_tidspunkt;
- Date output_tidspunkt;
- // Get beskrivelse with an infinite loop to hinder empty strings
- for (;;)
- {
- input_beskrivelse = (String) JOptionPane.showInputDialog(null, "Beskrivelse", "Beskrivelse", JOptionPane.QUESTION_MESSAGE, null, null, existing_beskrivelse);
- if (input_beskrivelse == null)
- {
- // If the user cancels
- return null;
- }
- else if (input_beskrivelse.isEmpty())
- {
- // If beskrivelse is empty, try again
- JOptionPane.showMessageDialog(null, "Beskrivelse kan ikke være tom!", "Feil", JOptionPane.WARNING_MESSAGE);
- continue;
- }
- else
- {
- // Otherwise, break the loop
- break;
- }
- }
- // Get innhold
- input_innhold = (String) JOptionPane.showInputDialog(null, "Innhold", "Innhold", JOptionPane.QUESTION_MESSAGE, null, null, existing_innhold);
- if (input_innhold == null)
- {
- // If the user cancels
- return null;
- }
- // Get tidspunkt - encapsulate date validation in an infinite loop
- for (;;)
- {
- // Get the string input from the user
- input_tidspunkt = (String) JOptionPane.showInputDialog(null, "Tidspunkt (yyyy-mm-dd hh:mm:ss", "Tidspunkt", JOptionPane.QUESTION_MESSAGE, null, null, existing_tidspunkt);
- if (input_tidspunkt == null)
- {
- // If the user cancels
- return null;
- }
- // Create a DateFormat object and try to parse the input string
- SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- try
- {
- output_tidspunkt = dateformat.parse(input_tidspunkt);
- // Break the loop and move on
- break;
- }
- catch (ParseException e)
- {
- JOptionPane.showMessageDialog(null, "Klarte ikke å parse datoformatet! Vennligst prøv på nytt", "Ugyldig datoformat", JOptionPane.ERROR_MESSAGE);
- // Restart loop
- continue;
- }
- }
- return new Avtale(input_beskrivelse, input_innhold, output_tidspunkt);
- }
- /**
- * Asks the user for input for each field in the Avtale class
- *
- * @return An Avtale object with the user input information
- */
- public static Avtale requestFromUser()
- {
- return Avtale.requestFromUser("", "", "");
- }
- /*
- * Getters and setters
- */
- public String getBeskrivelse()
- {
- return this.beskrivelse;
- }
- public void setBeskrivelse(String beskrivelse)
- {
- this.beskrivelse = beskrivelse;
- }
- public String getInnhold()
- {
- return this.innhold;
- }
- public void setInnhold(String innhold)
- {
- this.innhold = innhold;
- }
- public Date getTidspunkt()
- {
- return this.tidspunkt;
- }
- public void setTidspunkt(Date tidspunkt)
- {
- this.tidspunkt = tidspunkt;
- }
- /*
- * Comparable interface
- */
- public int compareTo(Avtale b)
- {
- if (this.tidspunkt.before(b.getTidspunkt())) { return -1; }
- if (this.tidspunkt.after(b.getTidspunkt())) { return 1; }
- return 0;
- }
- /*
- * Other functionality
- */
- /**
- * Compare this Avtale object with another one by it's data
- */
- public Boolean equals(Avtale b)
- {
- Boolean output = true;
- if (!this.beskrivelse.equals(b.getBeskrivelse()))
- {
- output = false;
- }
- if (!this.innhold.equals(b.getInnhold()))
- {
- output = false;
- }
- if (!this.tidspunkt.equals(b.getTidspunkt()))
- {
- output = false;
- }
- return output;
- }
- /**
- * @return A string summarizing this Avtale object
- */
- public String toString()
- {
- return this.getBeskrivelse();
- }
- /**
- * Displays the details of the parent Avtale object in a message box
- */
- public void displayDetails()
- {
- // Format the Date object nicely
- SimpleDateFormat dateformat = new SimpleDateFormat("EEEEEEE d MMM yyyy - HH:mm:ss");
- String tidspunkt_string = dateformat.format(this.getTidspunkt());
- // Display the details
- JOptionPane.showMessageDialog(null, String.format("%s - %s - %s", this.getBeskrivelse(), this.getInnhold(), tidspunkt_string));
- }
- /**
- * Gets user input to change the data in the parent Avtale object
- */
- public void editDetails()
- {
- // Format the Date object the same way as it's input
- SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String tidspunktString = dateformat.format(this.getTidspunkt());
- // Create a new Avtale object from an user request. Use the old data as
- // default values.
- Avtale n = Avtale.requestFromUser(this.getBeskrivelse(), this.getInnhold(), tidspunktString);
- // Check if the user cancelled
- if (n != null)
- {
- // Set the new data
- this.setBeskrivelse(n.getBeskrivelse());
- this.setInnhold(n.getInnhold());
- this.setTidspunkt(n.getTidspunkt());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement