Advertisement
Telor_Goreng0

nomor 3 laporan 5

May 2nd, 2021
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. package SDA;
  2. import java.util.Scanner;
  3. import java.util.Arrays;
  4. import java.util.LinkedList;
  5. class bukutelepon{
  6. static LinkedList <String> kontak = new LinkedList<String>();
  7. static String dataNode[] = new String[3];
  8. static Scanner data = new Scanner(System.in);
  9. static Scanner id = new Scanner(System.in);
  10. public static void addKontak(){
  11. System.out.print("Masukkan nama kontak yang ingin ditambahkan: ");
  12. String nama = data.nextLine();
  13. System.out.print("Masukkan nomor teleponnya: ");
  14. String nomor = data.nextLine();
  15. System.out.print("Masukkan alamatnya: ");
  16. String alamat = data.nextLine();
  17. String dataNode[] = {nama,nomor,alamat};
  18. String dataString = Arrays.toString(dataNode);
  19. kontak.add(dataString);}
  20. public static void removeKontak(){
  21. try {
  22. displayKontak();
  23. System.out.print("Masukkan indeks kontak yang ingin dihapuskan: ");
  24. int hapus = id.nextInt();
  25. kontak.remove(hapus);
  26. } catch (Exception e) {
  27. System.out.println("Masukkan indeks dengan benar!");}}
  28. public static void getKontak(){
  29. try {
  30. displayKontak();
  31. System.out.print("Masukkan indeks kontak yang ingin ditampilkan: ");
  32. int get = id.nextInt();
  33. System.out.println(kontak.get(get));
  34. } catch (Exception e) {
  35. System.out.println("Masukkan indeks dengan benar!");}}
  36. public static void displayKontak(){
  37. System.out.println("\nDaftar kontak:");
  38. for(int i=0; i<kontak.size(); i++){
  39. System.out.println((i)+". "+kontak.get(i));}}
  40. public static void main(String[] args){
  41. while(true){
  42. System.out.println("\n==PROGRAM BUKU TELEPON==");
  43. System.out.println("1. Tampilkan daftar kontak");
  44. System.out.println("2. Tambah kontak");
  45. System.out.println("3. Hapus kontak");
  46. System.out.println("4. Tampilkan kontak sesuai indeks");
  47. System.out.println("5. Keluar");
  48. System.out.print("Masukkan Pilihan: ");
  49. try {
  50. int opsi = id.nextInt();
  51. switch (opsi){
  52. case 1: displayKontak();break;
  53. case 2: addKontak();break;
  54. case 3: removeKontak();break;
  55. case 4: getKontak();break;
  56. case 5: System.exit(0);break;
  57. default:
  58. System.out.println("Masukkan pilihan antara 1 - 4!");}
  59. } catch (Exception e) {
  60. System.out.println("Masukkan dengan benar!");
  61. return;
  62. }}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement