-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMahasiswaArray.java
More file actions
154 lines (138 loc) · 4.94 KB
/
Copy pathMahasiswaArray.java
File metadata and controls
154 lines (138 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.util.Scanner;
public class MahasiswaArray {
static int rows = 10;
static String[][] students = new String[rows][2];
static int count = 0;
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("\n=== MENU MAHASISWA ===");
System.out.println("1. Insert at Beginning\n2. Insert Given Position\n3. Insert at End");
System.out.println("4. Delete from Beginning\n5. Delete Given Position\n6. Delete from End");
System.out.println("7. Delete First Occurrence\n8. Show Data\n9. Exit");
System.out.print("Pilih Menu: ");
int menu = input.nextInt();
input.nextLine();
if (menu == 1)
insertBegn();
else if (menu == 2)
insertGiven();
else if (menu == 3)
insertEnd();
else if (menu == 4)
deltBegn();
else if (menu == 5)
deltGiven();
else if (menu == 6)
deltEnd();
else if (menu == 7)
deltFirstOccur();
else if (menu == 8)
display();
else if (menu == 9)
break;
}
}
static void insertBegn() {
for (int i = rows - 1; i > 0; i--) {
students[i][0] = students[i - 1][0];
students[i][1] = students[i - 1][1];
}
inputData(0);
updateCount();
}
static void insertGiven() {
System.out.print("Masukkan no baris tabel (1-10): ");
int target = input.nextInt() - 1;
input.nextLine();
if (target >= 0 && target < rows) {
for (int i = rows - 1; i > target; i--) {
students[i][0] = students[i - 1][0];
students[i][1] = students[i - 1][1];
}
inputData(target);
updateCount();
} else {
System.out.println("Posisi tidak valid!");
}
}
static void insertEnd() {
for (int i = 0; i < rows - 1; i++) {
students[i][0] = students[i + 1][0];
students[i][1] = students[i + 1][1];
}
inputData(rows - 1);
updateCount();
}
static void deltBegn() {
for (int i = 0; i < rows - 1; i++) {
students[i][0] = students[i + 1][0];
students[i][1] = students[i + 1][1];
}
students[rows - 1][0] = null;
students[rows - 1][1] = null;
updateCount();
}
static void deltGiven() {
System.out.print("Masukkan no baris tabel (1-10): ");
int target = input.nextInt() - 1;
input.nextLine();
if (target >= 0 && target < rows) {
for (int i = target; i < rows - 1; i++) {
students[i][0] = students[i + 1][0];
students[i][1] = students[i + 1][1];
}
students[rows - 1][0] = null;
students[rows - 1][1] = null;
updateCount();
}
}
static void deltEnd() {
students[rows - 1][0] = null;
students[rows - 1][1] = null;
updateCount();
}
static void deltFirstOccur() {
System.out.print("Masukkan NIM Mahasiswa akan dihapus: ");
String targetNim = input.nextLine();
for (int i = 0; i < rows; i++) {
if (students[i][1] != null && students[i][1].equals(targetNim)) {
for (int j = i; j < rows - 1; j++) {
students[j][0] = students[j + 1][0];
students[j][1] = students[j + 1][1];
}
students[rows - 1][0] = null;
students[rows - 1][1] = null;
updateCount();
System.out.println("Data berhasil dihapus!");
return;
}
}
System.out.println("Data Mahasiswa tidak ditemukan!");
}
static void updateCount() {
count = 0;
for (int i = 0; i < rows; i++) {
if (students[i][1] != null)
count++;
}
}
static void inputData(int index) {
System.out.print("Masukkan nama: ");
students[index][0] = input.nextLine();
System.out.print("Masukkan NIM: ");
students[index][1] = input.nextLine();
}
static void display() {
System.out.print("\nDalam Array: [");
for (int i = 0; i < rows; i++) {
String nama = (students[i][0] == null) ? "None" : students[i][0];
String nim = (students[i][1] == null) ? "None" : students[i][1];
System.out.print("[" + nama + ", " + nim + "]");
if (i < rows - 1)
System.out.print(", ");
}
System.out.println("]");
System.out.println("Total count: " + count);
}
}