Professional Documents
Culture Documents
The Final Program: Import Public Class Static New Static New in Public Static Void
The Final Program: Import Public Class Static New Static New in Public Static Void
1. import java.util.Scanner;
2.
3. public class Main {
4.
5. static String[] guests = new String[10];
6. static Scanner scanner = new Scanner(System.in);
7.
8. public static void main(String[] args) {
9.
10. insertTestNames();
11.
12. do {
13. displayGuests();
14. displayMenu();
15. int option = getOption();
16.
17. if (option == 1) {
18. addGuest();
19. }
20. else if (option == 2) {
21. insertGuest();
22. }
23. else if (option == 3) {
24. renameGuest();
25. }
26. else if (option == 4) {
27. removeGuest();
28. }
29. else if (option == 5) {
30. System.out.println("Exiting...");
31. break;
32. }
33. } while (true);
34.
35. }
36.
37. static void displayGuests() {
38. System.out.println("______________________________\n- Guests -\n");
39. boolean isEmpty = true;
40. for (int i = 0; i < guests.length; i++) {
41. if (guests[i] != null) {
42. System.out.println((i + 1) + ". " + guests[i]);
43. isEmpty = false;
44. }
45. }
46.
47. if (isEmpty) {
48. System.out.println("Guest list is empty.");
49. }
50. }
51.
52. static void displayMenu() {
53. System.out.println("______________________________\n- Menu -\n");
54. System.out.println("1 - Add Guest");
55. System.out.println("2 - Insert Guest");
56. System.out.println("3 - Rename Guest");
57. System.out.println("4 - Remove Guest");
58. System.out.println("5 - Exit");
59. }
60.
61. static int getOption() {
62. System.out.print("Option: ");
63. int option = scanner.nextInt(); //
64. scanner.nextLine();
65. System.out.println();
66. return option;
67. }
68.
69. static void addGuest() {
70. for (int i = 0; i < guests.length; i++) {
71. if (guests[i] == null) {
72. System.out.print("Name: ");
73. guests[i] = scanner.nextLine();
74. break;
75. }
76. }
77. }
78.
79. static void insertGuest() {
80. System.out.print("Number: ");
81. int num = scanner.nextInt();
82. scanner.nextLine();
83.
84. if (num >= 1 && num <= 10 && guests[num - 1] != null) {
85. System.out.print("Name: ");
86. String name = scanner.nextLine();
87.
88. for (int i = guests.length - 1; i > num - 1; i--) {
89. guests[i] = guests[i - 1];
90. }
91.
92. guests[num - 1] = name;
93. }
94. else {
95. System.out.println("\nError: There is no guest with that number.");
96. }
97. }
98.
99. static void renameGuest() {
100. System.out.print("Number: ");
101. int num = scanner.nextInt();
102. scanner.nextLine();
103.
104. if (num >= 1 && num <= 10 && guests[num - 1] != null) {
105. System.out.print("Name: ");
106. String name = scanner.nextLine();
107.
108. guests[num - 1] = name;
109. }
110. else {
111. System.out.println("\nError: There is no guest with that number.");
112. }
113. }
114.
115. static void removeGuest() {
116. System.out.print("Number: ");
117. int num = scanner.nextInt();
118.
119. if (num >= 1 && num <= 10 && guests[num - 1] != null) {
120. guests[num - 1] = null;
121.
122. String[] temp = new String[guests.length];
123. int ti = 0;
124. for (int i = 0; i < guests.length; i++) {
125. if (guests[i] != null) {
126. temp[ti] = guests[i];
127. ti++;
128. }
129. }
130. guests = temp;
131. }
132. else {
133. System.out.println("\nError: There is no guest with that number.");
134. }
135. }
136.
137. static void insertTestNames() {
138. guests[0] = "Jacob Hughes";
139. guests[1] = "Edward Jones";
140. guests[2] = "Rose Morrison";
141. guests[3] = "Molly Jones";
142. guests[4] = "Christopher Morgan";
143. }
144.
145. }