Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Main {
- public static Scanner in = new Scanner(System.in);
- public static ArrayList<Pet> arr = new ArrayList<>();
- public static void inputPet() {
- try {
- System.out.print("Name : ");
- String name = in.next();
- System.out.print("Gender(M/F) : ");
- String gender = in.next();
- if (gender.equalsIgnoreCase("M") || gender.equalsIgnoreCase("F")) {
- System.out.print("Age : ");
- int age = in.nextInt();
- Pet pet = new Pet(name, gender, age);
- arr.add(pet);
- } else {
- System.out.println("Gender should be either M nor F");
- System.out.print("Gender(M/F) : ");
- gender = in.next();
- System.out.print("Age : ");
- int age = Integer.parseInt(in.next());
- Pet pet = new Pet(name, gender, age);
- arr.add(pet);
- }
- } catch(NumberFormatException e) {
- System.out.println("Age should be number");
- }
- }
- public static void showPet() {
- Collections.sort(arr, Pet.byAge);
- for (int i = 0; i < arr.size() ; i++) {
- System.out.println(arr.get(i).toString());
- }
- }
- public static void runThis() {
- int pilih;
- do {
- System.out.println("Main Menu");
- System.out.println("1. Input Pet");
- System.out.println("2. Show Pet");
- System.out.print("Masukan Pilihan : ");
- pilih = in.nextInt();
- if (pilih == 1) {
- inputPet();
- } else if (pilih == 2) {
- showPet();
- } else {
- System.out.println("GBLK");
- }
- } while (pilih < 3);
- }
- public static void main(String[] args) {
- runThis();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement