Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==============================================================================================
- | kode ini merupakan jawaban dari quiz ( https://ibb.co/ZKZKz5g ) yang sedikit dialihkan |
- | dengan HashMap agar tak menggunakan IF ( ^_^)//. |
- ==============================================================================================
- -----------------------------------------------------------------------------------
- Nama file: ASewa.Java
- --------------------------------------------------------------------------------------
- [package ---punya kamu---]
- import java.util.HashMap;
- import java.util.Map;
- abstract class AbsSewa {
- Map<String, String> mJenis;
- Map<String, String> mPelayanan;
- String nama;
- int total;
- public void pelayanan() {
- mPelayanan = new HashMap<>();
- //tampung kedalam "key" dan "value"
- mPelayanan.put("Eksekutif", "2500");
- mPelayanan.put("VIP", "3500");
- mPelayanan.put("Suite", "5500");
- System.out.println("--- P E L A Y A N A N ---");
- int no = 1;
- for (String i : mPelayanan.keySet()) {
- System.out.println( (no++) +". " +
- i + ": " + mPelayanan.get(i));
- }
- }
- public void jenis() {
- mJenis = new HashMap<>();
- //tampung kedalam "key" dan "value"
- mJenis.put("Anggrek", "3500");
- mJenis.put("Mawar", "5000");
- mJenis.put("Lily", "6500");
- System.out.println("--- J E N I S ---");
- int no = 1;
- for (String i : mJenis.keySet()) {
- System.out.println( (no++) +". " +
- i + ": " + mJenis.get(i));
- }
- }
- public void total() {
- this.total = 0;
- }
- String cekData( String nama ){
- this.nama = nama;
- return this.nama;
- }
- }
- ----------------------------------
- Nama File: Sewa.java
- -----------------------------------
- [package ---punya kamu---]
- public class Sewa extends AbsSewa{
- @Override
- String cekData(String nama) {
- return super.cekData(nama);
- }
- @Override
- public void total() {
- super.total = 0;
- }
- @Override
- public void pelayanan() {
- super.pelayanan();
- }
- @Override
- public void jenis() {
- super.jenis();
- }
- String[] getJenis(int x){
- Object xKey = super.mJenis.keySet().toArray()[x];
- Object xValue = super.mJenis.get(xKey);
- //set ke array
- String[] hasil = { xKey.toString(), xValue.toString() };
- return hasil;
- }
- String[] getPelayanan(int x){
- Object xKey = super.mPelayanan.keySet().toArray()[x];
- Object xValue = super.mPelayanan.get(xKey);
- //set ke array
- String[] hasil = { xKey.toString(), xValue.toString() };
- return hasil;
- }
- }
- ----------------------------------
- Nama File MainSewa.java
- -----------------------------------
- [package ---punya kamu---]
- import java.util.Scanner;
- public class MainSewa {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String nama, telepon;
- int jn, pl;
- Sewa s = new Sewa();
- System.out.print("Nama : ");
- nama = sc.nextLine();
- System.out.print("Telepon : ");
- telepon = sc.nextLine();
- s.jenis();
- s.pelayanan();
- System.out.print("Jenis : ");
- jn = sc.nextInt();
- System.out.print("Pelayanan: ");
- pl = sc.nextInt();
- System.out.println("--- Data Pemesanan ---");
- System.out.print("Nama :"+ nama);
- String[] hJenis = s.getJenis( ( jn-1 ) );
- System.out.println("Jenis : "+ hJenis[0] +" - "+ hJenis[1]);
- String[] hPelayanan= s.getPelayanan( ( pl-1 ) );
- System.out.println("Jenis : "+ hPelayanan[0] +" - "+ hPelayanan[1]);
- System.out.println("=======================");
- s.total = Integer.parseInt( hJenis[1] ) + Integer.parseInt( hPelayanan[1] );
- System.out.print("Total: "+ s.total );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement