Advertisement
Guest User

prog-quiz02

a guest
Dec 5th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     Scanner scan = new Scanner(System.in);
  7.     ArrayList<String> patientName = new ArrayList<>();
  8.     ArrayList<String> patientGender = new ArrayList<>();
  9.     ArrayList<String> roomList = new ArrayList<>();
  10.  
  11.     void MainMenu() {
  12.         System.out.println("=== BJ HOSPITAL ===");
  13.         System.out.println("1. Add Patient");
  14.         System.out.println("2. Remove Patient");
  15.         System.out.println("3. Update Patient");
  16.         System.out.println("4. View Patient");
  17.         System.out.println("5. Exit");
  18.     }
  19.     private void AddPatient() {
  20.         String name;
  21.         String gender;
  22.         String room;
  23.         //nama nich
  24.         do {
  25.             System.out.print("Input your name[starts with Mr. and Mrs.]: ");
  26.             name = scan.nextLine();
  27.         }while(!name.startsWith("Mr.") && !name.startsWith("Mrs."));
  28.         //gender nich
  29.         do {
  30.             System.out.print("Input your gender: ");
  31.             gender = scan.nextLine();
  32.         }while(!gender.equalsIgnoreCase("Male") && !gender.equalsIgnoreCase("Female"));
  33.         //tipe kamar nich
  34.         do {
  35.             System.out.print("Choose your room[VIP/Regular]: ");
  36.             room = scan.nextLine();
  37.         }while(!room.equals("VIP") && !room.equals("Regular"));
  38.         patientName.add(name);
  39.         patientGender.add(gender);
  40.         roomList.add(room);
  41.         System.out.println("Success Add Patient");
  42.     }
  43.     private void ViewPatient() {
  44.         if(patientName.isEmpty()) {
  45.             System.out.println("There is no data of patient");
  46.         }else {
  47.             //biar bisa display semua nya pake looping cihuy
  48.             int i = 0;
  49.             System.out.println("No. | Name | Gender | Room Type");
  50.             while(i < patientName.size()) {
  51.                 System.out.println((i+1) + "|" + patientName.get(i) + "|" + patientGender.get(i) + "|" + roomList.get(i));
  52.                 i++;
  53.             }
  54.         }
  55.     }
  56.     private void RemovePatient() {
  57.         if(patientName.isEmpty()) {
  58.             System.out.println("There is no data of patient");
  59.         }else {
  60.             int index = -1;
  61.             ViewPatient();
  62.             do {
  63.                 System.out.print("Input number you want to delete: ");
  64.                 try {
  65.                     index = scan.nextInt();
  66.                 }catch(Exception e) {
  67.                     index = -1;
  68.                 }
  69.                 scan.nextLine();
  70.                 //-1 biar ke hapus sesuai sama yg dipengenin
  71.                 index = index - 1;
  72.             }while(index < 0 || index > patientName.size() -1);
  73.             patientName.remove(index);
  74.             patientGender.remove(index);
  75.             roomList.remove(index);
  76.             System.out.println("Success Remove Patient");
  77.         }
  78.     }
  79.     private void UpdatePatient() {
  80.         if(patientName.isEmpty()) {
  81.             System.out.println("There is no data of patient");
  82.         }else {
  83.             int index = -1;
  84.             ViewPatient();
  85.             do {
  86.                 System.out.print("Input number you want to update: ");
  87.                 try {
  88.                     index = scan.nextInt();
  89.                 }catch(Exception e) {
  90.                     index = -1;
  91.                 }
  92.                 scan.nextLine();
  93.                 index = index - 1;
  94.             }while(index < 0 || index > patientName.size() -1);
  95.            
  96.             String name;
  97.             String gender;
  98.             String room;
  99.             do {
  100.                 System.out.print("Input your name[starts with Mr. and Mrs.]: ");
  101.                 name = scan.nextLine();
  102.             }while(!name.startsWith("Mr.") && !name.startsWith("Mrs."));
  103.             do {
  104.                 System.out.print("Input your gender: ");
  105.                 gender = scan.nextLine();
  106.             }while(!gender.equalsIgnoreCase("Male") && !gender.equalsIgnoreCase("Female"));
  107.             do {
  108.                 System.out.print("Choose your room[VIP/Regular]: ");
  109.                 room = scan.nextLine();
  110.             }while(!room.equals("VIP") && !room.equals("Regular"));
  111.            
  112.             patientName.set(index, name);
  113.             patientGender.set(index, gender);
  114.             roomList.set(index, name);
  115.             System.out.println("Success Update Patient");
  116.         }
  117.     }
  118.     public Main() {
  119.         // TODO Auto-generated constructor stub
  120.         int choose;
  121.         do {
  122.             MainMenu();
  123.             System.out.print(">> ");
  124.             try {
  125.                 choose = scan.nextInt();
  126.             }catch(Exception e) {
  127.                 choose = -1;
  128.             }
  129.             scan.nextLine();
  130.             if(choose == 1) {
  131.                 AddPatient();
  132.             }else if(choose == 2) {
  133.                 RemovePatient();
  134.             }else if(choose == 3) {
  135.                 UpdatePatient();
  136.             }else if(choose == 4) {
  137.                 ViewPatient();
  138.             }
  139.         }while(choose != 5);
  140.     }
  141.  
  142.     public static void main(String[] args) {
  143.         // TODO Auto-generated method stub
  144.         new Main();
  145.  
  146.     }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement