import java.util.Scanner;

/**
 * Holds a filename, allowing various queries and modifications.
 *
 * @author Zach Tomaszewski
 * @version Sep 18, 2007
 */
public class Filename {

  private String filename;


  /**
   * Creates a new Filename with the given contents,
   * which must contain a '.' character.
   */
  public Filename(String name){
    this.filename = name;
  }

  /**
   * Returns only the part of the filename that comes before
   * the last dot.
   */
  public String getName() {
    return this.filename.substring(0, this.filename.lastIndexOf('.'));
  }

  /**
   * Returns only the part of the filename that comes after
   * the last dot.
   */
  public String getExtension() {
    return this.filename.substring(this.filename.lastIndexOf('.') + 1);
  }

  /**
   * Returns the file extension, all in caps.
   */
  public String getType() {
    return this.getExtension().toUpperCase();
  }

  /**
   * Returns the length of the whole filename.
   */
  public int getLength(){
    return this.filename.length();
  }

  /**
   * Returns a "safe" version of the filename, with all spaces
   * replaced with '_' and all lowercased.
   */
  public String getSaferFilename() {
    String safe = this.filename;
    safe = safe.replace(' ', '_');
    safe = safe.toLowerCase();
    return safe;
  }

  /**
   * Returns a the complete filename.
   */
  public String toString() {
    return this.filename;
  }



  /**
   * Tests Filename by asking the user for one and then
   * demonstrating all possible methods.
   */
  public static void main(String[] args) {
    //ask the user for a filename
    System.out.print("Enter a filename: ");
    Scanner keybd = new Scanner(System.in);
    String name = keybd.nextLine();

    //demo methods
    Filename file = new Filename(name);
    System.out.println("\nCreated new Filename object.");

    System.out.println("Original filename: " + file);
    System.out.println("Name part of filename: " + file.getName());
    System.out.println("Extension part of filename: " + file.getExtension());
    System.out.println("File type: " + file.getType());
    System.out.println("Safer version of filename: " + file.getSaferFilename());

  }

}
