In this exercise we are going to create a program that can be used to create and modify a to-do list. The final product will work in the following manner.
My Code
Main
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Here you can try out the combined functionality of your classes
TodoList todo = new TodoList();
Scanner scanner = new Scanner(System.in);
UserInterface ui = new UserInterface(todo, scanner);
ui.start();
}
}
TodoList
import java.util.ArrayList;
public class TodoList {
private ArrayList<String> todo;
public TodoList() {
this.todo = new ArrayList<String>();
}
// Add task to todo list
public void add(String task){
this.todo.add(task);
}
// Print tasks
public void print(){
int i = 1;
for (String task: this.todo){
System.out.println(i + ": " + task);
i++;
}
}
// Remove task
public void remove(int number){
this.todo.remove(number -1);
}
}
UserInterface
import java.util.Scanner;
import java.util.ArrayList;
public class UserInterface {
private Scanner scanner;
private TodoList list;
public UserInterface(TodoList list, Scanner scanner) {
this.scanner = scanner;
this.list = list;
}
public void start() {
while (true) {
System.out.print("Command: ");
String input = this.scanner.nextLine();
if (input.contentEquals("stop")) {
break;
}
if (input.contentEquals("add")) {
add();
}
if (input.contentEquals("list")) {
list();
}
if (input.contentEquals("remove")) {
remove();
}
}
}
public void add() {
System.out.print("To add: ");
String input = this.scanner.nextLine();
this.list.add(input);
}
public void list() {
this.list.print();
}
public void remove(){
System.out.print("Which one is removed? ");
int input = Integer.parseInt(scanner.nextLine());
this.list.remove(input);
}
}
Model Code
Main
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Here you can try out the combined functionality of your classes
TodoList list = new TodoList();
Scanner scanner = new Scanner(System.in);
UserInterface ui = new UserInterface(list, scanner);
ui.start();
}
}
TodoList
import java.util.ArrayList;
public class TodoList {
private ArrayList<String> tasks;
public TodoList() {
this.tasks = new ArrayList<>();
}
public void add(String task) {
this.tasks.add(task);
}
public void print() {
for (int i = 0; i < this.tasks.size(); i++) {
System.out.println((i + 1) + ": " + this.tasks.get(i));
}
}
public void remove(int number) {
int index = number - 1;
if (index < 0 || index >= this.tasks.size()) {
return;
}
this.tasks.remove(index);
}
}
UserInterface
import java.util.Scanner;
public class UserInterface {
private TodoList todoList;
private Scanner scanner;
public UserInterface(TodoList todoList, Scanner scanner) {
this.todoList = todoList;
this.scanner = scanner;
}
public void start() {
while (true) {
System.out.print("Command: ");
String command = scanner.nextLine();
if (command.equals("stop")) {
break;
}
if (command.equals("add")) {
String toAdd = scanner.nextLine();
this.todoList.add(toAdd);
} else if (command.equals("list")) {
this.todoList.print();
} else if (command.equals("remove")) {
int toRemove = Integer.valueOf(scanner.nextLine());
this.todoList.remove(toRemove);
}
}
}
}
Comments
OK, on my code side of things I have forgotten to remove some unneeded imports, I haven’t sanitised my input and weirdly the whole thing took me ages. My variable names could also be a bit better. On their code side of things, there is a whole class in the Model Code that doesn’t exist in the instructions. Very strange.