Advertisement
Guest User

progquiz

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