-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparserAndReadin.java
119 lines (108 loc) · 4.06 KB
/
parserAndReadin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class parserAndReadin
{
public ArrayList<Block> readInData(String fileName)
{
ArrayList<Block> blocks = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName)))
{
String line;
while ((line = reader.readLine()) != null)
{
String[] data = line.split(",");
if (data.length == 10) {
String name = data[0];
boolean renewability = Boolean.parseBoolean(data[1]);
String stackability = data[2];
double blastres = Double.parseDouble(data[3]);
double hardness = Double.parseDouble(data[4]);
double luminous = Double.parseDouble(data[5]);
boolean flammable = Boolean.parseBoolean(data[6]);
String dimension = data[7];
boolean craftabilty = Boolean.parseBoolean(data[8]);
String SFX = data[9];
Block block = new Block(name, renewability, stackability, blastres, hardness, luminous, flammable, dimension, craftabilty, SFX);
blocks.add(block);
}
}
}
catch (IOException iox)
{
System.out.println("Problem Reading " + fileName);
}
return blocks;
}
//edit entry function
public boolean editEntry(ArrayList<Block> blocks, String blockName, Block updatedBlock, String fileName)
{
System.out.println("Blocks before update: " + blocks); // Debugging
for (int i = 0; i < blocks.size(); i++)
{
Block block = blocks.get(i);
if (block.getName().equals(blockName))
{
blocks.set(i, updatedBlock);
saveBlocksToFile(blocks, fileName);
System.out.println("Blocks after update: " + blocks); // Debugging
return true;
}
}
System.out.println("Block not found: " + blockName); // Debugging
return false; // Return false if block is not found
}
//Remove entry function
public boolean removeEntry(ArrayList<Block> blocks, String blockName, String fileName)
{
boolean blockRemoved = blocks.removeIf(block -> block.getName().equals(blockName));
if (blockRemoved) {
saveBlocksToFile(blocks, fileName); // Save to file after removing
}
return blockRemoved;
}
//Add entry function
public boolean addEntry(ArrayList<Block> blocks, Block newBlock, String fileName)
{
for (Block block : blocks)
{
if (block.getName().equals(newBlock.getName()))
{
return false; // Block with the same name already exists
}
}
blocks.add(newBlock);
saveBlocksToFile(blocks, fileName); // Save to file after adding the new block
return true; // Block added successfully
}
public void saveBlocksToFile(ArrayList<Block> blocks, String fileName)
{
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)))
{
for (Block block : blocks)
{
writer.write(block.toCsvString());
writer.newLine();
}
}
catch (IOException iox)
{
System.out.println("Problem Writing " + fileName);
}
}
public Block getBlockByName(ArrayList<Block> blocks, String blockName)
{
for (Block block : blocks)
{
if (block.getName().equalsIgnoreCase(blockName))
{
return block;
}
}
return null;
}
}