@@ -220,6 +220,31 @@ public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
220
220
221
221
binding .back .setOnClickListener (v -> onBackPressed ());
222
222
223
+ binding .tvReducequantity .setOnClickListener (v -> {
224
+ try {
225
+ int currentQuantity = Integer .parseInt (binding .edtQuantity .getText ().toString ());
226
+
227
+ if (currentQuantity > 0 ) {
228
+ currentQuantity --;
229
+ binding .edtQuantity .setText (String .valueOf (currentQuantity ));
230
+ }
231
+
232
+ } catch (NumberFormatException e ) {
233
+ binding .edtQuantity .setText ("0" );
234
+ }
235
+ });
236
+
237
+ binding .tvAddquantity .setOnClickListener (v -> {
238
+ try {
239
+ int currentQuantity = Integer .parseInt (binding .edtQuantity .getText ().toString ());
240
+ currentQuantity ++;
241
+ binding .edtQuantity .setText (String .valueOf (currentQuantity ));
242
+
243
+ } catch (NumberFormatException e ) {
244
+ binding .edtQuantity .setText ("1" );
245
+ }
246
+ });
247
+
223
248
DecimalFormatSymbols symbols = new DecimalFormatSymbols (Locale .getDefault ());
224
249
symbols .setGroupingSeparator ('.' );
225
250
decimalFormat = new DecimalFormat ("#,##0" , symbols );
@@ -328,6 +353,8 @@ private void setupTakePictureLauncher() {
328
353
binding .imageViewCaptured .setImageBitmap (capturedImageBitmap );
329
354
binding .imageViewCaptured .setVisibility (View .VISIBLE );
330
355
binding .cameraPreview .setVisibility (View .GONE );
356
+ binding .edtQuantity .setText ("1" );
357
+
331
358
currentModelIndex = 0 ;
332
359
analyzeImageWithGemini (capturedImageBitmap );
333
360
}
@@ -748,43 +775,89 @@ private android.hardware.Camera.Size getOptimalPreviewSize(List<android.hardware
748
775
749
776
return optimalSize ;
750
777
}
778
+ private String addDiscountData () {
779
+ JSONArray discountsJsonArray = new JSONArray ();
780
+ double sumOfDiscounts = 0.0 ;
781
+
782
+ for (int i = 0 ; i < discountAdapter .getItemCount () - 1 ; i ++) {
783
+ String discountStr = discountAdapter .getDiscountAt (i );
784
+ if (!discountStr .trim ().isEmpty ()) {
785
+ try {
786
+ double discountValue = Double .parseDouble (discountStr );
787
+ if (discountValue < 0 || discountValue > 100 ) {
788
+ Toast .makeText (this , "Discount must be between 0 and 100." , Toast .LENGTH_SHORT ).show ();
789
+ return null ;
790
+ }
791
+ discountsJsonArray .put (discountValue );
792
+ sumOfDiscounts += discountValue ;
793
+ } catch (NumberFormatException | JSONException e ) {
794
+ Toast .makeText (this , "Invalid discount value: " + discountStr , Toast .LENGTH_SHORT ).show ();
795
+ return null ;
796
+ }
797
+ }
798
+ }
751
799
800
+ if (sumOfDiscounts >= 100.0 ) {
801
+ Toast .makeText (this , "Total discount cannot exceed 100%. Adjusting discounts." , Toast .LENGTH_SHORT ).show ();
802
+ return null ;
803
+ }
804
+
805
+ return discountsJsonArray .toString ();
806
+ }
752
807
private void addItemToDatabase () {
753
808
String name = binding .edtTitle .getText ().toString ();
754
809
String description = binding .edtDesc .getText ().toString ();
755
810
String category = binding .edtCategory .getText ().toString ();
756
- int count = 1 ;
811
+
812
+ int count ;
813
+ try {
814
+ count = Integer .parseInt (binding .edtQuantity .getText ().toString ());
815
+ } catch (NumberFormatException e ) {
816
+ Toast .makeText (this , "Invalid quantity amount." , Toast .LENGTH_SHORT ).show ();
817
+ return ;
818
+ }
819
+ if (count < 1 ) {
820
+ Toast .makeText (this , "Minimum quantity must be 1 to save the item." , Toast .LENGTH_SHORT ).show ();
821
+ return ;
822
+ }
757
823
758
824
double price = 0.0 ;
759
825
try {
760
826
String priceString = binding .edtPrice .getText ().toString ();
761
827
String cleanPriceString = priceString .replaceAll ("[.]" , "" );
762
- price = Double .parseDouble (cleanPriceString );
828
+ if (cleanPriceString .isEmpty ()) {
829
+ price = 0.0 ;
830
+ } else {
831
+ price = Double .parseDouble (cleanPriceString );
832
+ }
763
833
} catch (NumberFormatException e ) {
764
- Toast .makeText (this , "Masukkan harga yang valid." , Toast .LENGTH_SHORT ).show ();
834
+ Toast .makeText (this , "Please enter a valid price ." , Toast .LENGTH_SHORT ).show ();
765
835
return ;
766
836
}
837
+ double totalBasePrice = price * count ;
767
838
768
- JSONArray discountsJsonArray = new JSONArray ();
839
+ String discountsJson = addDiscountData ();
840
+ if (discountsJson == null ) {
841
+ return ;
842
+ }
843
+
844
+ JSONArray discountsJsonArray ;
769
845
double sumOfDiscounts = 0.0 ;
770
- for (String discountStr : discountItems ) {
771
- if (!discountStr .trim ().isEmpty ()) {
772
- try {
773
- double discountValue = Double .parseDouble (discountStr );
774
- discountsJsonArray .put (discountValue );
775
- sumOfDiscounts += discountValue ;
776
- } catch (NumberFormatException | JSONException e ) {
777
- Toast .makeText (this , "Nilai diskon tidak valid: " + discountStr , Toast .LENGTH_SHORT ).show ();
778
- return ;
779
- }
846
+ try {
847
+ discountsJsonArray = new JSONArray (discountsJson );
848
+ for (int i = 0 ; i < discountsJsonArray .length (); i ++) {
849
+ sumOfDiscounts += discountsJsonArray .getDouble (i );
780
850
}
851
+ } catch (JSONException e ) {
852
+ Toast .makeText (this , "An error occurred while processing discounts." , Toast .LENGTH_SHORT ).show ();
853
+ return ;
781
854
}
782
855
783
856
double finalPrice ;
784
857
if (sumOfDiscounts >= 100.0 ) {
785
858
finalPrice = 0.0 ;
786
859
} else {
787
- finalPrice = calculateFinalPrice (price , discountsJsonArray );
860
+ finalPrice = calculateFinalPrice (totalBasePrice , discountsJsonArray );
788
861
}
789
862
790
863
String date = new SimpleDateFormat ("yyyy-MM-dd" , Locale .getDefault ()).format (new Date ());
@@ -800,19 +873,19 @@ private void addItemToDatabase() {
800
873
newItem .setCategory (category );
801
874
newItem .setImageData (imageData );
802
875
newItem .setPrice (price );
803
- newItem .setDiscountsJson (discountsJsonArray .toString ());
804
876
newItem .setFinalPrice (finalPrice );
805
877
newItem .setItemListId (item_list_id );
806
878
newItem .setParentListId (parentListIdFromIntent );
807
879
newItem .setTotalDiscountPercentage (sumOfDiscounts );
880
+ newItem .setDiscountsJson (discountsJson );
808
881
809
882
long newRowId = dbHelper .addItem (newItem );
810
883
811
884
if (newRowId != -1 ) {
812
- Toast .makeText (this , "Item berhasil ditambahkan !" , Toast .LENGTH_SHORT ).show ();
885
+ Toast .makeText (this , "Item added successfully !" , Toast .LENGTH_SHORT ).show ();
813
886
finish ();
814
887
} else {
815
- Toast .makeText (this , "Gagal menambahkan item." , Toast .LENGTH_SHORT ).show ();
888
+ Toast .makeText (this , "Failed to add item." , Toast .LENGTH_SHORT ).show ();
816
889
}
817
890
}
818
891
0 commit comments