Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: An Array List of a Class that contains two other Classes

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question An Array List of a Class that contains two other Classes

    Hi!

    My current assignment is essentially as follows:

    • I have two classes that are subclasses of a Person class, called Patient and Physician.
    • I then have a class called Bill that contains Patient and Physician objects (mimicking a patient's visit to the physician's office where a bill would be required).
    • In my main class, I have created an Array List of the Bill class to hold all bill info (Patient info and Physician info) as it's entered by the user.
    • At the end of the main class, I need to give a summary off all the data.


    Here's the Class Diagram if it helps: http://aceteam.ca/adev1000/assignmen...ProjectUML.pdf

    Question: Is there any way for me to access the getVisitFee() method I have within Patient if I've only created an Array List of Bill, which contains Patient objects? I need to give a summary of all visit fees totalled up for the session. I of course can easily add them up as the user input is collected, but I want to make sure there isn't a way to access this using the getVisitFee() method that's available first.

    I've read over our class notes and nothing remotely like this is explained...and Google is not being helpful.

    If you could help me think this through, I'd be very grateful! I don't need the actual code...just an explanation is fine.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: An Array List of a Class that contains two other Classes

    If you can get a reference to the object that contains the method, you can use that reference to call the method.
    The ArrayList class has methods to access its contents.

    Post some code to show the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: An Array List of a Class that contains two other Classes

    The UML PDF doesn't open for me unless you actually download it.
    For reference, the home page is here. ADEV-1000 Programming (Java 1)

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: An Array List of a Class that contains two other Classes

    Quote Originally Posted by Norm View Post
    If you can get a reference to the object that contains the method, you can use that reference to call the method.
    The ArrayList class has methods to access its contents.

    Post some code to show the problem.
    sorry for taking so long to get the code...my dad's been staying at my house lol. hard to find a free time.

    here's what i've coded so far. appreciate the help! reminder...i don't need the actual code done for me, just the theory to get there, please.

    the part i'm trying to figure out is the part near the end in the summary.

    import java.util.*;
     
    public class NewPatientRecordsSystem {
        public static void main(String[] args) {
            String appHeading = "  Java Clinic\nPatient Billing\n===============";
            String patientName;
            String healthNumber;
            String physicianName;
            String specialty;
            double visitFee;
            double totalVisitFee = 0;
            Boolean newBill = true;
     
            Patient patient;
            Physician physician;
     
            // Initiate keyboard scanner
            Scanner keyboard = new Scanner(System.in);
     
            // Create an ArrayList to hold the bills & people
            ArrayList <Bill> billList = new ArrayList<Bill>();
            ArrayList <Person> personList = new ArrayList<Person>();
     
            while (newBill) {    
                // Clears the screen before entering each bill.
                AssignmentHelper.clearScreen();
     
                // Print application header
                System.out.println(appHeading);
     
                // Collect patient's name.
                patientName = AssignmentHelper.getStringInputFromKeyboard("\nEnter patient's name: ",
                    "\n  **Error** - The patient's name is required");
     
                // Collect patient's health number.    
                healthNumber = AssignmentHelper.getStringInputFromKeyboard("\nEnter " + patientName + 
                    "'s health number: ", "\n  **Error** - The patient's health number is required");
     
                // Create new patient record.
                patient = new Patient(patientName, healthNumber);
     
                // Collect physician's name.
                physicianName = AssignmentHelper.getStringInputFromKeyboard("\nEnter physician's name: ",
                    "\n  **Error** - The physician's name is required");
     
                // Collect physician's specialty.
                specialty = AssignmentHelper.getStringInputFromKeyboard("\nEnter " + physicianName + 
                    "'s specialty: ", "\n  **Error** - The physician's specialty is required");
     
                // Collect physician's visit fee.
                visitFee = AssignmentHelper.getDoubleInputFromKeyboard("\nEnter physician's visit fee: ",
                    "\n  **Error** - Invalid visit fee");
     
                // Create new physician record.
                physician = new Physician(physicianName, specialty, visitFee);
     
                // Create new bill record & store it in the bill array list.
                billList.add(new Bill(patient, physician));
     
                // Prompt user for another bill.
                newBill = AssignmentHelper.getYesNoFromKeyboard("\nAnother bill? (y/n): ");
            }
     
            System.out.println("\nBilling Summary\n===============");
     
            for (int i = 0; i < billList.size(); i++) {
                System.out.println(billList.get(i).toString());
                Bill currentBill = billList.get(i);
                if (currentBill instanceof Physician) {
                    totalVisitFee += ((Physician)billList).getVisitFee();
                }
            }
     
            System.out.println("Total Billings: $" + totalVisitFee);
        }
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: An Array List of a Class that contains two other Classes

    i'm trying to figure out is the part near the end in the summary.
    Please ask something specific about your problem.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: An Array List of a Class that contains two other Classes

    Quote Originally Posted by Norm View Post
    Please ask something specific about your problem.
    sorry about that.

    What I'm trying to do is properly fetch the visit fee for bill i using the getVisitFee() method (within the physician class). The Bill class contains Physician objects.

    Is this possible? The code I have right now won't compile...the compile issue is at this line: if (currentBill instanceof Physician) {

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: An Array List of a Class that contains two other Classes

    fetch the visit fee
    Where is the data for the fee? Its location will determine how it is "fetched".
    Read a disk file or get it from a user.

    The code I have right now won't compile
    When you get errors, copy the full text of the messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: An Array List of a Class that contains two other Classes

    Quote Originally Posted by Norm View Post
    Where is the data for the fee? Its location will determine how it is "fetched".
    Read a disk file or get it from a user.


    When you get errors, copy the full text of the messages and paste it here.
    the visit fee is collected from the user via the console, then a new physician is created (with the fee part of the constructor) and then a new bill is created and stored in the billList array list. So what I am hoping to do is get the visit fee stored in the array list in the physician object.

                // Collect physician's visit fee.
                visitFee = AssignmentHelper.getDoubleInputFromKeyboard("\nEnter physician's visit fee: ",
                    "\n  **Error** - Invalid visit fee");
     
                // Create new physician record.
                physician = new Physician(physicianName, specialty, visitFee);
     
                // Create new bill record & store it in the bill array list.
                billList.add(new Bill(patient, physician));

    the compile error i'm getting is:

    inconvertible types.
    found: Bill; required: Physician

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: An Array List of a Class that contains two other Classes

    found: Bill; required: Physician
    The compiler wants the code to use a variable of type: Physician where it now has one of type Bill.


    You've left off some of the error message so I don't know what statement is causing that error.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: An Array List of a Class that contains two other Classes

    Quote Originally Posted by Norm View Post
    The compiler wants the code to use a variable of type: Physician where it now has one of type Bill.


    You've left off some of the error message so I don't know what statement is causing that error.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    we have to use BlueJ to compile...and unfortunately it hides a lot of info. :/

    it's showing it's stopping at this line:

    for (int i = 0; i < billList.size(); i++) {
    System.out.println(billList.get(i).toString());
    Bill currentBill = billList.get(i);
    if (currentBill instanceof Physician) {
    totalVisitFee += ((Physician)billList).getVisitFee();
    }
    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: An Array List of a Class that contains two other Classes

    What is the error for the statement in bold?

    if (currentBill instanceof Physician)
    That's a strange question to ask. currentBill was just define as a Bill????
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: An Array List of a Class that contains two other Classes

    how can i get access to the get methods of the Physicians within an array list of Bills?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: An Array List of a Class that contains two other Classes

    Does the Bill class container Physicians? Does the Bill class have a method that will return a reference to the Physicians? Call that method to get to the Physicians.

    I'm done for tonight.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: An Array List of a Class that contains two other Classes



    Yes, the Bill class contains Physicians and Patients. There is no method in the Bill class that returns reference to the Physicians, from what I can tell, unless the toString can do so? I'm currently using the toString to get other data at the moment, though.

    We must use the structure of the classes outlined in the diagram above for the assignment. So if it's not possible to do it using the current structure, that's all I need to know and I can just go ahead and use the non-OO method to track and add the total fees based on user input.

    Thanks for your help. Good night!

  15. #15
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi I'm just wondering if anyone else has any perspective on whether it can be coded as I laid out? Thank you so much!

  16. #16
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: An Array List of a Class that contains two other Classes

    i think I got it figured out. within the for loop at the end i added the 3 lines of code that was able to fetch the visit fee. In order for it to work, I had to create a separate Physician arraylist before user input and store the physician into it (I still couldn't figure out how to grab this information using the Bill arraylist...)

            for (int i = 0; i < billList.size(); i++) {
                System.out.println(billList.get(i).toString());
     
                Physician tempPhysician = physicianList.get(i);
                double tempVisitFee = tempPhysician.getVisitFee();
                totalVisitFee += tempVisitFee;
            }


    --- Update ---

    so...i realized that the code i just posted was much more complicated than required. here's the simplified code:

            // loop through the bill arraylist for each bill.
            for (int i = 0; i < billList.size(); i++) {
                // print the summary of bill i.
                System.out.println(billList.get(i).toString());
     
                // Get the visit fee for physician i.
                visitFee = physicianList.get(i).getVisitFee();
     
                // add physician i's visit fee to the session's visit fee total.
                totalVisitFee += visitFee;
            }

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: An Array List of a Class that contains two other Classes

    Does the Bill class have a way to get the Physician associated with the bill?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: An Array List of a Class that contains two other Classes

    Quote Originally Posted by Norm View Post
    Does the Bill class have a way to get the Physician associated with the bill?
    nope. just the Bill constructor and the overridden toString. And our assignment doesn't allow us to modify the class structure.

    So I guess that means there is no way to do it other than the way I laid it out, correct?

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: An Array List of a Class that contains two other Classes

    What use is there for the Bill class to have members that can't be accessed? What are they used for?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: An Array List of a Class that contains two other Classes

    That's a great question for my prof! lol... all I'm using them for now is for each the patient and physician's toString() methods to describe them, and then i'm accessing those toString methods in the Bill's toString() method. That allowed me to give a summary of the bill at once. It would have been just as easy to just forego the Bill altogether

    The assignment was mainly to introduce us to polymorphism. I assume if the assignment was more fleshed out it would have more methods and be more useful.

    I'm working with what I'm given!

    Thanks.

Similar Threads

  1. How to automatically convert from Array of Classes to Class of Arrays
    By Vortex16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 11th, 2012, 07:03 PM
  2. Automatically convert from Array of Classes to Class of arrays
    By Vortex16 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 11th, 2012, 01:08 PM
  3. Replies: 2
    Last Post: February 21st, 2012, 01:25 AM
  4. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM
  5. In a class create an array list of elements of another class, help!
    By LadyBelka in forum Collections and Generics
    Replies: 3
    Last Post: May 4th, 2011, 05:00 PM

Tags for this Thread