Java Write a program that allows the user to add or display a memo. Memos should
ID: 3919101 • Letter: J
Question
Java
Write a program that allows the user to add or display a memo. Memos should be added to the end of the file. If the user wants to display a memo, a list of the existing topics should be provided, so that the user can select which one he/she wants to see. Complete the following program:
public class MemoManager
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
System.out.println("Input file:");
String filename = console.nextLine();
try
{
MemoPad memoPad = new MemoPad();
memoPad.open(. . .);
boolean done = false;
while (!done)
{
System.out.print("Enter A to add a memo, D to display an existing memo,"
+ "or Q to quit: ");
String choice = console.nextLine();
if (choice.equalsIgnoreCase("A"))
{
System.out.println("Topic (max 25 characters):");
String topic = console.nextLine();
String dateStamp = (new Date()).toString();
System.out.println("Message (max 250 characters):");
String message = console.nextLine();
memoPad.write(. . ., topic, dateStamp, message);
}
else if (choice.equalsIgnoreCase("D"))
{
if (memoPad.size() > 0)
{
// Display the list of topics
for (int i = 0; i < . . .; i++)
{
memoPad.read(. . .);
System.out.println(i + " " + . . .);
}
System.out.println("Enter the number of the memo to display:");
int n = Integer.parseInt(console.nextLine());
memoPad.read(. . .);
System.out.println(. . .); // topic
System.out.println(. . .); // date stamp
System.out.println(. . .); // message
}
else
{
System.out.println("There are no memos in the file.");
}
}
else if (choice.equalsIgnoreCase("Q"))
{
done = true;
}
}
memoPad.close();
}
catch (IOException exception)
{
System.out.println("Input/output error: " + exception);
}
}
}
Explanation / Answer
If you have any doubts, please give me comment...
public class MemoManager {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Input file:");
String filename = console.nextLine();
try {
MemoPad memoPad = new MemoPad();
memoPad.open(filename);
boolean done = false;
while (!done) {
System.out.print("Enter A to add a memo, D to display an existing memo," + "or Q to quit: ");
String choice = console.nextLine();
if (choice.equalsIgnoreCase("A")) {
System.out.println("Topic (max 25 characters):");
String topic = console.nextLine();
String dateStamp = (new Date()).toString();
System.out.println("Message (max 250 characters):");
String message = console.nextLine();
memoPad.write(filename, topic, dateStamp, message);
} else if (choice.equalsIgnoreCase("D")) {
if (memoPad.size() > 0) {
// Display the list of topics
for (int i = 0; i < memoPad.size(); i++) {
memoPad.read(i);
System.out.println(i + " " + memoPad.getTopic());
}
System.out.println("Enter the number of the memo to display:");
int n = Integer.parseInt(console.nextLine());
memoPad.read(n);
System.out.println(memoPad.getTopic()); // topic
System.out.println(memoPad.getDatestamp()); // date stamp
System.out.println(memoPad.getMessage()); // message
} else {
System.out.println("There are no memos in the file.");
}
} else if (choice.equalsIgnoreCase("Q")) {
done = true;
}
}
memoPad.close();
} catch (IOException exception) {
System.out.println("Input/output error: " + exception);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.