-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTax_tariff_info.java
More file actions
195 lines (149 loc) · 7.64 KB
/
Copy pathTax_tariff_info.java
File metadata and controls
195 lines (149 loc) · 7.64 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.util.Scanner;
import java.io.IOException;
import java.io.*;
import java.util.*;
public class Tax_tariff_info {
public String meterType;
public int regularUnitPrice;
public Integer peakHourUnitPrice; // use Integer to handle null values
public int taxPercentage;
public int fixedCharges;
public static String filename;
Tax_tariff_info(String fn)
{
this.filename=fn;
}
Tax_tariff_info(){}
// Constructor
public Tax_tariff_info(String meterType, int regularUnitPrice, Integer peakHourUnitPrice,
int taxPercentage, int fixedCharges) {
this.meterType = meterType;
this.regularUnitPrice = regularUnitPrice;
this.peakHourUnitPrice = peakHourUnitPrice;
this.taxPercentage = taxPercentage;
this.fixedCharges = fixedCharges;
}
// Method to display the meter pricing info
public void displayInfo() {
System.out.println("Meter Type: " + meterType);
System.out.println("Regular Unit Price: " + regularUnitPrice);
System.out.println("Peak Hour Unit Price: " + (peakHourUnitPrice != null ? peakHourUnitPrice : "N/A"));
System.out.println("Tax Percentage: " + taxPercentage + "%");
System.out.println("Fixed Charges: " + fixedCharges);
System.out.println();
}
// Getters and Setters for updating fields
public String getMeterType() { return meterType; }
public void setMeterType(String meterType) { this.meterType = meterType; }
public int getRegularUnitPrice() { return regularUnitPrice; }
public void setRegularUnitPrice(int regularUnitPrice) { this.regularUnitPrice = regularUnitPrice; }
public Integer getPeakHourUnitPrice() { return peakHourUnitPrice; }
public void setPeakHourUnitPrice(Integer peakHourUnitPrice) { this.peakHourUnitPrice = peakHourUnitPrice; }
public int getTaxPercentage() { return taxPercentage; }
public void setTaxPercentage(int taxPercentage) { this.taxPercentage = taxPercentage; }
public int getFixedCharges() { return fixedCharges; }
public void setFixedCharges(int fixedCharges) { this.fixedCharges = fixedCharges; }
// Method to load pricing data from file
public static List<Tax_tariff_info> loadPricingData() {
List<Tax_tariff_info> pricingList = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
// Read file line by line
while ((line = reader.readLine()) != null) {
// Split the line by comma
String[] parts = line.split(",");
if (parts.length == 5)
{
String meterType = parts[0].trim();
int regularUnitPrice = Integer.parseInt(parts[1].trim());
// Handle blank value for peak hour unit price (assign null if it's blank)
Integer peakHourUnitPrice = parts[2].trim().isEmpty() ? null : Integer.parseInt(parts[2].trim());
int taxPercentage = Integer.parseInt(parts[3].trim());
int fixedCharges = Integer.parseInt(parts[4].trim());
// Create a MeterPricingInfo object and add to list
Tax_tariff_info pricingInfo = new Tax_tariff_info(meterType, regularUnitPrice, peakHourUnitPrice, taxPercentage, fixedCharges);
pricingList.add(pricingInfo);
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return pricingList;
}
//save file data
public static void savePricingData( List<Tax_tariff_info> pricingList) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
for (Tax_tariff_info info : pricingList) {
writer.write(info.getMeterType() + "," + info.getRegularUnitPrice() + "," +
(info.getPeakHourUnitPrice() != null ? info.getPeakHourUnitPrice() : "") + "," +
info.getTaxPercentage() + "," + info.getFixedCharges());
writer.newLine();
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void updateOrAddData(List<Tax_tariff_info> pricingList) {
Scanner scanner = new Scanner(System.in);
System.out.println("Would you like to update an existing entry or add a new one? (update/add): ");
String choice = scanner.nextLine().trim().toLowerCase();
if (choice.equals("update")) {
System.out.println("Enter meter type to update: ");
String meterType = scanner.nextLine().trim();
// Find the entry by meter type
for (Tax_tariff_info info : pricingList) {
if (info.getMeterType().equalsIgnoreCase(meterType)) {
System.out.println("Current data for " + meterType + ":");
info.displayInfo();
// Update values
System.out.println("Enter new Regular Unit Price: ");
int newRegularPrice = Integer.parseInt(scanner.nextLine().trim());
info.setRegularUnitPrice(newRegularPrice);
System.out.println("Enter new Peak Hour Unit Price (leave blank if N/A): ");
String peakInput = scanner.nextLine().trim();
Integer newPeakPrice = peakInput.isEmpty() ? null : Integer.parseInt(peakInput);
info.setPeakHourUnitPrice(newPeakPrice);
System.out.println("Enter new Tax Percentage: ");
int newTaxPercentage = Integer.parseInt(scanner.nextLine().trim());
info.setTaxPercentage(newTaxPercentage);
System.out.println("Enter new Fixed Charges: ");
int newFixedCharges = Integer.parseInt(scanner.nextLine().trim());
info.setFixedCharges(newFixedCharges);
savePricingData(pricingList);
System.out.println("Data updated successfully!");
return;
}
}
System.out.println("Meter type not found!");
} else if (choice.equals("add"))
{
if(pricingList.size()==4)
{
System.out.println("There are already four rows You should update them ");
return;
}
System.out.println("Enter new Meter Type: ");
String newMeterType = scanner.nextLine().trim();
System.out.println("Enter Regular Unit Price: ");
int newRegularPrice = Integer.parseInt(scanner.nextLine().trim());
System.out.println("Enter Peak Hour Unit Price (leave blank if N/A): ");
String peakInput = scanner.nextLine().trim();
Integer newPeakPrice = peakInput.isEmpty() ? null : Integer.parseInt(peakInput);
System.out.println("Enter Tax Percentage: ");
int newTaxPercentage = Integer.parseInt(scanner.nextLine().trim());
System.out.println("Enter Fixed Charges: ");
int newFixedCharges = Integer.parseInt(scanner.nextLine().trim());
// Add new entry to the list
Tax_tariff_info newInfo = new Tax_tariff_info(newMeterType, newRegularPrice, newPeakPrice, newTaxPercentage, newFixedCharges);
pricingList.add(newInfo);
savePricingData(pricingList);
System.out.println("New entry added successfully!");
} else {
System.out.println("Invalid choice! Please choose 'update' or 'add'.");
}
}
}