Java lang
import java.util.Date;
// Klas Induk (Abstract)
abstract class Product {
. protected String id;
. protected String name;
. protected int price;
. protected int stock;
. public Product(String id, String name, int price) {
. this.id = id;
. this.name = name;
. this.price = price;
. this.stock = 10; // Otomatis stock bernilai 10
. }
. public abstract void displayInfo();
}
// Klas Turunan untuk Food
class Food extends Product {
. private String expiryDate;
. public Food(String id, String name, int price, String expiryDate) {
. super(id, name, price);
. this.expiryDate = expiryDate;
. }
. @Override
. public void displayInfo() {
. System.out.printf(“| %-6s | %-6s | %-15s | Rp%-8d | %-5d | Exp: %-10s |\n”,
Get Florenza Patricia’s stories in your inbox
Join Medium for free to get updates from this writer.
. “Food”, id, name, price, stock, expiryDate);
. }
}
// Klas Turunan untuk Medicine
class Medicine extends Product {
. private String expiryDate;
. public Medicine(String id, String name, int price, String expiryDate) {
. super(id, name, price);
. this.expiryDate = expiryDate;
. }
. @Override
. public void displayInfo() {
. System.out.printf(“| %-6s | %-6s | %-15s | Rp%-8d | %-5d | Exp: %-10s |\n”,
. “Med”, id, name, price, stock, expiryDate);
. }
}
// Klas Turunan untuk Toys
class Toy extends Product {
. public Toy(String id, String name, int price) {
. super(id, name, price);
. }
. @Override
. public void displayInfo() {
. System.out.printf(“| %-6s | %-6s | %-15s | Rp%-8d | %-5d | Exp: %-10s |\n”,
. “Toy”, id, name, price, stock, “N/A”);
. }
}





