Kelompok-9-PA-CAPSTONE / kerjakan-codingan-nya

Ketentuan: Wajib Manajemen Data (CRUD). Program wajib menggunakan salah satu struktur data (Linked List, Stack, Queue, Tree, Graph), disesuaikan dengan judul project kalian. Memiliki Fitur Sorting dan Searching. Memiliki Multiuser. Terdapat Login.
0 stars 0 forks source link

kerjakan codingan nya #1

Open Nuno-Hadianto opened 7 months ago

Shofwanf commented 7 months ago

import mysql.connector from prettytable import PrettyTable

mydb = mysql.connector.connect( host="localhost", user="root", password="", database="pemkot" ) mycursor = mydb.cursor()

class Node: def init(self, data): self.data = data self.next = None

class LinkedList: def init(self): self.head = None

def append(self, data):
    new_node = Node(data)
    if not self.head:
        self.head = new_node
        return
    last_node = self.head
    while last_node.next:
        last_node = last_node.next
    last_node.next = new_node

def display_admin(self):
    current = self.head
    x = PrettyTable()
    x.field_names = ["id_orang", "username", "password", "hak_akses", "email"]
    while current:
        x.add_row(current.data)
        current = current.next
    print(x)

def display_samarinda(self):
    current = self.head
    x = PrettyTable()
    x.field_names = ["id_samarinda", "kelurahan", "kecamatan", "kode_pos"]
    while current:
        x.add_row(current.data)
        current = current.next
    print(x)

def display_pemerintah(self):
    current = self.head
    x = PrettyTable()
    x.field_names = ["id_pemerintah", "pejabat", "gelar", "partai", "hak_akses"]
    while current:
        x.add_row(current.data)
        current = current.next
    print(x)

def display_program(self):
    current = self.head
    x = PrettyTable()
    x.field_names = ["id_program", "nama_program", "bidang_program", "status_program"]
    while current:
        x.add_row(current.data)
        current = current.next
    print(x)

def quick_sort(arr, ascending=True): if len(arr) <= 1: return arr pivot = arr[len(arr) // 2][0] left = [x for x in arr if x[0] < pivot] middle = [x for x in arr if x[0] == pivot] right = [x for x in arr if x[0] > pivot] if ascending: return quick_sort(left, ascending) + middle + quick_sort(right, ascending) else: return quick_sort(right, ascending) + middle + quick_sort(left, ascending)

def jump_search(arr, x): n = len(arr) step = int(n 0.5) prev = 0 while arr[min(step, n)-1][0] < x: prev = step step += int(n 0.5) if prev >= n: return -1 while arr[prev][0] < x: prev += 1 if prev == min(step, n): return -1 if arr[prev][0] == x: return prev return -1

def read_admin(): mycursor.execute("SELECT * FROM admin") myresult = mycursor.fetchall() ll = LinkedList() for data in myresult: ll.append(data) ll.display_admin()

def tambah_data_admin(): id_orang = int(input("Masukkan ID Orang: ")) username = input("Masukkan Username Admin: ") password = input("Masukkan Password Admin: ") hak_akses = input("Masukkan Hak Akses Admin: ") email = input("Masukkan Email Admin: ") query = f""" INSERT INTO admin (id_orang, username, password, hak_akses, email) VALUES ({id_orang}, '{username}', '{password}', '{hak_akses}', '{email}') """

mycursor.execute(query)
mydb.commit()
print("Data Admin berhasil ditambahkan")

def hapus_data_admin(): read_admin() id_orang = int(input("Masukkan ID Orang yang ingin dihapus: "))

query = f"DELETE FROM admin WHERE id_orang = {id_orang}"

mycursor.execute(query)
mydb.commit()
print("Data Admin berhasil dihapus")

def perbarui_data_admin(): read_admin() id_orang = int(input("Masukkan ID Admin yang ingin diperbarui: ")) username = input("Masukkan Username Admin Baru: ") password = input("Masukkan Password Admin Baru: ") hak_akses = input("Masukkan Hak Akses Admin Baru: ") email = input("Masukkan Email Admin Baru: ")

query = f"""
UPDATE admin
SET nama_admin = '{username}',
password_admin = '{password}',
hak_akses = '{hak_akses}'
email = '{email}'
WHERE id_orang = {id_orang}
"""

mycursor.execute(query)
mydb.commit()
print("Data Admin berhasil diperbarui")

def read_samarinda(): mycursor.execute("SELECT * FROM samarinda") myresult = mycursor.fetchall() data_list = [] for data in myresult: data_list.append(data)

while True:
    print("============================================================")
    print("|                 TAMPILKAN DATA SAMARINDA                 |")
    print("============================================================")
    print("|   1. Tampilkan Data (urutkan berdasarkan ID - Ascending) |")
    print("|   2. Tampilkan Data (urutkan berdasarkan ID - Descending)|")
    print("|   3. Cari Data (berdasarkan ID)                          |")
    print("|   4. Kembali ke Menu Utama                               |")
    print("============================================================")

    pilihan = input("Masukkan pilihan Anda: ")

    if pilihan == "1":
        sorted_data = quick_sort(data_list, ascending=True)
        ll = LinkedList()
        for data in sorted_data:
            ll.append(data)
        ll.display_samarinda()
    elif pilihan == "2":
        sorted_data = quick_sort(data_list, ascending=False)
        ll = LinkedList()
        for data in sorted_data:
            ll.append(data)
        ll.display_samarinda()
    elif pilihan == "3":
        id_to_search = int(input("Masukkan ID yang ingin dicari: "))
        result_index = jump_search(data_list, id_to_search)
        if result_index != -1:
            print("Data ditemukan:")
            x = PrettyTable()
            x.field_names = ["id_samarinda", "kecamatan", "kelurahan", "kode_pos"]
            x.add_row(data_list[result_index])
            print(x)
        else:
            print("Data tidak ditemukan.")
    elif pilihan == "4":
        break
    else:
        print("Pilihan tidak valid. Silakan pilih kembali.")

def tambah_data_samarinda(): id_samarinda = int(input("Masukkan ID Samarinda: ")) kecamatan = input("Masukkan Nama Kecamatan: ") kelurahan = input("Masukkan Nama Kelurahan: ") kode_pos = int(input("Masukkan Kode Pos: ")) query = f""" INSERT INTO samarinda (id_samarinda, kecamatan, kelurahan, kode_pos) VALUES ({id_samarinda}, '{kecamatan}', '{kelurahan}', {kode_pos}) """

mycursor.execute(query)
mydb.commit()
print("Data berhasil ditambahkan")

def hapus_data_samarinda(): mycursor.execute("SELECT * FROM samarinda") myresult = mycursor.fetchall() x = PrettyTable() x.field_names = ["id_samarinda", "kecamatan", "kelurahan", "kode_pos"] for data in myresult: x.add_row(data) print(x)

id_samarinda = int(input("Masukkan ID Samarinda yang ingin dihapus: "))

query = f"DELETE FROM samarinda WHERE id_samarinda = {id_samarinda}"

mycursor.execute(query)
mydb.commit()
print("Data berhasil dihapus")

def perbarui_data_samarinda(): mycursor.execute("SELECT * FROM samarinda") myresult = mycursor.fetchall() x = PrettyTable() x.field_names = ["id_samarinda", "kecamatan", "kelurahan", "kode_pos"] for data in myresult: x.add_row(data) print(x)

id_samarinda = int(input("Masukkan ID Samarinda yang ingin diperbarui: "))
kecamatan = input("Masukkan Nama Kecamatan Baru: ")
kelurahan = input("Masukkan Nama Kelurahan Baru: ")
kode_pos = int(input("Masukkan Kode Pos Baru: "))

query = f"""
UPDATE samarinda
SET kecamatan = '{kecamatan}',
kelurahan = '{kelurahan}',
kode_pos = {kode_pos}
WHERE id_samarinda = {id_samarinda}
"""

mycursor.execute(query)
mydb.commit()
print("Data berhasil diperbarui")

def read_pemerintah(): mycursor.execute("SELECT * FROM pemerintah") myresult = mycursor.fetchall() data_list = [] for data in myresult: data_list.append(data)

while True:
    print("============================================================")
    print("|                 TAMPILKAN DATA PEMERINTAH                |")
    print("============================================================")
    print("|   1. Tampilkan Data (urutkan berdasarkan ID - Ascending) |")
    print("|   2. Tampilkan Data (urutkan berdasarkan ID - Descending)|")
    print("|   3. Cari Data (berdasarkan ID)                          |")
    print("|   4. Kembali ke Menu Utama                               |")
    print("============================================================")

    pilihan = input("Masukkan pilihan Anda: ")

    if pilihan == "1":
        sorted_data = quick_sort(data_list, ascending=True)
        ll = LinkedList()
        for data in sorted_data:
            ll.append(data)
        ll.display_pemerintah()
    elif pilihan == "2":
        sorted_data = quick_sort(data_list, ascending=False)
        ll = LinkedList()
        for data in sorted_data:
            ll.append(data)
        ll.display_pemerintah()
    elif pilihan == "3":
        id_to_search = int(input("Masukkan ID yang ingin dicari: "))
        result_index = jump_search(data_list, id_to_search)
        if result_index != -1:
            print("Data ditemukan:")
            x = PrettyTable()
            x.field_names = ["id_pemerintah", "nama_pejabat", "jabatan", "partai", "hak_akses"]
            x.add_row(data_list[result_index])
            print(x)
        else:
            print("Data tidak ditemukan.")
    elif pilihan == "4":
        break
    else:
        print("Pilihan tidak valid. Silakan pilih kembali.")

def tambah_data_pemerintah(): id_pemerintah = int(input("Masukkan ID Pemerintah: ")) nama_pejabat = input("Masukkan Nama Pemerintah Baru: ") jabatan = input("Masukkan Jabatan Baru: ") partai = input("Masukkan Partai Baru: ") hak_akses = input("Masukkan Hak Akses Baru: ")

query = f"""
INSERT INTO pemerintah (id_pemerintah, nama_pejabat, jabatan, partai, hak_akses)
VALUES ({id_pemerintah}, '{nama_pejabat}', '{jabatan}', '{partai}', '{hak_akses}')
"""

mycursor.execute(query)
mydb.commit()
print("Data berhasil ditambahkan")

def hapus_data_pemerintah(): mycursor.execute("SELECT * FROM pemerintah") myresult = mycursor.fetchall() x = PrettyTable() x.field_names = ["id_pemerintah", "nama_pejabat", "jabatan", "partai", "hak_akses"] for data in myresult: x.add_row(data) print(x)

id_pemerintah = int(input("Masukkan ID Pemerintah yang ingin dihapus: "))

query = f"DELETE FROM pemerintah WHERE id_pemerintah = {id_pemerintah}"

mycursor.execute(query)
mydb.commit()
print("Data berhasil dihapus")

def perbarui_data_pemerintah(): mycursor.execute("SELECT * FROM pemerintah") myresult = mycursor.fetchall() x = PrettyTable() x.field_names = ["id_pemerintah", "nama_pejabat", "jabatan", "partai", "hak_akses"] for data in myresult: x.add_row(data) print(x)

id_pemerintah = int(input("Masukkan ID Pemerintah yang ingin diperbarui: "))
nama_pemerintah = input("Masukkan Nama Pemerintah Baru: ")
jabatan = input("Masukkan Jabatan Baru: ")
partai = input("Masukkan Partai Baru: ")
hak_akses = input("Masukkan Hak Akses Baru: ")

query = f"""
UPDATE pemerintah
SET nama_pejabat = '{nama_pemerintah}',
jabatan = '{jabatan}',
partai = '{partai}',
hak_akses = '{hak_akses}'
WHERE id_pemerintah = {id_pemerintah}
"""

mycursor.execute(query)
mydb.commit()
print("Data berhasil diperbarui")

def read_program(): mycursor.execute("SELECT * FROM program") myresult = mycursor.fetchall() data_list = [] for data in myresult: data_list.append(data)

while True:
    print("============================================================")
    print("|                  TAMPILKAN DATA PROGRAM                  |")
    print("============================================================")
    print("|   1. Tampilkan Data (urutkan berdasarkan ID - Ascending) |")
    print("|   2. Tampilkan Data (urutkan berdasarkan ID - Descending)|")
    print("|   3. Cari Data (berdasarkan ID)                          |")
    print("|   4. Kembali ke Menu Utama                               |")
    print("============================================================")
    pilihan = input("Masukkan pilihan Anda: ")

    if pilihan == "1":
        sorted_data = quick_sort(data_list, ascending=True)
        ll = LinkedList()
        for data in sorted_data:
            ll.append(data)
        ll.display_program()
    elif pilihan == "2":
        sorted_data = quick_sort(data_list, ascending=False)
        ll = LinkedList()
        for data in sorted_data:
            ll.append(data)
        ll.display_program()
    elif pilihan == "3":
        id_to_search = int(input("Masukkan ID yang ingin dicari: "))
        result_index = jump_search(data_list, id_to_search)
        if result_index != -1:
            print("Data ditemukan:")
            x = PrettyTable()
            x.field_names = ["id_program", "nama_program", "bidang_program", "status_program"]
            x.add_row(data_list[result_index])
            print(x)
        else:
            print("Data tidak ditemukan.")
    elif pilihan == "4":
        break
    else:
        print("Pilihan tidak valid. Silakan pilih kembali.")

def tambah_data_program(): id_program = int(input("Masukkan ID Program: ")) nama_program = input("Masukkan Nama Proyek: ") bidang_program = input("Masukkan Bidang Program: ") status_program = input("Masukkan Status Program: ") query = f""" INSERT INTO program (id_program, nama_program, bidang_program, status_program) VALUES ({id_program}, '{nama_program}', '{bidang_program}', '{status_program}') """

mycursor.execute(query)
mydb.commit()
print("Data berhasil ditambahkan")

def hapus_data_program(): mycursor.execute("SELECT * FROM program") myresult = mycursor.fetchall() x = PrettyTable() x.field_names = ["id_program", "nama_program", "bidang_program", "status_program"] for data in myresult: x.add_row(data) print(x)

id_program = int(input("Masukkan ID Program yang ingin dihapus: "))

query = f"DELETE FROM program WHERE id_program = {id_program}"

mycursor.execute(query)
mydb.commit()
print("Data berhasil dihapus")

def perbarui_data_program(): mycursor.execute("SELECT * FROM program") myresult = mycursor.fetchall() x = PrettyTable() x.field_names = ["id_program", "nama_program", "bidang_program", "status_program"] for data in myresult: x.add_row(data) print(x)

id_program = int(input("Masukkan ID Program yang ingin diperbarui: "))
nama_program = input("Masukkan Nama Program Baru: ")
bidang_program = input("Masukkan Bidang Program Baru: ")
status_program = input("Masukkan Status Program Baru: ")

query = f"""
UPDATE program
SET nama_program = '{nama_program}',
bidang_program = '{bidang_program}',
status_program = '{status_program}'
WHERE id_program = {id_program}
"""

mycursor.execute(query)
mydb.commit()
print("Data berhasil diperbarui")

def menu_utama_admin(): while True: print("============================================================") print("| DATABASE ADMIN |") print("============================================================") print("| 1. TAMBAH DATA ADMIN |") print("| 2. HAPUS DATA ADMIN |") print("| 3. PERBARUI DATA ADMIN |") print("| 4. TAMPILKAN DATA ADMIN |") print("| 5. Keluar |") print("============================================================") pilihan = input("Masukkan pilihan Anda: ")

    if pilihan == "1":
        tambah_data_admin()
    elif pilihan == "2":
        hapus_data_admin()
    elif pilihan == "3":
        perbarui_data_admin()
    elif pilihan == "4":
        read_admin()
    elif pilihan == "5":
        print("Terima kasih!")
        break
    else:
        print("Pilihan tidak valid. Silakan pilih kembali.")

def menu_utama_samarinda(): while True: print("============================================================") print("| DATABASE SAMARINDA |") print("============================================================") print("| 1. TAMBAH DATA SAMARINDA |") print("| 2. HAPUS DATA SAMARINDA |") print("| 3. PERBARUI DATA SAMARINDA |") print("| 4. TAMPILKAN DATA SAMARINDA |") print("| 5. Keluar |") print("============================================================") pilihan = input("Masukkan pilihan Anda: ")

    if pilihan == "1":
        tambah_data_samarinda()
    elif pilihan == "2":
        hapus_data_samarinda()
    elif pilihan == "3":
        perbarui_data_samarinda()
    elif pilihan == "4":
        read_samarinda()
    elif pilihan == "5":
        print("Terima kasih!")
        break
    else:
        print("Pilihan tidak valid. Silakan pilih kembali.")

def menu_utama_pemerintah(): while True: print("============================================================") print("| DATABASE PEMERINTAH |") print("============================================================") print("| 1. TAMBAH DATA PEMERINTAH |") print("| 2. HAPUS DATA PEMERINTAH |") print("| 3. PERBARUI DATA PEMERINTAH |") print("| 4. TAMPILKAN DATA PEMERINTAH |") print("| 5. Keluar |") print("============================================================") pilihan = input("Masukkan pilihan Anda: ")

    if pilihan == "1":
        tambah_data_pemerintah()
    elif pilihan == "2":
        hapus_data_pemerintah()
    elif pilihan == "3":
        perbarui_data_pemerintah()
    elif pilihan == "4":
        read_pemerintah()
    elif pilihan == "5":
        print("Terima kasih!")
        break
    else:
        print("Pilihan tidak valid. Silakan pilih kembali.")

def menu_utama_program(): while True: print("============================================================") print("| DATABASE PROGRAM |") print("============================================================") print("| 1. TAMBAH DATA PROGRAM |") print("| 2. HAPUS DATA PROGRAM |") print("| 3. PERBARUI DATA PROGRAM |") print("| 4. TAMPILKAN DATA PROGRAM |") print("| 5. Keluar |") print("============================================================") pilihan = input("Masukkan pilihan Anda: ")

    if pilihan == "1":
        tambah_data_program()
    elif pilihan == "2":
        hapus_data_program()
    elif pilihan == "3":
        perbarui_data_program()
    elif pilihan == "4":
        read_program()
    elif pilihan == "5":
        print("Terima kasih!")
        break
    else:
        print("Pilihan tidak valid. Silakan pilih kembali.")

def menu_utama_superuser(): while True: print("============================================================") print(" selamat datang:",username ) print("============================================================") print("| 1. DATABASE ADMIN |") print("| 2. DATABASE SAMARINDA |") print("| 3. DATABASE PEMERINTAH |") print("| 4. DATABASE PROGRAM |") print("| 5. Keluar |") print("============================================================") pilihan = input("Masukkan pilihan Anda: ")

    if pilihan == "1":
        menu_utama_admin()
    elif pilihan == "2":
        menu_utama_samarinda()
    elif pilihan == "3":
        menu_utama_pemerintah()
    elif pilihan == "4":
        menu_utama_program()
    elif pilihan == "5":
        print("Terima kasih!")
        break
    else:
        print("Pilihan tidak valid. Silakan pilih kembali.")

class LoginSystem: def init(self): self.users = { "nuno": "kemakmuran", "asnan": "bengkuring", "shofwan": "kemuning", "trisna": "pemuda" }

def register(self, username, password):
    if username in self.users:
        print("Username sudah ada. Silakan pilih username lain.")
    else:
        self.users[username] = password
        print("Pendaftaran berhasil. Silakan login dengan akun baru Anda.")

def login(self, username, password):
    if username in self.users:
        if self.users[username] == password:
            menu_utama_superuser()
        else:
            print("Password salah. Silakan coba lagi.")
    else:
        print("Username tidak ditemukan.")

login_system = LoginSystem()

while True: print("============================================================") print("| Sistem Informasi Samarinda dan Pemerintah Berkelanjutan |") print("============================================================") print("| 1. Login |") print("| 2. Buat Akun Baru |") print("| 3. Keluar |") print("============================================================") choice = input("Pilih menu: ")

if choice == "1":
    username = input("Masukkan username: ")
    password = input("Masukkan password: ")
    login_system.login(username, password)
elif choice == "2":
    username = input("Masukkan username baru: ")
    password = input("Masukkan password baru: ")
    login_system.register(username, password)
elif choice == "3":
    print("Terima kasih!")
    break
else:
    print("Pilihan tidak valid. Silakan pilih kembali.")