therrontelford

Editor2

Jul 30th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. public class Editor2 implements Ed
  2. {
  3.    private String str;
  4.    private int slice;
  5.    
  6.    public Editor2( String _str, int index )
  7.    {
  8.        str = _str;
  9.        slice = index;
  10.    }
  11.    
  12.    public String toString()
  13.    {
  14.        return str.substring( 0, slice ) + "||" + str.substring( slice );
  15.    }
  16.    
  17.    public String getBefore() { return str.substring( 0, slice );  }
  18.    public String getAfter()  { return str.substring( slice ); }
  19.    
  20.    // working example that needs error case to be finished
  21.    public Ed rightArrow()
  22.    {
  23.        if (slice == str.length())
  24.          return this;
  25.        return new Editor2( str, slice + 1 );
  26.    }
  27.    public Ed leftArrow()
  28.    {
  29.        if (slice == 0)
  30.            return this;
  31.        return new Editor2( str, slice - 1 );
  32.    }
  33.    public Ed delete()
  34.    {
  35.        if (slice == str.length())
  36.          return this;
  37.        return new Editor2(str.substring(0, slice) + str.substring(slice + 1), slice);
  38.    }
  39.    public Ed backspace()
  40.    {
  41.        if (slice == 0)
  42.           return this;
  43.        return new Editor2( str.substring(0,slice-1) + str.substring(slice), slice - 1 );
  44.    }
  45.    public Ed insertString(String c)  // Originally insert(char c), which is fine if you teach the char type
  46.    {
  47.    
  48.        return new Editor2( str.substring(0,slice) + c + str.substring(slice), slice );
  49.    }
  50.    public Ed homeKey()
  51.    {
  52.        return new Editor2( str, 0   );
  53.    }
  54.    
  55.    public Ed endKey()
  56.    {
  57.        return new Editor2( str, str.length() );
  58.    }
  59.    
  60.    public static void main( String [] args )
  61.    {
  62.        Editor2 eddie = new Editor2( "bigdog", 3 );
  63.        System.out.println( eddie + " right arrow = " + eddie.rightArrow() );
  64.        System.out.println( eddie + " left arrow = " + eddie.leftArrow() );      
  65.    }
  66. }
Add Comment
Please, Sign In to add comment