*The following are not various questions, they are merely instructions to a sing
ID: 3630727 • Letter: #
Question
*The following are not various questions, they are merely instructions to a single question. Please do not get confused with the numberings.Instructions:
Congratulations, you have just become Java TV's newest owner! Java TV is the greatest TV channel ever, and it prides itself in its 24 hour interesting ' programming' . To help you run this channel you will need to write a dynamic program that uses Data Lists to store all its TV show information.
Your program will contain the following Menu Options:
1. Enter TV Shows/ Add a TV Show:
This option should allow the user to enter as many shows as he wants, your program should always keep track of how many shows the user has entered.
2. Modify TV Show:
This option should ask the user what show he would like to modify and search for it using the name of the show as the search key. The user should be able to modify that show information
3. Delete TV Show:
Like the Modify option, the delete option searches for a show using the show's name. Once found the show should be removed by shifting the array one space.
(Example: to delete array position 4, 5->4, 6->5,7->6...and so on)
4. Sort TV Shows:
The sort function asks the user what sort key he would like to use (name, day, time) and sorts the list using that key.
Note: The sort by day will sort alphabetically, that is fine,
see the Bonus for a proper day sort
5. Show all TV Shows:
This option outputs all the shows and also gives totals for the number of shows per day.
6. Exit: This will quit the program
Also: add the following Menu Options:
1. Save TV Shows:
This should save your arrays to a file named "TV.txt", Don't forget to add a header that says how many tv shows are in the file.
2. Load TV Shows:
This should load the TV Shows from "TV.txt", you will need to read the header first to see how many Shows are in the file.
Explanation / Answer
public class TVShow {
public static String[] programs;
public static int index = 0;
public static java.util.Scanner input;
public static void main(String[] args) {
programs = new String[25];
input = new java.util.Scanner(System.in);
int choise;
showMenu();
do {
System.out.print("Enter your choice: ");
choise = input.nextInt();
input.nextLine();
switch (choise) {
case 1:
System.out.print("Enter program name: ");
String prog = input.nextLine();
add(prog);
break;
case 2:
modify(); break;
case 3:
delete(); break;
case 4:
sort(); break;
case 5:
show(); break;
case 7:
System.out.println("Enter input filename: ");
readFromFile(input.nextLine());
break;
case 6:
System.out.println("Enter output filename: ");
writeToFile(input.nextLine());
break;
case 8:
System.exit(0);
}
} while (true);
}
public static void showMenu() {
System.out.println("1.Enter/Add a TV Show");
System.out.println("2.Modify a TV Show");
System.out.println("3.Delete a TV Show");
System.out.println("4.Sort TV Shows");
System.out.println("5.Show all TV Shows");
System.out.println("6.Save TV Shows");
System.out.println("7.Load TV Shows");
System.out.println("8.Exit");
}
public static void show() {
for (int i = 0; i < index; i++) {
System.out.println(i + 1 + ". " + programs[i]);
}
}
public static void add(String program) {
if (index >= programs.length - 1) {
String[] tmp = new String[programs.length];
tmp = programs;
programs = new String[tmp.length + 20];
programs = tmp;
programs[tmp.length] = program;
index = tmp.length + 1;
} else {
programs[index++] = program;
}
}
public static void modify() {
if (programs.length > 0) {
int progNum;
show();
do {
System.out.println("Enter program number to modify: ");
progNum = input.nextInt();
input.nextLine();
} while (progNum < 1 || progNum > index);
System.out.println("Enter new program name: ");
programs[progNum - 1] = input.nextLine();
} else {
System.out.println("No progrms in the list");
}
}
public static void delete() {
if (programs.length > 0) {
int progNum;
show();
do {
System.out.println("Enter program number to delete: ");
progNum = input.nextInt();
input.nextLine();
} while (progNum < 1 || progNum > index);
for (int i = progNum - 1; i < index - 1; i++) {
programs[i] = programs[i + 1];
}
index--;
} else {
System.out.println("No progrms in the list");
}
}
public static void sort() {
for (int i = 0; i < index - 1; i++) {
for (int j = i + 1; j < index; j++) {
if ((programs[i].compareTo(programs[j])) > 0) {
String tmp = programs[i];
programs[i] = programs[j];
programs[j] = tmp;
}
}
}
}
public static void readFromFile(String fileName) {
try {
java.util.Scanner inFile = new java.util.Scanner(
new java.io.FileInputStream(fileName));
int numOfProgs = inFile.nextInt();
inFile.nextLine();
while (inFile.hasNext()) {
add(inFile.nextLine());
}
inFile.close();
} catch (Exception exp) {
exp.printStackTrace();
}
}
public static void writeToFile(String fileName) {
try {
java.io.BufferedWriter outFile = new java.io.BufferedWriter(
new java.io.FileWriter(fileName));
outFile.write("" + index + " ");
for (int i = 0; i < index; i++) {
outFile.write(programs[i] + " ");
}
outFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.