I am trying to assign a number to each pet in the ArrayList petList and then I would like to call this number from the index in order to retrieve this pet. Is there any way to do this? I have tried adding petSpaceNbr to the createPet method under petList, but it won't let me convert a string to an int. How do I do this?
import java.util.ArrayList;
import java.util.Scanner;
public class EnhancementOne {
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. Show Pets");
System.out.println("5. 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:
showPets();
case 5:
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();
ArrayList<Integer> petSpaceNbr = petList;
String newPetArray[] = newPet.split(" ");
petList = new ArrayList<>();
for (int i = 0; i < newPetArray.length; i ) {
petList.add(newPetArray[i]);
}
petSpaceNbr = petSpaceNbr.size() 1;
System.out.println("Pets in list are " petList petSpaceNbr);
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();
}
public static void showPets() {
System.out.println(petList);
showMainMenu();
}
}
CodePudding user response:
ArrayList is a list of Objects. If you want to store a list of pets with a name and a number for each you could create class Pets with these attributes and store them in an ArrayList. It could be easier to your use a HashMap with the numbers as the keys and the names as the values.
CodePudding user response:
Create a Pet class to keep track of all your pets. Each pet can have a name, and id, any any other attributes you decide to give it.
public class Pet {
private int id;
private String name;
public Pet(id, name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
}
You can then make an ArrayList<Pet> to store and retrieve them.
ArrayList<Pet> myPets = new ArrayList<>();
You can add pets like this:
public void addPet(String name) {
Pet newPet = new Pet(pets.size(), name);
pets.add(new Pet);
}
and retrieve them like this:
public Pet getPet(int id) {
for (Pet pet: pets) {
if (pet.getId() == id) {
return pet;
}
}
System.out.println("No pet exists with an id of " id);
return null;
}
(Something else you might want to consider, though, is if it really makes sense to get pets by id and not by name. In this case, make sure that no pet can be added with a name that already exists)
CodePudding user response:
Cannot 1 on a list
Your code:
petSpaceNbr = petSpaceNbr.size() 1
… makes no sense. You defined petSpaceNbr as a List.
ArrayList petSpaceNbr = petList;
You cannot do 1 to a list.
Incrementing an int
If you want to keep track of an ever-increasing number, use an int primitive.
int petSpaceNbr ;
…
petSpaceNbr = ( petSpaceNbr 1 ) ;
Parallel lists
If you want to keep track of two lists in parallel (not usually recommended), make another list. Add your increasing numbers as an element in that second list.
List< Integer > petNumbers = new ArrayList<>() ;
List< String > petNames = new ArrayList<>() ;
…
petSpaceNbr = ( petSpaceNbr 1 ) ;
petNumbers.add( petSpaceNbr ) ;
petNames.add( newPetNameEnteredByUser ) ;
Map
If you want to associate each pet with an increasing number, use a Map. A map tracks pairings of a key with a value. This usually is a neater way of associating, better than parallel lists.
NavigableMap
To keep the keys in sorted order, use a NavigableMap like TreeMap.
NavigableMap < Integer , String > numberToPetNameMap = new TreeMap<>() ;
…
numberToPetNameMap.put( petSpaceNbr , newPetNameEnteredByUser ) ;
Custom class
If you want to assign an identifier to each pet, define a custom class.
Record
If the main purpose of the class is to carry data transparently and immutably, define the class as a record. The compiler implicitly creates the constructor, getters, equals & hashCode, and toString.
record Pet ( int id , String name ) {}
…
List<Pet> pets = new ArrayList<>() ;
…
petSpaceNbr = ( petSpaceNbr 1 ) ;
Pet pet = new Pet( petSpaceNbr , newPetNameEnteredByUser ) ;
pets.add( pet ) ;
UUID
If you don’t want to be bothered with tracking an incrementing number, use a universally unique identifier (UUID) instead. Java includes a UUID class.
record Pet ( UUID id , String name ) {}
…
List<Pet> pets = new ArrayList<>() ;
…
Pet pet = new Pet( UUID.randomUUID() , newPetNameEnteredByUser ) ;
pets.add( pet ) ;
