The purpose of this assignment is to get familiar with the linked list. Create a
ID: 3700878 • Letter: T
Question
The purpose of this assignment is to get familiar with the linked list. Create a will then write four classes: project called Assignment 2.You Assignment2.java-A driver class that contains only a main method which should: prt Create a new Customer List object Prompt the user to enter a file name Call the gerCustomer ListO method with the file name that the user entered While (true) f Prompt user to enter command; if (command is "a") f Prompt user for customerNumber firstName lastName balance Create a customerRecord and inserts the customer record at the head of a singly linked list; ) else if (command is "f") )else if(command is "d") ) else if(command is "r")t Prompt the user to enter a customer number and then displays the corresponding record on the screen Delete the first customer record from the singly linked list. Prompt the user to enter a customer number; Delete the given customer record corresponding to the customer number from the singly linked list If the customer number does not exist, please show the error message; ) else ifcommand is "n"f Call getTotalCustomerNumber() method and then displays the total number of customers in the database on the screen else if (command is "q" Prompt the user to enter a file name to save the latest update; Save the information in the database to the file specified by the user Terminate program; ) else f Display error messageExplanation / Answer
here is your file :-------------------->>>>>>>>>>>
CustomerRecord.java: ----------->>>>>>
public class CustomerRecord{
private int custNumber;
private String fname;
private String lname;
private float balance;
public CustomerRecord(int c,String fn,String ln,float bal){
custNumber = c;
fname = fn;
lname = ln;
balance = bal;
}
public void setFirstName(String fn){
fname = fn;
}
public void setLastName(String ln){
lname = ln;
}
public void setCustomerNumber(int cn){
custNumber = cn;
}
public void setBalance(float bal){
balance = bal;
}
public String getFirstName(){
return fname;
}
public String getLastName(){
return lname;
}
public int getCustomerNumber(){
return custNumber;
}
public float getBalance(){
return balance;
}
public String toString(){
return custNumber+" "+fname+" "+lname+" "+balance;
}
}
Node.java : --------->>>>>>>>
public class Node{
private CustomerRecord element;
private Node next;
public Node(CustomerRecord el){
element = el;
next = null;
}
public void setElement(CustomerRecord el){
element = el;
}
public void setNext(Node nex){
next = nex;
}
public Node getNext(){
return next;
}
public CustomerRecord getElement(){
return element;
}
}
CustomerList.java : ---------->>>>>>>>>
import java.io.File;
import java.util.Scanner;
import java.io.PrintWriter;
public class CustomerList{
private Node head;
private int size = 0;
public CustomerList(){
head = null;
size = 0;
}
public void getCustomerList(String file){
try{
Scanner sc = new Scanner(new File(file));
String line;
while(sc.hasNextLine()){
line = sc.nextLine();
String[] str = line.trim().split(" ");
if(str.length == 4){
int n = Integer.parseInt(str[0]);
enterCustomerRecord(new CustomerRecord(n,str[1],str[2],Float.parseFloat(str[3])));
}
}
sc.close();
}catch(Exception e){
System.out.println("File opening Error Check Input File");
}
}
public void enterCustomerRecord(CustomerRecord new_record){
Node temp = new Node(new_record);
temp.setNext(head);
head = temp;
size++;
}
public CustomerRecord getCustomerRecord(int custNum){
Node cur = head;
while(cur != null){
if(cur.getElement().getCustomerNumber() == custNum){
return cur.getElement();
}
cur = cur.getNext();
}
return null;
}
public void removeFirstCustomerRecord(){
if(size == 0){
return;
}
head = head.getNext();
size--;
}
public int getTotalCustomerNumber(){
return size;
}
public void removeCutomerRecord(int custNum){
Node cur = head;
Node prev = null;
while(cur != null){
if(cur.getElement().getCustomerNumber() == custNum){
if(prev == null){
head = head.getNext();
size--;
return;
}
prev.setNext(cur.getNext());
size--;
return;
}
prev = cur;
cur = cur.getNext();
}
System.out.println("ERROR:Customer Record Not Found : ");
}
public void saveCustomerList(String file){
try{
PrintWriter pw = new PrintWriter(new File(file));
Node cur = head;
while(cur != null){
pw.println(cur.getElement());
cur = cur.getNext();
}
pw.close();
}catch(Exception e){
System.out.println("File Creating Error !!! ");
}
}
}
Assignment2.java : ----------->>>>>>>>>
import java.util.Scanner;
public class Assignment2{
public static void main(String[] args) {
CustomerList custList = new CustomerList();
System.out.println("Enter The File Name To Extract Customer List : ");
Scanner sc = new Scanner(System.in);
String file = sc.next();
custList.getCustomerList(file);
String command = "";
while(true){
System.out.print(" >>>");
command = sc.next();
if(command.equals("a")){
String fn;
String ln;
float bal;
int cn;
System.out.println("Enter Customer Number : ");
cn = sc.nextInt();
System.out.println("Enter Customer First name : ");
fn = sc.next();
System.out.println("Enter Customer Last name : ");
ln = sc.next();
System.out.println("Eneter Customer Balance : ");
bal = sc.nextFloat();
custList.enterCustomerRecord(new CustomerRecord(cn,fn,ln,bal));
}else if(command.equals("f")){
int cn;
System.out.println("Enter Customer Number : ");
cn = sc.nextInt();
CustomerRecord temp = custList.getCustomerRecord(cn);
if(temp == null){
System.out.println("Customer Record Not Found");
}else{
System.out.println(temp);
}
}else if(command.equals("d")){
custList.removeFirstCustomerRecord();
}else if(command.equals("r")){
int cn;
System.out.println("Enter Customer Number : ");
cn = sc.nextInt();
custList.removeCutomerRecord(cn);
}else if(command.equals("n")){
System.out.println("Total Customer = "+custList.getTotalCustomerNumber());
}else if(command.equals("q")){
break;
}else{
System.out.println("Not a valid command");
}
}
System.out.println("Enter File Name To Save List : ");
file = sc.next();
custList.saveCustomerList(file);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.