Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / mensaplan / MensaTagesplan.java
1 /* MensaTagesplan.java
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 package de.dhbwloe.campusapp.mensaplan;
17 import java.text.DateFormat;
18 import java.text.DecimalFormat;
19 import java.text.ParseException;
20 import java.text.SimpleDateFormat;
21 import java.util.Date;
22 import java.util.Locale;
23 import java.util.zip.CRC32;
24
25 import de.dhbwloe.campusapp.CampusAppContext;
26 import de.dhbwloe.campusapp.R;
27
28 /**
29  * Created by pk910 on 22.01.2016.
30  */
31 public class MensaTagesplan {
32     private long iPlanDate;
33     private long iChkSum;
34     private String sMenuName, sName, sNameHtml, sAdditional, sNotes;
35     private int[] aPriceArray = new int[4];
36
37     private boolean bIsNew = false;
38
39     public MensaTagesplan(long plandate, String menuname, long chksum, String name, String namehtml, String additional, String notes, int price1, int price2, int price3, int price4) {
40         iPlanDate = plandate;
41         sMenuName = menuname;
42         iChkSum = chksum;
43         sName = name;
44         sNameHtml = namehtml;
45         sAdditional = (additional == null ? "" : additional);
46         sNotes = notes;
47         aPriceArray[0] = price1; // Student
48         aPriceArray[1] = price2; // Employees
49         aPriceArray[2] = price3; // Guest
50         aPriceArray[3] = price4; // School
51     }
52
53     public long calculateChkSum() {
54         CRC32 crc = new CRC32();
55         crc.update(sMenuName.getBytes());
56         if(sName != null)
57             crc.update(sName.getBytes());
58         if(sNameHtml != null)
59             crc.update(sNameHtml.getBytes());
60         if(sAdditional!= null)
61             crc.update(sAdditional.getBytes());
62         if(sNotes!= null)
63             crc.update(sNotes.getBytes());
64         crc.update(aPriceArray[0]);
65         crc.update(aPriceArray[1]);
66         crc.update(aPriceArray[2]);
67         crc.update(aPriceArray[3]);
68
69         long crcvalue = crc.getValue();
70         iChkSum = crcvalue;
71         return crcvalue;
72     }
73
74     public long getPlanDate() {
75         return iPlanDate;
76     }
77
78     public String getFormatedDate() {
79         DateFormat df = new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH);
80         return df.format(new Date(iPlanDate * 1000));
81     }
82
83     public long getChkSum() {
84         return iChkSum;
85     }
86
87     public String getMenuName() {
88         return sMenuName;
89     }
90
91     public String getName() {
92         return sName;
93     }
94
95     public String getNameHtml() {
96         return sNameHtml;
97     }
98
99     public String getAdditional() {
100         return sAdditional;
101     }
102
103     public String getNotes() {
104         return sNotes;
105     }
106
107     public int[] getPlainPrice() {
108         return aPriceArray;
109     }
110
111     public double getStudentPrice() {
112         double price = (aPriceArray[0] / 100.0);
113         return price;
114     }
115
116     public double getEmployeePrice() {
117         double price = (aPriceArray[1] / 100.0);
118         return price;
119     }
120
121     public double getGuestPrice() {
122         double price = (aPriceArray[2] / 100.0);
123         return price;
124     }
125
126     public double getSchoolPrice() {
127         double price = (aPriceArray[3] / 100.0);
128         return price;
129     }
130
131     public String getFormatedRolePrice() {
132         String mensaRoleName = CampusAppContext.getInstance().getDatabaseManager().getRuntimeCache("MensaRole");
133         if (mensaRoleName == null || mensaRoleName.isEmpty())
134             mensaRoleName = "0";
135         int mensaRole = Integer.parseInt(mensaRoleName);
136         double price = (aPriceArray[mensaRole] / 100.0);
137
138         DecimalFormat df = new DecimalFormat(CampusAppContext.getInstance().getResString(R.string.mensaformat_price));
139         String formatedPrice = df.format(price);
140         return formatedPrice;
141     }
142
143     public void setIsNew() {
144         bIsNew = true;
145     }
146
147     public boolean getIsNew(boolean reset) {
148         boolean isnew = bIsNew;
149         if(reset)
150             bIsNew = false;
151         return isnew;
152     }
153
154 }