Laporan Praktikum Pemrograman Lanjut Bab 8 Fakultas Ilmu Komputer Universitas Brawijaya 2016/2017
Soal :
Source code :
Interface Payable
Class Invoice
Class Employee
Class Main
Download the file here
Soal :
Perusahaan
NV. Meneer memiliki koperasi karyawan yang memungkinkan karyawannya berbelanja
di koperasi tersebut. Tentunya, karyawan tersebut bisa membayar belanjaanya tersebut
di akhir bulan melalui pemotongan gaji. Ada 2 kelas yang terlibat disini,
Invoice dan Employee. Kedua class tadi mengimplementasikan interface Payable
yang mana ia hanya memiliki satu method yang harus diimplementasikan di kedua
class, yaitu getPayableAmount(). Program harus bisa mengolah gaji karyawan di
akhir bulan beserta invoice belanjaan karyawan yang nantinya gaji karyawan
perbulannya dikurang total harga belanjaanya secara polimorfis. Tampilkan
informasi dari karyawan tersebut beserta total gaji setelah dipotong hutang
belanjaan di koperasi dan tampilkan pula detail belanjaanya secara polimorfis
pula.
Attribut
dari Invoice: String productName, Integer quantity, Integer pricePerItem
Attribut dari Employee: Integer registrationNumber, String name, Integer
salaryPerMonth, Invoice[] invoices
Source code :
Interface Payable
package bab8interface; public interface Payable { public int getPayableAmount(); }
Class Invoice
package bab8interface; public class Invoice implements Payable { private String productName; private int quantity, pricePerItem; static int total; public Invoice(String productName, int quantity, int pricePerItem) { this.productName = productName; this.quantity = quantity; this.pricePerItem = pricePerItem; } public int getPayableAmount() { return quantity * pricePerItem; } public void getBought() { System.out.println("><><><><><><><><><><><><><><><><><"); System.out.println(quantity + " " + productName + " cost IDR " + getPayableAmount()); total += getPayableAmount(); } public static int getTotal() { return total; } }
Class Employee
package bab8interface; public class Employee implements Payable { private int registNumb, salaryPerMonth; private String name; private Invoice[] invoice; public Employee(String name, int registNumb, int salaryPerMonth, Invoice[] invoice) { this.name = name; this.registNumb = registNumb; this.salaryPerMonth = salaryPerMonth; this.invoice = invoice; } public void show() { System.out.println("My name is " + name); System.out.println("My registration number is " + registNumb); System.out.println("I get IDR " + salaryPerMonth + " monthly as my salary"); } public int getPayableAmount() { return salaryPerMonth - Invoice.getTotal(); } }
Class Main
package bab8interface; import java.util.Scanner; public class Main { public static void main(String[] args) { Invoice s = new Invoice("Airliner", 2, 45000); Invoice z = new Invoice("Bronze", 1, 210000); Invoice k = new Invoice("Concealer", 2, 179000); Invoice h = new Invoice("Matte lip liquid", 3, 129000); Invoice m = new Invoice("Mascara", 2, 199000); Invoice[] bought = {s, z, k, h, m}; Employee e = new Employee("Ayami Nakojo", 57430003, 10000000, bought); e.show(); for (int i = 0; i < bought.length; i++) { bought[i].getBought(); } System.out.println("><><><><><><><><><><><><><><><><><"); System.out.println("Stuffs I buy cost IDR " + Invoice.getTotal()); System.out.println("My total salary after being cut is IDR " + e.getPayableAmount()); } }
Download the file here
0 comments:
Posting Komentar