ProfWendi

Container Class (lombok)

May 31st, 2021 (edited)
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. import java.io.Serializable;
  2.  
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. import lombok.NonNull;
  6.  
  7. /**
  8.  * This class models a simple container with a specific
  9.  * volume.  The container must have a name and the volume
  10.  * of the container must be greater than 0.
  11.  *
  12.  * @author Wendi Jollymore
  13.  */
  14. @Data
  15. @NoArgsConstructor
  16. public class Container implements Comparable<Container>, Serializable {
  17.    
  18.     private static final long serialVersionUID = 1L;
  19.    
  20.     private int id = 0;
  21.     @NonNull
  22.     private String name = "container";
  23.     private double volume = 1.0;
  24.    
  25.     /**
  26.      * Construct a container with a specific name and
  27.      * volume.  Name can't be empty
  28.      * and volume must be greater than 0.
  29.      *
  30.      * @param name the name of this container
  31.      * @param size the programmer-specified volume
  32.      */
  33.     public Container( String name, double size) {
  34.        
  35.         // make sure name and volume are valid
  36.         setName(name);
  37.         setVolume(size);
  38.     }
  39.    
  40.     /**
  41.      * Construct a container with a specific id, name, and
  42.      * volume.  ID can't be less than 0, name can't be empty
  43.      * and volume must be greater than 0.
  44.      *
  45.      * @param id the unique container ID
  46.      * @param name the name of this container
  47.      * @param size the programmer-specified volume
  48.      */
  49.     public Container(int id, String name, double size) {
  50.        
  51.         // make sure id, name, and volume are valid
  52.         setId(id);
  53.         setName(name);
  54.         setVolume(size);
  55.     }
  56.    
  57.     /**
  58.      * Attempts to place a valid id into this container's
  59.      * id member.  The id can't be less than, otherwise an
  60.      * exception is thrown.
  61.      *
  62.      * @param name the programmer-specified container ID
  63.      * @throws IllegalArgumentException if the IDis invalid
  64.      */
  65.     public void setId(int id) {
  66.        
  67.         // make sure id isn't invalid
  68.         if (id >= 0) {
  69.             this.id = id;
  70.         } else { // id is not valid
  71.             throw new IllegalArgumentException("Error: must be greater than 0.");
  72.         }
  73.     }
  74.        
  75.    
  76.     /**
  77.      * Attempts to place a valid volume into this container's
  78.      * volume member.  If the volume is not greater than 0, an
  79.      * exception is thrown.
  80.      *
  81.      * @param volume the programmer-specified volume
  82.      * @throws IllegalArgumentException if the volume is invalid
  83.      */
  84.     public void setVolume(double volume) {
  85.        
  86.         // make sure volume is valid
  87.         if (volume > 0) {
  88.             this.volume = volume;
  89.         } else { // volume is not valid
  90.             throw new IllegalArgumentException("Error: size must be greater"
  91.                 + " than 0.");
  92.         }
  93.     }
  94.    
  95.     /**
  96.      * Gets this container as a String.
  97.      *
  98.      * @return this container as a formatted string
  99.      */
  100.     public String toString() {
  101.        
  102.         // formatted container name and volume
  103.         return String.format("%s: %.2f", name, volume);
  104.     }
  105.     /**
  106.      * Compares this container to another container.  Containers are compared by
  107.      * volume.  If this container is larger than the other container, a 1 is
  108.      * returned.  If this container is smaller than the other container, a -1 is
  109.      * returned.  If both containers have the same volume, a 0 is returned.
  110.      *
  111.      * @param c the container to compare this container to
  112.      * @return 1, -1, or 0 depending on how the containers compare
  113.      */
  114.     @Override
  115.     public int compareTo(Container c) {
  116.        
  117.         if (this.volume > c.getVolume())
  118.             return 1;
  119.         else if (this.volume < c.getVolume())
  120.             return -1;
  121.         else
  122.             return 0;
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment