Advertisement
Guest User

Java cats list

a guest
Mar 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.00 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         // Declaring variables
  10.         int size = 3;
  11.         catData[] cat = new catData[size];
  12.        
  13.  
  14.         // Default cat list
  15.         cat[0] = new catData("German", 2, "Male", "White");
  16.         cat[1] = new catData("Sheraz", 4, "Female", "Brown");
  17.         cat[2] = new catData("Jordan", 7, "Male", "Black");
  18.  
  19.         catData.printCat(cat);
  20.  
  21.         int choiceFunction;
  22.         boolean flag = false;
  23.         while (!flag) {
  24.             System.out.println("Functions:");
  25.             System.out.println(" 1 - Add cat");
  26.             System.out.println(" 2 - Edit cat");
  27.             System.out.println(" 3 - Remove cat");
  28.             System.out.println(" 4 - Quit");
  29.             System.out.print("Choice: ");
  30.             choiceFunction = sc.nextInt();
  31.             System.out.println();
  32.  
  33.             if (choiceFunction == 1) {
  34.                 cat = addCat(cat);
  35.                 catData.printCat(cat);
  36.             }
  37.             if (choiceFunction == 2) {
  38.                 cat = editCat(cat);
  39.                 catData.printCat(cat);
  40.             }
  41.             if (choiceFunction == 3) {
  42.                 cat = removeCat(cat);
  43.                 catData.printCat(cat);
  44.             }
  45.             if (choiceFunction == 4) flag = true;
  46.         }
  47.     }
  48.  
  49.     private static catData[] addCat(catData[] catList){
  50.         Scanner sc = new Scanner(System.in);
  51.         catData[] temp = new catData[catList.length+1];
  52.  
  53.         System.out.print("(1/4) Insert cat type: ");
  54.         String type = sc.next();
  55.  
  56.         System.out.print("(2/4) Insert cat age: ");
  57.         int age = sc.nextInt();
  58.  
  59.         System.out.println("--- 1-Male 2-Female ---");
  60.         System.out.print("(3/4) Insert cat gender: ");
  61.         String gender;
  62.         int genderFlag = sc.nextInt();
  63.         while (genderFlag != 1 && genderFlag != 2){
  64.             if (genderFlag == 1 || genderFlag == 2) break;
  65.             System.out.print("Please Insert 1 or 2: ");
  66.             genderFlag = sc.nextInt();
  67.         }
  68.         if (genderFlag == 1) gender = "Male";
  69.         else gender = "Female";
  70.  
  71.         System.out.print("(4/4) Insert cat color: ");
  72.         String color = sc.next();
  73.  
  74.         // Copying the old date on the new Object
  75.         System.out.println("\n");
  76.         temp[catList.length] = new catData(type, age, gender, color);
  77.         for(int i = 0; i < catList.length; i++){
  78.             temp[i] = catList[i];
  79.         }
  80.  
  81.         return temp;
  82.     }
  83.  
  84.     private static catData[] editCat(catData[] catList){
  85.         Scanner sc = new Scanner(System.in);
  86.         System.out.print("Cat ID: ");
  87.         int catId = sc.nextInt();
  88.         System.out.println("Editing cat profile number ( " + catId + " )");
  89.         System.out.print("(1/4) Insert cat type: ");
  90.         String type = sc.next();
  91.  
  92.         System.out.print("(2/4) Insert cat age: ");
  93.         int age = sc.nextInt();
  94.  
  95.         System.out.println("--- 1-Male 2-Female ---");
  96.         System.out.print("(3/4) Insert cat gender: ");
  97.         String gender;
  98.         int genderFlag = sc.nextInt();
  99.         while (genderFlag != 1 && genderFlag != 2){
  100.             if (genderFlag == 1 || genderFlag == 2) break;
  101.             System.out.print("Please Insert 1 or 2: ");
  102.             genderFlag = sc.nextInt();
  103.         }
  104.         if (genderFlag == 1) gender = "Male";
  105.         else gender = "Female";
  106.  
  107.         System.out.print("(4/4) Insert cat color: ");
  108.         String color = sc.next();
  109.         catList[catId-1] = new catData(type, age, gender, color);
  110.         return catList;
  111.     }
  112.  
  113.     private static catData[] removeCat(catData[] catList){
  114.         Scanner sc = new Scanner(System.in);
  115.         System.out.print("Cat ID: ");
  116.         int catId = sc.nextInt();
  117.         System.out.println("Cat profile number has been removed\n");
  118.  
  119.         int ct = 0;
  120.         catData[] temp = new catData[catList.length-1];
  121.         for (int i = 0; i < catList.length; i++){
  122.             if (i == catId-1) continue;
  123.             temp[ct] = catList[i];
  124.             ct++;
  125.         }
  126.         return temp;
  127.     }
  128.  
  129. }
  130.  
  131.  
  132. class catData {
  133.     private String type;
  134.     private int age;
  135.     private String gender;
  136.     private String color;
  137.     catData(String catType, int catAge, String catGender, String catColor) {
  138.         type = catType;
  139.         age = catAge;
  140.         gender = catGender;
  141.         color = catColor;
  142.     }
  143.  
  144.     static void printCat(catData[] catList) {
  145.         System.out.println("ID " + "TYPE    " + "GENDER  " + "COLOR  " + "AGE  ");
  146.         System.out.print("----------------------------");
  147.         for (int i = 0; i < catList.length; i++){
  148.             System.out.println();
  149.             System.out.printf("%-2d %-7s %-7s %-7s %-3d " , (i+1), catList[i].type, catList[i].gender, catList[i].color, catList[i].age );
  150.         }
  151.         System.out.println();
  152.         System.out.println("----------------------------");
  153.     }
  154.  
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement