title: Comparing multiple objects with a method to find the largest value?
body: <p>Hey I'm trying to figure out a method in my main code to compare the age attribute of the class DVD with all other instances of the object to see which attribute is the largest and smallest. Can any one steer me in the right direction. I can post the main method and other class if that helps?</p>
  
  <p>DVD Class:</p>
  
  <pre><code>public class DVD
  {
      private String id;
      private String title;
      private String genre;
      private int age;
  
      public DVD(String id, String title, String genre, int age)
      {
  
          this.id = id;
          this.title = title;
          this.genre = genre;
          this.age = age;
      }
  
      public String getId()
      {
  
          return id;
      }
  
      public String getTitle()
      {
          return title;
      }
  
      public String getGenre()
      {
          return genre;
      }
  
      public int getAge()
      {
          return age;
      }
  
      //Setters
  
      public void setID(String idIn)
      {
          id = idIn;
      }
  
      public void setTitle(String titleIn)
      {
          title = titleIn;
      }
  
      public void setGenre(String genreIn)
      {
  
          genre = genreIn;
      }
  
      public void setAge(int ageIn)
      {
          age = ageIn;
      }
  
      public String toString()
      {
          //Building string here
          String x = "The id is: "+id+
              "\nThe title is: "+title+
              "\nThe genre is: "+genre+
              "\nThe age is: "+age;
          return x;
  
      }
  }//end of class
  </code></pre>
  
  <p>DVDShop Class:</p>
  
  <pre><code>public class DVDShop
  {
      //attributes (instance variables)
      private DVD[] dvdList;
      private int total;
  
      //Constructors
      public DVDShop (int maxNum)
      {
          dvdList = new DVD[maxNum];
          total=0;
      }
  
      //accessor 
      public int getTotal()
      {
          return total;
      }
  
      // check if the list is full
      public boolean isFull()
      {
         if (total == dvdList.length)
         {
            return true; // list is full
         }
         else
         {
            return false; // list is empty
         }
      }
  
      // check if the list is empty
      public boolean isEmpty()
      {
         if (total == 0)
         {
            return true; // list is empty
         }
         else
         {
            return false; // list is not empty
         }
      }
  
        // add an item to the array
    public boolean add(DVD DVDIn)
    {
      if (!isFull()) // check if list is full
      {
        dvdList[total] = DVDIn; // add item
        total++; // increment total
        return true; // indicate success
      }
      else
      {
        return false; // indicate failure
      }
    }
  
  
  
       // helper method to find the id of a specified account
        public int search(String dvdNumIn)
        {
          for(int i = 0; i &lt; total; i++)
          {
            DVD tempDVD = dvdList[i]; // find the account at index i
            String tempNumber = tempDVD.getId(); // get dvd number
            if(tempNumber.equals(dvdNumIn))
            {
              return i;
            }
          }
           return -999;
        }
  
  
    public boolean delete(String numberIn)
    {
       int index;
  
       index = search(numberIn); // call the search method first 
                                 // if it could find the DVD it will return the element in the array that it is stored
                                 // if  not it will return a dummy value of -999
       if(index == -999) 
       {
           return false; // remove was unsuccessful
       }
       else
       {   
           for(int i = index; i&lt;= total-2; i++)
           {
               dvdList[i] = dvdList[i+1];
           }
           total--; // decrement total number of DVDs
           return true; // remove was successful
       }
    }
  
  
     public DVD getItem(String numIn)
    {
      int index;
      index = search(numIn);
  
      if(index == -999)
      {
        return null; // indicate invalid index
      }
      else
      {
        return dvdList[index];
      }
    }
  }
  </code></pre>
  
  <p>Main Method: </p>
  
  <pre><code>    public class Question1
      {
          public static void main (String args[])
          {
              //creation of instance of DVDs called myDVDshop size 10
              DVDShop myDVDshop = new DVDShop(10);
  
              int choice;
              int i;
  
              //start of do while() loop for repetition
              do{
                  System.out.println();
  
                  System.out.println("Shop System");
  
                  System.out.println("1. Add DVD");
                  System.out.println("2. Search for DVD");
                  System.out.println("3. Show number of DVDs in shop");
                  System.out.println("4. Delete a DVD");
                  System.out.println("5. Show Details of a DVD");
                  System.out.println("6. Exit System");
                  System.out.print("Please enter your choice: ");
                  choice = EasyScanner.nextInt();
  
                  System.out.println();
  
                  //Start of switch()
  
                  switch(choice)
                  {
  
                      case 1:
                      addDVD(myDVDshop);
                      break;  
  
  
                      case 2:
                      searchDVD(myDVDshop);
                      break;
  
                      case 3:
                      Stock(myDVDshop);
                      break;
  
                      case 4:
                      deleteDVD(myDVDshop);
                      break;
  
                      case 5:
                      showDetails(myDVDshop); 
                      break;
  
                      case 6:
                      System.out.println("Goodbye..."); 
                      break;
                      default:
                      System.out.println("Invalid entry, please reenter valid selection");
  
                  }//end of switch()
  
             }while(choice!=6);
  
  
         }//end of class
         //start of methods
  
  
  
     private static void showDetails(DVDShop myDVDshop)
    {
          String number;
  
          // get details from user
  
          System.out.print("Enter Id of DVD: ");
          number = EasyScanner.nextString();
  
          DVD theDVD = myDVDshop.getItem(number);
          if (theDVD == null)
          {
              System.out.println();
              System.out.println("No such DVD exists");
          }
          else
          {
              System.out.println();
              System.out.println("DVD Id: " + theDVD.getId());
              System.out.println("DVD title: " + theDVD.getTitle());
              System.out.println("DVD genre: " + theDVD.getGenre());
              System.out.println("DVD age category: " + theDVD.getAge());
              System.out.println();
          }
    }
  
  
  
         private static void  deleteDVD(DVDShop myDVDshop)
         {
              String id;
              boolean toDelete;
  
              System.out.print("Please enter the id number of the DVD you would like to delete: ");
              id = EasyScanner.nextString();
  
              System.out.println();
  
             toDelete = myDVDshop.delete(id);
  
                      if (toDelete == true)           
                      {
                        System.out.println("The DVD has been deleted from the system");
                      }
                      else
                      {
                        System.out.println("The DVD could not be deleted from the system");
                      }
  
             System.out.println();
         }
  
  
         private static void addDVD(DVDShop myDVDshop)
             { 
                 String id, title, genre;
                 int age;
                 boolean ok;
                // DVDs myDVDshop = new DVDs(10);
                 // Entering required data
                System.out.print("Enter id number: ");
                id = EasyScanner.nextString();
  
                System.out.print("Enter title of DVD: ");
                title = EasyScanner.nextString();
  
                System.out.print("Enter genre of DVD: ");
                genre = EasyScanner.nextString();
  
                System.out.print("Enter age category of DVD: ");
                age = EasyScanner.nextInt();
  
                System.out.println();
  
                DVD DVD1 = new DVD(id,title,genre,age);
  
                ok = myDVDshop.add(DVD1);
  
                      if (ok == false)           
                      {
                        System.out.println("The list is full");
                      }
                      else
                      {
                        System.out.println("DVD added");
                      }
  
               System.out.println();
      }
  
          private static void searchDVD(DVDShop myDVDshop)
          {
  
             //Requesting value from the user to search for.
  
             System.out.print("Which DVD id do you want to search for: ");
             String idIn=EasyScanner.nextString();
  
             System.out.println();
  
             int search1 = myDVDshop.search(idIn);
  
                   if(search1 == -999)
                   { 
                       System.out.println("DVD not in the system");
                   }
                   else
                   {
                       System.out.println("The DVD is in the system at position number "+search1+ " in the array.");
  
                   }
  
             System.out.println();         
          }
  
          private static void Stock(DVDShop myDVDshop)
          { 
            System.out.println("Total stock is: "+myDVDshop.getTotal());
          }
  
  }//end of class
  </code></pre>