Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that uses a bag of strings to keep track of a list of chores you

ID: 3534174 • Letter: W

Question

Write a program that uses a bag of strings to keep track of a list of chores you have to accomplish today.The user of the program can request several services: (1) add an item to the list of chores; (2) ask how many chores are in the list; (3) Print the list of chores to the screen; (4) Delete an item from the list; (5) Exit the program
if you know how to read and write strings from a file,then have the program obtain its initial list of chores from a file. When the program ends, it should write all unfinished chores back to the file.

Explanation / Answer

Check if this works for you :


import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.Scanner;



public class BagManager

{

private ArrayList<String> readStrings=new ArrayList<String>();

public void read(String path) throws IOException

{

BufferedReader br=new BufferedReader(new FileReader(new File(path)));

String str="";

while((str=br.readLine())!=null)

{

readStrings.add(str);

}

br.close();

}


public void write(String path) throws IOException

{

File file=new File(path);

if (file.exists())

{

file.delete();

file.createNewFile();

}

else

{

file.createNewFile();

}

BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));

for(String e:readStrings)

{

bw.write(e);

bw.newLine();

}

bw.close();


System.out.println("Done");

}


public void add(String e)

{

readStrings.add(e);

}


public int sizeOfBag()

{

return readStrings.size();

}

public void showBag()

{

System.out.println("List of Chores :");

for(String e:readStrings)

{

System.out.println(e);

}

}

public void deleteItem(String e)

{

readStrings.remove(e);

}

public void exitProgram()

{

System.out.println("Exit Success");

System.exit(0);

}


public static void main(String[] args)

{

BagManager b=new BagManager();

String path="C:/chores.txt";

try {

b.read(path);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

while(true)

{

Scanner keyboard = new Scanner(System.in);

System.out.println("Choose an option from the following list.");

System.out.println();

System.out.println("Press 1: Add item to list");

System.out.println("Press 2: View number of items on list");

System.out.println("Press 3: View a list of chores");

System.out.println("Press 4: Remove an item from the list");

System.out.println("Press 5: Exit program");

String s = keyboard.nextLine();


switch(Integer.parseInt(s))

{

case 1:

{

System.out.println();

System.out.println("What would you like to add?");

String chore = keyboard.nextLine();

b.getReadStrings().add(chore);

break;

}

case 2: {

System.out.println();

System.out.println("There are " + b.sizeOfBag() +

" items on your list.");

break;

}

case 3: {

System.out.println();

b.showBag();

break;

}

case 4: {

System.out.println();

System.out.println("Which item would you like to remove?");

String chore = keyboard.nextLine();

b.deleteItem(chore);

break;

}

case 5:

{

try {

b.write(path);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

b.exitProgram();

break;

}

}

}

}


public ArrayList<String> getReadStrings() {

return readStrings;

}


public void setReadStrings(ArrayList<String> readStrings) {

this.readStrings = readStrings;

}

}



Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote