简体   繁体   中英

How to compare sales from an array and get the highest and lowest selling

Does anybody have the code to be able to be able to compare the sales, or a variable, from an array, and be able to print out the highest and lowest selling items. I have tried several things but cant exactly get it to work.

Here is my code, the stuff I am most concerned about is at the bottom:

package album;

public class Certifier {

    public static void main(String[] args) {

Album zero = new Album(0, "Dark Side of the Moon", "Pink Floyd", 1973, 50000000);

        System.out.println("The album information for ablum 0 is: " + zero.toString());

        Album one = new Album(1, "Superficial", "Heidi Montag",  2010, 658);

        System.out.println("The album information for ablum 1 is: " + one.toString());

        Album two = new Album(2, "Fearless", "Taylor Swift", 2011, 3200000);

        System.out.println("The album information for ablum 2 is: " + two.toString());

        Album three = new Album(3, "Made Up", "Sample Band", 2011, 1300000);

        System.out.println("The album information for ablum 3 is: " + three.toString());

        Album four = new Album(4, "Concerto One Night", "Andrea Bocelli", 2011, 500008);

        System.out.println("The album information for ablum 4 is: " + four.toString());

        Album[] albums = new Album[4];
            albums[0] = zero;
            albums[1] = one;
            albums[2] = two;
            albums[3] = three;
            albums[4] = four;


            ((Comparable<String>) albums[0]).compareTo(albums[1]);

        System.out.println("The highest selling album is");
        for (int i=0; i< Album[].length; i++) {
            double highestSales;
            if (i.getSales()<highestSales)
                highestSales= i;
            return highestSales;

        }

    System.out.println("The lowest selling album is");
        for (int i=0; i< Album[].length; i++) {
            double lowestSales;
            if (i.getSales()<lowestSales)
                lowestSales= i.getSales();
            return i.getName + i.getArtist;
        }
    }
}
Album highestSellingAlbum;
double highestSales = 0;

for (int i=0; i< albums.length; i++) 
{
  if (albums[i].getSales() > highestSales)
  {
    highestSales = albums[i].getSales();
    highestSellingAlbum = albums[i];
  }
}

highestSellingAlbum will now contain the highest selling album, assuming that your Album class has a proper getSales() method that returns the sales.

to get lowestSales ever set you need to initialize it with a higher value as the real lowest that you look for. that could be Double.MAX_VALUE or the highestSale that you found before (assuming you fix the bug). You also should initialize highestSale of course, maybe with 0.0.

The fix for highestSale would be to assign highestSales = i.getSales() in your loop;

here is a full and working version:

package album;


public class Certifier {

    public static void main(String[] args) {

        Album zero = new Album(0, "Dark Side of the Moon", "Pink Floyd", 1973, 50000000);
        System.out.println("The album information for ablum 0 is: " + zero.toString());

        Album one = new Album(1, "Superficial", "Heidi Montag",  2010, 658);
        System.out.println("The album information for ablum 1 is: " + one.toString());

        Album two = new Album(2, "Fearless", "Taylor Swift", 2011, 3200000);
        System.out.println("The album information for ablum 2 is: " + two.toString());

        Album three = new Album(3, "Made Up", "Sample Band", 2011, 1300000);
        System.out.println("The album information for ablum 3 is: " + three.toString());

        Album four = new Album(4, "Concerto One Night", "Andrea Bocelli", 2011, 500008);
        System.out.println("The album information for ablum 4 is: " + four.toString());

        Album[] albums = new Album[5]; //we have 5 albums!
            albums[0] = zero;
            albums[1] = one;
            albums[2] = two;
            albums[3] = three;
            albums[4] = four;

        Album highestSellingAlbum = zero; //must be initialized with any of the 5
        Album lowestSellingAlbum = zero; //must be initialized with any of the 5

        for (int i=0; i< albums.length; i++) {

            if (albums[i].getSales() > highestSellingAlbum.getSales()){
                highestSellingAlbum = albums[i];
            }
            if (albums[i].getSales() < lowestSellingAlbum.getSales()){
                lowestSellingAlbum = albums[i];
            }
        }
        System.out.println("The highest selling album is " + highestSellingAlbum);
        System.out.println("The highest sales is " + highestSellingAlbum.getSales() ); 

        System.out.println("The lowest selling album is " + lowestSellingAlbum);
        System.out.println("The lowest sales is " + lowestSellingAlbum.getSales() );

    }

}

There were numerous errors in your posted version, including compiling errors. You should start using an IDE like eclipse for your java experiments, because such environments show you your errors while programming.

To the algorithmic problem of yours. The key point is, that you need to initialize the variables against which you compare. I guess that is what you tried with that line ((Comparable<String>) albums[0]).compareTo(albums[1]); in your code? Well, that line made no sense. Also, you were mixing the index of the array of Albums with the sales value of the albums. Well, have a look at my solution and study it. In future, please post only code that at least has no compilation errors.

You can Implement Comparable Interface in Album class if you can change it and the simply call Arrays.sort(albums) , it will short your array you can use first and last element of array as lowest and highest selling albums respectively.


Code will be some thing like this

public class Album implements Comparable<Album>{
     /*
      your existing code here
     */
     public int compareTo(Album compareAlbum) {
       int compareSales = ((Album) compareAlbum).getSales(); 
       return this.sales - compareSales
     }
}



If you can't change Album class then Grammin's answer is ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM