-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionController.java
More file actions
134 lines (124 loc) · 4.35 KB
/
TransactionController.java
File metadata and controls
134 lines (124 loc) · 4.35 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.*;
import java.text.SimpleDateFormat;
public class TransactionController
{
private ArrayList<Transaction> transactionList;
/**
* A default constructor for objects of class TransactionController.
*/
public TransactionController()
{
transactionList = new ArrayList<Transaction>();
}
/**
* A constructor for objects of class TransactionController.
*/
public TransactionController(ArrayList<Transaction> newTransactionList)
{
transactionList = newTransactionList;
}
/**
* Create a customerViewTransaction method to display the transactions.
*/
public void customerViewTransaction(String userEmail)
{
for (int index = 0; index < transactionList.size(); index++)
{
if (transactionList.get(index).getUserEmail().equals(userEmail))
{
transactionList.get(index).displayTransaction();
System.out.println("---------------------------------------------------------------------------------------");
}
}
}
/**
* Create a generateId method to get generate transaction id.
*/
private String generateId()
{
String id = new String();
if (transactionList.size() == 0)
id = "T1";
else if (transactionList.size() > 0)
{
String previousId = transactionList.get(transactionList.size() - 1).getTransactionId();
id = "T" + (Integer.parseInt(previousId.substring(1)) + 1);
}
return id;
}
/**
* Create a generateTransaction method to get generate TransactionList detailed.
*/
public void generateTransaction(String userEmail, ArrayList<String[]> purchaseList, double totalPrice)
{
Transaction transaction = new Transaction();
transaction.setUserEmail(userEmail);
transaction.setTransactionId(generateId());
transaction.setPurchaseList(purchaseList);
transaction.setTotalPrice(totalPrice);
transaction.displayTransaction();
transactionList.add(transaction);
}
/**
* Create a getTransactionList method to get TransactionList.
*/
public ArrayList<Transaction> getTransactionList()
{
return transactionList;
}
/**
* Create a initialTransactionList method to initial Transaction List.
*/
public void initialTransactionList(ArrayList<String> detailList)
{
int size = detailList.size();
for(int i = 0;i < size;i++)
{
String[] transactionDetails = detailList.get(i).split(",");
String userEmail = transactionDetails[0];
String transactionId = transactionDetails[1];
Calendar date = strToCal(transactionDetails[2]);
double price = Double.parseDouble(transactionDetails[3]);
int length = transactionDetails.length - 4;
int currentIndex = 4;
ArrayList<String[]> purchaseList = new ArrayList<String[]>();
while (currentIndex < length)
{
String[] purchase = new String[]{transactionDetails[currentIndex],transactionDetails[currentIndex + 1],transactionDetails[currentIndex + 2],
transactionDetails[currentIndex + 3],transactionDetails[currentIndex + 4],transactionDetails[currentIndex + 5]};
purchaseList.add(purchase);
currentIndex += 6;
}
transactionList.add(new Transaction(userEmail, transactionId, purchaseList, date, price));
}
}
/**
* Create a setTransactionList method to set TransactionList.
*/
public void setTransactionList(ArrayList<Transaction> newTransactionList)
{
transactionList = newTransactionList;
}
/**
* Create a strToCal method to get the date.
*/
private Calendar strToCal(String str)
{
Date date = new Date();
SimpleDateFormat sdf= new SimpleDateFormat("hh:mm:ss dd/MM/yyyy");
try
{
date = sdf.parse(str);
}
catch(Exception e)
{
System.out.println("Calendar Exception.");
}
finally
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
}
}