reading events from a file, and use of object heirarchy

Viewed 32

I'm new to object hierarchy. Hope someone would help.

I have to read information from a .txt file. The file looks like:

 P D Felix 12:00
 
 P C Mosely 12:15
 
 D C Mosely 12:25
 
 C C Georgino 12:30
 etc...

where I have to read lines from the file.

  • The 1st character of each line represents an event

    ('P'=Pets arrival, 'D'= Pet died, 'C'= Pet cured, etc).

  • The 2nd character represents the type of pets

    ('D','C','R': Dog, Cat or Rabbit, they are also subclasses and Pet is superclass).

  • 3rd is the name of a pet, and 4th is the time of an event.

I created a method named createNewPet(Pet[] pets, String name, String animal type) which does the following:

1.Read 1st character from each line, if the 1st character is 'P', I need to add a pet to the array.

2.Read 2nd character (C, D, or R) and then create a new Dog(), new Cat(), or new Rabbit(), that is, I need to create new Objects and put them in my Pet array.

My code:

import java.io.*; 

public class PetMain {



public static void main(String[] args) {

    
}


public static void createNewPet(Pet[] pets, String name, String animalType) throws IOException {
    
    BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\12043\\Downloads\\A2Input.txt"));
    int numPets=0;
    
    String line= br.readLine();
    while(line !=null) {
        if((line.charAt(0))=='P') {
            pets = new Pet[numPets];
            numPets++;
        }
        /*for(int i=0; i<pets.length; i++) {
            if(line.charAt(1)=='D') {
               
            }
        }*/
    
        line=br.readLine();

    }

  }

}

I think that I'm doing something wrong in the 2nd part.

0 Answers
Related