Instructions
Requirements and Specifications
- You are to change the program to read the data into a linked list along with printing the data from the linked list.
- Copy your code to a word document
- Copy the screen print of the final output to the word document
- Submit the work document to blackboard
Source Code
package read_file_data;
import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;
import java.text.*;
public class read_file_data
{
public static void main(String[] args)
{
// Variable to hold the selection given by user
int selection;
// Create instance of surgery_info
surgery_info surgery = new surgery_info();
// Start app
surgery.start_system();
// Display selection menu and get option given by user
selection = surgery.menu();
// Show Menu until user enters option 3 which is "Exit"
while(selection !=3)
{
if(selection == 1) // user selected "Add Data"
surgery.add_data();
else if(selection==2) // user selected "Print all data"
surgery.report_data();
//surgery.report_data();
selection = surgery.menu(); // display menu again
}//while loop
System.exit(0);
}
}
class surgery_info
{
// LinkedList object
LinkedList list;
void start_system()
{
// Instantiate linkled list
list = new LinkedList();
// String variable to store each line read from file
String newLine;
// Variable to store attributes for each Node
int Pnumber;
String Pname;
String Dname;
String Type;
double Cost;
try
{
//define a file variable for Buffered read
BufferedReader Surgery_file = new BufferedReader(new FileReader("hospital.txt"));
//read lines in file until there are no more lines in the file to read
while ((newLine = Surgery_file.readLine()) != null)
{
//there is a "#" between each data item in each line
StringTokenizer delimiter = new StringTokenizer(newLine,"#");
// Get all attributes
Pnumber = Integer.parseInt(delimiter.nextToken());
Pname =delimiter.nextToken();
Dname =delimiter.nextToken();
Type =delimiter.nextToken();
Cost = Double.parseDouble(delimiter.nextToken());
// insert into the linked list
list.insert(new Node(Pnumber, Pname, Dname, Type, Cost));
}//while loop
Surgery_file.close();
}//end try
catch (IOException error)
{
//there was an error on the file writing
System.out.println("Error on file read " + error);
}//error on read
}//end start_system
// Function that shows the menu and gets the option from user
int menu()
{
String snum;
int selection;
String Output = "Surgery Info" + "\n" +"1. Add New Data" + "\n" +
"2. Print all Data" + "\n" +
"3. Exit System" + "\n" +
" " + "\n" +
"Enter your selection >";
snum = JOptionPane.showInputDialog(null,
Output, "",JOptionPane.QUESTION_MESSAGE);
selection=Integer.parseInt(snum);
return selection;
}//end menu
// This function adds new data to the linked list
void add_data()
{
//add new patient, doctor, and surgery information
String svalue,Output;
// Variable to hold patient's attributes
int Pnumber;
String Pname;
String Dname;
String Type;
double Cost;
// Ask for name
Output = "Enter the Patient Number (5 digits)";
svalue =JOptionPane.showInputDialog(null,Output,
"",JOptionPane.QUESTION_MESSAGE);
Pnumber = Integer.parseInt(svalue);
// Ask for last name
Output = "Enter the Patient Last Name";
Pname =JOptionPane.showInputDialog(null,Output,
"",JOptionPane.QUESTION_MESSAGE);
// Ask for Doctor last Name
Output = "Enter the Doctor Last Name (Atkins, Johnson or Brown)";
Dname =JOptionPane.showInputDialog(null,Output,
"",JOptionPane.QUESTION_MESSAGE);
// Ask for type of surgery
Output = "Enter the Type of Surgery (knee hip or heart)";
Type =JOptionPane.showInputDialog(null,Output,
"",JOptionPane.QUESTION_MESSAGE);
// Ask for cost of surgery
Output = "Enter the Cost of Surgery";
svalue =JOptionPane.showInputDialog(null,Output,
"",JOptionPane.QUESTION_MESSAGE);
Cost = Double.parseDouble(svalue);
// Insert into the linked list
list.insert(new Node(Pnumber, Pname, Dname, Type, Cost));
}//end add_data
void report_data()
{
list.print();
}//end report_data
}//end class
/**
* Class that represents a linkedlist node
*/
class Node {
// The data store by each node is a Patient's data
// Basically, we can see a node as a patient
int Pnumber;
String Pname;
String Dname;
String Type;
double Cost;
// Variable to store the next Node
Node next;
// overloaded constructor
public Node(int Pnumber, String Pname, String Dname, String Type, double Cost) {
this.Pnumber = Pnumber;
this.Pname = Pname;
this.Dname = Dname;
this.Type = Type;
this.Cost = Cost;
this.next = null;
}
}
/**
* This class represents a linked list with a head
*/
class LinkedList {
// head of the list
Node head;
// Size
int size;
// Default constructor. Creates an Empty Linked List
public LinkedList() {
head = null;
size = 0;
}
// Insert new node into the list
public void insert(Node node) {
// If the head is null, it is because the list is empty.
if(head == null){
head = node;
} else { // linked list not null, so search for last node and then insert the new node
// after that one
Node temp = head;
while(temp.next != null) {
temp = temp.next;
}
temp.next = node;
}
size++;
}
// Function used to print all the nodes in the list
public void print() {
Node current = head;
System.out.println("All Patient Information");
while(current != null) {
System.out.println(current.Pnumber + " " + current.Pname + " " + current.Dname + " " + current.Type + " " + current.Cost);
current = current.next;
}
}
}