使用方法比较多个对象以找到最大值?

时间:2014-03-16 14:09:16

标签: java class object methods compare

嘿我正在尝试在我的主代码中找出一种方法,将类DVD的age属性与该对象的所有其他实例进行比较,以查看哪个属性是最大和最小的。任何人都可以引导我朝着正确的方向前进。如果有帮助,我可以发布主方法和其他类吗?

DVD类:

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

DVDShop课程:

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 < 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<= 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];
    }
  }
}

主要方法:

    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

2 个答案:

答案 0 :(得分:2)

Comparable<T>类上实现DVD接口,如下所示:

public class DVD implements Comparable<DVD>

这需要您定义一个新方法; compareTo

这允许您定义两个对象相对于彼此的排序方式。在这种情况下,对于较旧的DVD来说,更大的&#34;更有意义。比较新的DVD,所以compareTo的(非常基本的)实现看起来像这样:

public int compareTo(DVD that) {
    if (age > that.getAge()) {
        // this is older than that
        return 1;
    } else if (age < that.getAge()) {
        // this is newer than that
        return -1;
    }

    // this is the same age as that
    return 0;
}

完成后,您可以使用Arrays.sort(dvdList)将您的收藏分类为年龄顺序。从那里开始,需要选择集合中的第一个和最后一个元素,这些元素将分别是您最早和最新的DVD

在实践中,您可能希望为compareTo添加一些空值检查,以避免在thatnull时出现问题。

答案 1 :(得分:0)

Can any one steer me in the right direction.

添加查找最大和最小的东西的方法到DVDShop类如下。

public class DVDShop
{
    private DVD[] dvdList;
    ---snip---
    public int largestAge()
    {
        //find and return largest age from dvdList.
    }

    public int smallestAge()
    {
        //find and return smallest age from dvdList.
    }
}
相关问题