-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShop.java
97 lines (82 loc) · 2.55 KB
/
Shop.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
import java.io.*;
import java.util.*;
import java.awt.*;
import java.lang.String;
/**
* This class stores tool objects of type Tool, for that it uses a
* method to retrieve the tools from an external txt file.
*
* @author (Gabriel Fioreze)
* @version (v2)
*/
public class Shop
{
// array toolList of type Tool
private ArrayList<Tool> toolList;
/**
* Constructor for objects of class Shop
*/
public Shop()
{
//initialise instance variables
toolList = new ArrayList<Tool>();
}
// adds a tool to toolList. This takes a tool object as a parameter
public void storeTool(Tool tool)
{
toolList.add(tool);
}
// the method uses FileDialog object to open a txt.file
public void readToolData()
{
//create a FileDialog object save to a variable
Frame myFrame = null;
FileDialog fileBox = new FileDialog(myFrame,
"Open", FileDialog.LOAD);
//fileBox.setDirectory("/Documents/Java/Projects/Tool Hire/Tool Hire Part 1 Step 2");
fileBox.setDirectory(".");
//make it visible
fileBox.setVisible(true);
//Create a new File Object
File file = new File(fileBox.getDirectory() + fileBox.getFile());
//create object from that file name and pass it to the scanner
//scan the entire line
Scanner scanner = null;
try
{
scanner = new Scanner(file);
//while there is more data in the file
}
catch(Exception e){
System.out.println(e.getMessage());
}
while (scanner.hasNextLine()) {
//read line into variable lineOfText
String lineOfText = scanner.nextLine();
// Condition (Cannot be a blank line or have
//double forward slashes
if (!lineOfText.isEmpty() && !lineOfText.startsWith("//"))
{
lineOfText = lineOfText.trim();
// Create a new scanner passing lineOfText
Scanner scanner2 = new Scanner(lineOfText);
// Read data for each of tool's fields
scanner2.useDelimiter(",");
Tool tool = new Tool();
tool.readData(scanner2);
//store object above in an array
toolList.add(tool);
}
}
scanner.close();
}
public void printAllTools()
{
for (Tool tool : toolList)
{
//
//System.out.println(tool);
tool.printDetails();
}
}
}