Created
January 25, 2019 15:40
-
-
Save ilhamarrouf/5e287a44dfe29b9565f1e4ca36bd26fb to your computer and use it in GitHub Desktop.
Example Phone Book
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
import java.util.*; | |
import java.io.*; | |
public class PhoneBook { | |
public static void main(String[] args) { | |
Scanner reader = new Scanner(System.in); | |
Scanner contactNameReader = new Scanner(System.in); | |
Scanner contactNumberReader = new Scanner(System.in); | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); | |
ArrayList<Contact> contacts = new ArrayList<>(); | |
String again = "Y"; | |
int menu; | |
while (again.equals("Y")) { | |
System.out.print("\033[H\033[2J"); | |
System.out.println("My Phone Book"); | |
System.out.println("1. Daftar Kontak"); | |
System.out.println("2. Tambah Kontak"); | |
System.out.print("Masukkan pilihan anda : "); | |
menu = reader.nextInt(); | |
switch (menu) { | |
case 1: | |
System.out.print("\033[H\033[2J"); | |
contacts.forEach((contact) -> { | |
System.out.printf("%-20s", contact.name); | |
System.out.printf("%-20s", contact.number); | |
System.out.println(""); | |
}); | |
break; | |
case 2: | |
System.out.print("\033[H\033[2J"); | |
System.out.print("Nama : "); | |
String name = contactNameReader.next(); | |
System.out.print("Nomor : "); | |
String number = contactNumberReader.next(); | |
contacts.add(new Contact(name, number)); | |
break; | |
} | |
System.out.print("Kembali ke menu utama? (Y/T) : "); | |
try { | |
again = bufferedReader.readLine(); | |
} catch (IOException e) { | |
} | |
} | |
} | |
public static class Contact { | |
String name; | |
String number; | |
public Contact(String name, String number) { | |
this.name = name; | |
this.number = number; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment