Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C PROGRAMMING Write a modular program that creates a linked list of client nodes

ID: 3719413 • Letter: C

Question

C PROGRAMMING

Write a modular program that creates a linked list of client nodes that is in ascending order by the account numbers of the clients and prints a table containing the data in the nodes by traversing the list. The program will read the data from a file named “alphaClients.txt”. The client records are stored in the file in the following format:

The account number for a client should be first.

The account number is followed by a space that separates it from the client name.

The name is followed by a newline character which separates it from the balance.

The balance is followed by a newline character.

This newline character separates this record from the next record.

The text file that you are reading is going to contain the following to further explain the format of the text file :

9412 Advent, Ltd.
245.31
1222 Adventure Travel
72.74
3910 Bank of America
-88.83
1035 Bausch & Lomb
3252.65
6900 Capital One
718.72
1072 Chick-fil-A
0.00
1015 Goodacre
1000.84
2000 HEB
1039.20
2407 Hathaway, Inc.
55.41
4100 Hilton
-41.17
8652 MKJ
-845.65
5300 Nike
20.26
1086 Quik Trip
94.89
6100 REI
201.80
2405 Ranger
0.00
2300 Southwest Airlines
0.00
1096 Starbucks
3016.78
7900 T-Mobile
-10.43
1078 Toyota North America
-93.79

A samlpe of the program output is shown below :

Account Name Balance

1015 Goodacre 1000.84

1035 Bausch & Lomb 3252.65

1072 Chick-fil-A 0.00

1078 Toyota North America -93.79

1086 Quik Trip 94.89

1096 Starbucks 3016.78

1222 Adventure Travel 72.74

2000 HEB 1039.20

2300 Southwest Airlines 0.00

2405 Ranger 0.00

2407 Hathaway, Inc. 55.41

3910 Bank of America -88.83

4100 Hilton -41.17

5300 Nike 20.26

6100 REI 201.80

6900 Capital One 718.72

7900 T-Mobile -10.43

8652 MKJ -845.65

9412 Advent, Ltd. 245.31

Explanation / Answer

package com.logicboxes.timepass;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.LinkedList;

public class Driver
{

public static void main(String[] args) throws IOException
{
System.out.println ("Current directory is " + new File(".").getAbsolutePath());

List<ClientNode> l=new LinkedList<>();
FileReader fileReader = new FileReader("alphaClients.txt");
BufferedReader bufferedReader =new BufferedReader(fileReader);
String line,name;
int accountNo;
double balance;
String[] temp,temp1;
ClientNode n=new ClientNode();
while((line = bufferedReader.readLine()) != null)
{
temp=line.split(" ");
if(temp.length==1)
{
balance=Double.parseDouble(temp[0]);
n.balance=balance;
}
else
{
n=new ClientNode();
accountNo=Integer.parseInt(temp[0]);
temp1=new String[temp.length-1];
for(int i=1;i<temp.length;i++)
{
temp1[i-1]=temp[i];
}
name= Arrays.toString(temp1).replace(",","").replace("]","").replace("[","");
n.accountNo=accountNo;
n.name=name;
l.add(n);
}
}
bufferedReader.close();
Collections.sort(l, new Comparator<ClientNode>() {
@Override
public int compare(ClientNode o1, ClientNode o2) {
return o1.accountNo- o2.accountNo;
}
});
System.out.printf("%-20s %-20s %20s","Account","Name","Balance");
System.out.println();
for(ClientNode n1:l)
{
System.out.printf("%-20s %-20s %20s.2f%n",n1.accountNo,n1.name,n1.balance);
}
}
}
class ClientNode
{
int accountNo;
String name;
double balance;
}