@@ -11,6 +11,12 @@ public class Tax_tariff_info {
1111 private Integer peakHourUnitPrice ; // use Integer to handle null values
1212 private int taxPercentage ;
1313 private int fixedCharges ;
14+ public static String filename ;
15+ Tax_tariff_info (String fn )
16+ {
17+ this .filename =fn ;
18+ }
19+ Tax_tariff_info (){}
1420
1521 // Constructor
1622 public Tax_tariff_info (String meterType , int regularUnitPrice , Integer peakHourUnitPrice ,
@@ -32,8 +38,26 @@ public void displayInfo() {
3238 System .out .println ();
3339 }
3440
41+ // Getters and Setters for updating fields
42+ public String getMeterType () { return meterType ; }
43+ public void setMeterType (String meterType ) { this .meterType = meterType ; }
44+
45+ public int getRegularUnitPrice () { return regularUnitPrice ; }
46+ public void setRegularUnitPrice (int regularUnitPrice ) { this .regularUnitPrice = regularUnitPrice ; }
47+
48+ public Integer getPeakHourUnitPrice () { return peakHourUnitPrice ; }
49+ public void setPeakHourUnitPrice (Integer peakHourUnitPrice ) { this .peakHourUnitPrice = peakHourUnitPrice ; }
50+
51+ public int getTaxPercentage () { return taxPercentage ; }
52+ public void setTaxPercentage (int taxPercentage ) { this .taxPercentage = taxPercentage ; }
53+
54+ public int getFixedCharges () { return fixedCharges ; }
55+ public void setFixedCharges (int fixedCharges ) { this .fixedCharges = fixedCharges ; }
56+
57+
58+
3559 // Method to load pricing data from file
36- public static List <Tax_tariff_info > loadPricingData (String filename ) {
60+ public static List <Tax_tariff_info > loadPricingData () {
3761 List <Tax_tariff_info > pricingList = new ArrayList <>();
3862 try {
3963 BufferedReader reader = new BufferedReader (new FileReader (filename ));
@@ -68,6 +92,100 @@ public static List<Tax_tariff_info> loadPricingData(String filename) {
6892 }
6993
7094
95+ //save file data
96+ public static void savePricingData ( List <Tax_tariff_info > pricingList ) {
97+ try {
98+ BufferedWriter writer = new BufferedWriter (new FileWriter (filename ));
99+
100+ for (Tax_tariff_info info : pricingList ) {
101+ writer .write (info .getMeterType () + "," + info .getRegularUnitPrice () + "," +
102+ (info .getPeakHourUnitPrice () != null ? info .getPeakHourUnitPrice () : "" ) + "," +
103+ info .getTaxPercentage () + "," + info .getFixedCharges ());
104+ writer .newLine ();
105+ }
106+ writer .close ();
107+ } catch (IOException e ) {
108+ e .printStackTrace ();
109+ }
110+ }
111+
112+
113+ public void updateOrAddData (List <Tax_tariff_info > pricingList ) {
114+ Scanner scanner = new Scanner (System .in );
115+
116+ System .out .println ("Would you like to update an existing entry or add a new one? (update/add): " );
117+ String choice = scanner .nextLine ().trim ().toLowerCase ();
118+
119+ if (choice .equals ("update" )) {
120+ System .out .println ("Enter meter type to update: " );
121+ String meterType = scanner .nextLine ().trim ();
122+
123+ // Find the entry by meter type
124+ for (Tax_tariff_info info : pricingList ) {
125+ if (info .getMeterType ().equalsIgnoreCase (meterType )) {
126+ System .out .println ("Current data for " + meterType + ":" );
127+ info .displayInfo ();
128+
129+ // Update values
130+ System .out .println ("Enter new Regular Unit Price: " );
131+ int newRegularPrice = Integer .parseInt (scanner .nextLine ().trim ());
132+ info .setRegularUnitPrice (newRegularPrice );
133+
134+ System .out .println ("Enter new Peak Hour Unit Price (leave blank if N/A): " );
135+ String peakInput = scanner .nextLine ().trim ();
136+ Integer newPeakPrice = peakInput .isEmpty () ? null : Integer .parseInt (peakInput );
137+ info .setPeakHourUnitPrice (newPeakPrice );
138+
139+ System .out .println ("Enter new Tax Percentage: " );
140+ int newTaxPercentage = Integer .parseInt (scanner .nextLine ().trim ());
141+ info .setTaxPercentage (newTaxPercentage );
142+
143+ System .out .println ("Enter new Fixed Charges: " );
144+ int newFixedCharges = Integer .parseInt (scanner .nextLine ().trim ());
145+ info .setFixedCharges (newFixedCharges );
146+ savePricingData (pricingList );
147+ System .out .println ("Data updated successfully!" );
148+ return ;
149+ }
150+ }
151+ System .out .println ("Meter type not found!" );
152+
153+ } else if (choice .equals ("add" ))
154+ {
155+ if (pricingList .size ()==4 )
156+ {
157+ System .out .println ("There are already four rows You should update them " );
158+ return ;
159+ }
160+ System .out .println ("Enter new Meter Type: " );
161+ String newMeterType = scanner .nextLine ().trim ();
162+
163+ System .out .println ("Enter Regular Unit Price: " );
164+ int newRegularPrice = Integer .parseInt (scanner .nextLine ().trim ());
165+
166+ System .out .println ("Enter Peak Hour Unit Price (leave blank if N/A): " );
167+ String peakInput = scanner .nextLine ().trim ();
168+ Integer newPeakPrice = peakInput .isEmpty () ? null : Integer .parseInt (peakInput );
169+
170+ System .out .println ("Enter Tax Percentage: " );
171+ int newTaxPercentage = Integer .parseInt (scanner .nextLine ().trim ());
172+
173+ System .out .println ("Enter Fixed Charges: " );
174+ int newFixedCharges = Integer .parseInt (scanner .nextLine ().trim ());
175+
176+ // Add new entry to the list
177+ Tax_tariff_info newInfo = new Tax_tariff_info (newMeterType , newRegularPrice , newPeakPrice , newTaxPercentage , newFixedCharges );
178+ pricingList .add (newInfo );
179+ savePricingData (pricingList );
180+ System .out .println ("New entry added successfully!" );
181+ } else {
182+ System .out .println ("Invalid choice! Please choose 'update' or 'add'." );
183+ }
184+ }
185+
186+
187+
188+
71189}
72190
73191
0 commit comments