Today, mainly modified several bugs, and then changed the background image and icon, added a change in the number of people, that is: the number of people who participate in AA, participate in AA calculation. There is also the use of Getresources (). Getidentifier () solves the problem of looping the component IDs in layout.
Here are a few pieces of core code:
Private class Onclicklistenerireture implements onclicklistener{@Overridepublic void OnClick (View arg0) {//TODO Auto-generated method stub//First gets number, that is, how many people you want to specify to participate in AA EditText EDT = (EditText) AAcharge.this.findViewById (r.id.ednum ); Number = Integer.parseint (Edt.gettext (). toString ()); int stuid[] = new Int[number]; TextView stu[] = new Textview[number];for (int k =1;k<stu.length+1;k++) {//get coststuid[k-1] = getresources (). Getidentifier ("Stu" +k, "id", Getpackagename ()); stu[k-1] = (TextView) AAcharge.this.findViewById (Stuid[k-1]);} Double num[] = new Double[number];int payid[] = new Int[number];int nameid[] = new Int[number]; Person p[] = new Person[number]; EditText ed[] = new Edittext[number];for (int i = 0;i<number;i++) {P[i] = new Person (); ed[i] = null;} for (int k =1;k<num.length+1;k++) {//get costpayid[k-1] = getresources (). Getidentifier ("Pay" +k, "id", Getpackagename ()); ed[k-1] = (EditText) AAcharge.this.popView.findViewById (payid[k-1]); num[k-1] = Double.parsedouble (ed[k-1]. GetText(). toString ()); Get input Money P[k-1].setcost (num[k-1]);//get namenameid[k-1] = getresources (). Getidentifier ("myed" +k, "id", Getpackagename ()); ed[k-1] = (EditText) AAcharge.this.popView.findViewById (nameid[k-1]);p [K-1].setname (Ed[k-1]). GetText (). toString ());} Double total =0;for (int t =0;t<p.length;t++) {total = total + P[t].getcost ();} Double average = ((int) ((total) *10/number))/10.0; AAcharge.this.statusinfo.setText ("Total Cost:" +string.valueof) + "Average Cost:" +string.valueof (average)); AAcharge.this.popWin.dismiss (); Calculate (average,p); for (int J =0;j<stu.length;j++) {Stu[j].settext (P[j].getinfo ());}}}
The main AA calculation algorithm, in fact, is very simple, first calculates the mean, then each person pays the money minus the mean value to the array, and then from small to large order, then take the first non-0 value, with non-0 value in front of the negative values to fill this non-0 value, until equal to zero, plus 1.public void Calculate (double avg, person p[]) {//TODO auto-generated method Stubint I =0;int J =0;int Index=0;sort (p); for (int k =0;k<p.length;k++) {P[k].setgetmoney (P[k].getcost ()-avg);} index = Findposindex (p); I=index;for (j = index-1;j>=0;) {if (i<p.length) {if (P[i].getgetmoney () +p[j]. Getgetmoney ()) >0) {P[j].setinfo (P[j].getname () + "pay" + p[i].getname () + ":" +string.valueof (Math.Abs (p[j). Getgetmoney ())) + "yuan;" + "\ n" +p[j].getinfo ());p [I].setgetmoney (P[i].getgetmoney () +p[j].getgetmoney ()); j--;} else if ((P[i].getgetmoney ()!=0) && ((P[i].getgetmoney () +p[j].getgetmoney ()) ==0) {p[j].setinfo (P[j]). GetName () + "pay" + p[i].getname () + ":" +string.valueof (Math.Abs (P[j].getgetmoney ())) + "Yuan" + "\ n" +p[j].getinfo ());p [i]. Setgetmoney (P[i].getgetmoney () +p[j].getgetmoney ()); i++;j--;} else if ((P[i].getgetmoney () +p[j].getgetmoney ()) <0) {P[j].setinfo (P[j].getname () + "pay" + p[i].getname () + ":" +strinG.valueof (Math.Abs (P[i].getgetmoney ())) + "yuan;" + "\ n" +p[j].getinfo ());p [J].setgetmoney (P[i].getgetmoney () +p[j]. Getgetmoney ()); i++;}}} private int Findposindex (person[] p) {//TODO auto-generated Method Stubint index =0;for (int i =0;i<p.length;i++) {if (P [I].getgetmoney () >0) {index = I;break;}} return index;} public void sort (person p[]) {//TODO auto-generated Method Stubperson temp =new person (); for (int i =0;i<p.length-1;i+ +) {for (int J =0;j<p.length-i-1;j++) {if (P[j].getcost () >p[j+1].getcost ()) {temp = P[j];p [j] = p[j+1];p [j+1] = temp;} }}}}
The last is attached to the app now, there is a girl said I was too dick, it doesn't matter, I just picture a happy.
Android Development Kind "AA Calculate Money Software" Development (ii)