Hi, i am facing a problem with storing data into linked list. I have previously store the data read from the text file into an array but now my assignment require me to store all this record into a linked list. Can anyone help? May i know how to do it? I have created the list node and linkedlist class. Below is the attachment of the text file and my code. Thank in advance.
textfile.txt
Account Id = 67
Name = John
Address = 465 Ripley Boulevard, Oscar Mansion, Singapore 7666322
DOB = 10-10-1970
Phone Number = 790-3233
Account Balance = 405600.00
Account Type = Fixed
Fixed Daily Interest = 0.05
Account Id = 80
Name = Tim
Address = 200 Hunting Street, Singapore 784563
DOB = 25-10-1968
Phone Number = 432-4579
Account Balance = 530045.00
Account Type = Saving
Account Id = 65
Name = Tracy
Address = 45 Mexican Boulevard, Hotel California, Singapore 467822
DOB = 06-04-73
Phone Number = 678-1234
Account Balance = 25.00
Account Type = Checking
Account Id = 124
Name = Helen
Address = 50 Apple Avenue, Singapore 639798
DOB = 11-08-64
Phone Number = 345-6780
Account Balance = 234.00
Account Type = Checking
Account Id = 678
Name = Ming Fong
Address = 60 Hill View Ave, Singapore 456872
DOB = 15-02-70
Phone Number = 555-1234
Account Balance = 7000.00
Account Type = Saving
RecNode.java
public class RecNode {
Record rec;
RecNode next;
//Constructor
public RecNode(Record r){
rec = r;
next = null;
}
}
RecLinkedList.java
public class RecLinkedList {
//reference to first item in list
private RecNode pHead;
//Constructor
public RecLinkedList(){
pHead = null;
}
public void insertFirst(Record r){
RecNode newNode = new RecNode(r);
newNode.next = pHead;
pHead = newNode;
}
public void displayList(){
System.out.println("\nLinked List: ");
//start of the list
RecNode current = pHead;
//loop through until end of list
while (current != null){
System.out.println(current.rec+ " ");
//move to the next link
current = current.next;
}
System.out.println();
}
//This method returns true if list is empty,false otherwise
public boolean isEmpty(){
return(pHead == null);
}
//This method removes the first node from the linked list and returns
//the reference to the deleted node.
public RecNode deleteFirst(){
RecNode p = null;
//check if list is not empty
if(!isEmpty()){
p = pHead;
pHead = pHead.next;
p.next = null;
}
return p;
}
//This method searches the linked list for a node with a specified key
//value and returns a reference to that node
public RecNode find(Record rKey){
RecNode p = pHead;
//if list is empty
if(p == null){
return null;
}
while(p.rec!= rKey){
//if end of list
if(p.next == null)
//not found
return null;
else
p = p.next;
}
//When found
return p;
}
//This method will first search the linked list for the node with a
//specified c value, using the find method, and insert a new node with the
//given value.
public RecNode insertAfter(Record rKey, Record r){
RecNode p = find(rKey);
//if list is empty or rKey not found,insert fail
if(p == null)
return null;
RecNode newNode = new RecNode (r);
newNode.next= p.next;
p.next = newNode;
return newNode;
}
//This method deletes the node from the linked
//link with the specified key value.
public RecNode delete(Record rKey){
RecNode curr = pHead;
RecNode prev = pHead;
//if list is empty
if(curr == null)
return null;
while(curr.rec != rKey){
//if end of list
if(curr.next == null)
//not found
return null;
else{
prev = curr;
curr = curr.next;
}
}
//find it
//if the first node
if(curr == pHead)
pHead = pHead.next;
else
prev.next = curr.next;
//set the next ref to null
curr.next = null;
return curr;
}
}
record.java
public class Record{
/**
* @param args the command line arguments
*/
private int accountID;
public String name;
private String address;
private String dob;
private String phoneNumber;
public String balance;
private String inAccType;
public double inInterest;
DecimalFormat formatter = new DecimalFormat("#0.00");
public Record(){
}
public Record(String name){
setName(name);
}
public Record(String name,double balance) {
setName(name);
setBalance(balance);
}
public Record(String name,double balance,double interest) {
setName(name);
setBalance(balance);
setDailyInterest(interest);
}
public Record(String name,int accId,String add,String dob,
String phoneNum,String type, double balance,
double interest) {
setName(name);
setAccountID(accId);
setAddress(add);
setDob(dob);
setPhoneNumber(phoneNum);
setAccType(type);
setBalance(balance);
setDailyInterest(interest);
}
public void setAccountID(int accId){
accountID = accId;
}
public int getAccountID() {
return accountID;
}
public void setName(String Custname){
name = Custname;
}
public String getName() {
return name;
}
public void setAddress(String add) {
address = add;
}
public String getAddress() {
return address;
}
public void setDob(String dateOfBirth){
dob = dateOfBirth;
}
public String getDob() {
return dob;
}
public void setPhoneNumber(String phoneNum) {
phoneNumber = phoneNum;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setBalance(double accBal) {
balance = formatter.format(accBal);
}
public double getBalance() {
return Double.parseDouble(balance);
}
public void setAccType(String accType) {
inAccType = accType;
}
public String getAccType() {
return inAccType;
}
public void setDailyInterest(double interest) {
inInterest = interest;
}
public double getDailyInterest() {
return inInterest;
}
public void display() {
if(inInterest != 0 ){
System.out.println("\nName = " + name);
System.out.println("Account Balance = " + balance);
System.out.println("Account Id = " + accountID);
System.out.println("Address = " + address);
System.out.println("DOB = " + dob);
System.out.println("Phone Number = " + phoneNumber);
System.out.println("Account Type = " + inAccType);
System.out.println("Fixed Daily Interest = " + inInterest);
}else if(inInterest == 0){
System.out.println("\nName = " + name);
System.out.println("Account Balance = " + balance);
System.out.println("Account Id = " + accountID);
System.out.println("Address = " + address);
System.out.println("DOB = " + dob);
System.out.println("Phone Number = " + phoneNumber);
System.out.println("Account Type = " + inAccType);
}
}
public void update() {
System.out.println("Name = " + name);
System.out.println("Account Balance = " + balance);
}
}
My read into array code
public static int readFile(String fileName) throws IOException {
BufferedReader br = null;
String line,add,dob,phoneNum,read,name = null,line1,line2, data,
accType,fixedInterest = "";
double accBal;
int accid,c = 0, count = 0, countDupRec = 0, till = 0;
boolean duplicate = false, check = false;
try {
if (recSize <= 30 && recSize != 0) {
rec= new Record[recSize];
br = new BufferedReader(new FileReader(fileName));
line = null;
while ((line = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, "=");
while (st.hasMoreTokens()) {
read = st.nextToken();
if (read.equals("Account Id ")) {
rec[r] = new Record();
accid = Integer.parseInt(st.nextToken().trim());
rec[r].setAccountID(accid);
}
if (read.equals("Name ")) {
name = st.nextToken().trim().toUpperCase();
rec[r].setName(name);
}
if (read.equals("Address ")) {
add = st.nextToken().trim();
rec[r].setAddress(add);
}
if (read.equals("DOB ")) {
dob = st.nextToken().trim();
rec[r].setDob(dob);
}
if (read.equals("Phone Number ")) {
phoneNum = st.nextToken().trim();
rec[r].setPhoneNumber(phoneNum);
}
if (read.equals("Account Balance ")) {
accBal = Double.parseDouble(st.nextToken().trim());
rec[r].setBalance(accBal);
}
if (read.equals("Account Type ")) {
accType = st.nextToken().trim();
rec[r].setAccType(accType);
r++;
}
}
}
//Get the fixed interest rate
br = new BufferedReader(new FileReader(fileName));
line2 = null;
while ((line2 = br.readLine()) != null) {
StringTokenizer st4 = new StringTokenizer(line2, "=");
while (st4.hasMoreTokens()) {
data = st4.nextToken();
if(data.equals("Fixed Daily Interest ")){
String aString = st4.nextToken();
fixedInterest = fixedInterest + " " + aString;
}
}
}
//Get the elment that has fixed account type
String val = "";
for (int i = 0; i < recSize; i++) {
if (rec[i].getAccType().equals("Fixed")) {
String aString = Integer.toString(i);
val = val + " " + aString;
}
}
//Set the value of fixed Interest into fixed account
StringTokenizer st5 = new StringTokenizer(val, " ");
StringTokenizer st6 = new StringTokenizer(fixedInterest, " ");
while (st5.hasMoreTokens()&& st6.hasMoreTokens()) {
rec[Integer.parseInt(st5.nextToken())].setDailyInterest(
Double.parseDouble(st6.nextToken()));
}
}