Answer:
See explaination
Explanation:
// Address.java
public class Address {
/**
* The name of the business
*/
private String name;
/**
* The name of the street the business is on
*/
private String street;
/**
* The street number of the business
*/
private int number;
/**
* Constructs an Address that represents a business with name nm,
* at number no on the street st
*/
public Address(String nm, String st, int no)
{
name = nm;
street = st;
number = no;
}
/**
* Returns the name of the business
*/
public String getName()
{
return name;
}
/**
* Returns the name of the street on which the business is located
*/
public String getStreet()
{
return street;
}
/**
* Returns the street number of the business
*/
public int getNumber()
{
return number;
}
}
//end of Address.java
//AddressBook.java
import java.util.ArrayList;
import java.util.List;
public class AddressBook {
/**
* The list of business addresses. No two businesses in the list
* can have the same address (both the same street and street number)
*/
private List<Address> addresses;
/**
* Constructs an empty AddressBook
*/
public AddressBook()
{
addresses = new ArrayList<Address>();
}
/**
* atparam st the name of a street
* atreturn a list with the names of each business with an address on that street
*/
public List<String> onStreet(String st)
{
// create an empty output list of names of business
List<String> businessName = new ArrayList<String>();
// loop over the list of addresses
for(int i=0;i<addresses.size();i++)
{
// if ith street of address = nm, add the name of the business to the output list
if(addresses.get(i).getStreet().equalsIgnoreCase(st))
businessName.add(addresses.get(i).getName());
}
return businessName; // return the list
}
/**
* Searches for an existing business with an identical address (street and number
* both match). Updates the record to an address with name nm, street st and number no.
* If no entry already exists adds a new address to the end of the list with these parameters.
*
* atparam nm the name of the business
* atparam st the street the business is on
* atparam no the street number of the business
* atreturn the index of where the business address is on the list
*/
public int newBusiness(String nm, String st, int no)
{
// loop over the list of addresses
for(int i=0;i<addresses.size();i++)
{
// if ith index addresses match the street and number of the input st and no
if((addresses.get(i).getStreet().equalsIgnoreCase(st)) && (addresses.get(i).getNumber() == no))
{
addresses.remove(i); // remove the ith address from list
addresses.add(i, new Address(nm,st,no)); // add a new address with the input name, street and number at ith index
return i; // return the index i
}
}
// if no address match, add the business at the end of the list
addresses.add(new Address(nm,st,no));
return addresses.size()-1; // return the last index
}
}
//end of AddressBook.java