You will be responsible for the completion of the groceryLine class. The toStrin
ID: 3914435 • Letter: Y
Question
You will be responsible for the completion of the groceryLine class. The toString() method and attribute have been given to you.
import java.util.*;
public class Person
{
//attributes
protected String firstName = null;
protected String lastName = null;
//constructor
public Person(String first, String last)
{
this.firstName = first;
this.lastName = last;
}
public String toString()
{
String personDetails = this.firstName + " " + this.lastName +" ";
return personDetails;
}
}
----------------------------------------------------------------------------------------
import java.util.*;
public class PoD
{
public static void main( String [] args )
{
Scanner in = new Scanner( System.in );
GroceryLine groceryLine = new GroceryLine();
//Read in initial line of customers
String inputLine1 = in.nextLine();
String[] initialLine = inputLine1.split(",");
for (int i=0; i<initialLine.length; i++)
{
String[] personDetails = initialLine[i].split(" ");
Person nextPerson = new Person(personDetails[0],personDetails[1]);
groceryLine.newCustomer(nextPerson);
}
System.out.println("--- INITIAL GROCERY LINE ---");
System.out.println(groceryLine);
//Modify initial line of customers
String inputLine2 = in.nextLine();
String[] tasks = inputLine2.split(",");
int lastTask = tasks.length;
Person lastCustomer = null;
for (int i=0; i<lastTask; i++)
{
if (tasks[i].equals("first"))
{
//Next Customer
lastCustomer = groceryLine.nextCustomer();
}
else if (tasks[i].equals("last"))
{
//New Cashier
lastCustomer = groceryLine.newCashier();
}
else
{
//New customer
String[] personDetails = tasks[i].split(" ",2);
lastCustomer = new Person(personDetails[0],personDetails[1]);
groceryLine.newCustomer(lastCustomer);
}
}
System.out.println("Last person to enter/leave line:");
System.out.println(lastCustomer);
System.out.println("--- FINAL GROCERY LINE ---");
System.out.println(groceryLine);
in.close();
System.out.print("END OF OUTPUT");
}
}
---------------------------------------------------------------------------------------
import java.util.*;
public class GroceryLine
{
//attributes
protected LinkedList<Person> groceryLine = new LinkedList<Person>();
//PLEASE FINISH THIS CLASS HERE
//===============================================
//constructor
// method: nextCustomer()
// @returns Person
// Next (first) customer in the line goes is called to the cashier
// method: newCustomer(Person)
// @params Person
// @returns void
// A new customer (Person) joins the end of the line.
// method: newCashier()
// @returns person
// A new cashier opens up. The last person in the line chooses to leave the current line.
//===============================================
//PLEASE END WORK HERE
public String toString()
{
String lineMembers = "GROCERY LINE: ";
int lineCount = 0;
for (Person person: groceryLine)
{
lineCount++;
lineMembers += lineCount + ". " + person.toString();
}
return lineMembers;
}
}
GroceryLine class Grocery Line #groceryLine LinkedList«Perso +GroceryLine() Constructor +nextCustomer) Person Next (first) customer in the line goes is called to the cashier, i.e. the first customer is removed from the line +newCustomer(Person) void A new customer (Person) joins the end of the line. +newCashier Person A new cashier opens up. The last person in the line chooses to leave the current line +toString() StringExplanation / Answer
import java.util.*; public class GroceryLine { //attributes protected LinkedList groceryLine = new LinkedList(); //PLEASE FINISH THIS CLASS HERE //=============================================== //constructor public GroceryLine() { } // method: nextCustomer() // @returns Person public Person nextCustomer() { return groceryLine.removeFirst(); } // Next (first) customer in the line goes is called to the cashier // method: newCustomer(Person) // @params Person // @returns void public void newCustomer(Person person) { groceryLine.add(person); } // A new customer (Person) joins the end of the line. // method: newCashier() // @returns person // A new cashier opens up. The last person in the line chooses to leave the current line. public Person newCashier() { return groceryLine.removeLast(); } //=============================================== //PLEASE END WORK HERE public String toString() { String lineMembers = "GROCERY LINE: "; int lineCount = 0; for (Person person: groceryLine) { lineCount++; lineMembers += lineCount + ". " + person.toString(); } return lineMembers; } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.