Voting City used: system.in, regular pattern,matcher, sorting interface comparable replication CompareTo sorting method
A class in the election monitor, according to the number of votes, and the highest number of votes on the first display
Design:
Student Student Class
Voting Menu Class
Voting Action Class
Accept input data classes
Main program Call
public class Student implements Comparable{private int id;private String name;private int vote;public Student (int id, stri ng name, int vote) {super (); this.id = Id;this.name = Name;this.vote = vote;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int Getvote () {return vote;} public void Setvote (int vote) {this.vote = vote;} @Overridepublic String toString () {return name:: "+ name +", Number of votes: "+ vote +"] ";} public int CompareTo (Student stu) {//TODO auto-generated method stub if (This.vote > Stu.vote) {return 1;} else if (This.vote < stu.vote) {return-1;} Else{return 0;}}}
public class Menu {private Student stu[] = {New Student (1, "Zhang San", 0), New Student (2, "John Doe", 0), New Student (3, "Harry", 0), new Stude NT (4, "Tianqi", 0)};p ublic Menu () throws Ioexception{operater oper = new Operater (this.stu);//output all the names that need to be voted oper.list ();// Voting while (Oper.vote ()) {;} After the poll is over, list all the floating numbers oper.list ();}}
Ublic class Operater {private Student stu[]=null;private inputdata inputdata = null;public operater (Student stu[]) {THIS.S Tu = Stu;this.inputdata = new Inputdata ();} public void list () {for (int i =0; i< this.stu.length; i++) {System.out.println (This.stu[i].getid () + "number, Name:" +this.stu[ I].getname () + ":" "+this.stu[i].getvote () +" "Ticket");}} Public Boolean vote () throws Ioexception{boolean flag = True;int Stuno = this.inputData.getInt ("Please enter the ID of the monitor candidate you want to select:", "Sorry you entered "), switch (Stuno) {case 1:{this.stu[0].setvote (this.stu[0].getvote () + 1); Case 2:{this.stu[1].setvote (This.stu[1].getvote () + 1); Case 3:{this.stu[2].setvote (This.stu[2].getvote () + 1); Case 4:{this.stu[3].setvote (This.stu[3].getvote () + 1); Case 0:{flag = False;break;} Default:{flag = False;break;}} return flag;}}
public class Inputdata {/*public static void main (string[] args) throws ioexception{inputstream IPT = system.in; System.out.println ("Please enter:"); int temp=0; StringBuffer buf = new StringBuffer (), while ((temp = Ipt.read ())! =-1) {char c = (char) temp;if (c = = ' \ n ') {break;} Buf.append (c);} String ipustr = new string (BUF);//ipustr = Ipustr.matches ("\\s*|\r|\n|\t"); SYSTEM.OUT.PRINTLN (replace (IPUSTR)), ipustr = replace (IPUSTR), if (Ipustr.matches ("^\\d$")) {System.out.println (new Integer (IPUSTR));} Else{system.out.println ("0");}} public static string replace (String str) {if (str! = NULL &&! "). Equals (str)) {Pattern p = pattern.compile ("\\s*|\r|\n|\t"); Matcher m = p.matcher (str); return M.replaceall ("");} Else{return str;}} */public string Replace (String str) {if (str! = NULL &&! "). Equals (str)) {Pattern p = pattern.compile ("\\s*|\r|\n|\t"); Matcher m = p.matcher (str); return M.replaceall ("");} Else{return str;}} public int getInt (String str, String str2) throws Ioexception{inputstream IPT = System.in; System.out.println (str); int temp=0; StringBuffer buf = new StringBuffer (), while ((temp = Ipt.read ())! =-1) {char c = (char) temp;if (c = = ' \ n ') {break;} Buf.append (c);} String ipustr = new String (BUF), ipustr = This.replace (IPUSTR), if (Ipustr.matches ("\\d")) {return new Integer (IPUSTR);} Else{return 0;}}}
public class Test {public static void main (string[] args) throws IOException {//TODO auto-generated method stub new Menu ();}}
Java: Casting a ticket procedure