SlideShare a Scribd company logo
java
Fix in the program below:
handle incomplete data for text file and allow to search multiple items at once
and let it sort duplicate values
and most importantly let it use get and set methods on the Nodes
New Internal Requirements :
For this assignment, you should replace your Database class with another implementation which
uses instances of the Java Collections java.util.LinkedList class to replace your hand-crafted
LinkedList classes. Note the following:
• LinkedList is a generic type; you must instantiate it with concrete class types. Thus, your
Database object will contain several LinkedList objects; you will need to organize these lists into
another instance of LinkedList. (Consider creating a Year object to help with this.)
• You are still required to maintain your lists in sorted order. The pre-built LinkedList methods
do not directly support relative insertion into a linked list; you will need to find ways to
accomplish this with the tools available to you. (There are at least a couple of ways to do this.)
New function to add:
• A new command should be implemented which allows the user to edit a storm from the main
menu. If selected, the program should prompt the user for the year and storm name to be edited.
If a matching storm is found, the corresponding record should be displayed and the user
prompted for new information to be placed in the record. Users may not change the year or storm
name, but may change any of the other information. If no matching storm is found, no action
should be taken. The user should be informed of the outcome in any case.
this is what the txt file the program is reading from looks like:
2000/Alex/0110/0120/0
2001/Bill/0210/0220/0
2002/Chris/0310/0320/0
2003/Devon/0140/0420/0
2004/Eli/0510/0520/1
2005/Fred/0610/0620/2
2006/Gilbert/0710/0820/3
2007/Herbert/0910/0920/4
2008/Kim/1010/1020/5
#######################################Java
Program#######################################################################
###
/*****************************************************************************
***************/
/*This program takes a a file and gives infomation to the user using a LinkedList */
/*****************************************************************************
***************/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
//setting variables to string or int
public class Storm {
private int stormYear;
private int stormMag;
private String stormStart;
private String stormEnd;
private String stormName;
/**
* Constructor
* Storing all variables from text file
* @param stormName
* @param stormYear
* @param stormStart
* @param stormEnd
* @param stormMag
*/
public Storm(String stormName, int stormYear, String stormStart, String stormEnd, int
stormMag) {
this.stormName = stormName;
this.stormYear = stormYear;
this.stormStart = stormStart;
this.stormEnd = stormEnd;
this.stormMag = stormMag;
}
/**************************************************************/
/*Method: Get and Set */
/*Purpose: They serve to set&get values from class variables */
/*Parameters: */
/*String target: Storm Name */
/*Return: Storm Name */
/**************************************************************/
public String getStormName() {
return stormName;
}
/**
* @param stormName the stormName to set
*/
public void setStormName(String stormName) {
this.stormName = stormName;
}
/**
* @return the stormYear
*/
public int getStormYear() {
return stormYear;
}
/**
* @param stormYear the stormYear to set
*/
public void setStormYear(int stormYear) {
this.stormYear = stormYear;
}
/**
* @return the stormStart
*/
public String getStormStart() {
return stormStart;
}
/**
* @param stormStart the stormStart to set
*/
public void setStormStart(String stormStart) {
this.stormStart = stormStart;
}
//return the stormEnd
public String getStormEnd() {
return stormEnd;
}
//param stormEnd the stormEnd to set
public void setStormEnd(String stormEnd) {
this.stormEnd = stormEnd;
}
//return the stormMag
public int getStormMag() {
return stormMag;
}
/**
* @param stormMag the stormMag to set
*/
public void setStormMag(int stormMag) {
this.stormMag = stormMag;
}
/**************************************************************/
/*Method:String toString */
/*Purpose: convert to a string */
/*Parameters: */
/*String target: Storm Name */
/*Return: Storm Name */
/**************************************************************/
public String toString() {
String s = " " + getStormYear() + "/ " + getStormName() + " " ;
if(getStormMag() == -1){
s= s + "(no info)";
}
else {
if((getStormMag() == 0)){
s = s + "(tropical storm)";
}
else{
s = s + "(hurricane level " + getStormMag() + ")";
}
if(getStormStart().equals("")){
s = s + "(no start)";
}
else{
s = s + getStormEnd().substring(0, 2) + "/" + getStormEnd().substring(2)+" - " ;
}
if(getStormEnd().equals("")){
s = s + "(no end)" ;
}
else{
s = s + getStormEnd().substring(0, 2) + "/" + getStormEnd().substring(2);
}
}
return s;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
class Database
{
// node store linked liststorm
private class Node
{
Storm storm;
Node next;
}
// node to store heads
private class HeadNode{
int year;
Node head;
HeadNode next;
}
// storing the liststorm of storms by the year of the storm
private HeadNode liststorm;
int count;
// Constructor
public Database(File fileName) {
liststorm = null;
count = 0;
//Readig the text file
try {
Scanner in = new Scanner(new File("/Users/anasmorsi/Downloads/cs102a2/stormdata.txt"));
while(in.hasNextLine()){
String line = in.nextLine();
String[] data = line.split("/");
if(data.length <5 || data.length>5)
System.out.println( line + "Format is incorrect" );
count++;
if( liststorm == null ){
liststorm = new HeadNode();
liststorm.year = Integer.parseInt(data[0]);
liststorm.head = new Node();
liststorm.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2],
data[3],(Integer.parseInt(data[4])));
liststorm.head.next = null;
}
else {
int year = Integer.parseInt(data[0]);
if( year < liststorm.year ){
HeadNode temp = new HeadNode();
temp.year = year;
temp.head = new Node();
temp.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2],
data[3],(Integer.parseInt(data[4])));
temp.head.next = null;
temp.next = liststorm;
liststorm = temp;
}
else if( year == liststorm.year ){
Node first = liststorm.head;
while( first.next != null ){
first = first.next;
}
Node temp = new Node();
temp.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2],
data[3],(Integer.parseInt(data[4])));
temp.next = null;
first.next = temp;
}
else{
HeadNode prev = liststorm;
HeadNode curr = liststorm.next;
while( curr != null ){
if( curr.year == year ){
Node first = liststorm.head;
while( first.next != null ){
first = first.next;
}
Node temp = new Node();
temp.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2],
data[3],(Integer.parseInt(data[4])));
temp.next = null;
first.next = temp;
break;
}
else if( curr.year > year ){
HeadNode temp = new HeadNode();
temp.year = year;
temp.head = new Node();
temp.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2],
data[3],(Integer.parseInt(data[4])));
temp.head.next = null;
temp.next = curr;
prev.next = temp;
break;
}
prev = curr;
curr = curr.next;
}
if( curr == null ){
curr = liststorm;
while( curr.next != null ){
curr = curr.next;
}
HeadNode temp = new HeadNode();
temp.year = year;
temp.head = new Node();
temp.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2],
data[3],(Integer.parseInt(data[4])));
temp.head.next = null;
curr.next = temp;
}
}
}
}
}
catch (FileNotFoundException F) {
System.out.println("Can not find file");
}
catch(InputMismatchException e) {
System.out.print("Inputnot found");
}
}
public void SearchStormName(String name) {
boolean found = false;
HeadNode temp = liststorm;
while( temp != null ){
Node first = temp.head;
while( first != null ){
if( first.storm.getStormName().equalsIgnoreCase(name) ){
found = true;
System.out.print(first.storm);
break;
}
first = first.next;
}
temp = temp.next;
}
if(!found)
System.out.println("The name you entered was" + name + " was not found ");
}
public void SearchStormYear(int year) {
boolean found = false;
HeadNode temp = liststorm;
while( temp != null ){
if( temp.year == year ){
Node first = temp.head;
while( first != null ){
found = true;
System.out.print(first.storm);
first = first.next;
}
break;
}
temp = temp.next;
}
if(!found)
System.out.println("The year you enter was " + year + " which was not found");
}
public void printAll() {
if( liststorm == null)
System.out.println("This data is not available.");
else {
HeadNode temp = liststorm;
while( temp != null ){
Node first = temp.head;
while( first != null ){
System.out.print(first.storm);
first = first.next;
}
temp = temp.next;
}
}
}
private boolean find(int year,String name){
HeadNode temp = liststorm;
while( temp != null ){
Node first = temp.head;
while( first != null ){
if( first.storm.getStormName().equalsIgnoreCase(name) && temp.year == year ){
return true;
}
first = first.next;
}
temp = temp.next;
}
return false;
}
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;
}
count--;
HeadNode curr = liststorm;
while( curr != null ){
if( curr.year == year ){
Node first = curr.head;
if( first.storm.getStormName().equalsIgnoreCase(name)){
curr.head = first.next;
return true;
}
Node prev = curr.head;
first = prev.next;
while( first != null ){
if( first.storm.getStormName().equalsIgnoreCase(name)){
Node t = first.next;
prev.next = t;
return true;
}
prev = first;
first = first.next;
}
}
curr = curr.next;
}
return true;
}
public boolean insert(){
Scanner s = new Scanner(System.in);
System.out.print("Enter Storm Name:");
String name = s.next();
System.out.print("Enter Storm Year:");
int year = s.nextInt();
if( find(year,name) ){
System.out.println("That exists");
return false;
}
System.out.print("Enter when storm started mmddm :");
String start = s.next();
System.out.print("Enter when storm ended mmdd :");
String end = s.next();
System.out.print("Enter magnitude of the Storm 1-6:");
int magnitude = s.nextInt();
count++;
if( liststorm == null ){
liststorm = new HeadNode();
liststorm.year = year;
liststorm.head = new Node();
liststorm.head.storm = new Storm(name,year,start,end,magnitude);
liststorm.head.next = null;
}
else{
if( year < liststorm.year ){
HeadNode temp = new HeadNode();
temp.year = year;
temp.head = new Node();
temp.head.storm = new Storm(name,year,start,end,magnitude);
temp.head.next = null;
temp.next = liststorm;
liststorm = temp;
}
else if( year == liststorm.year ){
Node first = liststorm.head;
while( first.next != null ){
first = first.next;
}
Node temp = new Node();
temp.storm = new Storm(name,year,start,end,magnitude);
temp.next = null;
first.next = temp;
}
else{
HeadNode prev = liststorm;
HeadNode curr = liststorm.next;
while( curr != null ){
if( curr.year == year ){
Node first = liststorm.head;
while( first.next != null ){
first = first.next;
}
Node temp = new Node();
temp.storm = new Storm(name,year,start,end,magnitude);
temp.next = null;
first.next = temp;
break;
}
else if( curr.year > year ){
HeadNode temp = new HeadNode();
temp.year = year;
temp.head = new Node();
temp.head.storm = new Storm(name,year,start,end,magnitude);
temp.head.next = null;
temp.next = curr;
prev.next = temp;
break;
}
prev = curr;
curr = curr.next;
}
if( curr == null ){
curr = liststorm;
while( curr.next != null ){
curr = curr.next;
}
HeadNode temp = new HeadNode();
temp.year = year;
temp.head = new Node();
temp.head.storm = new Storm(name,year,start,end,magnitude);
temp.head.next = null;
curr.next = temp;
}
}
}
return true;
}
}
import java.io.File;
import java.util.Scanner;
class Prog2{
public static void main(String args[]) {
final int SearchStormName=1;
final int SearchStormYear=2;
final int Print=3;
final int Close=6;
File file = new File("/Users/anasmorsi/Desktop/cs102a2/stormdata.txt");
if (!file.exists())
System.out.println("File for input not there.");
else {
Database dbase = new Database(file);
Scanner in = new Scanner(System.in);
int choice = 0;
//What the user will seee
System.out.println("Welcome to the CS-102 Storm Tracker Program ");
System.out.println("Please choose what you would like to do");
while (true) {
System.out.println("1. Search for a storm name");
System.out.println("2. Search for a storm year");
System.out.println("3. Print all storms");
System.out.println("4. add a storm");
System.out.println("5. Delete a storm");
System.out.println("6. Exit");
choice = in.nextInt();
in.nextLine();
if (choice==1){
System.out.println("Enter storm name ");
String name = in.nextLine();
dbase.SearchStormName(name);
}
else if(choice==2) {
System.out.println("Enter storm year");
int year = in.nextInt();
dbase.SearchStormYear(year);
}
else if(choice==3) {
dbase.printAll();
}
else if(choice==4) {
dbase.insert();
}
else if(choice==5) {
dbase.delete();
}
else if(choice==6) { //Exit
in.close();
System.exit(0);
}
else
System.out.println("Not available");
}
}
}
}
Solution
//Prog2.java
import java.io.File;
import java.util.Scanner;
public class Prog2 {
//Main method
public static void main(String args[]) {
final int SearchByName = 1;
final int SearchByYear = 2;
final int Print = 3;
final int Insert=4;
final int delete=5;
final int Close = 6;
//File file = new File("/Users/abdelmagidsiddig/Desktop/Tropicalstorms.txt");
File file = new File("Tropicalstorms.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;
System.out.println("Welcome to the CS-102 Storm Tracker Program ");
//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-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: //Adds a storm
dbase.add();
break;
case 5://Remove a storm
dbase.remove();
break;
case 6: //Exit
in.close();
System.exit(0);
default:
System.out.println("Option not available");
}
System.out.println();
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Database.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
class Database
{
//Variables
// node for linked list
private class Node
{
Storm storm;
Node next;
}
// Store the heads of linked lists
private class HeadNode
{
int year; // year
Node head; // to store linked list for this head
HeadNode next;
}
// Stores list of storms by year
private HeadNode list;
int count;
// Constructor
public Database(File fileName) {
list = null;
count = 0;
try {
// Scanner in = new Scanner(new
File("/Users/abdelmagidsiddig/Desktop/Tropicalstorms.txt"));
Scanner in = new Scanner(new File("Tropicalstorms.txt"));
while(in.hasNextLine())
{
String line = in.nextLine();
String[] data = line.split("/");
if(data.length <5 || data.length>5)
System.out.println("Entry is in an incorrect format " + line);
count++;
// Following functions handle the importing of the textfile and the cse in which the data format
could
// be an issue
if( list == null )
{
list = new HeadNode();
list.year = Integer.parseInt(data[0]);
list.head = new Node();
list.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2],
data[3],(Integer.parseInt(data[4])));
list.head.next = null;
}
else
{
int year = Integer.parseInt(data[0]);
if( year < list.year )
{
HeadNode alt = new HeadNode();
alt.year = year;
alt.head = new Node();
alt.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2],
data[3],(Integer.parseInt(data[4])));
alt.head.next = null;
alt.next = list;
list = alt;
}
else if( year == list.year )
{
Node first = list.head;
while( first.next != null )
{
first = first.next;
}
Node alt = new Node();
alt.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2],
data[3],(Integer.parseInt(data[4])));
alt.next = null;
first.next = alt;
}
else
{
HeadNode previous = list;
HeadNode current = list.next;
while( current != null )
{
if( current.year == year )
{
Node first = list.head;
while( first.next != null )
{
first = first.next;
}
Node alt = new Node();
alt.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2],
data[3],(Integer.parseInt(data[4])));
alt.next = null;
first.next = alt;
break;
}
else if( current.year > year )
{
HeadNode alt = new HeadNode();
alt.year = year;
alt.head = new Node();
alt.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2],
data[3],(Integer.parseInt(data[4])));
alt.head.next = null;
alt.next = current;
previous.next = alt;
break;
}
previous = current;
current = current.next;
}
if( current == null )
{
current = list;
while( current.next != null )
{
current = current.next;
}
HeadNode alt = new HeadNode();
alt.year = year;
alt.head = new Node();
alt.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2],
data[3],(Integer.parseInt(data[4])));
alt.head.next = null;
current.next = alt;
}
}
}
}
} 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;
HeadNode alt = list;
while( alt != null )
{
Node first = alt.head;
while( first != null )
{
if( first.storm.getNameOfStorm().equalsIgnoreCase(name) )
{
found = true;
System.out.print(first.storm);
break;
}
first = first.next;
}
alt = alt.next;
}
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;
HeadNode alt = list;
while( alt != null )
{
if( alt.year == year )
{
Node first = alt.head;
while( first != null )
{
found = true;
System.out.print(first.storm);
first = first.next;
}
break;
}
alt = alt.next;
}
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()
{
if( list == null)
System.out.println("No data available.");
else {
HeadNode alt = list;
while( alt != null )
{
Node first = alt.head;
while( first != null )
{
System.out.print(first.storm);
first = first.next;
}
alt = alt.next;
}
}
}
/*************************************************/
/*Method:Find */
/*Purpose: Checks if the storm is available */
/* to avoid duplicate entries */
/*Parameters:Head node alt */
/*Return: Boolean */
/*************************************************/
private boolean find(int year,String name)
{
HeadNode alt = list;
while( alt != null )
{
Node first = alt.head;
while( first != null )
{
if( first.storm.getNameOfStorm().equalsIgnoreCase(name) && alt.year == year )
{
return true;
}
first = first.next;
}
alt = alt.next;
}
return false;
}
/*************************************************/
/*Method:Remove */
/*Purpose: Removes the storm entered */
/*Parameters: N/A */
/*Return: Boolean */
/*************************************************/
public boolean remove()
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter name of the Storm :");
String name = scan.next();
System.out.print("Enter year of the Storm :");
int year = scan.nextInt();
if( !find(year,name) )
{
System.out.println("Storm with given name and year does not exists!");
return false;
}
count--;
HeadNode current = list;
while( current != null )
{
if( current.year == year )
{
Node first = current.head;
if( first.storm.getNameOfStorm().equalsIgnoreCase(name))
{
current.head = first.next;
return true;
}
Node previous = current.head;
first = previous.next;
while( first != null )
{
if( first.storm.getNameOfStorm().equalsIgnoreCase(name))
{
Node t = first.next;
previous.next = t;
return true;
}
previous = first;
first = first.next;
}
}
current = current.next;
System.out.println( "Storm " + name + " has been deleted");
}
return true;
}
/*************************************************/
/*Method:Add */
/*Purpose: Adds the storm entered */
/*Parameters: N/A */
/*Return: Boolean */
/*************************************************/
public boolean add ()
{
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 (mmdd):");
String start = s.next();
System.out.print("Enter the end of the Storm (mmdd) :");
String end = s.next();
System.out.print("Enter the strength of the Storm (1-6):");
int strength = s.nextInt();
count++;
//The functions below ensure the newly added storm is placed in the right place to keep the list
sorted.
if( list == null )
{
list = new HeadNode();
list.year = year;
list.head = new Node();
list.head.storm = new Storm(name,year,start,end,strength);
list.head.next = null;
}
else
{
if( year < list.year )
{
HeadNode alt = new HeadNode();
alt.year = year;
alt.head = new Node();
alt.head.storm = new Storm(name,year,start,end,strength);
alt.head.next = null;
alt.next = list;
list = alt;
}
else if( year == list.year )
{
Node first = list.head;
while( first.next != null )
{
first = first.next;
}
Node alt = new Node();
alt.storm = new Storm(name,year,start,end,strength);
alt.next = null;
first.next = alt;
}
else
{
HeadNode previous = list;
HeadNode current = list.next;
while( current != null )
{
if( current.year == year )
{
Node first = list.head;
while( first.next != null )
{
first = first.next;
}
Node alt = new Node();
alt.storm = new Storm(name,year,start,end,strength);
alt.next = null;
first.next = alt;
break;
}
else if( current.year > year )
{
HeadNode alt = new HeadNode();
alt.year = year;
alt.head = new Node();
alt.head.storm = new Storm(name,year,start,end,strength);
alt.head.next = null;
alt.next = current;
previous.next = alt;
break;
}
previous = current;
current = current.next;
}
if( current == null )
{
current = list;
while( current.next != null )
{
current = current.next;
}
HeadNode alt = new HeadNode();
alt.year = year;
alt.head = new Node();
alt.head.storm = new Storm(name,year,start,end,strength);
alt.head.next = null;
current.next = alt;
}
}
}
return true;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
//Storm.java
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));
}
}

