Advertisement
Guest User

Avtale.java

a guest
May 3rd, 2011
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.64 KB | None | 0 0
  1. package mineavtaler;
  2.  
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6.  
  7. import javax.swing.JOptionPane;
  8.  
  9. /**
  10.  * Denne klassen inneholder all data til hver individuelle avtale. Den tilbyr
  11.  * også funksjoner for endring av data.
  12.  *
  13.  * @author Tomas Sandven
  14.  */
  15. public class Avtale implements Comparable<Avtale>
  16. {
  17.     /*
  18.      * Fields
  19.      */
  20.  
  21.     private String beskrivelse;
  22.     private String innhold;
  23.     private Date tidspunkt;
  24.  
  25.     /*
  26.      * Constructors
  27.      */
  28.  
  29.     public Avtale()
  30.     {
  31.         this("Ingen beskrivelse", "Intet innhold", new Date());
  32.     }
  33.  
  34.     public Avtale(String beskrivelse, String innhold, Date tidspunkt)
  35.     {
  36.         this.setBeskrivelse(beskrivelse);
  37.         this.setInnhold(innhold);
  38.         this.setTidspunkt(tidspunkt);
  39.     }
  40.  
  41.     /*
  42.      * Static functions
  43.      */
  44.  
  45.     /**
  46.      * Asks the user for input for each field in the Avtale class
  47.      *
  48.      * @param existing_beskrivelse
  49.      *            The default value for beskrivelse
  50.      * @param existing_innhold
  51.      *            The default value for innhold
  52.      * @param existing_tidspunkt
  53.      *            The default value for tidspunkt
  54.      *
  55.      * @return An Avtale object with the user input information
  56.      */
  57.     public static Avtale requestFromUser(String existing_beskrivelse, String existing_innhold, String existing_tidspunkt)
  58.     {
  59.         String input_beskrivelse, input_innhold, input_tidspunkt;
  60.         Date output_tidspunkt;
  61.  
  62.         // Get beskrivelse with an infinite loop to hinder empty strings
  63.         for (;;)
  64.         {
  65.             input_beskrivelse = (String) JOptionPane.showInputDialog(null, "Beskrivelse", "Beskrivelse", JOptionPane.QUESTION_MESSAGE, null, null, existing_beskrivelse);
  66.             if (input_beskrivelse == null)
  67.             {
  68.                 // If the user cancels
  69.                 return null;
  70.             }
  71.             else if (input_beskrivelse.isEmpty())
  72.             {
  73.                 // If beskrivelse is empty, try again
  74.                 JOptionPane.showMessageDialog(null, "Beskrivelse kan ikke være tom!", "Feil", JOptionPane.WARNING_MESSAGE);
  75.                 continue;
  76.             }
  77.             else
  78.             {
  79.                 // Otherwise, break the loop
  80.                 break;
  81.             }
  82.         }
  83.  
  84.         // Get innhold
  85.         input_innhold = (String) JOptionPane.showInputDialog(null, "Innhold", "Innhold", JOptionPane.QUESTION_MESSAGE, null, null, existing_innhold);
  86.         if (input_innhold == null)
  87.         {
  88.             // If the user cancels
  89.             return null;
  90.         }
  91.  
  92.         // Get tidspunkt - encapsulate date validation in an infinite loop
  93.         for (;;)
  94.         {
  95.             // Get the string input from the user
  96.  
  97.             input_tidspunkt = (String) JOptionPane.showInputDialog(null, "Tidspunkt (yyyy-mm-dd hh:mm:ss", "Tidspunkt", JOptionPane.QUESTION_MESSAGE, null, null, existing_tidspunkt);
  98.             if (input_tidspunkt == null)
  99.             {
  100.                 // If the user cancels
  101.                 return null;
  102.             }
  103.  
  104.             // Create a DateFormat object and try to parse the input string
  105.             SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  106.             try
  107.             {
  108.                 output_tidspunkt = dateformat.parse(input_tidspunkt);
  109.  
  110.                 // Break the loop and move on
  111.                 break;
  112.             }
  113.             catch (ParseException e)
  114.             {
  115.                 JOptionPane.showMessageDialog(null, "Klarte ikke å parse datoformatet! Vennligst prøv på nytt", "Ugyldig datoformat", JOptionPane.ERROR_MESSAGE);
  116.  
  117.                 // Restart loop
  118.                 continue;
  119.             }
  120.         }
  121.  
  122.         return new Avtale(input_beskrivelse, input_innhold, output_tidspunkt);
  123.     }
  124.  
  125.     /**
  126.      * Asks the user for input for each field in the Avtale class
  127.      *
  128.      * @return An Avtale object with the user input information
  129.      */
  130.     public static Avtale requestFromUser()
  131.     {
  132.         return Avtale.requestFromUser("", "", "");
  133.     }
  134.  
  135.     /*
  136.      * Getters and setters
  137.      */
  138.  
  139.     public String getBeskrivelse()
  140.     {
  141.         return this.beskrivelse;
  142.     }
  143.  
  144.     public void setBeskrivelse(String beskrivelse)
  145.     {
  146.         this.beskrivelse = beskrivelse;
  147.     }
  148.  
  149.     public String getInnhold()
  150.     {
  151.         return this.innhold;
  152.     }
  153.  
  154.     public void setInnhold(String innhold)
  155.     {
  156.         this.innhold = innhold;
  157.     }
  158.  
  159.     public Date getTidspunkt()
  160.     {
  161.         return this.tidspunkt;
  162.     }
  163.  
  164.     public void setTidspunkt(Date tidspunkt)
  165.     {
  166.         this.tidspunkt = tidspunkt;
  167.     }
  168.  
  169.     /*
  170.      * Comparable interface
  171.      */
  172.  
  173.     public int compareTo(Avtale b)
  174.     {
  175.         if (this.tidspunkt.before(b.getTidspunkt())) { return -1; }
  176.         if (this.tidspunkt.after(b.getTidspunkt())) { return 1; }
  177.  
  178.         return 0;
  179.     }
  180.  
  181.     /*
  182.      * Other functionality
  183.      */
  184.  
  185.     /**
  186.      * Compare this Avtale object with another one by it's data
  187.      */
  188.     public Boolean equals(Avtale b)
  189.     {
  190.         Boolean output = true;
  191.  
  192.         if (!this.beskrivelse.equals(b.getBeskrivelse()))
  193.         {
  194.             output = false;
  195.         }
  196.         if (!this.innhold.equals(b.getInnhold()))
  197.         {
  198.             output = false;
  199.         }
  200.         if (!this.tidspunkt.equals(b.getTidspunkt()))
  201.         {
  202.             output = false;
  203.         }
  204.  
  205.         return output;
  206.     }
  207.  
  208.     /**
  209.      * @return A string summarizing this Avtale object
  210.      */
  211.     public String toString()
  212.     {
  213.         return this.getBeskrivelse();
  214.     }
  215.  
  216.     /**
  217.      * Displays the details of the parent Avtale object in a message box
  218.      */
  219.     public void displayDetails()
  220.     {
  221.         // Format the Date object nicely
  222.         SimpleDateFormat dateformat = new SimpleDateFormat("EEEEEEE d MMM yyyy - HH:mm:ss");
  223.         String tidspunkt_string = dateformat.format(this.getTidspunkt());
  224.  
  225.         // Display the details
  226.         JOptionPane.showMessageDialog(null, String.format("%s - %s - %s", this.getBeskrivelse(), this.getInnhold(), tidspunkt_string));
  227.     }
  228.  
  229.     /**
  230.      * Gets user input to change the data in the parent Avtale object
  231.      */
  232.     public void editDetails()
  233.     {
  234.         // Format the Date object the same way as it's input
  235.         SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  236.         String tidspunktString = dateformat.format(this.getTidspunkt());
  237.  
  238.         // Create a new Avtale object from an user request. Use the old data as
  239.         // default values.
  240.         Avtale n = Avtale.requestFromUser(this.getBeskrivelse(), this.getInnhold(), tidspunktString);
  241.  
  242.         // Check if the user cancelled
  243.         if (n != null)
  244.         {
  245.             // Set the new data
  246.             this.setBeskrivelse(n.getBeskrivelse());
  247.             this.setInnhold(n.getInnhold());
  248.             this.setTidspunkt(n.getTidspunkt());
  249.         }
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement