简体   繁体   中英

CRUD operations with arrays

I am trying to create a pet app that allows user input to enter a pet name, update the pet name and delete the pet name. Here is what I have so far. I have made an array for the create method but I can't figure out how to use it in the update or delete methods.

import java.util.Scanner;
import java.io.*;

public class Menu2 {
    static Scanner scan = new Scanner(System.in);

    public static void main(final String[] args) {

        showMainMenu();
    }

    public static void showMainMenu(){
        System.out.println("--- MAIN MENU ---");
        System.out.println("1. Create Pet");
        System.out.println("2. Update Pet");
        System.out.println("3. Delete Pet");
        System.out.println("4. Exit");

        System.out.print("Enter your Choice : ");

        int option = scan.nextInt();

        switch(option){
            case 1:
                createPet();
                break;
            case 2:
                updatePet();
                break;
            case 3:
                deletePet();
                break;
            case 4: 
                System.exit(0);
                break;
            default:
                System.out.println("Invalid option!");
                showMainMenu();
        }
     
    }
    public static void createPet(){
       
        Scanner myObj = new Scanner(System.in);
        //String newPet = myObj.nextLine();
        System.out.print("Enter Pet Name: ");
        String newPet = myObj.nextLine();
        String newPetArray[] = newPet.split(" ");
       // newPet = scan.nextLine();
        System.out.println("Pet Name is " + newPet);

        // use for READ
        for (int i = 0; i < newPetArray.length; i++){
            System.out.println(newPetArray[i]);
        }
        showMainMenu();
    }
    public static void updatePet() {

    }

    public static void deletePet() {

    }
}

If your goal is to store the pets in an array, for updating you would loop through the array (such as with a "for" or "while" loop), identify the index of the entry to be updated and update it. This could also be done with filters, but that would be a more advanced approach.

In order to delete the pet, you would need to find the pet (like you would in the update portion) and then shift all the remaining pets forward, so newPetArray[x] gets the value of newPetArray[x+1] and the last pet gets removed.

This task is easier with other datastructures, like ArrayList's, that support removing and inserting items directly.

for the delete method.

Public static String[] updatePet(String newPetArray[], int index) {
//creating another array for size one less  
String[] newArray = new String[newPetArray.length - 1];
 for(int i =0,j=0; i<newPetArray.length; i++) {
     if (i==index){
 continue;
      }
  newArray[k++]= newPetAray[i];
}
return newArray;

 }

As you haven't mentioned the details about the update function, I am assuming it is going to update the name of the pet present in the list. As ArrayList is more flexible when it comes to array in terms of addition of deletion and since you havent specified that you need to use array in particular, I'm providing the solution with ArrayList.

Logic for Updation

You iterate through the list and when the element matches, you simply update the name

Logic for Deletion

You iterate through the list and when the element matches, you simply remove the element present at that index.

import java.util.ArrayList;
import java.util.Scanner;

public class Menu2 {
    static Scanner scan = new Scanner(System.in);
    static ArrayList<String> petList;

    public static void main(final String[] args) {

        showMainMenu();
        scan.close();
    }

    public static void showMainMenu() {
        System.out.println("--- MAIN MENU ---");
        System.out.println("1. Create Pet");
        System.out.println("2. Update Pet");
        System.out.println("3. Delete Pet");
        System.out.println("4. Exit");

        System.out.print("Enter your Choice : ");

        int option = scan.nextInt();

        switch (option) {
            case 1:
                createPet();
                break;
            case 2:
                updatePet();
                break;
            case 3:
                deletePet();
                break;
            case 4:
                System.exit(0);
                break;
            default:
                System.out.println("Invalid option!");
                showMainMenu();
        }

    }

    public static void createPet() {
        Scanner myObj = new Scanner(System.in);

        System.out.print("Enter Pet Name: ");
        String newPet = myObj.nextLine();

        String newPetArray[] = newPet.split(" ");
        petList = new ArrayList<>();

        // use for READ
        for (int i = 0; i < newPetArray.length; i++) {
            petList.add(newPetArray[i]);
        }
        System.out.println("pets in list are " + petList);
        showMainMenu();
    }

    public static void updatePet() {

        System.out.println("Enter the name of the pet to be updated");
        String name = scan.next();
        System.out.println("Enter the updated name");
        String newName = scan.next();
        for (int i = 0; i < petList.size(); i++) {
            if (petList.get(i).equals(name)) {
                petList.set(i, newName);
                break;
            }
        }
        System.out.println("pets in list after updating the pet  " + petList);
        showMainMenu();
    }

    public static void deletePet() {
        System.out.println("Enter the name of the pet to be deleted");
        String name = scan.next();
        for (int i = 0; i < petList.size(); i++) {
            if (petList.get(i).equals(name)) {
                petList.remove(i);
                break;
            }
        }
        System.out.println("pets in list after deleting the specific pet " + petList);
        showMainMenu();
    }
}

and the output is as follows:

--- MAIN MENU ---
1. Create Pet
2. Update Pet
3. Delete Pet
4. Exit
Enter your Choice : 1
Enter Pet Name: buddy max mac 
pets in list are [buddy, max, mac]
--- MAIN MENU ---
1. Create Pet
2. Update Pet
3. Delete Pet
4. Exit
Enter your Choice : 2
Enter the name of the pet to be updated
mac
Enter the updated name
macKing
pets in list after updating the pet  [buddy, max, macKing]
--- MAIN MENU ---
1. Create Pet
2. Update Pet
3. Delete Pet
4. Exit
Enter your Choice : 3
Enter the name of the pet to be deleted
max
pets in list after deleting the specific pet [buddy, macKing]
--- MAIN MENU ---
1. Create Pet
2. Update Pet
3. Delete Pet
4. Exit
Enter your Choice : 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM