-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inventory.java
118 lines (106 loc) · 2.91 KB
/
Inventory.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
package assign1Package;
import java.io.*;
import java.util.*;
/**
* Inventory is an object that holds an array of movies
* @author Dylan Wagner
* @author Ethan Stewart
*
*/
public class Inventory implements Serializable{
public ArrayList<Product> inventory = new ArrayList<Product>();
/**
* Creates an inventory array from a file. If no file exists then
* the array is empty
*/
public Inventory()
{
try {
FileInputStream fis = new FileInputStream("InventoryFile");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Product> products =
(ArrayList<Product>)ois.readObject(); // explicit cast reqd
fis.close();
this.inventory = products;
} catch (FileNotFoundException e) {
System.out.println("Cannot find datafile.");
} catch (IOException e) {
System.out.println("Problem with file input.");
} catch (ClassNotFoundException e) {
System.out.println("Class not found on input from file.");
}
}
/**
* Adds movie to the array.Also checks for duplicate
* @param e The movie to add
*/
public void add(Product p) {
int index = -1; //numeric value that updates if sku is found,
//index = -1 means index not found
for (Product s : inventory) {
if (s.getSku() == p.getSku()) {
index = inventory.indexOf(s);
break;
}
}
if (index == -1)
inventory.add(p);
else
System.out.println("A movie with that SKU already exists.");
}
/**
* Removes movie from array if the sku exists
* @param sku The sku of the movie to be removed
*/
public void remove(int sku) {
int index = -1;//numeric value that updates if sku is found,
//index = -1 means index not found
for (Product p : inventory) {
if (p.getSku() == sku) {
index = inventory.indexOf(p);
break;
}
}
if (index == -1)
System.out.println("No match found in the inventory, sorry.");
else {
inventory.remove(index);
System.out.println("Movie successfully removed from inventory.");
}
}
/**
* Displays info for a specific movie
* @param sku The sku to search for.
*/
public void displayInfo(int sku) {
for (Product p : inventory) {
if (p.getSku() == sku) {
p.display();
break;
}
}
}
/**
* Displays all of the movies in a table form
*/
public void displayTable() {
System.out.println("Inventory Table:\n");
for (Product p : inventory) {
p.displayt();
}
}
/**
* Saves the inventory array to a file so it can be reused.
*/
public void save()
{
try {
FileOutputStream fos = new FileOutputStream("InventoryFile");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(inventory); //ArrayList and contents are serializable
fos.close();
} catch (IOException e) {
System.out.println("Problem with file output");
}
}
}