java - how do i finish sorting this in descending order? -


we need make 2 classes: girlscoutcheck , cookieanalyzercheck. need user input - ask them name of each girl until type in runaway. ask them number of cookie boxes each of girl sold, , add cookie boxes each girl separately , multiply 3.50. that's total income. sort them in descending order based on total income. have done everything. need on sorting , printing sorted info. can me finish this? thank you.

package pcs;  import java.text.numberformat; import java.util.arraylist; import java.util.collections; import java.util.scanner;  public class cookieanalyzercheck {  public static void main(string args[]) {     numberformat nf = numberformat.getcurrencyinstance();     string name = "";     arraylist al = new arraylist();      {         scanner kb = new scanner(system.in);         system.out.println("please enter name of each"                 + " girl scout, or \"runaway\" "                 + "to exit.");         name = kb.nextline();         if (!name.equalsignorecase("runaway")) {             system.out.println("enter number of thin mints sold");             int thinmints = kb.nextint();             system.out.println("enter number of caramel delights sold");             int carameldelights = kb.nextint();             system.out.println("enter number of lemonades sold");             int lemonades = kb.nextint();             system.out.println("enter number of thanksalot sold");             int thanksalot = kb.nextint();             system.out.println("enter number of mangocremes sold");             int mangocremes = kb.nextint();             girlscoutcheck ba = new girlscoutcheck(name, thinmints, carameldelights, lemonades, thanksalot, mangocremes);             al.add(ba);             system.out.println(""); //makes output easier read             system.out.println();         }     } while (!name.equalsignorecase("runaway"));       girlscoutcheck thisba = (girlscoutcheck) al.get(0);      double maxbalance = thisba.totalincome;     string maxname = thisba.name;      (int = 1; < al.size(); i++) {         thisba = (girlscoutcheck) al.get(i);         if (thisba.totalincome > maxbalance) {             //we have new winner, save the..             maxbalance = thisba.totalincome;             maxname = thisba.name;            int a= al.size();                 }         }     }   }    package pcs;  import java.util.collections;    public class girlscoutcheck implements comparable { public string name; public int thinmints; public int carameldelights; public int lemonades; public int thanksalot; public int mangocremes; public int totalcookies; public double totalincome;  public girlscoutcheck(string nm, int tm,int cd, int lem, int tal, int mc){     name = nm;     thinmints = tm;     carameldelights = cd;     lemonades = lem;     thanksalot = tal;     mangocremes = mc;     totalcookies = thinmints+carameldelights+lemonades+thanksalot+mangocremes;     totalincome = totalcookies*3.50; }  public int totalcookies(){     return totalcookies;  }  public double totalincome(){     return totalincome;  }   public int compareto(object done){    girlscoutcheck b= (girlscoutcheck)done;    if (totalincome>b.totalincome){        return 1;    }        else if(totalincome<b.totalincome)  {            return -1;         }        else{            return 0;     }   }   public string getname(){         string nameparts[] = name.split(" ");     string first = nameparts[0];     string last = nameparts[1];      return string.format(last + "," + first); }         @override public string tostring() {     return "girlscoutcheck [name=" + getname() + ", thinmints=" + thinmints             + ", carameldelights=" + carameldelights + ", lemonades="             + lemonades + ", thanksalot=" + thanksalot + ", mangocremes="             + mangocremes + ", totalcookies=" + totalcookies + "]"+ system.lineseparator()+ " (total income thin mints $" +                     thinmints*3.50+ ") ( total income caramel delights $" +                     carameldelights*3.50+ ") total income lemonades $" +                     lemonades*3.50+ ")( total income mangocremes $" +                     mangocremes*3.50+ ") (total income thanksalot $" +                     thanksalot*3.50+ ")"+system.lineseparator() ;  }   } 

i think you're missing few things in main method:

  • your variable nf never used
  • since want sort girlscoutcheck's, recommend use treeset store them, instead of arraylist. treeset collection of elements sorted (here javadoc entry).

ideally, should declare , initialise container follows:

final set<girlscoutcheck> al = new treeset<>(); 
  • you should close streams , external resource in general. id est: closing scanner before leaving main method.

you using try-with-resource syntax, follows:

try (final scanner kb = new scanner(system.in)) {     {         // stuff         kb.nextline();     } while (!name.equalsignorecase("runaway")); } 

by way, might need consume last line-feed before reading name of girl: advise call kb.nextline(); right before re-entering do/while loop (as did in example of try-with-resource above).

basically, main method be:

final set<girlscoutcheck> al = new treeset<>();  try (final scanner kb = new scanner(system.in)) {     {         // stuff         kb.nextline();     } while (!name.equalsignorecase("runaway")); }  // on, al contain girlscoutcheck's ordered "as want"  (final girlscoutcheck gsc : al) {     system.out.println(gsc); } 

and... that's it!


now, actually, achieve "auto-ordering", need declare how compare girlscoutcheck's. doing right thing making girlscoutcheck implement comparable :)

however, should specify you're implementing comparable<girlscoutcheck>. thus, signature of compareto method you're overriding becomes:

public int compareto(final girlscoutcheck other) 

instead of:

public int compareto(final object other); 

you can simplify this:

@override public int compareto(final girlscoutcheck o) {     return (int) (o.totalincome - this.totalincome); } 

one last point: consistency equals required ensuring sorted collections (such treeset) well-behaved.
source: implementing compareto - javapractices

thus, should implement equals , hashcode girlscoutcheck's, using totalincome field (to consistent compareto implementation). don't worry, ide :) , anyway, can too. here is:

@override public int hashcode() {     final int prime = 31;     int result = 1;     long temp;     temp = double.doubletolongbits(this.totalincome);     result = (prime * result) + (int) (temp ^ (temp >>> 32));     return result; }  @override public boolean equals(final object obj) {     if (this == obj) {         return true;     }     if (obj == null) {         return false;     }     if (getclass() != obj.getclass()) {         return false;     }     final girlscoutcheck other = (girlscoutcheck) obj;     if (double.doubletolongbits(this.totalincome) != double.doubletolongbits(other.totalincome)) {         return false;     }     return true; } 

tl; dr

just...

  • shove piece code girlscoutcheck class,
  • replace compareto method 1 gave you,
  • be sure make implement comparable<girlscoutcheck> instead of comparable,
  • modify main method recommended...

and should work ;)


Comments

Popular posts from this blog

visual studio - vb.net filter binding source by time -

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -