-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuy_game.py
60 lines (47 loc) · 2.59 KB
/
buy_game.py
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
#Penulis Modul: Nicholas Liem - 16521108
#Judul Modul: List Game
#Tanggal: 17 April 2022
import operasi_array as arr
import constant as c
def buy_game(data_user, data_game, data_riwayat, data_kepemilikan, username):
user_id = username
game_id = input(c.bg_idGame)
#Mengambil data user (satu row)
row_data_user = arr.find_row_id(data_user, c.csvID_user_username, user_id)
data_user_baris = data_user[row_data_user]
saldo_user = int(data_user_baris[c.csvID_user_saldo])
#Mencari apakah game id yang dicari ada tau tidak
found_game_id = arr.found_in_kolom(data_game, c.csvID_game_id, game_id)
if found_game_id:
#Mengambil satu row baris data game
row_game_id = arr.find_row_id(data_game, c.csvID_game_id, game_id)
data_row_game_id = data_game[row_game_id]
data_pemilik_game = arr.all_valid_row(data_kepemilikan, c.csvID_game_id, game_id)
harga_game = int(data_row_game_id[c.csvID_game_price])
stok_game = data_row_game_id[c.csvID_game_stock]
sudah_punya = arr.found_in_kolom(data_pemilik_game, c.csvID_kepemilikan_user_id, username)
#Kasus sudah dimiliki
if sudah_punya:
print(c.bg_sudahPunya)
else: #Kasus game tidak dimiliki
#Kasus tidak memiliki saldo
if saldo_user < harga_game:
print(c.bg_saldoTidakCukup)
else: #Kasus tidak memiliki game, saldo user melebihi harga_game
if int(stok_game) == 0:
print(c.bg_stokHabis)
else: #Kasus tidak memiliki game, saldo user melebihi harga game, dan stok lebih dari 0. (Intinya kasus berhasil)
#Update saldo
data_user[row_data_user][c.csvID_user_saldo] = str(int(saldo_user - harga_game))
#Update stok
data_game[row_game_id][c.csvID_game_stock] = str(int(stok_game - 1)) #update stok game
# Bagian update kepemilikan
data_kepemilikan = arr.fungsi_append(data_kepemilikan, [game_id,user_id])
# Bagian update riwayat
nama_game = data_game[row_game_id][c.csvID_game_nama]
temp_riwayat = [game_id, nama_game, str(harga_game), username, "2022"]
data_riwayat = arr.fungsi_append(data_riwayat, temp_riwayat)
print(f"Kamu berhasil membeli '{nama_game}'! Saldo sekarang: {data_user[row_data_user][c.csvID_user_saldo]}.")
else:
print(c.bg_notFound)
return data_user, data_game, data_riwayat, data_kepemilikan