-1

I need to create an organ donation registry class that has a list for the donator name and organ and a list for the recipient name and organ, I can't figure out how to find out when the organ of both objects is equal to create a string detailing who gave what to who.

Here's the code that I currently have, the main problem lies within the for loops, I want to compare both lists and when there's a match for the organs I want to print out the line detailing who the donor is, what they donated, and who they donated to. When I've tried this nothing is output.

import java.util.Scanner;

import DSAndAlgos.\*;
public class OrganDonation
{

    public static void main(String[] args) 
    {
        ArrayIndexedList<OrganInfo> oDonated = new ArrayIndexedList<OrganInfo>();
        ArrayIndexedList<OrganInfo> oNeeded = new ArrayIndexedList<OrganInfo>();
        
        boolean running = true;
        
        //menu of options and taking the input
        Scanner sc = new Scanner(System.in);
        System.out.println(menuOfOptions());
        String input = sc.nextLine();
        
        //loop for the program
        while(running == true)
        {
        
        while(!input.equals("3"))
        {
            sc = new Scanner(System.in);
            switch(input)
            {
            //adding a recipient 
            case "1":
                System.out.println("For who? ");
                String need = sc.nextLine();
                System.out.println("What organ? ");
                String organ = sc.nextLine();
                OrganInfo needed = new OrganInfo(organ, need);
                oNeeded.add(needed);
                break;  
            //adding a donor
            case "2":
                sc = new Scanner(System.in);
                System.out.println("By who?");
                String donor = sc.nextLine();
                System.out.println("What organ?");
                organ = sc.nextLine();
                OrganInfo donated = new OrganInfo(organ,donor);
                oDonated.add(donated);
                
                for(int i = 0; i < oDonated.size();i++)
                {
                    for(int k = 0; k < oNeeded.size();k++)
                    {
                        if(oNeeded.get(k).getOrgan() == oDonated.get(i).getOrgan())
                        {
                            System.out.println(oDonated.get(i).getPerson() + " 's " + oDonated.get(i).getOrgan() + " was given to " + oNeeded.get(k).getPerson());
                        }
                    }
                }
                break;
                //when anything other than 1, 2, or 3 is input
            default:
                System.out.println("Invalid selection. Please try again.");
                break;
    
            }
            
            System.out.println(menuOfOptions());
            sc = new Scanner(System.in);
            input = sc.next();
        }
        
        //closing the program when 3 is input
        if(input.contains("3"))
        {
            running = false;
            System.out.println("Goodbye!");
        }
        sc.close();
        }
        
    }
    
    
    public static class OrganInfo
    {
        private String _organ;
        private String _person;
        
        public OrganInfo(String organ, String person)
        {
            _organ = organ;
            _person = person;
        }
        
        public String getOrgan() {return _organ;}
        public String getPerson() {return _person;}
    }
    
        
    
    
    
    public static String menuOfOptions()
    {
        return """
                1.) Register organ needed.
                2.) Register organ donated.
                3.) Exit.
                ----------------------------
                Select 1, 2 or 3: 
                """;
    }
}
starball
  • 20,030
  • 7
  • 43
  • 238
eggshell
  • 1
  • 2

1 Answers1

-1

You are comparing the organs (Strings) with ==. Strings are objects and in Java Objects need to be compared with equals. == checks if it is the same instance.

user2999226
  • 129
  • 4