Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- public class leaderboard {
- static ArrayList<Object> scores = new ArrayList<>();
- private String username, displayArray, password;
- private int numCorrect, numAnswered, playerNum;
- private double perc;
- public leaderboard() {
- username = "";
- numCorrect = 0;
- numAnswered = 0;
- //password = "";
- perc = 0.0;
- }
- public leaderboard(String user, int numC, int numA) {
- username = user;
- numCorrect = numC;
- numAnswered = numA;
- //password = pass;
- }
- public static void addLeaderboard(String user, int numC, int numA) {
- scores.add(user);
- scores.add(numC);
- scores.add(numA);
- }
- public static void setLeaderboard(String user, int numC, int numA, int i){
- scores.set((i*3),user);
- scores.set((i*3)+1,numC);
- scores.set((i*3)+2,numA);
- }
- public static ArrayList<Object> getScores() {
- return scores;
- }
- public int returnNumUsers() {
- return scores.size() / 4;
- }
- public String displayUsername(int n) {
- return scores.get(n * 3).toString();
- }
- public int displayNumCor(int n) {
- return (int) scores.get((n * 3) + 1);
- }
- public int displayNumAns(int n) {
- return (int) scores.get((n * 3) + 2);
- }
- public int getScore(int n) {
- int numCorrect = (int) scores.get((n * 4) + 1);
- int numAnswered = (int) scores.get((n * 4) + 2);
- perc = ((double) numCorrect / (double) numAnswered) * 100.0;
- return (int) perc;
- } //Score is percent
- public void viewLeaderboard() {
- for (int i = 0; i < this.returnNumUsers(); i++) {
- playerNum = i + 1;
- System.out.println("Rank " + playerNum + ":\n\tUsername: " + this.displayUsername(i));
- System.out.println("\tCorrect Answers: " + this.displayNumCor(i));
- System.out.println("\tTotal Questions: " + this.displayNumAns(i));
- System.out.println("\tScore: " + this.getScore(i));
- if (displayUsername(i) == null) {
- break;
- }
- }
- }
- public void sortBoard() {
- List<Integer> sortScores = new ArrayList<>();
- List<String> test = new ArrayList<>();
- test.clear();
- for (int k = 0; k < this.returnNumUsers(); k++) {
- test.add(this.displayUsername(k));
- test.add(Integer.toString(this.displayNumCor(k)));
- test.add(Integer.toString(this.displayNumAns(k)));
- }
- for (int i = 0; i < this.returnNumUsers(); i++) {
- sortScores.add(this.getScore(i));
- }
- Collections.sort(sortScores, Collections.reverseOrder());
- scores.clear();
- for (int x = 0; x < (test.size() / 3); x++) {
- for (int k = 0; k < (test.size() / 3); k++) {
- int perc = (int) (((Double.parseDouble(test.get((k * 4) + 1))) / (Double.parseDouble(test.get((k * 4) + 2)))) * 100);
- if (sortScores.get(x) == perc) {
- addLeaderboard((test.get(k * 3)), (Integer.parseInt(test.get((k * 3) + 1))), (Integer.parseInt(test.get((k * 3) + 2))));//
- }
- }
- }
- }
- public void searchUser(String username) {
- for (int i = 0; i < returnNumUsers(); i++) {
- if (username.equals(displayUsername(i))) {
- playerNum = i + 1;
- System.out.println("Rank " + playerNum + ":\n\tUsername: " + this.displayUsername(i));
- System.out.println("\tCorrect Answers: " + this.displayNumCor(i));
- System.out.println("\tTotal Questions: " + this.displayNumAns(i));
- System.out.println("\tScore: " + this.getScore(i));
- } else {
- System.out.println("There is noone with the username" + username + "Please enter a new username");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement