Game Entry

***************************************************************************
public class GameEntry{
public String name;
public int score;

public GameEntry(String n, int s){
name = n;
score = s;
}

public String getName(){
return name;
}

public int getScore(){
return score;
}

public String toString(){
return “(“+name+”,”+score+”)”;
}

}
**************************************************************************
import java.util.Scanner;
public class Scores{
public static final int maxEntries = 10;
public GameEntry[] entries;
public int numEntries;

public Scores(){
entries = new GameEntry[maxEntries];
numEntries =0;
}

public void setEntry(String name, int score){
GameEntry gE  = new GameEntry(name,score);
if(numEntries ==0){
entries[numEntries] = gE;
numEntries++;
}
else if(numEntries >0 && numEntries !=0 && numEntries<=10){
entries[numEntries] = gE;
for(int i=numEntries; i>0; i–){
if(entries[i].getScore() > entries[i-1].getScore()){
GameEntry temp =entries[i];
entries[i] = entries[i-1];
entries[i-1] =temp;
}
}
numEntries++;
}

else if(numEntries >10 && numEntries !=0){
entries[9] = gE;
for(int i=numEntries; i>0; i–){
if(entries[i].getScore() > entries[i-1].getScore()){
GameEntry temp =entries[i];
entries[i] = entries[i-1];
entries[i-1] =temp;
}
}
numEntries++;
}

for(int i=0; i<numEntries; i++){
System.out.println((i+1)+”========> “+entries[i]);
}
}




public static void main(String args[]){
Scores SE = new Scores();
Scanner S = new Scanner(System.in);
int choice;
do{
System.out.println(“Enter your choice\n1:Entering the Score\n2:Exit”);
choice= S.nextInt();
if(choice ==1){
System.out.print(“Enter your name : “);
String name = S.next();

System.out.println(“Enter Your Score : “);
int score = S.nextInt();

SE.setEntry(name,score);
}
else if(choice ==2){
System.exit(0);
}
}while(choice != 2);
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s