-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal-project-simple.js
More file actions
83 lines (71 loc) · 2.51 KB
/
Copy pathfinal-project-simple.js
File metadata and controls
83 lines (71 loc) · 2.51 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
/**
* PROJECT: SISTEM KASIR SEDERHANA
* Menggabungkan: Array, Object, Arrow Function, forEach, Switch Case,
* Ternary Operator, dan Template Literals.
*/
// 1. Data Source (Array of Objects)
const keranjang = [
{ produk: "Tas", harga: 50000, jumlah: 1 },
{ produk: "Baju", harga: 30000, jumlah: 2 },
{ produk: "Topi", harga: 15000, jumlah: 0 }, // Contoh stok habis
];
// 2. Fungsi Utama Transaksi
const hitungTransaksi = (daftarBelanja, pakaiDiskon) => {
let totalBayar = 0;
console.log("===============================");
console.log(" RINCIAN BELANJA ");
console.log("===============================");
// A. Iterasi daftar belanja menggunakan forEach
daftarBelanja.forEach((item) => {
// Pengecekan stok menggunakan If-Else
if (item.jumlah > 0) {
const subTotal = item.harga * item.jumlah;
totalBayar += subTotal; // Operator Aritmatika (Compound Assignment)
// Template Literals untuk output yang rapi
console.log(
`- ${item.produk.padEnd(7)} | ${item.jumlah} pcs | Rp${subTotal.toLocaleString()}`,
);
} else {
console.log(`- ${item.produk.padEnd(7)} | STOK HABIS`);
}
});
let diskon = 0;
let hargaAkhir = totalBayar;
// B. Logika Diskon menggunakan Switch Case
// .toLowerCase() digunakan agar input tidak case-sensitive
switch (pakaiDiskon.toLowerCase()) {
case "iya":
// Ternary Operator: Syarat diskon jika belanja > 50.000
diskon = totalBayar > 50000 ? 10000 : 0;
hargaAkhir = totalBayar - diskon;
if (diskon === 0) {
console.log("\n[INFO] Belum mencapai syarat diskon Rp50.000");
} else {
console.log("\n[INFO] Selamat! Anda mendapatkan potongan Rp10.000");
}
break;
case "tidak":
diskon = 0;
hargaAkhir = totalBayar;
break;
default:
console.log(
"\n[WARNING] Input diskon tidak valid (gunakan 'iya'/'tidak')",
);
diskon = 0;
hargaAkhir = totalBayar;
break;
}
// 3. Output Final
console.log("-------------------------------");
console.log(`TOTAL GROSS : Rp${totalBayar.toLocaleString()}`);
console.log(`POTONGAN : Rp${diskon.toLocaleString()}`);
console.log("-------------------------------");
console.log(`TOTAL AKHIR : Rp${hargaAkhir.toLocaleString()}`);
console.log("===============================");
console.log(" Terima Kasih Sudah Belanja! ");
};
// 4. Eksekusi Program
// Argumen 1: Data keranjang
// Argumen 2: Pilihan diskon ("iya" atau "tidak")
hitungTransaksi(keranjang, "iya");