Class Passport

java.lang.Object
  extended by Passport

public class Passport
extends Object

The Passport class is a simplified representation of a real passport. It contains information about which countries the owner of the passport has visited, and for how long.

This class should have THREE instance variables (you must not add any other instance variables):


Constructor Summary
Passport(String n, int s)
          Create a new passport with name n, serial number s, and no countries visited.
 
Method Summary
 void addVisited(String country, int duration)
          Add a new entry to the countries visited section of this passport, using the information provided by the user (in the 2 arguments); The new information should be appended to the end of the current variable;

Make sure to preserve the format of this variable, as described above; You can assume that the user will not put in negative integers as the duration.
Also, there must be NO consecutive entries with the same country; if the previous entry has the same country, then the two entries must be merged and their durations added together to form one entry (e.g., "Canada, 10; Canada, 10" should be "Canada, 20").
 void clear()
          Clear the history of the countries visited for this passport (i.e., make it empty).
 String firstCountryVisited()
          Return the first country that the owner of this passport has visited, or empty string ("") if no countries have been visited.
 String getCountriesVisited()
          Return the countries visited section of this passport.
 String getName()
          Return the name of this passport's owner.
 int getSerialNumber()
          Return the serial number of this passport.
 String lastCountryVisited()
          Return the last country that the owner of this passport has visited, or empty string ("") if no countries have been visited.
 String longestVisitedCountry()
          Return the country that the owner of this passport has visited for the longest duration, or empty string ("") if no countries have been visited;
If there is a tie between two or more countries, return the most recent of these countries (i.e., the first one from the right of the String variable).
 int numEntries()
          Return the total number of entries in this passport's countries visited section; You must count every entry, including multiple visits to the same country.
 int numVisits(String country)
          Return the number of times this passport's owner has visited the country specified by the user.
 String toString()
          Return the information about this passport as a String in the following format (3 lines; there must be one space after the first ":" on each line):

Name: n
Serial Number: s
Countries Visited: cv

where n is the name of this passport's owner, s is the serial number of this passport, and cv is this passport's record of countries visited (along with the durations);
If no countries have been visited, then the value of cv must be the String "none".
 int totalDuration(String country)
          Return the total number of days that the owner of this passport spent in the specified country.
 String whichCountryVisited(int index)
          Return the country that the owner of this passport visited at the specified index -- which starts at 1 (not zero!);
If the user specified an invalid index (i.e., less than 1 or greater than the number of entries), then return the String: "Invalid index!"
(it's important that the String be EXACTLY like this, with the capital I at the beginning and ! at the end).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Passport

public Passport(String n,
                int s)
Create a new passport with name n, serial number s, and no countries visited.

Method Detail

getName

public String getName()
Return the name of this passport's owner.


getSerialNumber

public int getSerialNumber()
Return the serial number of this passport.


getCountriesVisited

public String getCountriesVisited()
Return the countries visited section of this passport.


addVisited

public void addVisited(String country,
                       int duration)
Add a new entry to the countries visited section of this passport, using the information provided by the user (in the 2 arguments); The new information should be appended to the end of the current variable;

Make sure to preserve the format of this variable, as described above; You can assume that the user will not put in negative integers as the duration.
Also, there must be NO consecutive entries with the same country; if the previous entry has the same country, then the two entries must be merged and their durations added together to form one entry (e.g., "Canada, 10; Canada, 10" should be "Canada, 20").


clear

public void clear()
Clear the history of the countries visited for this passport (i.e., make it empty).


numEntries

public int numEntries()
Return the total number of entries in this passport's countries visited section; You must count every entry, including multiple visits to the same country.


numVisits

public int numVisits(String country)
Return the number of times this passport's owner has visited the country specified by the user.


firstCountryVisited

public String firstCountryVisited()
Return the first country that the owner of this passport has visited, or empty string ("") if no countries have been visited.


lastCountryVisited

public String lastCountryVisited()
Return the last country that the owner of this passport has visited, or empty string ("") if no countries have been visited.


whichCountryVisited

public String whichCountryVisited(int index)
Return the country that the owner of this passport visited at the specified index -- which starts at 1 (not zero!);
If the user specified an invalid index (i.e., less than 1 or greater than the number of entries), then return the String: "Invalid index!"
(it's important that the String be EXACTLY like this, with the capital I at the beginning and ! at the end).


longestVisitedCountry

public String longestVisitedCountry()
Return the country that the owner of this passport has visited for the longest duration, or empty string ("") if no countries have been visited;
If there is a tie between two or more countries, return the most recent of these countries (i.e., the first one from the right of the String variable).


totalDuration

public int totalDuration(String country)
Return the total number of days that the owner of this passport spent in the specified country.


toString

public String toString()
Return the information about this passport as a String in the following format (3 lines; there must be one space after the first ":" on each line):

Name: n
Serial Number: s
Countries Visited: cv

where n is the name of this passport's owner, s is the serial number of this passport, and cv is this passport's record of countries visited (along with the durations);
If no countries have been visited, then the value of cv must be the String "none".

Overrides:
toString in class Object