Using Array Approach Linked List approach and Delete Byte Ap
Using Array Approach, Linked List approach, and Delete Byte Approach: (add code for all three approaches) (different classes preffered)
I am adding the sample program that you can use and modify, and add code to it.
You need to create a program that reads the input file (items.txt), and store the items in the file in the Item array as well as the Array List. After creating that array, send the output records from the file to a new file \"ex1out1.txt\"
Now, according to \"p2changes.txt\", you need to add or delete the record specified ( A or a: add D or d: delete)
items.txt:
items.txt:
p2changes.txt:
A, 106,ChainSaw 12\"
D,102
d,104
a,107,ChainSaw 10\"
Sample program: (Sample program does input output files and array outputting which is required)
Main:
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Main {
private static void step1() {
// Creating an array and an array list itemArray and itemArrayList
List<Item> itemArray = new ArrayList<>(10);
List<Item> itemArrayList = new ArrayList<>();
try {
//reading from file \"items.txt\"
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(\"C:/Users/patel/Desktop/items.txt\")));
String line = reader.readLine();
while (line != null) {
//splitting file by comma
String[] array = line.split(\",\");
Item item = new Item(Integer.parseInt(array[0]),array[1]);
itemArray.add(item);
itemArrayList.add(item);
line = reader.readLine();
}
//opening file for output
PrintWriter writer = new PrintWriter(\"C:/Users/patel/Desktop/ex1out1.txt\");
writer.println(\"Aashiv \" + \" Patel\");
//sorting in reverse order
Collections.sort(itemArray, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return o2.getItemNumber() - o1.getItemNumber();
}
});
// moving through the ArrayList and printing the items
writer.println();
writer.print(\"Array Format: \");
writer.println();
for(Item item : itemArray) {
writer.println();
writer.println(item);
}
//sorting in sequential order of numbers
Collections.sort(itemArrayList, new Comparator<Item>() {
@Override
public int compare(Item item1, Item item2) {
return item1.getItemNumber() - item2.getItemNumber();
}
});
// moving through the ArrayList and printing the items
writer.println();
writer.print(\"Array List Format: \");
writer.println();
for(Item item : itemArrayList) {
writer.println();
writer.println(item);
}
//closing both writers and readers
writer.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// Main method to call the step1 method
public static void main(String args[]) {
step1();
}
}
Item.java:
public class Item {
private int itemNumber; //number of item
private String itemName; //name of item
Item(int number, String name)
{
this.itemNumber = number;
this.itemName = name;
}
//return the artistName
public String getItemName() {
return itemName;
}
//set the artistName
public void set(int itemNumber, String itemName) {
this.itemName = itemName;
this.itemNumber = itemNumber;
}
//return the artistId
public int getItemNumber() {
return itemNumber;
}
// toString method
public String toString() {
return this.itemNumber + \",\" + this.itemName;
}
}
Solution
//In this code all changes from p2changes.txt file are made on ex1out1.txt and ex1out2.txt files.
//Item.txt file contains original contents.
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Main
{
private static List<Item> itemArray = new ArrayList<>(10);
private static List<Item> itemArrayList = new ArrayList<>();
private static void writeFile()
{
//opening file for output
try
{
PrintWriter writer = new PrintWriter(\"./ex1out1.txt\");
PrintWriter writer2=new PrintWriter(\"./ex1out2.txt\");
//sorting in reverse order
Collections.sort(itemArray, new Comparator<Item>()
{
@Override
public int compare(Item o1, Item o2)
{
return o2.getItemNumber() - o1.getItemNumber();
}
});
writer.println(\"Array Format :\");
System.out.println(\"Array Format :\");
for(Item item : itemArray)
{
System.out.println(item);
writer.println(item);
}
//sorting in sequential order of numbers
Collections.sort(itemArrayList, new Comparator<Item>()
{
@Override
public int compare(Item item1, Item item2)
{
return item1.getItemNumber() - item2.getItemNumber();
}
});
// moving through the ArrayList and printing the items
writer2.println(\"Array List Format: \");
System.out.println(\"Array List Format:\");
for(Item item : itemArrayList)
{
System.out.println(item);
writer2.println(item);
}
//closing both writers and readers
writer.close();
writer2.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private static void step1()
{
// Creating an array and an array list itemArray and itemArrayList
try
{
//reading from file \"items.txt\"
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(\"./items.txt\")));
String line = reader.readLine();
while (line != null)
{
//splitting file by comma
String[] array = line.split(\",\");
Item item = new Item(Integer.parseInt(array[0]),array[1]);
itemArray.add(item);
itemArrayList.add(item);
line = reader.readLine();
}
System.out.println(\"Before making changes content of files ex1out1 and ext1out2 files\");
writeFile();
System.out.println(\"==============\");
reader.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void step2()
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(\"./p2changes.txt\")));
String line = reader.readLine();
while (line != null)
{
//splitting file by comma
String[] array = line.split(\",\");
//checking the string of whether it is for adding new item or deleting an item
if(array[0].equals(\"A\") || array[0].equals(\"a\"))
{
Item item = new Item(Integer.parseInt(array[1]),array[2]);
itemArray.add(item);
itemArrayList.add(item);
}
else if(array[0].equals(\"D\") || array[0].equals(\"d\"))
{
//iterating through array for finding an item to delete
for(Item item : itemArray)
{
if(item.getItemNumber()==Integer.parseInt(array[1]))
{
itemArray.remove(item);
itemArrayList.remove(item);
break;
}
}
}
line = reader.readLine();
}
System.out.println(\"After making changes as in file p2changes.txt ====\");
writeFile();
reader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
// Main method to call the step1 method
public static void main(String args[])
{
step1();
step2();
}
}
//ex1out2.txt
Array List Format:
101,Nail #1
103,Nail #3
105,Hammer Large
106,ChainSaw 12\"
107,ChainSaw 10\"
//ex1out1.txt
Array Format :
107,ChainSaw 10\"
106,ChainSaw 12\"
105,Hammer Large
103,Nail #3
101,Nail #1
//p2changes.txt
A,106,ChainSaw 12\"
D,102
d,104
a,107,ChainSaw 10\"
//item.txt
101,Nail #1
102,Nail #2
103,Nail #3
104,Hammer Small
105,Hammer Large





