0

My program has 6 classes so I'm going to try and only post the methods involved with the issue I'm having. I'm trying to add donation objects that get their attributes from reading information from a file. My program wasn't printing out any of the donationList related information so I did a System.out.println(donationList.size()); and it's telling me that there are 0 objects in the list. I've been looking at this for a while and can't figure out where in the process the donation object is failing to be created correctly or added to the arraylist correctly.

This is where I call the method that starts the process.

public static void main(String[] args) {
        readAndProcess(); 

This is the method that starts the process.

public static void readAndProcess() {
        final String INPUT_FILENAME = "input/assn2input.txt";
        File dataFile = new File(INPUT_FILENAME);
        Scanner fileScanner = null;

        try {
            fileScanner = new Scanner(dataFile);
           }catch (FileNotFoundException e) {
            System.out.println("File not found exception for file " + e); 
            System.exit(0);
            }

        String oneLine;
        String [] lineValues;

        while(fileScanner.hasNextLine()) {
             oneLine = fileScanner.nextLine();
             lineValues = oneLine.split(",");

            if(lineValues[0].equals("DONOR")) {
                if (lineValues[1].equals("ADD") ) { 

                    addDonor(lineValues);
                }
                else if (lineValues[1].equals("DEL")) {

                    // call method to delete
                }
             }

            else if ( lineValues[0].equals("Donation")) {
                if (lineValues[1].equals("ADD")) {

                    addDonation(lineValues);
                }
                else if (lineValues[1].equals("DEL")) {

                      // call method to delete
                }
            }
        }
    }

This is the addDonation method which happens after the readAndProcess method.

public static void addDonation(String [] lineValues) {
    Donation donation = new Donation();
    setDonationAttributes(donation, lineValues);
    if (donorImpl.isIDUnique(donation.getDonorID()) == false &&
        donationImpl.isIDUnique(donation.getDonationID()) == true) {
        donationImpl.add(donation);
    }
    else {
      System.out.println("ERROR: The Donation either had a non-unique"
                       + " donation ID or a unique Donor ID. Was not "
                       + "added to list." + donation.toString());
    }

}

This is the method that sets the donation object's attributes.

public static Donation setDonationAttributes (Donation donation, 
                                                  String [] lineValues) {
        donation.setDonationID(Integer.parseInt(lineValues[2]));
        donation.setDonorID(Integer.parseInt(lineValues[3]));
        donation.setDonationDescription(lineValues[4]);
        if (donation.checkDescriptionLength() == false) {
            System.out.println("ERROR: Donation description is longer "
                             + "than 25 characters");
        }
        donation.setDonationAmount(Double.parseDouble(lineValues[5]));
        donation.setDonationDate(lineValues[6]); 
        if (lineValues[7].equalsIgnoreCase("Y") ) {
            donation.setTaxDeductible(true);
        }
        else {
            donation.setTaxDeductible(false);
        }        
        donation.setCheckNumber(Integer.parseInt(lineValues[8]));
        if (donation.checkNumberCheck()== false) {
            System.out.println("ERROR: Invalid check number is not between 100 "
                              + "and 5000: " + lineValues[8]);
        }
        return donation;
    }

This is the method that checks for unique ID for donationID.

public boolean isIDUnique(int donationID) {
       int index;

       for (index = 0; index < donationList.size(); ++index) {
           if (donationID == donationList.get(index).getDonationID() ) {
               return false;
           }

       }
       return true;
    }

This is the method for checking unique donorID.

public boolean isIDUnique(int donorID) {
       int index;
       for (index = 0; index < donorList.size(); ++index) {
           if (donorID == donorList.get(index).getDonorID() ) {
               return false;
           }
       }
       return true;

   }

This is the method in the DonationImpl class that adds the object to the arraylist. The instructions for this method told me to set it up as a boolean for whatever reason, I'm not exactly sure why.

public boolean add (Donation donation) {
       if (donationList.add(donation)) {
       return true;
       }
       return false;

   }

The donationImpl class to show what the arrayList creation looks like.

public class DonationImpl {

    // Data Field
   private ArrayList<Donation> donationList = new ArrayList<Donation>();

   //Getter
   public ArrayList<Donation> getDonationList() {return donationList;}

The 1 and 3 in the following examples refer to a donorID. My donorID methods and creation are all working correctly.

Example lines of text:

DONATION,ADD,101,1,Payroll deduction,22.22,07/04/1776,Y,1001 DONATION,ADD,303,3,Anniversary contribution,111.00,07/04/1777,N,2244

1 Answer 1

1

You have a typo

else if ( lineValues[0].equals("Donation")) {

should be

else if ( lineValues[0].equals("DONATION")) {
Sign up to request clarification or add additional context in comments.

1 Comment

Well that explains why I have been so confused! Thank you for catching that, was banging my head against the wall.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.