Starting with the starting program you are to modify the cop
Starting with the starting program, you are to modify the cope and create new classes as well as output to files according to the specifications:
1) Construct a class named ItemD.java : Code:
public class ItemD extends Item {
private boolean deleteByte;
// constructor
public ItemD(int artistID, String artistName, boolean deleteByte) {
super(artistID, artistName);
this.deleteByte = deleteByte;
// TODO Auto-generated constructor stub
}
// setting the delete byte
public void setDeleteByte(boolean deleteByte)
{
this.deleteByte = deleteByte;
}
// getting the delete byte
public boolean getDeleteByte()
{
return deleteByte;
}
public String toString()
{
return (super.toString() + \"\\t\" + deleteByte);
}
}
2) Class name ReadItemD to open the \"items.txt\" file, read the file and store all records in to an Array List of ItemD and close the file.
3) Add a class named \"step2\" in the main program which does the following: a) Make an instance of \"ReadItemD\" using a name of your choice
b) Arrange to have records of \"items.txt\" file stored in the Array Lisf of itemD.
c) Print out the contents of Array List of ItemD to an output file named \"ex1out2.txt\"
4) Make an instance of step2 in main method, and run the program.
5) Please attach the output file when done
Now,
1) Implement \"add\" and \"delete\" methods which which add or delete the records specified in the \"changes.txt\" file after reading the file, and calling the appropriate methods to update the Array List of ItemD.
2) Send the contents of the updated ArrayList of ItemD to \"ex1out2.txt\".
3) Please attach the updated output file.
Example output:
101 Nail #1 false
102 Nail #2 false
103 Nail #3 false
104 hammer small true
104 Hammer Large false
106 Chainsaw 12\" false
107 Chainsaw 10\" false
Now,
1) Construct a class ItemNode.java that inherits Item :
public class ItemNode extends Item {
// Instance variable
private ItemNode next;
/**
* Constructor
* @param ID
* @param name
* @param next
*/
public ItemNode(int itemNumber, String itemName, ItemNode next)
{
super(itemNumber, itemName);
this.next = next;
}
/**
* @return the next
*/
public ItemNode getNext()
{
return next;
}
/**
* @param next the next to set
*/
public void setNext(ItemNode next)
{
this.next = next;
}
public String toString()
{
return super.toString();
}
}
2) Construct class ItemList.java: (feel free to modify if needed)
public class ItemList {
// Instance variables
private ItemNode list;
ItemNode firstNode;
private int totalEntries;
private int nextID;
/**
* Constructor
*/
public ItemList() {
list = null;
this.firstNode = null;
this.totalEntries = 0;
nextID = 0;
}
public void setNextID(int ID)
{
this.nextID = ID;
}
public int getNextID()
{
return nextID;
}
/**
* Returns true if totalEntries = 0 false otherwise
*
* @return
*/
public boolean isEmpty() {
if (this.totalEntries == 0)
return true;
else
return false;
}
/**
* Prints the list
*/
public void print() {
System.out.println(this);
}
/**
* Adds an artist with id and name
*
* @param id
* @param name
*/
public void addNode(int itemNumber, String itemName) {
//this.nextID = id + 1;
// Create a new ItemNode
ItemNode newNode = new ItemNode(itemNumber, itemName, null);
if (isEmpty())
{
// Set newNode as firstNode
this.firstNode = newNode;
this.totalEntries += 1;
}
}
/**
* Adds an artist with name to the list
*
* @param name
*/
public void add(String name) {
addNode(this.nextID, name);
}
/**
* Deletes an artist from the list
*
* @param id
* @return
*/
public boolean delete(int id) {
if (!isEmpty()) {
ItemNode node = list;
boolean found = false;
// Traverse through the list
while (node != null) {
if (node.getItemNumber() == id) {
found = true;
// Set data of this node to the data of the firstNode
node.set(this.firstNode.getItemNumber(), this.firstNode.getItemName());
// Remove firstNode
this.firstNode = this.firstNode.getNext();
this.totalEntries -= 1;
break;
}
// Move node to the next node
node = node.getNext();
}
return found;
}
return false;
}
/**
* Finds a node containing an artist by name and returns a reference to that
* node
*
* @param name
* @return
*/
public ItemNode find(String name) {
if (!isEmpty()) {
ItemNode node = this.firstNode;
// Traverse through the list
while (node != null) {
if (node.getItemName().equalsIgnoreCase(name)) {
return node;
}
// Move node to the next node
node = node.getNext();
}
}
return null;
}
public String toString() {
StringBuffer sb = new StringBuffer();
if (this.firstNode != null) {
ItemNode node = this.firstNode;
// Traverse through the list
while (node != null) {
sb.append(node + \"\ \");
node = node.getNext();
}
}
return sb.toString();
}
}
3) For this part, just construct a linked list from reading \"items.txt\" and then print the list out to the output file \"ex1out3.txt\". (Attach the output file)
4) Construct a class named \"ReadItemList\" that is similar to \"ReadItemD\" except that you will store the record in Linked list instead of Array List.
5) Send the output to a file named \"ex1out4.txt\"
Example output:
101 Nail #1
102 Nail #2
103 Nail #3
104 hammer small
104 Hammer Large
101 Nail #1 false
103 Nail #3 false
105 Hammer Large
106 Chainsaw 12\" false
107 Chainsaw 10\" false
Starting program:
Sample output :
Contents of ex1out1.txt
FirstName LastName
105 Hammer Large
104 Hammer Small
103 Nail #3
102 Nail #2
101 Nail #1
101 Nail #1
102 Nail #2
103 Nail #3
104 Hammer Small
105 Hammer Large
Item.java:
Contents of items.txt
101,Nail #1
102,Nail #2
103,Nail #3
104,Hammer Small
105,Hammer Large
changes.txt:
A,106,Chainsaw 12\"
D,102
d,104
a,107,Chainsaw 10\"
Solution
PROGRAM CODE:
Main.java
package simple;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
class step2
{
ReadItemD read;
ArrayList<ItemD> list;
public step2() {
read = new ReadItemD(\"items.txt\");
list = read.getItems();
}
public void writeToFile()
{
try {
PrintWriter writer = new PrintWriter(new File(\"ex1out2.txt\"));
for(int i=0; i<list.size(); i++)
{
writer.write(list.get(i).toString() + \"\ \");
}
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add()
{
Scanner reader;
try {
reader = new Scanner(new File(\"changes.txt\"));
while(reader.hasNextLine())
{
String[] keys = reader.nextLine().split(\",\");
if(keys[0].toLowerCase().equals(\"a\"))
{
list.add(new ItemD(Integer.valueOf(keys[1]), keys[2], false));
}
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void delete()
{
Scanner reader;
try {
reader = new Scanner(new File(\"changes.txt\"));
while(reader.hasNextLine())
{
String[] keys = reader.nextLine().split(\",\");
if(keys[0].toLowerCase().equals(\"d\"))
{
for(int i=0; i<list.size(); i++)
{
if(list.get(i).getNumber() == Integer.valueOf(keys[1]))
{
list.get(i).setDeleteByte(true);
break;
}
}
}
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Main {
private static void step1() {
List<Item> itemArray = new ArrayList<>(10);
List<Item> itemArrayList = new ArrayList<>();
try {
//reading from file
Scanner reader = new Scanner(new File(\"items.txt\"));
while (reader.hasNextLine()) {
String line = reader.nextLine();
//splitting file by comma
String[] array = line.split(\",\");
Item item = new Item(Integer.parseInt(array[0]),array[1]);
itemArray.add(item);
itemArrayList.add(item);
}
reader.close();
//opening file for output
PrintWriter writer = new PrintWriter(new File(\"ex1out1.txt\"));
writer.println(\"FirstName LastName\");
//sorting in reverse order
Collections.sort(itemArray, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return o2.getNumber() - o1.getNumber();
}
});
for(Item item : itemArray) {
writer.println(item);
}
//sorting in ascending order of numbers
Collections.sort(itemArrayList, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return o1.getNumber() - o2.getNumber();
}
});
for(Item item : itemArrayList) {
writer.println(item);
}
//closing both connections
writer.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void itemList()
{
//constructing a linked list using ItemList
Scanner reader;
ItemList list = new ItemList();
try {
reader = new Scanner(new File(\"items.txt\"));
while(reader.hasNextLine())
{
String keys[] = reader.nextLine().split(\",\");
list.addNode(Integer.valueOf(keys[0]), keys[1]);
}
PrintWriter writer = new PrintWriter(new File(\"ex1out3.txt\"));
writer.write(list.toString());
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void step3()
{
ReadItemList reader = new ReadItemList(\"items.txt\");
ItemList list = reader.getItems();
try {
PrintWriter writer = new PrintWriter(new File(\"ex1out4.txt\"));
writer.write(list.toString());
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[]) {
step1();
step2 step2 = new step2();
//writing to file the items from items.txt
step2.writeToFile();
//adding items from changes.txt
step2.add();
step2.writeToFile();
//removing items from changes.txt
step2.delete();
step2.writeToFile();
//using ItemList class
itemList();
//step3 for reading and writing using ReadItemList class
step3();
}
}
ReadItemD.java
package simple;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ReadItemD {
private ArrayList<ItemD> listOfItems;
public ReadItemD(String filename) {
listOfItems = new ArrayList<>();
try {
Scanner scanner = new Scanner(new File(filename));
while(scanner.hasNextLine())
{
String keys[] = scanner.nextLine().split(\",\");
ItemD itemd = new ItemD(Integer.valueOf(keys[0]), keys[1], false);
listOfItems.add(itemd);
}
scanner.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ArrayList<ItemD> getItems()
{
return listOfItems;
}
}
Item.java
package simple;
public class Item {
private Integer number;
private String name;
public Item(Integer number, String name) {
this.number = number;
this.name = name;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return number + \" \" + name;
}
}
ItemD.java
package simple;
public class ItemD extends Item {
private boolean deleteByte;
// constructor
public ItemD(int artistID, String artistName, boolean deleteByte) {
super(artistID, artistName);
this.deleteByte = deleteByte;
// TODO Auto-generated constructor stub
}
// setting the delete byte
public void setDeleteByte(boolean deleteByte)
{
this.deleteByte = deleteByte;
}
// getting the delete byte
public boolean getDeleteByte()
{
return deleteByte;
}
public String toString()
{
return (super.toString() + \"\\t\" + deleteByte);
}
}
ItemList.java
package simple;
public class ItemList {
// Instance variables
private ItemNode list;
ItemNode firstNode;
private int totalEntries;
private int nextID;
/**
* Constructor
*/
public ItemList() {
list = null;
this.firstNode = null;
this.totalEntries = 0;
nextID = 0;
}
public void setNextID(int ID)
{
this.nextID = ID;
}
public int getNextID()
{
return nextID;
}
/**
* Returns true if totalEntries = 0 false otherwise
*
* @return
*/
public boolean isEmpty() {
if (this.totalEntries == 0)
return true;
else
return false;
}
/**
* Prints the list
*/
public void print() {
System.out.println(this);
}
/**
* Adds an artist with id and name
*
* @param id
* @param name
*/
public void addNode(int itemNumber, String itemName) {
//this.nextID = id + 1;
// Create a new ItemNode
ItemNode newNode = new ItemNode(itemNumber, itemName, null);
if (isEmpty())
{
// Set newNode as firstNode
this.firstNode = newNode;
}
else
{
ItemNode node = firstNode;
while(node.getNext()!= null)
node = node.getNext();
node.setNext(newNode);
}
this.totalEntries += 1;
}
/**
* Adds an artist with name to the list
*
* @param name
*/
public void add(String name) {
addNode(this.nextID, name);
}
/**
* Deletes an artist from the list
*
* @param id
* @return
*/
public boolean delete(int id) {
if (!isEmpty()) {
ItemNode node = list;
boolean found = false;
// Traverse through the list
while (node != null) {
if (node.getNumber() == id) {
found = true;
// Set data of this node to the data of the firstNode
node.set(this.firstNode.getNumber(), this.firstNode.getName());
// Remove firstNode
this.firstNode = this.firstNode.getNext();
this.totalEntries -= 1;
break;
}
// Move node to the next node
node = node.getNext();
}
return found;
}
return false;
}
/**
* Finds a node containing an artist by name and returns a reference to that
* node
*
* @param name
* @return
*/
public ItemNode find(String name) {
if (!isEmpty()) {
ItemNode node = this.firstNode;
// Traverse through the list
while (node != null) {
if (node.getName().equalsIgnoreCase(name)) {
return node;
}
// Move node to the next node
node = node.getNext();
}
}
return null;
}
public String toString() {
StringBuffer sb = new StringBuffer();
if (this.firstNode != null) {
ItemNode node = this.firstNode;
// Traverse through the list
while (node != null) {
sb.append(node + \"\ \");
node = node.getNext();
}
}
return sb.toString();
}
}
ItemNode.java
package simple;
public class ItemNode extends Item{
// Instance variable
private ItemNode next;
/**
* Constructor
* @param ID
* @param name
* @param next
*/
public ItemNode(int itemNumber, String itemName, ItemNode next)
{
super(itemNumber, itemName);
this.next = next;
}
/**
* @return the next
*/
public ItemNode getNext()
{
return next;
}
/**
* @param next the next to set
*/
public void setNext(ItemNode next)
{
this.next = next;
}
public String toString()
{
return super.toString();
}
public void set(Integer number, String name) {
setName(name);
setNumber(number);
}
}
OUTPUT:
ex1out1.txt
FirstName LastName
105 Hammer Large
104 Hammer Small
103 Nail #3
102 Nail #2
101 Nail #1
101 Nail #1
102 Nail #2
103 Nail #3
104 Hammer Small
105 Hammer Large
ex1out2.txt
101 Nail #1 false
102 Nail #2 true
103 Nail #3 false
104 Hammer Small true
105 Hammer Large false
106 Chainsaw 12\" false
107 Chainsaw 10\" false
ex1out3.txt
101 Nail #1
102 Nail #2
103 Nail #3
104 Hammer Small
105 Hammer Large
ex1out3.txt
101 Nail #1
102 Nail #2
103 Nail #3
104 Hammer Small
105 Hammer Large




















