Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- // Declaring variables
- int size = 3;
- catData[] cat = new catData[size];
- // Default cat list
- cat[0] = new catData("German", 2, "Male", "White");
- cat[1] = new catData("Sheraz", 4, "Female", "Brown");
- cat[2] = new catData("Jordan", 7, "Male", "Black");
- catData.printCat(cat);
- int choiceFunction;
- boolean flag = false;
- while (!flag) {
- System.out.println("Functions:");
- System.out.println(" 1 - Add cat");
- System.out.println(" 2 - Edit cat");
- System.out.println(" 3 - Remove cat");
- System.out.println(" 4 - Quit");
- System.out.print("Choice: ");
- choiceFunction = sc.nextInt();
- System.out.println();
- if (choiceFunction == 1) {
- cat = addCat(cat);
- catData.printCat(cat);
- }
- if (choiceFunction == 2) {
- cat = editCat(cat);
- catData.printCat(cat);
- }
- if (choiceFunction == 3) {
- cat = removeCat(cat);
- catData.printCat(cat);
- }
- if (choiceFunction == 4) flag = true;
- }
- }
- private static catData[] addCat(catData[] catList){
- Scanner sc = new Scanner(System.in);
- catData[] temp = new catData[catList.length+1];
- System.out.print("(1/4) Insert cat type: ");
- String type = sc.next();
- System.out.print("(2/4) Insert cat age: ");
- int age = sc.nextInt();
- System.out.println("--- 1-Male 2-Female ---");
- System.out.print("(3/4) Insert cat gender: ");
- String gender;
- int genderFlag = sc.nextInt();
- while (genderFlag != 1 && genderFlag != 2){
- if (genderFlag == 1 || genderFlag == 2) break;
- System.out.print("Please Insert 1 or 2: ");
- genderFlag = sc.nextInt();
- }
- if (genderFlag == 1) gender = "Male";
- else gender = "Female";
- System.out.print("(4/4) Insert cat color: ");
- String color = sc.next();
- // Copying the old date on the new Object
- System.out.println("\n");
- temp[catList.length] = new catData(type, age, gender, color);
- for(int i = 0; i < catList.length; i++){
- temp[i] = catList[i];
- }
- return temp;
- }
- private static catData[] editCat(catData[] catList){
- Scanner sc = new Scanner(System.in);
- System.out.print("Cat ID: ");
- int catId = sc.nextInt();
- System.out.println("Editing cat profile number ( " + catId + " )");
- System.out.print("(1/4) Insert cat type: ");
- String type = sc.next();
- System.out.print("(2/4) Insert cat age: ");
- int age = sc.nextInt();
- System.out.println("--- 1-Male 2-Female ---");
- System.out.print("(3/4) Insert cat gender: ");
- String gender;
- int genderFlag = sc.nextInt();
- while (genderFlag != 1 && genderFlag != 2){
- if (genderFlag == 1 || genderFlag == 2) break;
- System.out.print("Please Insert 1 or 2: ");
- genderFlag = sc.nextInt();
- }
- if (genderFlag == 1) gender = "Male";
- else gender = "Female";
- System.out.print("(4/4) Insert cat color: ");
- String color = sc.next();
- catList[catId-1] = new catData(type, age, gender, color);
- return catList;
- }
- private static catData[] removeCat(catData[] catList){
- Scanner sc = new Scanner(System.in);
- System.out.print("Cat ID: ");
- int catId = sc.nextInt();
- System.out.println("Cat profile number has been removed\n");
- int ct = 0;
- catData[] temp = new catData[catList.length-1];
- for (int i = 0; i < catList.length; i++){
- if (i == catId-1) continue;
- temp[ct] = catList[i];
- ct++;
- }
- return temp;
- }
- }
- class catData {
- private String type;
- private int age;
- private String gender;
- private String color;
- catData(String catType, int catAge, String catGender, String catColor) {
- type = catType;
- age = catAge;
- gender = catGender;
- color = catColor;
- }
- static void printCat(catData[] catList) {
- System.out.println("ID " + "TYPE " + "GENDER " + "COLOR " + "AGE ");
- System.out.print("----------------------------");
- for (int i = 0; i < catList.length; i++){
- System.out.println();
- System.out.printf("%-2d %-7s %-7s %-7s %-3d " , (i+1), catList[i].type, catList[i].gender, catList[i].color, catList[i].age );
- }
- System.out.println();
- System.out.println("----------------------------");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement