Skip to content

Instantly share code, notes, and snippets.

@uluumbch
Created September 29, 2021 09:22
Show Gist options
  • Save uluumbch/3a4626a9cee2d75fcedda3af4dab551a to your computer and use it in GitHub Desktop.
Save uluumbch/3a4626a9cee2d75fcedda3af4dab551a to your computer and use it in GitHub Desktop.
program java sederhana dengan constructor, abstract, input
package praktikum4.soal2;
public abstract class Buku {
protected String judul;
protected String penulis;
protected int tahun;
protected void display() {
}
}
package praktikum4.soal2;
public class Komik extends Buku {
private int volume;
private String sinopsis;
public Komik(String j, String p, String t, int v, String s) {
judul = j;
penulis = p;
tahun = Integer.parseInt(t);
volume = v;
sinopsis = s;
}
public String getKomikDetail() {
return "Data yang baru di inputkan adalah\nSebuah Komik dengan judul " + judul
+ ".\nKomik tersebut dibuat oleh " + penulis + " dan diterbitkan pada tahun " + tahun
+ " .Sampai saat ini komik tersebut memiliki jumlah volume sebanyak " + volume + " buah."
+ "\nSinopsis : " + sinopsis.substring(0, 64) + "...";
}
}
package praktikum4.soal2;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int pilih;
System.out.println("Pilih buku yang ingin diinputkan:");
System.out.println("1 = Novel");
System.out.println("2 = Komik");
System.out.println("Masukkan pilihan: ");
pilih = sc.nextInt();
sc.nextLine();
switch (pilih) {
case 1:
System.out.println("Judul: ");
String j = sc.nextLine();
System.out.println("Penulis: ");
String p = sc.nextLine();
System.out.println("Tahun Terbit: ");
String t = sc.nextLine();
System.out.println("Genre :");
String g = sc.nextLine();
System.out.println("Sinposis: ");
String s = sc.nextLine();
System.out.println(s);
Novel novel = new Novel(j, p, t, g, s);
System.out.println();
System.out.println(novel.getNovelDetail());
break;
case 2:
System.out.println("Judul: ");
String jk = sc.nextLine();
System.out.println("Penulis: ");
String pk = sc.nextLine();
System.out.println("Tahun Terbit: ");
String tk = sc.nextLine();
System.out.println("Volume :");
int vk = sc.nextInt();
sc.nextLine();
System.out.println("Sinopsis: ");
String sk = sc.nextLine();
Komik komik = new Komik(jk, pk, tk, vk, sk);
System.out.println();
System.out.println(komik.getKomikDetail());
break;
default:
System.out.println("Perintah tidak ditemukan!!!");
break;
}
sc.close();
}
}
package praktikum4.soal2;
public class Novel extends Buku {
private String genre;
private String sinopsis;
public Novel(String j, String p, String t, String g, String s) {
judul = j;
penulis = p;
tahun = Integer.parseInt(t);
genre = g;
sinopsis = s;
}
public String getNovelDetail() {
return "Data yang baru di inputkan adalah\nSebuah novel bergenre Drama dengan judul " + judul
+ " .\nNovel tersebut ditulis oleh " + penulis + " dan diterbitkan pada tahun " + tahun
+ " . \nSinopsis : " + sinopsis.substring(0, 59) + "...";
}
}
@alkusr
Copy link

alkusr commented Oct 6, 2021

Izin comot ngab😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment