Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.InputMismatchException;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9.  
  10. public static Scanner in = new Scanner(System.in);
  11. public static ArrayList<Pet> arr = new ArrayList<>();
  12.  
  13. public static void inputPet() {
  14. try {
  15. System.out.print("Name : ");
  16. String name = in.next();
  17. System.out.print("Gender(M/F) : ");
  18. String gender = in.next();
  19.  
  20. if (gender.equalsIgnoreCase("M") || gender.equalsIgnoreCase("F")) {
  21. System.out.print("Age : ");
  22. int age = in.nextInt();
  23. Pet pet = new Pet(name, gender, age);
  24. arr.add(pet);
  25. } else {
  26. System.out.println("Gender should be either M nor F");
  27. System.out.print("Gender(M/F) : ");
  28. gender = in.next();
  29. System.out.print("Age : ");
  30. int age = Integer.parseInt(in.next());
  31. Pet pet = new Pet(name, gender, age);
  32. arr.add(pet);
  33. }
  34.  
  35. } catch(NumberFormatException e) {
  36. System.out.println("Age should be number");
  37.  
  38. }
  39. }
  40.  
  41. public static void showPet() {
  42. Collections.sort(arr, Pet.byAge);
  43. for (int i = 0; i < arr.size() ; i++) {
  44. System.out.println(arr.get(i).toString());
  45. }
  46. }
  47.  
  48. public static void runThis() {
  49. int pilih;
  50. do {
  51. System.out.println("Main Menu");
  52. System.out.println("1. Input Pet");
  53. System.out.println("2. Show Pet");
  54. System.out.print("Masukan Pilihan : ");
  55. pilih = in.nextInt();
  56.  
  57. if (pilih == 1) {
  58. inputPet();
  59. } else if (pilih == 2) {
  60. showPet();
  61. } else {
  62. System.out.println("GBLK");
  63. }
  64. } while (pilih < 3);
  65. }
  66.  
  67. public static void main(String[] args) {
  68. runThis();
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement