CodeBlanks Logo Icon CodeBlanks

Toplam hesaplama

Ara toplam ve vergi dahil toplamı hesaplarsın.

Toplam hesaplama

Bu derste sepetteki ürünlerin fiyatlarını toplayacağız. Fiyatlar bir ArrayList<Double> listesinde tutulur; for-each ile toplam hesaplanır:

ArrayList<Double> prices = new ArrayList<>();
prices.add(10.0);
prices.add(20.0);
double subtotal = 0;
for (double price : prices) {
    subtotal += price;
}
System.out.println(subtotal);  // 30.0

subtotal += price her tekrarda price'ı subtotal'a ekler. Bu, liste toplamı için temel yöntemdir.

Vergi dahil toplam

Sepetin vergi dahil toplamı: subtotal + (subtotal * vergiOrani). Örneğin %10 vergi ile:

double subtotal = 30.0;
double tax = subtotal * 0.10;   // 3.0
double total = subtotal + tax; // 33.0
System.out.println("Toplam: " + total);

Burada tax vergi tutarı, total vergi dahil ödenecek tutardır. Vergi oranı 0.10 gibi ondalık yazılır; yüzde işareti kullanılmaz.

Uygulama Ara toplam ve vergi dahil toplam

Önceki dersin koduna ekle: ArrayList<Double> prices tanımla, 10.0 ve 20.0 ekle. double subtotal'ı for (double price : prices) ile hesapla. double tax = subtotal * 0.10, double total = subtotal + tax ata. println ile "Ara toplam: " + subtotal, "Vergi: " + tax, "Toplam: " + total yazdır.

Başlangıç kodu

Main.java
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> cart = new ArrayList<>();
        cart.add("Elma");
        cart.add("Ekmek");
        System.out.println(cart);
        System.out.println(cart.size());
        // Fiyat listesi ekle ve ara toplam ile vergi dahil toplamı hesapla
    }
}

Örnek çözüm

Main.java
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> cart = new ArrayList<>();
        cart.add("Elma");
        cart.add("Ekmek");
        System.out.println(cart);
        System.out.println(cart.size());
        ArrayList<Double> prices = new ArrayList<>();
        prices.add(10.0);
        prices.add(20.0);
        double subtotal = 0;
        for (double price : prices) {
            subtotal += price;
        }
        double tax = subtotal * 0.10;
        double total = subtotal + tax;
        System.out.println("Ara toplam: " + subtotal);
        System.out.println("Vergi: " + tax);
        System.out.println("Toplam: " + total);
    }
}

Beklenen çıktı

[Elma, Ekmek]
2
Ara toplam: 30.0
Vergi: 3.0
Toplam: 33.0

Soru ve cevaplar

Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.

1 prices = [10.0, 20.0] listesinin toplamı kaçtır?

  • 20.0
  • 10.0
  • 30.0 Doğru cevap
  • 200.0

Açıklama: 10 ve 20'nin toplamı 30'dur.

2 Toplam döngüsünü tamamla

Soru
double subtotal = 0;
for (double price : prices) {
    subtotal ____ price;
}
Cevap
double subtotal = 0;
for (double price : prices) {
    subtotal += price;
}

Açıklama: += her tekrarda price'ı subtotal'a ekler.

3 Toplam kavramlarını eşleştir

Kavram Anlamı
subtotal Ara toplam
tax Vergi
total Vergi dahil
+= Toplama atama

Başlangıç bölümündeki tüm dersler