请理解,我绝对没有错误.但我的问题是,我不明白如何划分这个百分比问题.所以,我有一个叫Bill的类,每个账单都有自己的价格.(我的应用程序是一个支出跟踪器).因此,在我的观点中,我想知道如何衡量每个类别的百分比,因 for each 钞票都有自己的类别.有4个类别--

  1. 杂货
  2. 旅行
  3. 贷款/票据/其他
  4. 购物

每一个加起来都是一个价格,我不知道怎么追踪 每一张都进入饼图,满分100%.

    package com.functionality.mylifehelper.drawerUI.ui.expenditure;

import java.util.ArrayList;
import java.util.List;

public class ExpensesManager {
List<Bill> bills;
Earning obj;

/**
 * Prices
 */
int allPrices;

int pricesFor杂货;
int pricesForLoansEtc;
int pricesFor旅行Etc;
int pricesFor购物;

int percentage杂货;
int percentageLoans;
int percentage旅行;
int percentage购物;

int total = 100;
List<Bill> TYPE_1, TYPE_2, TYPE_3, TYPE_4;
ExpensesManager(List<Bill> expenses, Earning earning) {
    bills=expenses;
    obj=earning;
    calculateCost();
}

private void calculateCost() {
    List<Bill> groceries = new ArrayList<>(), loans = new ArrayList<>(), trips
            = new ArrayList<>(), shopping = new ArrayList<>();
    for(Bill bill : bills) {
        if(bill.getType().equals(Bill.food)) {
            groceries.add(bill);
            pricesFor杂货 += bill.getAmount();
        } else if(bill.getType().equals(Bill.loans)) {
            pricesForLoansEtc += bill.getAmount();
            loans.add(bill);
        } else if(bill.getType().equals(Bill.shopping)) {
            pricesFor购物 += bill.getAmount();
            shopping.add(bill);
        } else {
            pricesFor旅行Etc += bill.getAmount();
            trips.add(bill);
        }
    }
    TYPE_1 = groceries;
    TYPE_2 = loans;
    TYPE_3 = trips;
    TYPE_4 = shopping;

    total = bills.size();

    // tasks : 10

    // say there are 90 tasks
    // for e.g only - >
    // NOW, YOU SAY THAT 50 are groceries, 10 loans etc
    //
    // groceries = 90 / 50 = 45.000 something
    percentage杂货 = total / percentage杂货;
    percentageLoans = total / percentageLoans;

}

public int getAllItemsCost() {
    return pricesFor杂货+pricesFor旅行Etc+pricesFor购物+pricesForLoansEtc;
}

}

推荐答案

我会感谢你的 comments ,但这是最终奏效的方法:

    package com.functionality.mylifehelper.drawerUI.ui.expenditure;

import java.util.ArrayList;
import java.util.List;

public class ExpensesManager {
List<Bill> bills;
Earning obj;

/**
 * Prices
 */
int allPrices;

int pricesForGroceries;
int pricesForLoansEtc;
int pricesForTripsEtc;
int pricesForShopping;

int percentageGroceries;
int percentageLoans;
int percentageTrips;
int percentageShopping;

int total = 100;
List<Bill> TYPE_1, TYPE_2, TYPE_3, TYPE_4;
ExpensesManager(List<Bill> expenses, Earning earning) {
    bills=expenses;
    obj=earning;
    calculateCost();
}

public List<Bill> getBills() {
    return bills;
}

public Earning getObj() {
    return obj;
}

public int getAllPrices() {
    return allPrices;
}

public int getPricesForGroceries() {
    return pricesForGroceries;
}

public int getPricesForLoansEtc() {
    return pricesForLoansEtc;
}

public int getPricesForTripsEtc() {
    return pricesForTripsEtc;
}

public int getPricesForShopping() {
    return pricesForShopping;
}

public int getPercentageGroceries() {
    return percentageGroceries;
}

public int getPercentageLoans() {
    return percentageLoans;
}

public int getPercentageTrips() {
    return percentageTrips;
}

public int getPercentageShopping() {
    return percentageShopping;
}

public int getTotal() {
    return total;
}

public List<Bill> getTYPE_1() {
    return TYPE_1;
}

public List<Bill> getTYPE_2() {
    return TYPE_2;
}

public List<Bill> getTYPE_3() {
    return TYPE_3;
}

public List<Bill> getTYPE_4() {
    return TYPE_4;
}

private void calculateCost() {
    List<Bill> groceries = new ArrayList<>(), loans = new ArrayList<>(), trips
            = new ArrayList<>(), shopping = new ArrayList<>();
    for(Bill bill : bills) {
        if(bill.getType().equals(Bill.food)) {
            groceries.add(bill);
            pricesForGroceries += bill.getAmount();
        } else if(bill.getType().equals(Bill.loans)) {
            pricesForLoansEtc += bill.getAmount();
            loans.add(bill);
        } else if(bill.getType().equals(Bill.shopping)) {
            pricesForShopping += bill.getAmount();
            shopping.add(bill);
        } else {
            pricesForTripsEtc += bill.getAmount();
            trips.add(bill);
        }
    }
    TYPE_1 = groceries;
    TYPE_2 = loans;
    TYPE_3 = trips;
    TYPE_4 = shopping;

    total = bills.size();

    // tasks : 10

    // say there are 90 tasks
    // for e.g only - >
    // NOW, YOU SAY THAT 50 are groceries, 10 loans etc
    //
    // groceries = 90 / 50 = 45.000 something
    try {
        percentageGroceries = total / TYPE_1.size();
    } catch(Exception e) {
        percentageGroceries = 0;
    }
    try {
        percentageLoans = total / TYPE_2.size();
    } catch(Exception e) {
        percentageLoans = 0;
    }
    try {
        percentageShopping = total / TYPE_4.size();
    } catch(Exception e) {
        percentageShopping = 0;
    }
    try {
        percentageTrips = total / TYPE_3.size();
    } catch(Exception e) {
        percentageTrips = 0;
    }
}

public int getAllItemsCost() {
    return pricesForGroceries+pricesForTripsEtc+pricesForShopping+pricesForLoansEtc;
}

}

谢谢你的建议,@TheLibrary,但我在你之前就知道了,:)我要结束了,别忘了为你自己投票并使用它.

Android相关问答推荐

在Android中点击滑动时触发的手势

如何在"不同活动"中添加不同按钮?

AdMob:MobileAds. initialize()—java. lang. xml对于某些设备不能强制转换为java. lang. String""

了解数据加载在Kotlin中的工作原理

组成底部导航栏,自定义形状,周围透明

在Jetpack Compose中,我可以配置动画以恒定的速度而不是恒定的时间运行吗?

在 kotlin 上向适配器添加绑定视图功能

使用 List 和 LazyColumn 重新组合所有项目

任务:app:kaptGenerateStubsDebugKotlin执行失败. > 'compileDebugJavaWithJavac' 任务(当前目标是 1.8)

修复报错 RecyclerView: No adapter attached;跳过布局

Android - 如何使 TextInputEditText 的高度恰好为 2 行?

使用 settings.gradle 文件将 Firebase 依赖项添加到 Android 项目

Jetpack Compose 动画的行为是什么?

Jetpack Compose 惰性列在单个项目更新时重新组合所有项目

如何正确地将图像上传到 Jetpack Compose 中的 LazyList 中的项目?

是否可以在 Kotlin 中为 mutableStateOf() 设置自定义设置器

如何放置在柱子的角落(底端)

更新后 Firebase 服务无法在模拟器上运行

compose FontFamily 错误:必须初始化变量

多个项目 react-native android 构建错误