-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from idabagusangga/master
live code 4 om svaha
- Loading branch information
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// implementasi class Penumpang tidak boleh diubah | ||
class Penumpang { | ||
constructor(nama, tujuan) { | ||
this.nama = nama; | ||
this.tujuan = tujuan; | ||
this.jarak = 0; | ||
} | ||
tambahJarak() { | ||
this.jarak++; | ||
} | ||
bayar() { | ||
return this.jarak * 150; | ||
} | ||
} | ||
|
||
class Angkot{ | ||
constructor(penumpang,penghasilan){ | ||
this.penumpang = [] | ||
this.penghasilan = 0 | ||
} | ||
jalan(){ | ||
for(var i = 0;i<this.penumpang.length;i++){ | ||
this.penumpang[i].jarak+=2.2 | ||
} | ||
|
||
|
||
} | ||
naik(arg1,arg2){ | ||
var pass = new Penumpang(arg1,arg2); | ||
this.penumpang.push(pass) | ||
// console.log(this.penumpang) | ||
} | ||
turun(argumen){ | ||
for(var i = 0; i < this.penumpang.length; i++){ | ||
if(this.penumpang[i].tujuan == argumen){ | ||
// console.log(this.penumpang[i].bayar()) | ||
this.penghasilan += this.penumpang[i].bayar() | ||
// console.log(this.penghasilan) | ||
} | ||
else{ | ||
console.log('masi jauh pak'+' tujuan saya di'+this.penumpang[i].tujuan + ` nama saya ${this.penumpang[i].nama}`) | ||
} | ||
} | ||
|
||
} | ||
selesai(){ | ||
console.log(this.penghasilan) | ||
} | ||
} | ||
|
||
// CONSTRUCTOR | ||
// tambahkan 2 properti | ||
// - (a) untuk menampung penumpang-penumpang | ||
// - (b) untuk menyimpan penghasilan | ||
|
||
// METHOD jalan() | ||
// tambahkan jarak tempuh setiap penumpang | ||
|
||
// METHOD naik(argumen1, argumen2) | ||
// komposisi objek dari class Penumpang dgn data dari argumen | ||
// lalu tambahkan ke properti (a) | ||
|
||
// METHOD turun(argumen) | ||
// cek tujuan setiap penumpang | ||
// bila tujuan penumpang == argumen | ||
// tagih bayaran dan totalkan ke properti (b) | ||
// keluarkan penumpang dari properti (a) | ||
// console.log properti (a) | ||
|
||
// METHOD selesai() | ||
// tampilkan total penghasilan | ||
|
||
// driver code #tidak boleh diubah# | ||
let angkot = new Angkot(); | ||
angkot.naik('Anung', 'kolong'); | ||
angkot.naik('Bhoma', 'karet'); | ||
angkot.naik('Cucun', 'ujung'); | ||
// angkot berangkat! | ||
angkot.jalan(); | ||
angkot.jalan(); | ||
// kolong | ||
angkot.turun('kolong'); | ||
angkot.jalan(); | ||
// karet | ||
angkot.turun('karet'); | ||
angkot.jalan(); | ||
angkot.jalan(); | ||
angkot.jalan(); | ||
// ujung | ||
// angkot.turun('ujung'); | ||
angkot.selesai(); // total penghasilan: 1650 | ||
// driver code #tidak boleh diubah# | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
class Cart{ | ||
constructor(item){ | ||
this.item = [] | ||
} | ||
addItem(arg){ | ||
this.item.push(arg); | ||
// console.log(this.item) | ||
} | ||
editItemQuantity(arg1,arg2){ | ||
for(var i = 0; i < this.item.length;i++){ | ||
if(this.item[i].nama == arg1){ | ||
// console.log(this.item[i]) | ||
this.item[i].jumlah = arg2 | ||
// console.log(this.item[i].jumlah) | ||
} | ||
} | ||
} | ||
removeItem(arg1){ | ||
for(var i = 0; i < this.item.length;i++){ | ||
if(this.item[i].nama == arg1){ | ||
this.item.splice(i,1) | ||
} | ||
console.log(this.item) | ||
} | ||
} | ||
checkout(){ | ||
var total = 0 | ||
for(var i = 0; i<this.item.length;i++){ | ||
total += this.item[i].jumlah * this.item[i].harga | ||
} | ||
console.log(total) | ||
} | ||
} | ||
|
||
class Item{ | ||
constructor(nama,jumlah,harga){ | ||
this.nama = nama | ||
this.jumlah = jumlah | ||
this.harga = harga | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
let cart = new Cart(); // objek cart | ||
cart.addItem(new Item("pensil", 50, 100)); // tambahkan belanja 50 pensil, harga per-item 100 | ||
cart.addItem(new Item("buku", 20, 200)); | ||
cart.addItem(new Item("ondel-ondel", 1, 1000000)); | ||
cart.editItemQuantity("pensil", 60); // mengubah belanjaan pensil menjadi 60 buah | ||
cart.removeItem("buku"); // membatalkan belanja ondel-ondel | ||
cart.checkout(); // menampilkan detail belanjaan dan total |