|
| 1 | +package chapter_07; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +/** |
| 6 | + * 15. Write a program that manages a list of up to 10 players and their high |
| 7 | + * scores in the computer’s memory. Use two arrays to manage the list. One |
| 8 | + * array should store the player’s name and the other array should store the |
| 9 | + * player’s high score. Use the index of the arrays to correlate the name with |
| 10 | + * the score. In the next Programming Project, we ask you to do the same |
| 11 | + * thing, but using an array of a class. Your program should start with zero |
| 12 | + * players in the list and support the following features: |
| 13 | + * a. Add a new player and score. If it is one of the top 10 scores then add it |
| 14 | + * to the list of scores. The same name and score can appear multiple |
| 15 | + * times. For example, if Bill played 3 times and scored 100,100, and 99, |
| 16 | + * and Bob played once and scored 50, then the top scores would be Bill |
| 17 | + * 100, Bill 100, Bill 99, Bob 50. |
| 18 | + * b. Print the top 10 names and scores to the screen sorted by score (highest |
| 19 | + * at the top). |
| 20 | + * c. Allow the user to enter a player name and output that player’s highest |
| 21 | + * score if it is on the top 10 list or a message if the player’s name has not |
| 22 | + * been input or is not in the top 10. |
| 23 | + * d. Allow the user to enter a player name and remove the highest score for |
| 24 | + * that player from the list. |
| 25 | + * Create a menu system that allows the user to select which option to |
| 26 | + * invoke. |
| 27 | + * |
| 28 | + * |
| 29 | + * |
| 30 | + * @author Sharaf Qeshta |
| 31 | + * */ |
| 32 | +public class Project_07_15 |
| 33 | +{ |
| 34 | + private static String[] names = new String[10]; |
| 35 | + private static int[] scores = new int[10]; |
| 36 | + private static Scanner scanner = new Scanner(System.in); |
| 37 | + |
| 38 | + public static void main(String[] args) |
| 39 | + { |
| 40 | + while (true) |
| 41 | + start(); |
| 42 | + } |
| 43 | + |
| 44 | + public static void start() |
| 45 | + { |
| 46 | + printOptions(); |
| 47 | + System.out.println("\nEnter a choice: "); |
| 48 | + int choice = scanner.nextInt(); |
| 49 | + |
| 50 | + switch (choice) |
| 51 | + { |
| 52 | + case 1 : addPlayer(); break; |
| 53 | + case 2 : printTop10(); break; |
| 54 | + case 3 : search(); break; |
| 55 | + case 4 : remove(); break; |
| 56 | + case 5 : System.exit(0); |
| 57 | + default: |
| 58 | + System.out.println("Invalid Choice"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static void printOptions() |
| 63 | + { |
| 64 | + System.out.println("1. Add new player"); |
| 65 | + System.out.println("2. Print top 10 players"); |
| 66 | + System.out.println("3. Search for a player"); |
| 67 | + System.out.println("4. Remove a player"); |
| 68 | + System.out.println("5. Exit"); |
| 69 | + } |
| 70 | + |
| 71 | + public static void remove() |
| 72 | + { |
| 73 | + System.out.print("Enter player name: "); |
| 74 | + String playerName = scanner.next(); |
| 75 | + |
| 76 | + int index = -1; |
| 77 | + for (int i = 0; i < 10; i++) |
| 78 | + { |
| 79 | + if (names[i] == null) |
| 80 | + break; |
| 81 | + |
| 82 | + if (names[i].equals(playerName)) |
| 83 | + { |
| 84 | + index = i; |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (index == -1) |
| 90 | + { |
| 91 | + System.out.println("the name is not exist"); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + for (int i = index; i < scores.length-1; i++) |
| 97 | + { |
| 98 | + names[i] = names[i+1]; |
| 99 | + scores[i] = scores[i+1]; |
| 100 | + } |
| 101 | + |
| 102 | + names[names.length-1] = null; |
| 103 | + scores[scores.length-1] = 0; |
| 104 | + } |
| 105 | + |
| 106 | + public static void search() |
| 107 | + { |
| 108 | + System.out.print("Enter player name: "); |
| 109 | + String playerName = scanner.next(); |
| 110 | + |
| 111 | + for (int i = 0; i < 10; i++) |
| 112 | + { |
| 113 | + if (names[i] == null) |
| 114 | + { |
| 115 | + System.out.println(playerName + " is not exist"); |
| 116 | + break; |
| 117 | + } |
| 118 | + |
| 119 | + if (names[i].equals(playerName)) |
| 120 | + { |
| 121 | + System.out.println(playerName + " highest score : " + scores[i]); |
| 122 | + return; |
| 123 | + } |
| 124 | + } |
| 125 | + System.out.println(playerName + " is not on the top ten"); |
| 126 | + } |
| 127 | + |
| 128 | + public static void printTop10() |
| 129 | + { |
| 130 | + for (int i = 0; i < 10; i++) |
| 131 | + { |
| 132 | + if (names[i] == null) |
| 133 | + break; |
| 134 | + System.out.println(names[i] + " : " + scores[i]); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public static void addPlayer() |
| 139 | + { |
| 140 | + System.out.print("Enter player name: "); |
| 141 | + String playerName = scanner.next(); |
| 142 | + |
| 143 | + System.out.print("Enter player score: "); |
| 144 | + int score = scanner.nextInt(); |
| 145 | + |
| 146 | + int index = 0; |
| 147 | + for (int i = scores.length-1; i > -1; i--) |
| 148 | + { |
| 149 | + if (score <= scores[i]) |
| 150 | + { |
| 151 | + index = i + 1; |
| 152 | + break; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + for (int i = index; i < scores.length-1; i++) |
| 158 | + { |
| 159 | + String indexName = names[i+1]; |
| 160 | + int indexScore = scores[i+1]; |
| 161 | + |
| 162 | + names[i+1] = names[index]; |
| 163 | + scores[i+1] = scores[index]; |
| 164 | + |
| 165 | + names[index] = indexName; |
| 166 | + scores[index] = indexScore; |
| 167 | + } |
| 168 | + |
| 169 | + if (index < scores.length) |
| 170 | + { |
| 171 | + names[index] = playerName; |
| 172 | + scores[index] = score; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments