2015-12-09 38 views
0
/* 
* Notes here 
*/ 
package billboardsign; 

/** 
* 
* @author John Parada 
*/ 
import java.util.ArrayList; 
public class Billboard 
{ 
    private String text; 
    private ArrayList<String> messages = new ArrayList<String>(); 

    public Billboard() 

    { 
     messages.add("Happy New Year"); 
     messages.add("Happy Holidays"); 
     messages.add("Team Pizza"); 
     messages.add("Game On"); 
     messages.add("Let's Go Team"); 
    } 

public ArrayList<String> getMessages() 
{ 
    return messages; 
} 
public void setMessages(String msg) 
{ 
    messages.add(msg); 
} 
public boolean isEmpty() 
{ 
    return text == null || text.isEmpty(); 
} 
public String substring(int begin, int end) 
{ 
    if (begin >= 0 && end < text.length()) 
    { 
     return text.substring(begin, end); 
    } 
    else 
     return null; 
} 



//add the method Reverse here  
// THIS CODE IS NO GOOD!for (int count = text.length() - 1; count >= 0; count--) 
     //FOLLOW UP LINE System.out.printf("%s ", text.charAt(count)); 
public String reverse() 
{ 
    if (isEmpty()) 
     return null; 
    else 
    { 
     char[] chars = text.toCharArray(); 

     //create antoher arrayList 
     char[] reverse = new char[chars.length]; 

     for (int i = chars.length - 1, j = 0; i < 0; i--, j++) 
     { 
     reverse[j] = chars[i]; 
     } 
    return new String(reverse); 

    } 
} 

//add the method Replace string here 
public String replace(char oldChar, char newChar) 
{ 
return text.replace(oldChar, newChar); 
} 

//add the method displayInfo here 
public void displayInfo() 
{ 
    System.out.printf("\n%s\nMessage", messages); 

} 


} 







/* 
* Notes on the Billboard Project 
*/ 
package billboardsign; 

/** 
* 
* @author John Parada 
*/ 
import java.util.Scanner; 

public class BillboardSign 
{ 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) 
{ 

    Billboard newBillboard = new Billboard(); //default constructor 


    //Menu 
    int choose_again = 1; 
    int choiceNumber; 
    Scanner newInput = new Scanner(System.in); 
    while (choose_again > 0) 
    { 
     //list options to user 
     System.out.println ("\nPlease choose a number between 1 and 6 to choose an\n" 
      + "option for the Messages Board to display: " 
      + "\n1) Display Default Message 1" 
      + "\n2) Display Default Message 2" 
      + "\n3) Display Default Message 3" 
      + "\n4) Display Default Message 4" 
      + "\n5) Display Default Message 5" 
      + "\n6) Enter a New Message" 
      + "\n7) Reverse a Message" 
      + "\n8) Replace a Message - Substring" 
      + "\n9) Exit Program"); 

     //get the user to input a selection 
     System.out.print ("\nPlease Enter Your Selection: "); 
     choiceNumber = newInput.nextInt(); 

     //use switch statement to help with thieir choice input 
     switch (choiceNumber) 
      { 
       case 1: 
        //execute get() and displayInfo() method for default Message 1 
        //newBillboard = Billboard(); 
        messages.get(0); 
        displayInfo(); 
        break; 
       case 2: 
        //execute get() and displayInfo() method for default Message 2 
        newBillboard = Billboard(); 
        Billboard = displayInfo(); 
        break; 
       case 3: 
        //execute get() and displayInfo() method for default Message 3 
        newBillboard = Billboard(); 
        Billboard = displayInfo(); 
        break; 
       case 4: 
        //execute get() and displayInfo() method for default Message 4 
        newBillboard = Billboard(); 
        Billboard = displayInfo(); 
        break; 
       case 5: 

        displayInfo(); 
        break; 
       case 6: 
        //execute set() and displayInfo() methods to create a new message 
        break; 
       case 7: 
        //execute reverse message method 
        break; 
       case 8: 
        //execute Replace message - Substring method 
       case 9: 
        //Dispaly to the user that they have chose to exit 
        System.out.print ("\nYou have chosen to cancel and exit. \n"); 
        System.exit(0); 
      } 




     //prompt user for antother selection? (Contiune=1 and Exit= -1) 
     System.out.println(); 
     System.out.print ("\nIf you would like to select another animal enter 1 or if your are done enter -1: "); 
     choose_again = newInput.nextInt(); 
    } 

} 

private static void displayInfo() { 
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
} 
} 

总之,当用户选择1时,他或她应该获取ArrayList(0)的第一个元素。当他或她选择2时,他们将获得ArrayList(1)中的第二个元素。我需要调用get方法,并使用Billboard的displayInfo。从另一个类的ArrayList中获取和设置方法

非常感谢您提供任何帮助。

问候,

约翰·帕拉达

回答

0

做一些事情同样,

case 1: 
    newBillboard.getMessages().get(0); 
    ... 
case 1: 
    newBillboard.getMessages().get(1); 
.... 
And so on. 
0

您的getMessages()方法返回一个ArrayList所以要么你应该有一个ArrayList变量在你喜欢的主类这

ArrayList<String> messages = newBillboard.getMessages(); 

然后像这样调用它

case 1: 
        //execute get() and displayInfo() method for default Message 1 
        //newBillboard = Billboard(); 
        System.out.println (messages.get(0)); 
        //displayInfo(); 
        break; 

,或者你可以简单地调用它

newBillboard.getMessages.get(0);//here 0 is for first element 

而且也没有必要让你的类的对象,在任何情况下 ,什么是displayInfo需要的是不明所以,如果你告诉你为什么添加我可能会看到它。

相关问题