Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to add each guest into the corresponding party object by the addguest( )m

ID: 3561873 • Letter: I

Question

I need to add each guest into the corresponding party object by the addguest( )method to the party class.

// java.io classes, used for file i/o
  

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

   // load party data from file and
   public class PartyDriver
   {
      public static void main(String[] args) throws IOException
      {
        Party[] parties = new Party[50];
        int partyCount = 0; // actual number of parties

        partyCount = (new PartyDriver()).load(parties, "data/input.txt");

        // 2. process parties
        // 2.0 test print
        System.out.println(partyCount + " parties loaded successfully");
        for (int i = 0; i<partyCount; i++)
        {
          System.out.printf("Party[%d]: hosted by %s ", i, parties[i]);
        }
        // 2.1 more processing
        // The system prints
        System.out.println("");
        System.out.println("Party with most attendants:");
        System.out.println("Party of lion King ("+ partyCount +" out of " + parties.length + " spots filled):");
      }

   /**
       * Fills party array with data from text file.
       *
       *
       * @param parties array of Party to hold parties
       * @param fileName name of input text file
       * @return number of parties loaded
       * @throws IOException any exception during file opening, reading, and closing
       */
      @SuppressWarnings("unused")
   public int load(Party[] parties, String fileName)
          throws IOException
      {
        Scanner fileIn = null; // scanner object to connect to file
        int partyCount = 0;   // track # of parties

        try {
          // open input file
          fileIn = new Scanner(new BufferedReader(new FileReader(fileName)));

          // set up Scanner object fileIn to read one
          // line at a time, and ignore extra whitespace
          // characters (space, tab, newline character)
          fileIn.useDelimiter("\s* \s*");

          // loop through multiple parties
          while (fileIn.hasNext())
          {
            // start one party

            // 1. host name
            String hostName = fileIn.next();
            parties[partyCount] = new Party(partyCount, hostName);
          
            // 2. party capacity
            int partyMaxSize = fileIn.nextInt();

            // 3. actual # of guests
            int guestNum = fileIn.nextInt();

            // Test print part 1: display party level data
            //System.out.printf("Host: %s, Max: %d, Actual: %d ",
                //hostName, partyMaxSize, guestNum);

            // 4. that many # of guests
            for (int i=0; i<guestNum; i++)
            {
              String guestName = fileIn.next();

              // Test print part 2: print each guest name
              //System.out.println(guestName);
            }

public class Party
{
private final int size; // constant to specify maximum number of guest allowed
private String hostName; // name of party host
private String[] guestList; // List of party guest names
private int guestCount; // number of guest recorded

public Party (int size, String hostName)
{
    this.size = size;
    this.hostName = hostName;
    guestList = new String[size];
    guestCount = 0;
   }// end constructor


    public void addGuest (String guestName)
{
   if(guestCount >= size) //full
    {
       System.out.println(guestName + "cannot come to the party. The guest list is full");
    }
    else if (isOnList(changeNameOrder(guestName))) //duplicate
    {
      System.out.println( guestName + " is already on the guest list.");
    }
    else // add to list
    {
        int i =guestCount-1;
       
        while (i>=0 && guestList[i].compareToIgnoreCase(guestName) > 0)
        {
            guestList[i +1] = guestList[i];
            i--;
        }
      guestList[i +1] = guestName;
      guestCount++;
    }
} // end addGuest

//*************************************************

//This method checks whether an given guest is on the list
public void printParty()
{
       
        System.out.println("Guest list for " + hostName + "'s party:");
    for (int i=0; i<guestCount; i++)
    {
      System.out.println(guestList[i]);
    }
   
} // end printGuestList

// *************************************************

private String changeNameOrder(String nameFirstLast)
{
    String firstname, lastname;
    firstname = nameFirstLast.substring(0, nameFirstLast.lastIndexOf(' '));
    lastname = nameFirstLast.substring(nameFirstLast.lastIndexOf(' ')+ 1,nameFirstLast.length());
            return (lastname + ", " + firstname);
} // end changeNameOrder
  
//*************************************************

private boolean isOnList(String guestName)
{
    if (guestCount==0) // empty list
        return false;
   
    for (int i=0; i<guestCount; i++)
    {
        if (guestName.equalsIgnoreCase(guestList[i]))
        {
            return true; // yes already on list
        }
    } // end of for loop
    return false; // not found        
} // end of isOnList
public String gethostName()
{
    return hostName;
   
} // end get hostName

public int getparties()
{
    return getparties();
   
} // end get size

public int getguestCount()
{
    return guestCount;
   
} // end getguestCount
}// end class

Explanation / Answer

Java class party.java

public class party

{

private int size; // constant to specify maximum number of guest allowed

private String hostName; // name of party host

private String[] guestList; // List of party guest names

private int guestCount; // number of guest recorded

public

party (int size, String hostName)

{

this.size = size;

this.hostName = hostName;

guestList = new String[size];

guestCount = 0;

}

// end constructor

public void addGuest (String guestName)

{

if(guestCount >= size) //full

{

System.

out.println(guestName + "cannot come to the party. The guest list is full");

}

else if (isOnList(changeNameOrder(guestName))) //duplicate

{

System.

out.println( guestName + " is already on the guest list.");

}

else // add to list

{

int i =guestCount-1;

while (i>=0 && guestList[i].compareToIgnoreCase(guestName) > 0)

{

guestList[i +1] = guestList[i];

i--;

}

guestList[i +1] = guestName;

guestCount++;

}

}

// end addGuest

//*************************************************

//This method checks whether an given guest is on the list

public void printParty()

{

System.

out.println("Guest list for " + hostName + "'s party:");

for (int i=0; i<guestCount; i++)

{

System.

out.println(guestList[i]);

}

}

// end printGuestList

// *************************************************

private String changeNameOrder(String nameFirstLast)

{

String

firstname, lastname;

firstname = nameFirstLast.substring(0, nameFirstLast.lastIndexOf(' '));

lastname = nameFirstLast.substring(nameFirstLast.lastIndexOf(' ')+ 1,nameFirstLast.length());

return (lastname + ", " + firstname);

}

// end changeNameOrder

//*************************************************

private boolean isOnList(String guestName)

{

if (guestCount==0) // empty list

return false;

for (int i=0; i<guestCount; i++)

{

if (guestName.equalsIgnoreCase(guestList[i]))

{

return true; // yes already on list

}

}

// end of for loop

return false; // not found

}

// end of isOnList

public String gethostName()

{

return hostName;

}

// end get hostName

public int getparties()

{

return getparties();

}

// end get size

public int getguestCount()

{

return guestCount;

}

// end getguestCount

}

// end class

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

main Class:

package sample;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

// load party data from file and

public class PartyDriver

{

public static void main(String[] args) throws IOException

{

party[] parties = new party[50];

int partyCount = 0; // actual number of parties

partyCount = (new PartyDriver()).load(parties, "data/input.txt");

// 2. process parties

// 2.0 test print

System.

out.println(partyCount + " parties loaded successfully");

for (int i = 0; i<partyCount; i++)

{

System.out.printf("Party[%d]: hosted by %s ", i, parties[i]);

}

// 2.1 more processing

// The system prints

System.out.println("");

System.out.println("Party with most attendants:");

System.out.println("Party of lion King ("+ partyCount +" out of " + parties.length + " spots filled):");

}

/**

* Fills party array with data from text file.

*

*

*

@param parties array of Party to hold parties

*

@param fileName name of input text file

*

@return number of parties loaded

*

@throws IOException any exception during file opening, reading, and closing

*/

@SuppressWarnings("unused")

public int load(party[] parties, String fileName)

throws IOException

{

Scanner

fileIn = null; // scanner object to connect to file

int partyCount = 0; // track # of parties

try {

// open input file

fileIn = new Scanner(new BufferedReader(new FileReader(fileName)));

}

catch(FileNotFoundException e)

{

System.

out.println(e);

}

// set up Scanner object fileIn to read one

// line at a time, and ignore extra whitespace

// characters (space, tab, newline character)

fileIn.useDelimiter("\s* \s*");

// loop through multiple parties

while (fileIn.hasNext())

{

// start one party

// 1. host name

String

hostName = fileIn.next();

parties[partyCount] = new party(partyCount, hostName);

// 2. party capacity

int partyMaxSize = fileIn.nextInt();

// 3. actual # of guests

int guestNum = fileIn.nextInt();

// Test print part 1: display party level data

//System.out.printf("Host: %s, Max: %d, Actual: %d ",

//hostName, partyMaxSize, guestNum);

// 4. that many # of guests

for (int i=0; i<guestNum; i++)

{

String

guestName = fileIn.next();

// Test print part 2: print each guest name

System.

out.println(guestName);

}

}

return partyCount;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote