Question
Please help me to fix my Java code. Its not working. Promise to give like ! ! The_code_is_about_a_to-do_list_application. Keep_track_of_things_to_do, cross_things_off. Possible_features_include_different_views_showing_all_items, or_just_completed_items, or_only_outstanding_items, or_lists_sorted_in_different_ways. The
Please help me to fix my Java code. Its not working. Promise to give like ! !
The_code_is_about_a_to-do_list_application. Keep_track_of_things_to_do, cross_things_off. Possible_features_include_different_views_showing_all_items, or_just_completed_items, or_only_outstanding_items, or_lists_sorted_in_different_ways. The code MUST contain all the 7 steps below (Im struggling to add or make these steps work in my code):
1. The_class_might_be_Item (the_thing_to_do), and_it_may_have_some_fields_like_name , status (doneot_done), and_dueDate. It_would_need_setters_and_getters_for_those_fields (for_example, you_must_have_something_like_setName_and_getName_to_access_and_change_the_value_in_the_name_field). All_methods_would_be_specified_using_REQUIRES/MODIFIES/EFFECTS_appropriately. It_would_also_need_some_internal_implementation, so_in_this_case_it_could_be_something_like_determining_whether_the_item_is_past_its_due_date.
2. Instead_of_just_calling_everything_from_Main, in_the_ui_package, make_a_new_package_called "model", (move_your_Item_class_into_there), and_make_a_second_class "ToDoList". From_your_ui/Main.main construct_the_new_ToDoList,_and_get_the_program_going_from_there. Put_all_the_code_that_was_once_in_Main.main_into_a_new_method (ToDoList.run()), and_move_all_the_helper_methods_from_the_original_class_into_your_ToDoList_class.
3. You_would_also_include_a_class_"TestToDoList"_that_tests_the_ToDoList_class_methods, and_a_TestItem_class, that_has_tests_of_the_Item_methods, ensuring_they_are_behaving_properly.
1. The_ToDoList_class_now_contains_the_interactive_loop_method (called_the "run()" method) that_gets_user_input. ToDoList_has_a_field_that_is_a_list_of_Items,_this_run_method_it_would_let_the_user_add_items_to_that_list. That_means_that_when_the_user_enters "new_item" (the_same_way_we_entered "plus" in_the_logging_calculator)_a_new_Item_would_be_constructed, and_added_to_the_list. The_user_would_be_prompted_for_the_name_of_the_item, and_the_due_date, and_then_those_attributes_would_be_saved_to_the_fields_of_the_Item_using_setters.
2. The_loop_would_also_have_the_option_to_save_the_items_to_a_file. You_would_need_to_define_a_method (save()) that_opens_a_file, loops_through_the_list_of_items_writing_each_Item_in_the_lists's_name_and_dueDate_on_a_separate_line, and_then_closes_the_file. This_behaviour_could_be_launched_when_the_user_chooses_this_option_from_the_interaction_loop, or_maybe_it_happens_automatically_when_they_say "quit". It's_up_to_you_whether_the_user_chooses_the_filename, or_if_it's_just_something_hardcoded_into_the_program_without_telling_the_user_the_name.
3. You_also_need_to_define_a_method_that_reads_each_item_from_the_file (load()). This_would_loop_through_the_lines_of_the_file. For_each_line, you_would_call_new_Item(), and_then_save_the_name_and_due_date_as_the_fields_of_that_new_Item. Each_item_would_be_added_to_the_items_list. This_could_be_launched_either_upon_startup, or_when_the_option_is_selected_by_the_user.
4. You_would_need_to_think_of_two_interfaces -- a_straightforward_combination_is "Loadable" and "Saveable". Each_of_those_would_have_one_method_declared: load() and_save() respectively. The_ToDoList_class_would_then_implement_Loadable_and_Saveable. (Note -- this_is_just_one_arbitrarily_chosen_arrangement -- it's_also_possible_to_make_the_Item_be_the_loadable_and_saveable_class, and_then_load_and_save_would_be_placed_there ... that_may_be_better_for_you_depending_on_your_arrangement.
5. There_would_also_be_tests_for_load() and_save(), testing_just_the_load_method, and_the_same_method, respectively. Testing_file_input/output_involves_checking_that_the_loaded/saved_data_matches_what_was_expected. To_test_that_the_load() method_is_functioning_correctly_you_would_read_from_a_file_for_which_you_know_the_contents, and_then_test_that_the_list_of_objects_contains_objects_that_correspond_to_the_lines_from_the_file. To_test_the_save() method, you_would_populate_a_list_of_objects_with_a_set_of_simple_objects, and_then_write_those_to_a_file. You_could_then_read_all_the_lines_of_the_file_back_in_and_compare_what_has_been_read_back_in_to_a_String_that_looks_the_way_it_should.
6. In_those_tests, you_would_declare_a_ToDoList, so_that_you_could_call_the_helper_methods_on_the_ToDoList (like_the_getters_to_see_whether_the_list_had_the_correct_contents).
7. To_satisfy_this_one_with_the_ToDoList, you_would_likely_want_to_make_a_helper_method_in_the_TestLoadable_class_called "testLoad" that_takes_a_Loadable_as_a_parameter. And_you_would_have_already_instantiated_the_ToDoList_as_a_ToDoList, meaning_you_would_have_instantiated_the_implementation (the_ToDoList, that_contains_the_implementation_of_the_methods). All_testLoad() would_do_would_be_to_call "load()" and_check_what_it_could_reasonably_expect, because_that's_the_only_thing_the_Loadable_type_knows_how_to_do. This_helper_method_would_be_called_from_the_test_method_that_is_testing_loading_of_all_the_Items.
THE CODE:
ToDoList class
package model;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Scanner;
public class ToDoList implements Loadable, Saveable {
private ArrayList
private Scanner scanner;
public ToDoList() {
this.items = new ArrayList
this.scanner = new Scanner(System.in);
}
public void run() {
int choice = 0;
while (choice != 4) {
System.out.println("What would you like to do?");
System.out.println("[1] Add a to do list item");
System.out.println("[2] Cross off an item");
System.out.println("[3] Show all the items");
System.out.println("[4] Quit");
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
addItem();
break;
case 2:
crossOffItem();
break;
case 3:
showAllItems();
break;
case 4:
save();
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice.");
break;
}
}
}
public void addItem() {
System.out.print("Enter the name of the new item: ");
String name = scanner.nextLine();
System.out.print("Enter the due date (yyyy-mm-dd) of the new item: ");
LocalDate dueDate = LocalDate.parse(scanner.nextLine());
Item item = new Item(name, dueDate);
items.add(item);
}
private void crossOffItem() {
System.out.println("Which item would you like to cross off?");
for (int i = 0; i
Item item = items.get(i);
System.out.println("[" + (i + 1) + "] " + item.getName());
}
int choice = scanner.nextInt();
scanner.nextLine();
if (choice >= 1 && choice
Item item = items.get(choice - 1);
item.setDueDate(true);
items.remove(item);
System.out.println("Crossed off: " + item.getName());
} else {
System.out.println("Invalid choice.");
}
}
public void showAllItems() {
for (int i = 0; i
Item item = items.get(i);
System.out.print(i + 1 + ". " + item.getName() + " - ");
if (item.getStatus()) {
System.out.print("done");
} else if (item.isPastDue()) {
System.out.print("past due");
} else {
System.out.print("not done");
}
System.out.println(" (due " + item.getDueDate() + ")");
}
}
public void save() {
System.out.print("Enter the filename to save to: ");
String filename = scanner.nextLine();
try {
FileWriter writer = new FileWriter(filename);
for (Item item : items) {
writer.write(item.getName() + "," + item.getDueDate() + "," + item.isDone() + " ");
}
writer.close();
System.out.println("Saved to " + filename + ".");
} catch (IOException e) {
System.out.println("Error saving to file.");
}
}
public void load() {
System.out.print("Enter the filename to load from: ");
String filename = scanner.nextLine();
try {
File file = new File(filename);
Scanner fileScanner = new Scanner(file);
items.clear();
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
String[] parts = line.split(",");
String name = parts[0];
String dueDate = parts[1];
boolean isDone = Boolean.parseBoolean(parts[2]);
Item item = new Item(name, dueDate, isDone);
items.add(item);
}
System.out.println("Loaded " + items.size() + " items from " + filename + ".");
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
@Override
public void save(String fileName) {
// TODO Auto-generated method stub
}
@Override
public void load(String fileName) {
// TODO Auto-generated method stub
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started