Lapres StrukDat 3

public class Lapres3 { static Scanner inp = new Scanner(System.in); public static void main(String[] args) { int pil; Kereta obj = new Kereta(); do{ System.out.print("MENU:\n1. Input\n2. View\n3. Update\n4. Exit\nPilihan:"); pil = Integer.parseInt(inp.nextLine()); switch(pil){ case 1: obj.input(); break; case 2: obj.view(); break; case 3: obj.update(); break; case 4: System.out.println("Program End"); break; default: System.out.println("Pilihan salah"); break; } }while(pil!=4); } } public class Kereta { Gerbong head,tail; Scanner inp = new Scanner(System.in); void set(String tipe){ String upd=""; System.out.println(tipe+": 1. Gerbong 2. Penumpang"); int choose = Integer.parseInt(inp.nextLine()); if(tipe.equals("Update")){ System.out.print("Masukkan nama yang akan anda cari: "); upd = inp.nextLine(); } switch(choose){ case 1: if(tipe.equals("Input")){ Gerbong tampung = new Gerbong(); tampung.input(); if(head == null) head = tampung; else tail.next = tampung; tail = tampung; }else{ if(head == null){ System.out.println("Data masih kosong"); } Gerbong temp = search(upd); if(temp == null){ System.out.println("Gerbong yang dicari tidak ada"); }else{ temp.input(); } } break; case 2: if(head == null){ System.out.println("Data masih kosong"); return; } System.out.print("Masukkan gerbong yang akan di "+tipe+" : " ); String strCari = inp.nextLine(); Gerbong cari = search(strCari); if(cari == null){ System.out.println("Gerbong yang dicari tidak ada"); return; } if(tipe.equals("Input")){ cari.inputPenumpang(); }else{ Penumpang temp = cari.search(upd); if(temp == null){ System.out.println("Penumpang yang dicari tidak ada"); }else{ temp.input(); } } break; default: System.out.println("Wrong choice"); break; } } void input(){ set("Input"); } void update(){ set("Update"); } Gerbong search(String nama){ Gerbong ptr = head; while(!ptr.gb.equals(nama)){ if(ptr.next == null){ System.out.println("Data tidak ada"); return null; } ptr = ptr.next; } return ptr; } void view(){ Gerbong pt = this.head; if(head == null){ System.out.println("Data masih kosong"); return; } while(pt != null){ pt.view(); System.out.println(); pt = pt.next; } } } public class Gerbong { String gb; int jm; Penumpang head,tail; Gerbong next; Scanner inp = new Scanner(System.in); void input(){ System.out.print("Input nama gerbong: "); gb = inp.nextLine(); } void view(){ System.out.println("Gerbong: "+gb); Penumpang ptr = this.head; if(head == null){ System.out.println("\n\tPenumpang : Kosong"); } while(ptr != null){ ptr.view(); ptr = ptr.next; } } void inputPenumpang(){ Penumpang temp = new Penumpang(); temp.input(); if(head == null) head = temp; else tail.next = temp; tail = temp; } Penumpang search(String nama){ Penumpang ptr = head; while(!ptr.nm.equals(nama)){ if(ptr.next == null){ System.out.println("Data tidak ada"); return null; } ptr = ptr.next; } return ptr; } } public class Penumpang { String nm; int jml; Scanner inp = new Scanner(System.in); Penumpang next; void input(){ System.out.print("Input nama penumpang:"); nm = inp.nextLine(); System.out.print("Input jumlah tiket:"); jml = Integer.parseInt(inp.nextLine()); } void view(){ System.out.println("\n\tNama penumpang\t : "+nm); System.out.println("\tJumlah tiket\t : "+jml); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.