Can you add the an option to \"Write to disk\" function and a function to reinit
ID: 3773843 • Letter: C
Question
Can you add the an option to "Write to disk" function and a function to reinitiliaze the function. in other words, a save and load function? import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; import java.util.LinkedList; public class Storm { //Variables private String NameOfStorm, StartOfStorm, EndOfStorm; private int YearOfStorm, StrengthOfStorm; //Storm Constructor public Storm(String NameOfStorm, int YearOfStorm, String StartOfStorm, String EndOfStorm, int StrengthOfStorm) { this.NameOfStorm = NameOfStorm; this.YearOfStorm = YearOfStorm; this.StartOfStorm = StartOfStorm; this.EndOfStorm = EndOfStorm; this.StrengthOfStorm = StrengthOfStorm; } /*************************************************/ /*Method:Get functions */ /*Purpose: Retrieve value of parameters required */ /*Parameters:NameOfStorm,YearOfStorm,StartOfStorm*/ /* EndOfStorm, StrengthOfStorm */ /*Return:NameOfStorm,YearOfStorm,StartOfStorm */ /* EndOfStorm, StrengthOfStorm */ /*************************************************/ public String getNameOfStorm() { return NameOfStorm; } public int getNameOfYear() { return YearOfStorm; } public String getStartOfStorm() { return StartOfStorm; } public String getEndOfStorm() { return EndOfStorm; } public int getStrengthOfStorm() { return StrengthOfStorm; } /*************************************************/ /*Method:Set functions */ /*Purpose: set value of parameters of a class */ /*Parameters:NameOfStorm,YearOfStorm,StartOfStorm*/ /* EndOfStorm, StrengthOfStorm */ /*Return: N/A */ /*************************************************/ public void setNameOfStorm(String NameOfStorm) { this.NameOfStorm = NameOfStorm; } //Set method for storm year public void setNameOfYear(int YearOfStorm) { this.YearOfStorm = YearOfStorm; } //Set method for starting month of storm public void setStartOfStorm(String StartOfStorm) { this.StartOfStorm = StartOfStorm; } //Set method for ending month of storm public void setEndOfStorm(String EndOfStorm) { this.EndOfStorm = EndOfStorm; } //Set method for Stength of storm public void setStrengthOfStorm(int StrengthOfStorm) { this.StrengthOfStorm = StrengthOfStorm; } /*************************************************/ /*Method:ToString */ /*Purpose: set value of parameters of a class */ /*Parameters:NameOfStorm,YearOfStorm,StartOfStorm*/ /* EndOfStorm, StrengthOfStorm */ /*Return: N/A */ /*************************************************/ // toString method returning the data as a String public String toString() { return " " + getNameOfYear() + ":" + getNameOfStorm() + "-" + ((getStrengthOfStorm() == -1) ? "(N/A)" : ((getStrengthOfStorm() == 0) ? "(tropical storm)" : "(hurricane level " + getStrengthOfStorm() + ")")) + "- " + ((getStartOfStorm().equals("")) ? "(no start)" : getStartOfStorm().substring(0, 2) + "/" + getStartOfStorm().substring(2)) + " - " + ((getEndOfStorm().equals("")) ? "(no end)" : getEndOfStorm().substring(0, 2) + "/" + getEndOfStorm().substring(2)); } } class Database { //Variables // node to store linked list private LinkedList<Storm> list; int count; // Constructor public Database(File fileName) { list = null; count = 0; //Scanner used to read file try { Scanner in = new Scanner(new File("tropical.txt")); while(in.hasNextLine()) { String line = in.nextLine(); String[] data = line.split("/"); if(data.length <2 || data.length>5) { System.out.println("Entry is in an incorrect format " + line); continue; } Storm storm = null; System.out.println(line); if( data.length < 2 ) continue; if( data.length == 2 ) { storm = new Storm( data[1], Integer.parseInt(data[0]), "", "", 0); } else if( data.length == 3 ) { storm = new Storm( data[1], Integer.parseInt(data[0]), data[2], "", 0); } else if( data.length == 4 ) { storm = new Storm( data[1], Integer.parseInt(data[0]), data[2],data[3], 0); } else { storm = new Storm( data[1], Integer.parseInt(data[0]), data[2],data[3], (Integer.parseInt(data[4]))); } count++; if( list == null ) { list = new LinkedList<Storm>(); list.add( storm ); } else { int year = Integer.parseInt(data[0]); int size = list.size(); int i; for( i=0; i<size; i++) { if( list.get(i).getNameOfYear() < year ) continue; list.add(i,storm); break; } if( i == size ) { list.add(storm); } } } in.close(); } catch (FileNotFoundException F) { System.out.println("File not found"); } catch(InputMismatchException e) { System.out.print("Input was not found"); } } /*************************************************/ /*Method:SearchByName */ /*Purpose: search through the array for */ /* the name entered */ /*Parameters:String name */ /*Return: N/A */ /*************************************************/ public void SearchByName(String name) { boolean found = false; for( int i=0; i<list.size(); i++) { if( list.get(i).getNameOfStorm().toLowerCase().indexOf(name.toLowerCase()) == 0) { found = true; System.out.print(list.get(i)); } } if(!found) System.out.println( name + " was not found in the database "); } /*************************************************/ /*Method:SearchByYear */ /*Purpose: search through the array for */ /* the year entered */ /*Parameters:int year */ /*Return: N/A */ /*************************************************/ public void SearchByYear(int year) { boolean found = false; for( int i=0; i<list.size(); i++) { if( list.get(i).getNameOfYear() == year) { found = true; System.out.print(list.get(i)); } } if(!found) System.out.println("Storm " + year + " was not found"); } /*************************************************/ /*Method:printAll */ /*Purpose: Print the array of storms */ /*Parameters:N/A */ /*Return: N/A */ /*************************************************/ public void printAll() { for( int i=0; i<list.size(); i++) { System.out.print(list.get(i)); } } private boolean find(int year,String name) { for( int i=0; i<list.size(); i++) { if( list.get(i).getNameOfYear() == year && list.get(i).getNameOfStorm().equalsIgnoreCase(name) ) { return true; } } return false; } private int getIndex(int year,String name) { for( int i=0; i<list.size(); i++) { if( list.get(i).getNameOfYear() == year && list.get(i).getNameOfStorm().equalsIgnoreCase(name) ) { return i; } } return -1; } public boolean delete() { Scanner s = new Scanner(System.in); System.out.print("Enter the name of the Storm :"); String name = s.next(); System.out.print("Enter the year of the Storm :"); int year = s.nextInt(); if( !find(year,name) ) { System.out.println("Storm with given name and year does not exists!"); return false; } for( int i=0; i<list.size(); i++) { if( list.get(i).getNameOfYear() == year && list.get(i).getNameOfStorm().equalsIgnoreCase(name) ) { list.remove(i); return true; } } return false; } public boolean insert() { Scanner s = new Scanner(System.in); System.out.print("Enter the name of the Storm :"); String name = s.next(); System.out.print("Enter the year of the Storm :"); int year = s.nextInt(); if( find(year,name) ) { System.out.println("Storm with given name and year already exists!"); return false; } System.out.print("Enter the start of the Storm :"); String start = s.next(); System.out.print("Enter the end of the Storm :"); String end = s.next(); System.out.print("Enter the strength of the Storm :"); int strength = s.nextInt(); int size = list.size(); int i; for( i=0; i<size; i++) { if( list.get(i).getNameOfYear() < year ) continue; list.add(i, new Storm(name,year,start,end,strength)); break; } if( i == size ) { list.add(new Storm(name,year,start,end,strength)); } return true; } public boolean edit() { Scanner s = new Scanner(System.in); System.out.print("Enter the name of the Storm :"); String name = s.next(); System.out.print("Enter the year of the Storm :"); int year = s.nextInt(); if( !find(year,name) ) { System.out.println("Storm with given name and year does not exist!"); return false; } System.out.println("Enter choice to edit :"); System.out.println("1. Start of storm"); System.out.println("2. End of storm"); System.out.println("3. Level of storm"); System.out.print("Enter choice : "); int choice = s.nextInt(); if( choice == 1 ) { System.out.println("Enter the start of the Storm :"); s.nextLine(); String start = s.nextLine(); for( int i=0; i<list.size(); i++) { if( list.get(i).getNameOfYear() == year && list.get(i).getNameOfStorm().equalsIgnoreCase(name) ) { list.get(i).setStartOfStorm(start); break; } } } else if( choice == 2 ) { System.out.println("Enter the end of the Storm :"); s.nextLine(); String end = s.nextLine(); for( int i=0; i<list.size(); i++) { if( list.get(i).getNameOfYear() == year && list.get(i).getNameOfStorm().equalsIgnoreCase(name) ) { list.get(i).setEndOfStorm(end); break; } } } else if( choice == 3 ) { s.nextLine(); System.out.print("Enter the strength of the Storm :"); int strength = s.nextInt(); for( int i=0; i<list.size(); i++) { if( list.get(i).getNameOfYear() == year && list.get(i).getNameOfStorm().equalsIgnoreCase(name) ) { list.get(i).setStrengthOfStorm(strength); break; } } } return true; } public boolean searchMultiple() { Scanner s = new Scanner(System.in); System.out.print("Enter number of storms to search : "); int numberOfStorms = s.nextInt(); int[] years = new int[ numberOfStorms ]; String[] names = new String[ numberOfStorms ]; boolean found = true; for( int i=0; i<numberOfStorms; i++) { System.out.print("Enter year for storm number " + (i+1) + " : " ); years[i] = s.nextInt(); s.nextLine(); System.out.println("Enter name for storm number " + (i+1) + " : " ); names[i] = s.nextLine(); } for( int i=0; i<numberOfStorms ; i++) { if( find( years[i] , names[i])) { System.out.println("Found!"); System.out.println( list.get( getIndex( years[i], names[i] ))); } else { System.out.println("No storm found with name : " + names[i] + " and year : " + years[i]); found = false; } } if( found ) { System.out.println("All storms were found!"); } else { System.out.println("Not all storms were found!"); } return found; } } class Prog2{ //Main method public static void main(String args[]) { final int SearchByName=1; final int SearchByYear=2; final int Print=3; final int Close=9; //File file = new File("/Users/abdelmagidsiddig/Desktop/Tropicalstorms.txt"); File file = new File("tropical.txt"); if (!file.exists()) // Check if file is available System.out.println("Input file does not exist."); else { Database dbase = new Database(file); Scanner in = new Scanner(System.in); int choice = 0; //Start interactive session System.out.println("Please choose what you would like to do"); while (true) { System.out.println("1-Search for storm by name"); System.out.println("2-Search for storm by year"); System.out.println("3-Print out all storms"); System.out.println("4-Insert a storm"); System.out.println("5-Delete a storm"); System.out.println("6-Edit a storm"); //System.out.println("7-Search multiple storms"); System.out.println("9-Close menu"); choice = in.nextInt(); in.nextLine(); switch (choice) { case 1: //Search storm by name System.out.println("Enter storm name "); String name = in.nextLine(); dbase.SearchByName(name); break; case 2: //Search storm by year System.out.println("Enter storm year: "); int year = in.nextInt(); dbase.SearchByYear(year); break; case 3: //Print all storms dbase.printAll(); break; case 4: dbase.insert(); break; case 5: dbase.delete(); break; case 6: dbase.edit(); break; //case 7: // dbase.searchMultiple(); // break; case 9: //Exit in.close(); System.exit(0); default: System.out.println("Option not available"); } System.out.println(); } } } }
Explanation / Answer
class Database { //Variables // node to store linked list private LinkedList list; int count; // Constructor public Database(File fileName) { list = null; count = 0; //Scanner used to read file try { Scanner in = new Scanner(new File("tropical.txt")); while(in.hasNextLine()) { String line = in.nextLine(); String[] data = line.split("/"); if(data.length 5) { System.out.println("Entry is in an incorrect format " + line); continue; } Storm storm = null; System.out.println(line); if( data.length < 2 ) continue; if( data.length == 2 ) { storm = new Storm( data[1], Integer.parseInt(data[0]), "", "", 0); } else if( data.length == 3 ) { storm = new Storm( data[1], Integer.parseInt(data[0]), data[2], "", 0); } else if( data.length == 4 ) { storm = new Storm( data[1], Integer.parseInt(data[0]), data[2],data[3], 0); } else { storm = new Storm( data[1], Integer.parseInt(data[0]), data[2],data[3], (Integer.parseInt(data[4]))); } count++; if( list == null ) { list = new LinkedList(); list.add( storm ); } else { int year = Integer.parseInt(data[0]); int size = list.size(); int i; for( i=0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.