-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReservasiService.java
More file actions
271 lines (234 loc) · 10.3 KB
/
ReservasiService.java
File metadata and controls
271 lines (234 loc) · 10.3 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package com.mycompany.reservasi_lapangan_minisoccer.service;
import com.mycompany.reservasi_lapangan_minisoccer.model.*;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* Service untuk mengelola CRUD Reservasi dengan konsep OOP
* @author [Nama Anda]
*/
public class ReservasiService {
private ArrayList<Reservasi> daftarReservasi;
private Scanner scanner;
public ReservasiService() {
this.daftarReservasi = new ArrayList<>();
this.scanner = new Scanner(System.in);
}
public void tambahReservasi() {
System.out.println("\n=== TAMBAH RESERVASI ===");
try {
// Input nama dengan loop sampai benar
String nama;
while (true) {
System.out.print("Nama Pemesan: ");
nama = scanner.nextLine().trim();
if (!nama.isEmpty()) {
break;
}
System.out.println("Nama tidak boleh kosong!");
}
// Input tanggal dengan loop sampai benar
String tanggal;
while (true) {
System.out.print("Tanggal (dd-mm-yyyy): ");
tanggal = scanner.nextLine().trim();
if (!tanggal.isEmpty()) {
break;
}
System.out.println("Tanggal tidak boleh kosong!");
}
// Input jam dengan loop sampai benar
String jam;
while (true) {
System.out.print("Jam mulai (HH:MM): ");
jam = scanner.nextLine().trim();
if (!jam.isEmpty()) {
break;
}
System.out.println("Jam tidak boleh kosong!");
}
// Input durasi dengan loop sampai benar
int durasi;
while (true) {
try {
System.out.print("Durasi (jam): ");
durasi = scanner.nextInt();
if (durasi > 0) {
break;
}
System.out.println("Durasi harus lebih dari 0!");
} catch (InputMismatchException e) {
System.out.println("Durasi harus berupa angka!");
scanner.next();
}
}
scanner.nextLine();
// Pilihan jenis reservasi dengan loop sampai benar
int jenisReservasi;
while (true) {
try {
System.out.println("\nPilih jenis reservasi:");
System.out.println("1. Reguler (Rp 50.000/jam)");
System.out.println("2. VIP (Rp 100.000/jam)");
System.out.print("Pilihan (1-2): ");
jenisReservasi = scanner.nextInt();
scanner.nextLine();
if (jenisReservasi == 1 || jenisReservasi == 2) {
break;
}
System.out.println("Pilihan harus 1 atau 2!");
} catch (InputMismatchException e) {
System.out.println("Pilihan harus berupa angka!");
scanner.nextLine();
}
}
// Polymorphism - membuat objek berdasarkan pilihan
Reservasi reservasiBaru;
if (jenisReservasi == 1) {
reservasiBaru = new ReservasiReguler(nama, tanggal, jam, durasi);
System.out.println("\nKeuntungan Reservasi Reguler:");
((ReservasiReguler) reservasiBaru).tampilkanKeuntungan();
} else {
reservasiBaru = new ReservasiVIP(nama, tanggal, jam, durasi);
System.out.println("\nKeuntungan Reservasi VIP:");
((ReservasiVIP) reservasiBaru).tampilkanKeuntungan();
// Fitur khusus VIP - diskon member
System.out.print("\nApakah Anda member VIP? (y/n): ");
String isMemberInput = scanner.nextLine().trim().toLowerCase();
if (isMemberInput.equals("y")) {
double diskon = ((ReservasiVIP) reservasiBaru).hitungDiskonMember(true);
System.out.printf("Selamat! Anda mendapat diskon member: Rp %.0f\n", diskon);
}
}
daftarReservasi.add(reservasiBaru);
System.out.println("\nReservasi berhasil ditambahkan!");
// Method override - akan memanggil versi yang sesuai
System.out.println("Detail: " + reservasiBaru.infoReservasi());
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
public void lihatReservasi() {
System.out.println("\n=== DAFTAR RESERVASI ===");
if (daftarReservasi.isEmpty()) {
System.out.println("Belum ada data reservasi.");
return;
}
double totalPendapatan = 0;
int jumlahReguler = 0;
int jumlahVIP = 0;
for (int i = 0; i < daftarReservasi.size(); i++) {
Reservasi reservasi = daftarReservasi.get(i);
// Method override - format berbeda untuk setiap jenis
System.out.printf("%d. %s\n", (i + 1), reservasi.infoReservasi());
totalPendapatan += reservasi.hitungTotalBiaya();
// instanceof untuk mengecek jenis objek
if (reservasi instanceof ReservasiReguler) {
jumlahReguler++;
} else if (reservasi instanceof ReservasiVIP) {
jumlahVIP++;
}
}
System.out.println("\n=== STATISTIK ===");
System.out.printf("Total Reservasi: %d\n", daftarReservasi.size());
System.out.printf("Reguler: %d | VIP: %d\n", jumlahReguler, jumlahVIP);
System.out.printf("Total Pendapatan: Rp %.0f\n", totalPendapatan);
}
public void ubahReservasi() {
if (daftarReservasi.isEmpty()) {
System.out.println("Belum ada data reservasi untuk diubah.");
return;
}
System.out.println("\n=== UBAH RESERVASI ===");
lihatReservasi();
try {
System.out.print("\nPilih nomor reservasi yang ingin diubah: ");
int nomor = scanner.nextInt();
scanner.nextLine();
if (nomor < 1 || nomor > daftarReservasi.size()) {
System.out.println("Error: Nomor reservasi tidak valid!");
return;
}
Reservasi reservasi = daftarReservasi.get(nomor - 1);
System.out.println("Data saat ini: " + reservasi.infoReservasi());
System.out.println("\nPilih data yang ingin diubah:");
System.out.println("1. Nama Pemesan");
System.out.println("2. Tanggal");
System.out.println("3. Jam");
System.out.println("4. Durasi");
System.out.print("Pilihan (1-4): ");
int pilihan = scanner.nextInt();
scanner.nextLine();
switch (pilihan) {
case 1:
System.out.printf("Nama saat ini (%s): ", reservasi.getNamaPemesan());
String namaBaru = scanner.nextLine().trim();
if (!namaBaru.isEmpty()) {
// Menggunakan setter dengan validasi (Encapsulation)
reservasi.setNamaPemesan(namaBaru);
}
break;
case 2:
System.out.printf("Tanggal saat ini (%s): ", reservasi.getTanggal());
String tanggalBaru = scanner.nextLine().trim();
if (!tanggalBaru.isEmpty()) {
reservasi.setTanggal(tanggalBaru);
}
break;
case 3:
System.out.printf("Jam saat ini (%s): ", reservasi.getJam());
String jamBaru = scanner.nextLine().trim();
if (!jamBaru.isEmpty()) {
reservasi.setJam(jamBaru);
}
break;
case 4:
System.out.printf("Durasi saat ini (%d jam): ", reservasi.getDurasi());
try {
int durasiBaru = scanner.nextInt();
if (durasiBaru > 0) {
reservasi.setDurasi(durasiBaru);
}
} catch (InputMismatchException e) {
System.out.println("Error: Durasi harus berupa angka!");
}
break;
default:
System.out.println("Error: Pilihan tidak valid!");
return;
}
System.out.println("Reservasi berhasil diubah!");
System.out.println("Data baru: " + reservasi.infoReservasi());
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
public void hapusReservasi() {
if (daftarReservasi.isEmpty()) {
System.out.println("Belum ada data reservasi untuk dihapus.");
return;
}
System.out.println("\n=== HAPUS RESERVASI ===");
lihatReservasi();
try {
System.out.print("\nPilih nomor reservasi yang ingin dihapus: ");
int nomor = scanner.nextInt();
scanner.nextLine();
if (nomor < 1 || nomor > daftarReservasi.size()) {
System.out.println("Error: Nomor reservasi tidak valid!");
return;
}
Reservasi reservasiDihapus = daftarReservasi.remove(nomor - 1);
System.out.println("Reservasi berhasil dihapus!");
System.out.println("Data yang dihapus: " + reservasiDihapus.infoReservasi());
} catch (InputMismatchException e) {
System.out.println("Error: Input harus berupa angka!");
scanner.nextLine();
}
}
public void tutupScanner() {
if (scanner != null) {
scanner.close();
}
}
}