import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class StringTest extends JFrame
        implements ActionListener
{
  private JTextField input, result;

  public StringTest()
  {
    super("String test");
    Box box1 = Box.createVerticalBox();
    box1.add(new JLabel("Input:"));
    box1.add(Box.createVerticalStrut(10));
    box1.add(new JLabel("Result:"));

    input = new JTextField(20);
    input.setBackground(Color.WHITE);
    input.addActionListener(this);
    input.selectAll();

    result = new JTextField(20);
    result.setBackground(Color.WHITE);
    result.setEditable(false);

    Box box2 = Box.createVerticalBox();
    box2.add(input);
    box2.add(Box.createVerticalStrut(10));
    box2.add(result);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    c.add(box1);
    c.add(box2);
    input.requestFocus();
  }

  public void actionPerformed(ActionEvent e)
  {
    String str = input.getText();

    // String Exercise 1
    
    //str = str.replace('/', '-');
    // or
    //str = str.replaceAll("/", "-");
    
    
    // String Exercise 2
    
    // Inefficient since requires creation of three new Strings
    /*
    String day, month, year;
    day = str.substring(0,str.indexOf('/'));
    month = str.substring(day.length()+1, str.lastIndexOf('/'));
    year = str.substring(day.length()+1+month.length()+1);
    if (day.length()<2) { day = "0" + day; }
    if (month.length()<2) { month = "0" + month; }
    str = day.substring(0,2) + "-" + month.substring(0,2) + "-" + year.substring(0,4);
    */
    
    // Slightly more efficient method that just creates one String array
    // This method also forces the output into a date formula, even if it isn't correct ; )
    // ie. inputs 012/3456/789 and 1/34/7890 both return as 01/34/7890
    // Coding a check to validate the date, however, would be a different exercise ; P
    /*
    if (str.indexOf('/')!=-1) {
    	// Split into sections
    	String[] strSplit = str.split("/");
	    if (strSplit.length >=3) {
		    // Double digit day
		    if (strSplit[0].length()<2) { strSplit[0] = "0" + strSplit[0]; }
		    else if (strSplit[0].length()>2) { strSplit[0] = strSplit[0].substring(0,2); }
		    // Double digit month
		    if (strSplit[1].length()<2) { strSplit[1] = "0" + strSplit[1]; }
		    else if (strSplit[1].length()>2) { strSplit[1] = strSplit[1].substring(0,2); }
		    // Four digit year
		    // Note: adds zeros, then removes extras in next step
		    if (strSplit[2].length()<2) { strSplit[2] = strSplit[2] + "0000"; }
		    if (strSplit[2].length()>4) { strSplit[2] = strSplit[2].substring(0,4); }
		    str = strSplit[0] + "-" + strSplit[1] + "-" + strSplit[2];
	    }
    }
    */
    
    
    // String Exercise 4 - see after current method
    //testStartsEndsWith()
    
    // String Exercise 5 - see after String Exercise 4
    //str = removeTag(str);
    
    // String Exercise 6 - see after String Exercise 5
    //if (containsOnlyDigits(str)) { System.out.println("All digits"); }
    //else { System.out.println("Not all digits"); }
    
    
    
    result.setText(str);
    input.selectAll();
  }
  
// String Exercise 4)
  
  // Test startsWith() and endsWith() methods
  static public void testStartsEndsWith() {
	  if (startsWith("abcdef","abc")) {
		System.out.println("1 start matched"); // Prints this
		} else {
			System.out.println("1 start not matched"); 
		}
		if (startsWith("abcdef","123")) {
		System.out.println("2 start matched");  
		} else {
			System.out.println("2 start not matched"); // Prints this
		}
		if (endsWith("abcdef","def")) {
		System.out.println("3 end matched"); // Prints this
		} else {
			System.out.println("3 end not matched"); 
		}
		if (endsWith("abcdef","123")) {
		System.out.println("4 end matched"); 
		} else {
			System.out.println("4 end not matched"); // Prints this
		}
  }
	
  static public boolean startsWith(String s, String prefix) {
	  if (s.substring(0,prefix.length()).equals(prefix)) return true;
	  return false; // Returns false only when the previous "if" is false
  }

  static public boolean endsWith(String s, String prefix) {
	  if (s.substring(s.length()-prefix.length()).equals(prefix)) return true;
	  return false; // Returns false only when the previous "if" is false
  }
  
// String Exercise 5
  
  static public String removeTag(String s) {

	  if ( // Various checks confirming the format <tag>text</tag>
			  s.startsWith("<") // Starts with a "<"
			  && s.endsWith(">") // Ends with with a "<"
			  && (s.indexOf('<') != s.lastIndexOf('<')) // At least two "<"s
			  && (s.indexOf('>') != s.lastIndexOf('>')) // At least two ">"s
			  && (s.charAt(s.lastIndexOf('<')+1)=='/') // Closing tag starts with a "/"
			  && (s.substring(1,s.indexOf('>'))) // Prefix equals...
				  .equals((s.substring(s.lastIndexOf('<')+2, s.length()-1))) // Suffix
	  ) {
		  s = s.substring(s.indexOf('>')+1, s.lastIndexOf('<')); // In-between text
	  }
	  return s; // If doesn't match the checks, just returns the original
  }

// String Exercise 6
  
  static public boolean containsOnlyDigits(String s) {
	  for (int i = 0; i < s.length(); i++) {
		  if (!Character.isDigit(s.charAt(i))) { return false; }
	  }
	  return true; // Returns true only if all characters were digits
  }

  public static void main(String[] args)
  {
    StringTest window = new StringTest();
    window.setBounds(100, 100, 300, 100);
    window.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window.setVisible(true);
  }
}

