Java: Casting a ticket procedure

Source: Internet
Author: User
Tags comparable stub ticket

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.