Reputation: 55
New to this website and coding in java in general. I have about 5 months experience from last year's classes. and another half spent in python. We have reached our second lab this semester and I have ran into a slight problem that my new professor refuses to help / guide with.
The lab is as described.
Make a to do list that has 2 classes. One class (the main tester one) takes in all the IO and gives all the output. The other class contains the todo list itself (arraylist) and provides methods for the main class to call on. The scanner object is in the main tester class. the arraylist is in the other class.
The following commands of input are necessary.
List, New, Change, Delete, Quit.
The program basically prompts the message to add something to your to do list. add whatever the whole line is. Then modify it as you do them.
My current questions regards how to take the input from the tester program and pass it to the other class to store into the arraylist. We have done labs where they were all in the main class and I would just add with the method calls of the Arraylist class. however now we must do it this way and I am having troubles getting them to communicate. Im guessing that I just have some sort of constructor wrong or some object misreferenced. Here is a short snippet of what I have.
(Main Tester Class) import java.util.Scanner;
public class ToDoTester
{
public static void main(String[] args)
{
Scanner in = new Scanner (System.in);
ToDo info = new ToDo();
while(true)
{
System.out.println("* * * Start To Do List * * *");
System.out.println("Enter a command (List, New, Change, Done) or Quit
");
String x = in.next();
if(x.equals("List"))
{
System.out.println(info.getList());
//Print out the array list
}
else if(x.equals("New"))
{
//System.out.println(info.addList(addedListOption));
//Add new thing to do to arraylist
}
(Other Class With Methods)
public class ToDo {
ArrayList<String> data = new ArrayLst<String>();
ToDoTester scan = new ToDoTester();
private String list;
public void addList() {
//tring takeIn = ToDoTester.scan.next();
//data.add(scan.nextLine());
}
public String getList() {
System.out.println(data.size());
return list;
}
}
I realize this may be a very nooby problem and perhaps i'm making more of a deal out of it than I have to. But i truly want to understand what I am doing and this new professor as compared to my old one is slightly worse in the helping understanding department. Thus, i come here for help, not for someone doing my work! So any help would be appreciated and I can supply any other information as needed. Can even post a picture of the lab if necessary or easier.
Upvotes: 2
Views: 550
Reputation: 49606
You are doing well, but you probably forgot that we can pass in objects to methods via method arguments.
class ToDo {
public void addToList(String newToDo) {
data.add(newToDo);
}
}
class ToDoTester {
public static void main(String[] args) {
...
} else if (x.equals("New")) {
info.addToList(in.nextLine());
}
}
}
Here is a simplified version of how I would perform the task.
As I mentioned in the comments, there is room for improvement: we can replace the switch with the command pattern, we can separate the entry point from our domain classes, we can handle exceptions more accurately, we can customise the output messages more elaborately.
Anyway, first take a look at this, and then let me know if you want to see a more enhanced version.
final class ToDoTester {
private final Scanner reader = new Scanner(System.in);
public static void main(String[] args) {
final ToDo toDo = new ToDo();
final ToDoTester toDoTester = new ToDoTester();
final Scanner reader = toDoTester.getReader();
while (true) {
System.out.println("Enter a command (List, New, Change, Delete) or Quit:");
switch (reader.nextLine()) {
case "List":
toDoTester.display(toDo.list());
break;
case "New":
toDo.add(toDoTester.readToDo());
break;
case "Change":
final Pair<Integer, String> input = toDoTester.readInputToChange();
toDo.update(input.getKey(), input.getValue());
break;
case "Delete":
toDo.remove(toDoTester.readIndex());
break;
case "Quit":
return;
default:
System.out.println("Incorrect choice, let's try again.");
}
}
}
public Scanner getReader() {
return reader;
}
public void display(List<String> list) {
IntStream.range(0, list.size())
.mapToObj(i -> String.format("[%d] %s", i, list.get(i)))
.forEach(System.out::println);
}
public Pair<Integer, String> readInputToChange() {
return new Pair<>(readIndex(), readToDo());
}
public String readToDo() {
System.out.println("Enter a to-do:");
return reader.nextLine();
}
public int readIndex() {
System.out.println("Enter the index of the to-do:");
return Integer.valueOf(reader.nextLine());
}
}
final class ToDo {
private final List<String> list = new ArrayList<>();
public void add(String toDo) {
list.add(toDo);
}
public void remove(int position) {
validatePosition(position);
list.remove(position);
}
public void update(int position, String substitute) {
validatePosition(position);
list.set(position, substitute);
}
private void validatePosition(int position) {
if (position < 0 || position >= list.size()) {
// should be thrown an exception
System.out.println("Incorrect input.");
}
}
public List<String> list() {
return new ArrayList<>(list);
}
}
An example of execution:
Enter a command (List, New, Change, Delete) or Quit:
List
Enter a command (List, New, Change, Delete) or Quit:
New
Enter a to-do:
first to-do
Enter a command (List, New, Change, Delete) or Quit:
List
[0] first to-do
Enter a command (List, New, Change, Delete) or Quit:
Change
Enter the index of the to-do:
0
Enter a to-do:
my first to-do
Enter a command (List, New, Change, Delete) or Quit:
List
[0] my first to-do
Enter a command (List, New, Change, Delete) or Quit:
Delete
Enter the index of the to-do:
0
Enter a command (List, New, Change, Delete) or Quit:
List
Enter a command (List, New, Change, Delete) or Quit:
Upvotes: 1
Reputation: 23
Your todo class must have arraylist and puplic methods, that are get string (for add method) or int (for delete) or nothing (for list) as arguments, and operate on arraylist. Your ToDoTester class must contain example of todo class (ToDo info = new ToDo();) and operate on its methods in loop. Hope that helps
Upvotes: 1