简体   繁体   中英

Using Scanner to add user input to an array of objects Java

I'm a Level 3 student trying to build a Java program for my course. So far I have managed to create an Array of Objects and am able to add to it if I code every object in myself. I would like to add the functionality so the user will provide the information to create the object which will then be stored in the array. I have used scanners before but I can't seem to work out where to put the user input code in the main class. Sorry if this is a very basic question but I am so lost! Thank you in advance.

Main Class import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    Animal[] animals = new Animal[50];

    instantiateAll(animals);
    setAll(animals);
    displayAll(animals);



}
public static void instantiateAll(Animal[] animals)
{
    for (int ctr = 0; ctr < animals.length; ctr++)
        animals[ctr] = new Animal();

}
public static void setAll(Animal[] animals)
{ for (int ctr = 0; ctr < animals.length; ctr++) {

    animals[ctr].setAnimalname("Not Set Yet");
    animals[ctr].setSpecies("Not Set Yet");
    animals[ctr].setBreed("Not Set Yet");
    animals[ctr].setWeight(0);
    animals[ctr].setGender("Not Set Yet");
    animals[ctr].setAge(0);
    animals[ctr].setVet("Not Set Yet");
    animals[ctr].setVaccine(0);
    animals[ctr].setMedicine(0);
    animals[ctr].setFood(0);
    animals[ctr].setMuandv(0);
}}
public static void displayAll(Animal[] animals)
{ for (int ctr = 0; ctr < animals.length; ctr++)
    System.out.println("York Animals"+"\n"+"Animal Name:  "+animals[ctr].getAnimalname()+"\n" + "Species:   "+animals[ctr].getSpecies() + "\n" +"Breed:  "+animals[ctr].getBreed()+"\n" + "Weight:  "+animals[ctr].getWeight()+"\n" + "Gender:  "+animals[ctr].getGender()+"\n" +"Age:  "+animals[ctr].getAge()+"\n" + "Vet:  "+animals[ctr].getVet()+"\n" +"Vaccine Cost:  £"+animals[ctr].getVaccine()+"\n" +"Medicine Cost:  £"+animals[ctr].getMedicine()+"\n" +"Food Cost:  £"+animals[ctr].getFood()+"\n" + "Maintenance Cost:  £"+animals[ctr].getMuandv()+ "\n..................................................\n");}

}

Animal Class

public class Animal {

private String species;
private String animalname;
private String breed;
private double weight;
private String gender;
private int age;
private String vet;
private double vaccine;
private double medicine;
private double food;
private double muandv;

  public Animal(){
    this("Unknown Species","Unknown Name", "Unknown Breed", 0, "Unknown", 0, "No Vet Assigned", 0,0,0,0);
      }

    public Animal(String species,String animalname, String breed, double weight, String gender, int age, String vet, double vaccine, double medicine, double food, double muandv) {
    this.species = species;
    this.animalname = animalname;
    this.breed = breed;
    this.weight = weight;
    this.gender = gender;
    this.age = age;
    this.vet = vet;
    this.vaccine = vaccine;
    this.medicine = medicine;
    this.food = food;
    this.muandv = muandv;

}

public String getSpecies() {
    return species;
}

public void setSpecies(String species) {
    this.species = species;
}

public String getAnimalname() {
    return animalname;
}

public void setAnimalname(String name) {
    this.animalname = name;
}

public String getBreed() {
    return breed;
}

public void setBreed(String breed) {
    this.breed = breed;
}

public double getWeight() {
    return weight;
}

public void setWeight(double weight) {
    this.weight = weight;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    String validGender = gender.toLowerCase(); //this validates the entry into the Gender field
    if(validGender.equals("f")|| validGender.equals("m")) {
        this.gender = gender;
    } else {
        this.gender = "Invalid Gender";

    }
        }

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getVet() {
    return vet;
}

public void setVet(String vet) {
    this.vet = vet;
}

public double getVaccine() {
    return vaccine;
}

public void setVaccine(double vaccine) {
    this.vaccine = vaccine;
}

public double getMedicine() {
    return medicine;
}

public void setMedicine(double medicine) {
    this.medicine = medicine;
}

public double getFood() {
    return food;
}

public void setFood(double food) {
    this.food = food;
}

public double getMuandv() {
    return muandv;
}

public void setMuandv(double muandv) {
    this.muandv = muandv;
}

You can do some thing like

public static void main(String... args){
        Scanner input = new Scanner(System.in);
        Animal[] animals = new Animal[50];
        for(int i=0;i<animals.length;i++)
            animals[i] = getAnimal(input);
//        instantiateAll(animals);
//        setAll(animals);
        displayAll(animals);
    }
    public static Animal getAnimal(Scanner input){
        Animal animal = new Animal();
        System.out.println("Animal Name: ");
        animal.setAnimalname(input.next());
        System.out.println("Species: ");
        animal.setSpecies(input.next());
        /*
        .
        .
        .
        * */
        return animal;
    }

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