More Related Content

PDF
URGENTJavaPlease updated the already existing Java program and m.pdf
PDF
HELP IN JAVACreate a main method and use these input files to tes.pdf
PDF
JavaI have the full code complete i just need it to be replace th.pdf
PDF
Create an implementation of a binary tree using the recursive appr.pdf
PDF
Why Our Code Smells
PDF
Gabriele Petronella - FP for front-end development: should you care? - Codemo...
PPT
Apache Utilities At Work V5
DOCX
Description 1) Create a Lab2 folder for this project2.docx
URGENTJavaPlease updated the already existing Java program and m.pdf
HELP IN JAVACreate a main method and use these input files to tes.pdf
JavaI have the full code complete i just need it to be replace th.pdf
Create an implementation of a binary tree using the recursive appr.pdf
Why Our Code Smells
Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Apache Utilities At Work V5
Description 1) Create a Lab2 folder for this project2.docx

Similar to javaFix in the program belowhandle incomplete data for text fil.pdf (20)

DOCX
PDF
Refactoring In Tdd The Missing Part
PPTX
the next web now
PDF
Coding Ajax
PDF
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
PDF
Coding Ajax
PPTX
JQuery Presentation
PPTX
Sequelize
PDF
Having Fun with Play
PDF
Keep getting a null pointer exception for some odd reasonim creati.pdf
PPTX
Typescript barcelona
PDF
APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
PDF
Csmr2012 bettenburg presentation
DOCX
Please answer the 4 questions using C- The expected output is shown be.docx
PDF
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PPTX
Codestrong 2012 breakout session hacking titanium
PDF
Secrets of JavaScript Libraries
PPTX
Building High Performance Web Applications and Sites
PDF
IT6801-Service Oriented Architecture-Unit-2-notes
PDF
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Refactoring In Tdd The Missing Part
the next web now
Coding Ajax
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
Coding Ajax
JQuery Presentation
Sequelize
Having Fun with Play
Keep getting a null pointer exception for some odd reasonim creati.pdf
Typescript barcelona
APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
Csmr2012 bettenburg presentation
Please answer the 4 questions using C- The expected output is shown be.docx
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
Codestrong 2012 breakout session hacking titanium
Secrets of JavaScript Libraries
Building High Performance Web Applications and Sites
IT6801-Service Oriented Architecture-Unit-2-notes
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Ad

More from birajdar2 (20)

PDF
public static ArrayListInteger doArrayListSearchSmallest(int nu.pdf
PDF
Project selection methods and the project portfolio play an importan.pdf
PDF
James can row 14 km downstream in a river in 2 hours. He takes 7 hou.pdf
PDF
In the class we extensively discussed a generic singly linked list i.pdf
PDF
How do hydrophobic and hydrophilic hormones differ in their speeds a.pdf
PDF
Given below is an issue that you have identified as an issue in a ret.pdf
PDF
Explain Aquinas 5 proofs of God, being sure to identify the Aris.pdf
PDF
Exercise 7. Show that if C(0) (the punctured plane) and U C are conf.pdf
PDF
Consider the following segment table What are the physical addresse.pdf
PDF
Can you explain the movement of ions and ion channel activity during.pdf
PDF
Calculate the implied stock price assuming an EBITDA multiple of 11..pdf
PDF
Below is the graph of a polynomial function f with real coefficients.pdf
PDF
Are higher than average sea surface temperatures associated with a g.pdf
PDF
A table of values of an increasing function F is shown. Use the table.pdf
PDF
A polygenic trait is determined by a single gene with many different.pdf
PDF
4. Phil is conducting a seed germination experiment. He places 3 gro.pdf
PDF
21. What is the relationship between the maximum size of aggregates a.pdf
PDF
Which of the following are organizer molecules in the avian PMZ is a.pdf
PDF
What are the five stages of team development Describe each stage an.pdf
PDF
What kinds of molecules can be used as metabolic fuel to produce ATP.pdf
public static ArrayListInteger doArrayListSearchSmallest(int nu.pdf
Project selection methods and the project portfolio play an importan.pdf
James can row 14 km downstream in a river in 2 hours. He takes 7 hou.pdf
In the class we extensively discussed a generic singly linked list i.pdf
How do hydrophobic and hydrophilic hormones differ in their speeds a.pdf
Given below is an issue that you have identified as an issue in a ret.pdf
Explain Aquinas 5 proofs of God, being sure to identify the Aris.pdf
Exercise 7. Show that if C(0) (the punctured plane) and U C are conf.pdf
Consider the following segment table What are the physical addresse.pdf
Can you explain the movement of ions and ion channel activity during.pdf
Calculate the implied stock price assuming an EBITDA multiple of 11..pdf
Below is the graph of a polynomial function f with real coefficients.pdf
Are higher than average sea surface temperatures associated with a g.pdf
A table of values of an increasing function F is shown. Use the table.pdf
A polygenic trait is determined by a single gene with many different.pdf
4. Phil is conducting a seed germination experiment. He places 3 gro.pdf
21. What is the relationship between the maximum size of aggregates a.pdf
Which of the following are organizer molecules in the avian PMZ is a.pdf
What are the five stages of team development Describe each stage an.pdf
What kinds of molecules can be used as metabolic fuel to produce ATP.pdf
Ad

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
master seminar digital applications in india
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Structure & Organelles in detailed.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
VCE English Exam - Section C Student Revision Booklet
STATICS OF THE RIGID BODIES Hibbelers.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
GDM (1) (1).pptx small presentation for students
Abdominal Access Techniques with Prof. Dr. R K Mishra
2.FourierTransform-ShortQuestionswithAnswers.pdf
master seminar digital applications in india
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Pharma ospi slides which help in ospi learning
Cell Structure & Organelles in detailed.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Final Presentation General Medicine 03-08-2024.pptx
RMMM.pdf make it easy to upload and study
O5-L3 Freight Transport Ops (International) V1.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Microbial diseases, their pathogenesis and prophylaxis
102 student loan defaulters named and shamed – Is someone you know on the list?

javaFix in the program belowhandle incomplete data for text fil.pdf

  • 1. java Fix in the program below: handle incomplete data for text file and allow to search multiple items at once and let it sort duplicate values and most importantly let it use get and set methods on the Nodes New Internal Requirements : For this assignment, you should replace your Database class with another implementation which uses instances of the Java Collections java.util.LinkedList class to replace your hand-crafted LinkedList classes. Note the following: • LinkedList is a generic type; you must instantiate it with concrete class types. Thus, your Database object will contain several LinkedList objects; you will need to organize these lists into another instance of LinkedList. (Consider creating a Year object to help with this.) • You are still required to maintain your lists in sorted order. The pre-built LinkedList methods do not directly support relative insertion into a linked list; you will need to find ways to accomplish this with the tools available to you. (There are at least a couple of ways to do this.) New function to add: • A new command should be implemented which allows the user to edit a storm from the main menu. If selected, the program should prompt the user for the year and storm name to be edited. If a matching storm is found, the corresponding record should be displayed and the user prompted for new information to be placed in the record. Users may not change the year or storm name, but may change any of the other information. If no matching storm is found, no action should be taken. The user should be informed of the outcome in any case. this is what the txt file the program is reading from looks like: 2000/Alex/0110/0120/0 2001/Bill/0210/0220/0 2002/Chris/0310/0320/0 2003/Devon/0140/0420/0 2004/Eli/0510/0520/1 2005/Fred/0610/0620/2 2006/Gilbert/0710/0820/3 2007/Herbert/0910/0920/4 2008/Kim/1010/1020/5 #######################################Java Program####################################################################### ###
  • 2. /***************************************************************************** ***************/ /*This program takes a a file and gives infomation to the user using a LinkedList */ /***************************************************************************** ***************/ import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; //setting variables to string or int public class Storm { private int stormYear; private int stormMag; private String stormStart; private String stormEnd; private String stormName; /** * Constructor * Storing all variables from text file * @param stormName * @param stormYear * @param stormStart * @param stormEnd * @param stormMag */ public Storm(String stormName, int stormYear, String stormStart, String stormEnd, int stormMag) { this.stormName = stormName; this.stormYear = stormYear; this.stormStart = stormStart; this.stormEnd = stormEnd; this.stormMag = stormMag; }
  • 3. /**************************************************************/ /*Method: Get and Set */ /*Purpose: They serve to set&get values from class variables */ /*Parameters: */ /*String target: Storm Name */ /*Return: Storm Name */ /**************************************************************/ public String getStormName() { return stormName; } /** * @param stormName the stormName to set */ public void setStormName(String stormName) { this.stormName = stormName; } /** * @return the stormYear */ public int getStormYear() { return stormYear; } /** * @param stormYear the stormYear to set */ public void setStormYear(int stormYear) { this.stormYear = stormYear; } /** * @return the stormStart */
  • 4. public String getStormStart() { return stormStart; } /** * @param stormStart the stormStart to set */ public void setStormStart(String stormStart) { this.stormStart = stormStart; } //return the stormEnd public String getStormEnd() { return stormEnd; } //param stormEnd the stormEnd to set public void setStormEnd(String stormEnd) { this.stormEnd = stormEnd; } //return the stormMag public int getStormMag() { return stormMag; } /** * @param stormMag the stormMag to set */ public void setStormMag(int stormMag) { this.stormMag = stormMag; }
  • 5. /**************************************************************/ /*Method:String toString */ /*Purpose: convert to a string */ /*Parameters: */ /*String target: Storm Name */ /*Return: Storm Name */ /**************************************************************/ public String toString() { String s = " " + getStormYear() + "/ " + getStormName() + " " ; if(getStormMag() == -1){ s= s + "(no info)"; } else { if((getStormMag() == 0)){ s = s + "(tropical storm)"; } else{ s = s + "(hurricane level " + getStormMag() + ")"; } if(getStormStart().equals("")){ s = s + "(no start)"; } else{ s = s + getStormEnd().substring(0, 2) + "/" + getStormEnd().substring(2)+" - " ; } if(getStormEnd().equals("")){ s = s + "(no end)" ; } else{ s = s + getStormEnd().substring(0, 2) + "/" + getStormEnd().substring(2); } } return s; } } import java.io.File;
  • 6. import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; class Database { // node store linked liststorm private class Node { Storm storm; Node next; } // node to store heads private class HeadNode{ int year; Node head; HeadNode next; } // storing the liststorm of storms by the year of the storm private HeadNode liststorm; int count; // Constructor public Database(File fileName) { liststorm = null; count = 0; //Readig the text file try { Scanner in = new Scanner(new File("/Users/anasmorsi/Downloads/cs102a2/stormdata.txt")); while(in.hasNextLine()){ String line = in.nextLine();
  • 7. String[] data = line.split("/"); if(data.length <5 || data.length>5) System.out.println( line + "Format is incorrect" ); count++; if( liststorm == null ){ liststorm = new HeadNode(); liststorm.year = Integer.parseInt(data[0]); liststorm.head = new Node(); liststorm.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2], data[3],(Integer.parseInt(data[4]))); liststorm.head.next = null; } else { int year = Integer.parseInt(data[0]); if( year < liststorm.year ){ HeadNode temp = new HeadNode(); temp.year = year; temp.head = new Node(); temp.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2], data[3],(Integer.parseInt(data[4]))); temp.head.next = null; temp.next = liststorm; liststorm = temp; } else if( year == liststorm.year ){ Node first = liststorm.head; while( first.next != null ){ first = first.next; } Node temp = new Node(); temp.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2],
  • 8. data[3],(Integer.parseInt(data[4]))); temp.next = null; first.next = temp; } else{ HeadNode prev = liststorm; HeadNode curr = liststorm.next; while( curr != null ){ if( curr.year == year ){ Node first = liststorm.head; while( first.next != null ){ first = first.next; } Node temp = new Node(); temp.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2], data[3],(Integer.parseInt(data[4]))); temp.next = null; first.next = temp; break; } else if( curr.year > year ){ HeadNode temp = new HeadNode(); temp.year = year; temp.head = new Node(); temp.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2], data[3],(Integer.parseInt(data[4]))); temp.head.next = null; temp.next = curr; prev.next = temp; break; }
  • 9. prev = curr; curr = curr.next; } if( curr == null ){ curr = liststorm; while( curr.next != null ){ curr = curr.next; } HeadNode temp = new HeadNode(); temp.year = year; temp.head = new Node(); temp.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2], data[3],(Integer.parseInt(data[4]))); temp.head.next = null; curr.next = temp; } } } } } catch (FileNotFoundException F) { System.out.println("Can not find file"); } catch(InputMismatchException e) { System.out.print("Inputnot found"); } } public void SearchStormName(String name) { boolean found = false;
  • 10. HeadNode temp = liststorm; while( temp != null ){ Node first = temp.head; while( first != null ){ if( first.storm.getStormName().equalsIgnoreCase(name) ){ found = true; System.out.print(first.storm); break; } first = first.next; } temp = temp.next; } if(!found) System.out.println("The name you entered was" + name + " was not found "); } public void SearchStormYear(int year) { boolean found = false; HeadNode temp = liststorm; while( temp != null ){ if( temp.year == year ){ Node first = temp.head; while( first != null ){ found = true; System.out.print(first.storm); first = first.next; } break; } temp = temp.next; } if(!found) System.out.println("The year you enter was " + year + " which was not found");
  • 11. } public void printAll() { if( liststorm == null) System.out.println("This data is not available."); else { HeadNode temp = liststorm; while( temp != null ){ Node first = temp.head; while( first != null ){ System.out.print(first.storm); first = first.next; } temp = temp.next; } } } private boolean find(int year,String name){ HeadNode temp = liststorm; while( temp != null ){ Node first = temp.head; while( first != null ){ if( first.storm.getStormName().equalsIgnoreCase(name) && temp.year == year ){ return true; } first = first.next; } temp = temp.next; } return false; } public boolean delete(){
  • 12. 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; } count--; HeadNode curr = liststorm; while( curr != null ){ if( curr.year == year ){ Node first = curr.head; if( first.storm.getStormName().equalsIgnoreCase(name)){ curr.head = first.next; return true; } Node prev = curr.head; first = prev.next; while( first != null ){ if( first.storm.getStormName().equalsIgnoreCase(name)){ Node t = first.next; prev.next = t; return true; } prev = first; first = first.next; }
  • 13. } curr = curr.next; } return true; } public boolean insert(){ Scanner s = new Scanner(System.in); System.out.print("Enter Storm Name:"); String name = s.next(); System.out.print("Enter Storm Year:"); int year = s.nextInt(); if( find(year,name) ){ System.out.println("That exists"); return false; } System.out.print("Enter when storm started mmddm :"); String start = s.next(); System.out.print("Enter when storm ended mmdd :"); String end = s.next(); System.out.print("Enter magnitude of the Storm 1-6:"); int magnitude = s.nextInt(); count++; if( liststorm == null ){ liststorm = new HeadNode(); liststorm.year = year; liststorm.head = new Node(); liststorm.head.storm = new Storm(name,year,start,end,magnitude); liststorm.head.next = null;
  • 14. } else{ if( year < liststorm.year ){ HeadNode temp = new HeadNode(); temp.year = year; temp.head = new Node(); temp.head.storm = new Storm(name,year,start,end,magnitude); temp.head.next = null; temp.next = liststorm; liststorm = temp; } else if( year == liststorm.year ){ Node first = liststorm.head; while( first.next != null ){ first = first.next; } Node temp = new Node(); temp.storm = new Storm(name,year,start,end,magnitude); temp.next = null; first.next = temp; } else{ HeadNode prev = liststorm; HeadNode curr = liststorm.next; while( curr != null ){ if( curr.year == year ){ Node first = liststorm.head; while( first.next != null ){ first = first.next; } Node temp = new Node(); temp.storm = new Storm(name,year,start,end,magnitude); temp.next = null; first.next = temp;
  • 15. break; } else if( curr.year > year ){ HeadNode temp = new HeadNode(); temp.year = year; temp.head = new Node(); temp.head.storm = new Storm(name,year,start,end,magnitude); temp.head.next = null; temp.next = curr; prev.next = temp; break; } prev = curr; curr = curr.next; } if( curr == null ){ curr = liststorm; while( curr.next != null ){ curr = curr.next; } HeadNode temp = new HeadNode(); temp.year = year; temp.head = new Node(); temp.head.storm = new Storm(name,year,start,end,magnitude); temp.head.next = null; curr.next = temp; } } } return true; } } import java.io.File; import java.util.Scanner;
  • 16. class Prog2{ public static void main(String args[]) { final int SearchStormName=1; final int SearchStormYear=2; final int Print=3; final int Close=6; File file = new File("/Users/anasmorsi/Desktop/cs102a2/stormdata.txt"); if (!file.exists()) System.out.println("File for input not there."); else { Database dbase = new Database(file); Scanner in = new Scanner(System.in); int choice = 0; //What the user will seee System.out.println("Welcome to the CS-102 Storm Tracker Program "); System.out.println("Please choose what you would like to do"); while (true) { System.out.println("1. Search for a storm name"); System.out.println("2. Search for a storm year"); System.out.println("3. Print all storms"); System.out.println("4. add a storm"); System.out.println("5. Delete a storm"); System.out.println("6. Exit"); choice = in.nextInt(); in.nextLine(); if (choice==1){ System.out.println("Enter storm name "); String name = in.nextLine(); dbase.SearchStormName(name); }
  • 17. else if(choice==2) { System.out.println("Enter storm year"); int year = in.nextInt(); dbase.SearchStormYear(year); } else if(choice==3) { dbase.printAll(); } else if(choice==4) { dbase.insert(); } else if(choice==5) { dbase.delete(); } else if(choice==6) { //Exit in.close(); System.exit(0); } else System.out.println("Not available"); } } } } Solution //Prog2.java import java.io.File; import java.util.Scanner; public class Prog2 {
  • 18. //Main method public static void main(String args[]) { final int SearchByName = 1; final int SearchByYear = 2; final int Print = 3; final int Insert=4; final int delete=5; final int Close = 6; //File file = new File("/Users/abdelmagidsiddig/Desktop/Tropicalstorms.txt"); File file = new File("Tropicalstorms.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; System.out.println("Welcome to the CS-102 Storm Tracker Program "); //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-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
  • 19. System.out.println("Enter storm year: "); int year = in.nextInt(); dbase.SearchByYear(year); break; case 3: //Print all storms dbase.printAll(); break; case 4: //Adds a storm dbase.add(); break; case 5://Remove a storm dbase.remove(); break; case 6: //Exit in.close(); System.exit(0); default: System.out.println("Option not available"); } System.out.println(); } } } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// //Database.java import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; class Database { //Variables // node for linked list private class Node
  • 20. { Storm storm; Node next; } // Store the heads of linked lists private class HeadNode { int year; // year Node head; // to store linked list for this head HeadNode next; } // Stores list of storms by year private HeadNode list; int count; // Constructor public Database(File fileName) { list = null; count = 0; try { // Scanner in = new Scanner(new File("/Users/abdelmagidsiddig/Desktop/Tropicalstorms.txt")); Scanner in = new Scanner(new File("Tropicalstorms.txt")); while(in.hasNextLine()) { String line = in.nextLine(); String[] data = line.split("/"); if(data.length <5 || data.length>5) System.out.println("Entry is in an incorrect format " + line); count++; // Following functions handle the importing of the textfile and the cse in which the data format could // be an issue if( list == null ) { list = new HeadNode(); list.year = Integer.parseInt(data[0]);
  • 21. list.head = new Node(); list.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2], data[3],(Integer.parseInt(data[4]))); list.head.next = null; } else { int year = Integer.parseInt(data[0]); if( year < list.year ) { HeadNode alt = new HeadNode(); alt.year = year; alt.head = new Node(); alt.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2], data[3],(Integer.parseInt(data[4]))); alt.head.next = null; alt.next = list; list = alt; } else if( year == list.year ) { Node first = list.head; while( first.next != null ) { first = first.next; } Node alt = new Node(); alt.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2], data[3],(Integer.parseInt(data[4]))); alt.next = null; first.next = alt; } else { HeadNode previous = list; HeadNode current = list.next;
  • 22. while( current != null ) { if( current.year == year ) { Node first = list.head; while( first.next != null ) { first = first.next; } Node alt = new Node(); alt.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2], data[3],(Integer.parseInt(data[4]))); alt.next = null; first.next = alt; break; } else if( current.year > year ) { HeadNode alt = new HeadNode(); alt.year = year; alt.head = new Node(); alt.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2], data[3],(Integer.parseInt(data[4]))); alt.head.next = null; alt.next = current; previous.next = alt; break; } previous = current; current = current.next; } if( current == null ) { current = list; while( current.next != null )
  • 23. { current = current.next; } HeadNode alt = new HeadNode(); alt.year = year; alt.head = new Node(); alt.head.storm = new Storm(data[1], Integer.parseInt(data[0]), data[2], data[3],(Integer.parseInt(data[4]))); alt.head.next = null; current.next = alt; } } } } } 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; HeadNode alt = list;
  • 24. while( alt != null ) { Node first = alt.head; while( first != null ) { if( first.storm.getNameOfStorm().equalsIgnoreCase(name) ) { found = true; System.out.print(first.storm); break; } first = first.next; } alt = alt.next; } 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; HeadNode alt = list; while( alt != null ) { if( alt.year == year ) { Node first = alt.head; while( first != null ) {
  • 25. found = true; System.out.print(first.storm); first = first.next; } break; } alt = alt.next; } 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() { if( list == null) System.out.println("No data available."); else { HeadNode alt = list; while( alt != null ) { Node first = alt.head; while( first != null ) { System.out.print(first.storm); first = first.next; } alt = alt.next; } } } /*************************************************/
  • 26. /*Method:Find */ /*Purpose: Checks if the storm is available */ /* to avoid duplicate entries */ /*Parameters:Head node alt */ /*Return: Boolean */ /*************************************************/ private boolean find(int year,String name) { HeadNode alt = list; while( alt != null ) { Node first = alt.head; while( first != null ) { if( first.storm.getNameOfStorm().equalsIgnoreCase(name) && alt.year == year ) { return true; } first = first.next; } alt = alt.next; } return false; } /*************************************************/ /*Method:Remove */ /*Purpose: Removes the storm entered */ /*Parameters: N/A */ /*Return: Boolean */ /*************************************************/ public boolean remove() { Scanner scan = new Scanner(System.in); System.out.print("Enter name of the Storm :"); String name = scan.next(); System.out.print("Enter year of the Storm :");
  • 27. int year = scan.nextInt(); if( !find(year,name) ) { System.out.println("Storm with given name and year does not exists!"); return false; } count--; HeadNode current = list; while( current != null ) { if( current.year == year ) { Node first = current.head; if( first.storm.getNameOfStorm().equalsIgnoreCase(name)) { current.head = first.next; return true; } Node previous = current.head; first = previous.next; while( first != null ) { if( first.storm.getNameOfStorm().equalsIgnoreCase(name)) { Node t = first.next; previous.next = t; return true; } previous = first; first = first.next; } } current = current.next; System.out.println( "Storm " + name + " has been deleted"); } return true;
  • 28. } /*************************************************/ /*Method:Add */ /*Purpose: Adds the storm entered */ /*Parameters: N/A */ /*Return: Boolean */ /*************************************************/ public boolean add () { 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 (mmdd):"); String start = s.next(); System.out.print("Enter the end of the Storm (mmdd) :"); String end = s.next(); System.out.print("Enter the strength of the Storm (1-6):"); int strength = s.nextInt(); count++; //The functions below ensure the newly added storm is placed in the right place to keep the list sorted. if( list == null ) { list = new HeadNode(); list.year = year; list.head = new Node(); list.head.storm = new Storm(name,year,start,end,strength); list.head.next = null; }
  • 29. else { if( year < list.year ) { HeadNode alt = new HeadNode(); alt.year = year; alt.head = new Node(); alt.head.storm = new Storm(name,year,start,end,strength); alt.head.next = null; alt.next = list; list = alt; } else if( year == list.year ) { Node first = list.head; while( first.next != null ) { first = first.next; } Node alt = new Node(); alt.storm = new Storm(name,year,start,end,strength); alt.next = null; first.next = alt; } else { HeadNode previous = list; HeadNode current = list.next; while( current != null ) { if( current.year == year ) { Node first = list.head; while( first.next != null ) {
  • 30. first = first.next; } Node alt = new Node(); alt.storm = new Storm(name,year,start,end,strength); alt.next = null; first.next = alt; break; } else if( current.year > year ) { HeadNode alt = new HeadNode(); alt.year = year; alt.head = new Node(); alt.head.storm = new Storm(name,year,start,end,strength); alt.head.next = null; alt.next = current; previous.next = alt; break; } previous = current; current = current.next; } if( current == null ) { current = list; while( current.next != null ) { current = current.next; } HeadNode alt = new HeadNode(); alt.year = year; alt.head = new Node(); alt.head.storm = new Storm(name,year,start,end,strength); alt.head.next = null; current.next = alt; }
  • 31. } } return true; } } ///////////////////////////////////////////////////////////////////////////////////////////// //Storm.java 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() {
  • 32. 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 */
  • 33. /*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)); } }