Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

EXPLAIN THE CODE BELOW #NEED THIS ASAP WILL RATE HIGHLY import java.io.*; import java.io.FileWriter; import java.util.ArrayList; import java.util.Scanner; class Process { private Customer[] detailsArray; //

EXPLAIN THE CODE BELOW #NEED THIS ASAP WILL RATE HIGHLY import java.io.*; import java.io.FileWriter; import java.util.ArrayList; import java.util.Scanner; class Process { private Customer[] detailsArray; // "Has-A" relationship with Details class private int count; // constructor public Process() { detailsArray = new Customer[5]; count = 0; } public void addDetails(Customer c) { if (count < detailsArray.length) { this.detailsArray[count] = c; //count++; try { FileWriter writer = new FileWriter(detailsArray[count].getId()+".txt", true); writer.write(" ID : " + c.getId() +" Name : " + c.getName() + " Phone Number :"+ c.getPhoneNum()+ " Address :" + c.getAddress() +" Stall : " + c.getStall() + " Product : " + c.getProduct() + " Lot : " + c.getLot() + " "); writer.close(); count++; } catch (IOException e) { System.out.println("Error writing to file: " + e.getMessage()); } } else { System.out.println("Cannot add more details. Array is full."); } } public boolean isIdExist(String id) { for (int i = 0; i < count; i++) { if (detailsArray[i].getId().equalsIgnoreCase(id)) { return true; } } return false; } public void deleteFile(String id) { try { java.io.File file = new java.io.File(id +".txt"); if (file.delete()) { System.out.println("File with ID " + id + " has been deleted."); } else { System.out.println("File with ID " + id + " does not exist."); } } catch (Exception e) { System.out.println("Error deleting file: " + e.getMessage()); } } public void deleteDetail(String id) { for (int i = 0; i < count - 1; i++) { if(detailsArray[i].getId().equalsIgnoreCase(id)) { for(int j = i ; j < count ; j++) { detailsArray[j] = detailsArray[j + 1]; } } } count--; System.out.println("Detail with ID " + id + " has been deleted."); } public void updateDetail(String id, int update , String newValue) { for(int i = 0 ; i < count ; i++) { if (detailsArray[i].getId().equalsIgnoreCase(id)) { switch(update) { case 1: detailsArray[i].setId(newValue); break; case 2: detailsArray[i].setName(newValue); break; case 3: detailsArray[i].setStall(newValue); break; case 4: detailsArray[i].setProduct(newValue); break; case 5: detailsArray[i].setLot(newValue); break; case 6: detailsArray[i].setAddress(newValue); break; case 7: detailsArray[i].setPhoneNum(newValue); break; } String tempID = detailsArray[i].getId(); String tempName = detailsArray[i].getName(); String tempStall = detailsArray[i].getStall(); String tempProduct = detailsArray[i].getProduct(); String tempLot = detailsArray[i].getLot(); String tempPn = detailsArray[i].getPhoneNum(); String tempadd = detailsArray[i].getAddress(); deleteDetail(id); deleteFile(id); Customer cust = new Customer(tempID,tempName,tempStall,tempProduct,tempLot,tempPn,tempadd); addDetails(cust); } } } public void viewDetails(String id) { for(int i = 0 ; i < count ; i++) { if (detailsArray[i].getId().equalsIgnoreCase(id)) { System.out.println("ID found!"); System.out.println("ID :" + detailsArray[i].getId()); System.out.println("Name :" + detailsArray[i].getName()); System.out.println("Phone Number :" + detailsArray[i].getPhoneNum()); System.out.println("Address :" + detailsArray[i].getAddress()); System.out.println("Stall :" + detailsArray[i].getProduct()); System.out.println("Product :" + detailsArray[i].getStall()); System.out.println("Lot :" + detailsArray[i].getLot()); break; } } } // method to view details public void viewDetails() { for (int i = 0; i < count; i++) { System.out.println("Name: " + detailsArray[i].getName()); System.out.println("ID: " + detailsArray[i].getId()); } } public void fileFinder(String id) { File file = new File(id+".txt"); if (file.exists()) { try { BufferedReader reader = new BufferedReader(new FileReader(file)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("File not found"); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

i need correct answrrs 9 3 2 .

Answered: 1 week ago