Created
September 15, 2021 07:42
-
-
Save uluumbch/47bb74861a5e101e8e247a339d65c7a2 to your computer and use it in GitHub Desktop.
Aplikasi daftar nama negara berdasarkan input user, dibuat menggunakan LinkedList java dan HashMap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package praktikum3.soal2; | |
| import java.util.HashMap; | |
| public class Negara { | |
| private String nama; | |
| private String jenisKepemimpinan; | |
| private String namaPemimpin; | |
| private String tglMerdeka; | |
| private String blnMerdeka; | |
| private String thnMerdeka; | |
| public Negara(String nama, String jenisKepemimpinan, String namaPemimpin, String tglMerdeka, String blnMerdeka,String thnMerdeka) { | |
| this.nama = nama; | |
| this.jenisKepemimpinan = jenisKepemimpinan; | |
| this.namaPemimpin = namaPemimpin; | |
| this.tglMerdeka = tglMerdeka; | |
| this.blnMerdeka = blnMerdeka; | |
| this.thnMerdeka = thnMerdeka; | |
| } | |
| public String getTglMerdeka() { | |
| return tglMerdeka; | |
| } | |
| public String cetak() { | |
| HashMap<String, String> bulan = new HashMap<>(); | |
| bulan.put("1", "Januari"); | |
| bulan.put("2", "Februari"); | |
| bulan.put("3", "Maret"); | |
| bulan.put("4", "April"); | |
| bulan.put("5", "Mei"); | |
| bulan.put("6", "Juni"); | |
| bulan.put("7", "Juli"); | |
| bulan.put("8", "Agustus"); | |
| bulan.put("9", "September"); | |
| bulan.put("10", "Oktober"); | |
| bulan.put("11", "November"); | |
| bulan.put("12", "Desember"); | |
| if (jenisKepemimpinan.equalsIgnoreCase("monarki")) { | |
| return "Negara " + nama + " mempunyai raja bernama " + namaPemimpin + "\n"; | |
| } | |
| return "Negara " + nama + " mempunyai " + jenisKepemimpinan + " bernama " + namaPemimpin | |
| + "\nDeklarasi Kemerdekaan pada Tanggal " + tglMerdeka + " " + bulan.get(blnMerdeka) + " " + thnMerdeka | |
| + "\n"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package praktikum3.soal2; | |
| import java.util.LinkedList; | |
| import java.util.Scanner; | |
| public class NegaraMain { | |
| public static void main(String[] args) { | |
| LinkedList<Negara> negara = new LinkedList<>(); | |
| Scanner sc = new Scanner(System.in); | |
| int n = sc.nextInt(); // kode ini digunakan untuk menampung nilai baris baru (\n) dari hasil input variabel n | |
| sc.nextLine(); | |
| for (int i = 0; i < n; i++) { | |
| String nama = sc.nextLine(); | |
| String jenisKepemimpinan = sc.nextLine(); | |
| String namaPemimpin = sc.nextLine(); | |
| if (jenisKepemimpinan.equalsIgnoreCase("monarki")) { | |
| negara.add(new Negara(nama, jenisKepemimpinan, namaPemimpin, null, null, null)); | |
| continue; | |
| } | |
| String tglMerdeka = sc.nextLine(); | |
| String blnMerdeka = sc.nextLine(); | |
| String thnMerdeka = sc.nextLine(); | |
| negara.add(new Negara(nama, jenisKepemimpinan, namaPemimpin, tglMerdeka, blnMerdeka, thnMerdeka)); | |
| } | |
| for (int j = 0; j < n; j++) { | |
| System.out.println(negara.get(j).cetak()); | |
| } | |
| sc.close(); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Soal