-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrud_AppArray.java
80 lines (71 loc) · 2.63 KB
/
Crud_AppArray.java
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
import java.util.ArrayList;
import java.util.Scanner;
public class CrudApp {
private static ArrayList<String> data = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean isRunning = true;
while (isRunning) {
System.out.println("Enter a command: (1) Insert, (2) Delete, (3) Update, (4) Read, (5) Exit");
int command = scanner.nextInt();
switch (command) {
case 1:
System.out.println("Enter data to insert:");
String insertData = scanner.next();
insert(insertData);
break;
case 2:
System.out.println("Enter index of data to delete:");
int deleteIndex = scanner.nextInt();
delete(deleteIndex);
break;
case 3:
System.out.println("Enter index of data to update:");
int updateIndex = scanner.nextInt();
System.out.println("Enter new data:");
String newData = scanner.next();
update(updateIndex, newData);
break;
case 4:
read();
break;
case 5:
isRunning = false;
break;
default:
System.out.println("Invalid command");
}
}
scanner.close();
}
private static void insert(String data) {
CrudApp.data.add(data);
System.out.println("Data inserted successfully");
}
private static void delete(int index) {
if (index >= 0 && index < data.size()) {
CrudApp.data.remove(index);
System.out.println("Data deleted successfully");
} else {
System.out.println("Invalid index");
}
}
private static void update(int index, String newData) {
if (index >= 0 && index < data.size()) {
CrudApp.data.set(index, newData);
System.out.println("Data updated successfully");
} else {
System.out.println("Invalid index");
}
}
private static void read() {
if (data.isEmpty()) {
System.out.println("No data to display");
} else {
System.out.println("Data:");
for (int i = 0; i < data.size(); i++) {
System.out.println(i + ": " + data.get(i));
}
}
}
}