如果在满足条件的情况下仍未执行声明(按钮上的所有其他按钮都有效)

时间:2019-04-05 10:54:09

标签: java android sqlite

我目前正在为我的A Level计算机科学控制评估工作开发一名Scout Manager。我正在使用Android Studio,Java,XML和MySQL,但以前都没有使用过。

我目前正在尝试计算活动/夏令营的食品估计成本,以及保存用餐计划,但是尽管满足if语句的所有条件,但仍未执行。

以下是声明:

//Calculating the cost of all food, as well as creating records for the food available on the event.
        double TotalFoodCost=0;
        if (providingfood==true)
        {
            if (sessiontype=="Event"){
                Cursor FoodItem = DatabaseHandler.getRandomLunch();
                //Ensures that the necessary meal is actually in the database
                if (FoodItem.moveToFirst()) {
                    //Gets a random food, works out the cost for the number of people.
                    Integer foodID = FoodItem.getInt(FoodItem.getColumnIndex(
                            DatabaseHandler.COLUMN_MNAME));
                    Integer mealNo=1;
                    double FoodPricePerPerson = FoodItem.getDouble(FoodItem.getColumnIndex(
                            DatabaseHandler.COLUMN_FCOST));
                    Integer eventmealid = createEventMealId();
                    //Creates a record containing the meal and event ids
                    eventMealDetails eventmeal= new eventMealDetails(eventmealid, id, foodID,
                            1);
                    DatabaseHandler.createEventMeal(eventmeal);
                    //Rounds the food price per person, as the database will not always store decimals correctly.
                    BigDecimal bigdecimalfoodpriceperperson=new BigDecimal(FoodPricePerPerson);
                    BigDecimal bigdecimalproperfoodpriceperperson=((bigdecimalfoodpriceperperson.setScale(2,bigdecimalfoodpriceperperson.ROUND_DOWN)));
                    double properfoodpriceperperson=bigdecimalproperfoodpriceperperson.doubleValue();
                    TotalFoodCost = TotalFoodCost+(properfoodpriceperperson*noOfPeople);
                }
                else
                {
                    Toast.makeText(
                            context,
                            "Error - you have not added any lunches, therefore food " +
                                    "cannot be added.",
                            Toast.LENGTH_SHORT
                    ).show();
                    return;
                }
            }
            else if (sessiontype=="Camp"){
                //Getting necessary variables ready
                Cursor FirstItem;
                Integer foodID;
                Integer mealNo;
                double FoodPricePerPerson;
                Integer eventmealid;
                eventMealDetails eventmeal;
                int noofdaysleft=noofdays;

                //First meal
                FirstItem=DatabaseHandler.getRandomDinner();
                //Ensures that the necessary meal is actually in the database
                if (FirstItem.moveToFirst()) {
                    //Gets a random food, works out the cost for the number of people.
                    foodID = FirstItem.getInt(FirstItem.getColumnIndex(
                            DatabaseHandler.COLUMN_MNAME));
                    mealNo = 1;
                    FoodPricePerPerson = FirstItem.getDouble(FirstItem.getColumnIndex(
                            DatabaseHandler.COLUMN_FCOST));
                    //Creates a record containing the meal and event ids
                    eventmealid = createEventMealId();
                    eventmeal = new eventMealDetails(eventmealid, id, foodID, mealNo);
                    DatabaseHandler.createEventMeal(eventmeal);
                    //Rounds the food price per person, as the database will not always store decimals correctly.
                    BigDecimal bigdecimalfoodpriceperperson=new BigDecimal(FoodPricePerPerson);
                    BigDecimal bigdecimalproperfoodpriceperperson=((bigdecimalfoodpriceperperson.setScale(2,bigdecimalfoodpriceperperson.ROUND_DOWN)));
                    double properfoodpriceperperson=bigdecimalproperfoodpriceperperson.doubleValue();
                    TotalFoodCost = TotalFoodCost+(properfoodpriceperperson * noOfPeople);
                }
                else
                {
                    Toast.makeText(
                            context,
                            "Error - you have not added any dinners, therefore food " +
                                    "cannot be added.",
                            Toast.LENGTH_SHORT
                    ).show();
                    return;
                }

                //Rest of meals
                do{
                    Cursor breakfastItem=DatabaseHandler.getRandomBreakfast();
                    //Ensures that the necessary meal is actually in the database
                    if (breakfastItem.moveToFirst()) {
                        //Gets a random food, works out the cost for the number of people.
                        foodID = breakfastItem.getInt(breakfastItem.getColumnIndex(
                                DatabaseHandler.COLUMN_MNAME));
                        mealNo = mealNo + 1;
                        FoodPricePerPerson = breakfastItem.getDouble(breakfastItem.getColumnIndex(
                                DatabaseHandler.COLUMN_FCOST));
                        //Creates a record containing the meal and event ids
                        eventmealid = createEventMealId();
                        eventmeal = new eventMealDetails(eventmealid, id, foodID, mealNo);
                        DatabaseHandler.createEventMeal(eventmeal);
                        //Rounds the food price per person, as the database will not always store decimals correctly.
                        BigDecimal bigdecimalfoodpriceperperson=new BigDecimal(FoodPricePerPerson);
                        BigDecimal bigdecimalproperfoodpriceperperson=((bigdecimalfoodpriceperperson.setScale(2,bigdecimalfoodpriceperperson.ROUND_DOWN)));
                        double properfoodpriceperperson=bigdecimalproperfoodpriceperperson.doubleValue();
                        TotalFoodCost = TotalFoodCost + (properfoodpriceperperson * noOfPeople);
                    }
                    else
                    {
                        Toast.makeText(
                                context,
                                "Error - you have not added any breakfasts, therefore food " +
                                        "cannot be added.",
                                Toast.LENGTH_SHORT
                        ).show();
                        return;
                    }

                    Cursor lunchItem=DatabaseHandler.getRandomLunch();
                    //Ensures that the necessary meal is actually in the database
                    if (lunchItem.moveToFirst()) {
                        //Gets a random food, works out the cost for the number of people.
                        lunchItem = DatabaseHandler.getRandomDinner();
                        foodID = lunchItem.getInt(lunchItem.getColumnIndex(
                                DatabaseHandler.COLUMN_MNAME));
                        mealNo = mealNo + 1;
                        FoodPricePerPerson = lunchItem.getDouble(lunchItem.getColumnIndex(
                                DatabaseHandler.COLUMN_FCOST));
                        //Creates a record containing the meal and event ids
                        eventmealid = createEventMealId();
                        eventmeal = new eventMealDetails(eventmealid, id, foodID, mealNo);
                        DatabaseHandler.createEventMeal(eventmeal);
                        //Rounds the food price per person, as the database will not always store decimals correctly.
                        BigDecimal bigdecimalfoodpriceperperson=new BigDecimal(FoodPricePerPerson);
                        BigDecimal bigdecimalproperfoodpriceperperson=((bigdecimalfoodpriceperperson.setScale(2,bigdecimalfoodpriceperperson.ROUND_DOWN)));
                        double properfoodpriceperperson=bigdecimalproperfoodpriceperperson.doubleValue();
                        TotalFoodCost = TotalFoodCost + (properfoodpriceperperson * noOfPeople);
                    }
                    else
                    {
                        Toast.makeText(
                                context,
                                "Error - you have not added any lunches, therefore food " +
                                        "cannot be added.",
                                Toast.LENGTH_SHORT
                        ).show();
                        return;
                    }

                    Cursor dinnerItem=DatabaseHandler.getRandomDinner();
                    //Ensures that the necessary meal is actually in the database
                    if (breakfastItem.moveToFirst()) {
                        //Gets a random food, works out the cost for the number of people.
                        foodID = dinnerItem.getInt(dinnerItem.getColumnIndex(
                                DatabaseHandler.COLUMN_MNAME));
                        mealNo = mealNo + 1;
                        FoodPricePerPerson = dinnerItem.getDouble(dinnerItem.getColumnIndex(
                                DatabaseHandler.COLUMN_FCOST));
                        //Creates a record containing the meal and event ids
                        eventmealid = createEventMealId();
                        eventmeal = new eventMealDetails(eventmealid, id, foodID, mealNo);
                        DatabaseHandler.createEventMeal(eventmeal);
                        //Rounds the food price per person, as the database will not always store decimals correctly.
                        BigDecimal bigdecimalfoodpriceperperson=new BigDecimal(FoodPricePerPerson);
                        BigDecimal bigdecimalproperfoodpriceperperson=((bigdecimalfoodpriceperperson.setScale(2,bigdecimalfoodpriceperperson.ROUND_DOWN)));
                        double properfoodpriceperperson=bigdecimalproperfoodpriceperperson.doubleValue();
                        TotalFoodCost = TotalFoodCost + (properfoodpriceperperson * noOfPeople);
                    }
                    else
                    {
                        Toast.makeText(
                                context,
                                "Error - you have not added any dinners, therefore food " +
                                        "cannot be added.",
                                Toast.LENGTH_SHORT
                        ).show();
                        return;
                    }

                    noofdaysleft=noofdaysleft-1;

                }while (noofdaysleft>0);

                //Last Meal
                Cursor breakfastItem=DatabaseHandler.getRandomBreakfast();
                if (breakfastItem.moveToFirst()) {
                    //Gets a random food, works out the cost for the number of people.
                    foodID = breakfastItem.getInt(breakfastItem.getColumnIndex(
                            DatabaseHandler.COLUMN_MNAME));
                    mealNo = mealNo + 1;
                    FoodPricePerPerson = breakfastItem.getDouble(breakfastItem.getColumnIndex(
                            DatabaseHandler.COLUMN_FCOST));
                    //Creates a record containing the meal and event ids
                    eventmealid = createEventMealId();
                    eventmeal = new eventMealDetails(eventmealid, id, foodID, mealNo);
                    DatabaseHandler.createEventMeal(eventmeal);
                    //Rounds the food price per person, as the database will not always store decimals correctly.
                    BigDecimal bigdecimalfoodpriceperperson=new BigDecimal(FoodPricePerPerson);
                    BigDecimal bigdecimalproperfoodpriceperperson=((bigdecimalfoodpriceperperson.setScale(2,bigdecimalfoodpriceperperson.ROUND_DOWN)));
                    double properfoodpriceperperson=bigdecimalproperfoodpriceperperson.doubleValue();
                    TotalFoodCost = TotalFoodCost + (properfoodpriceperperson * noOfPeople);
                }
                else
                {
                    Toast.makeText(
                            context,
                            "Error - you have not added any breakfasts, therefore food " +
                                    "cannot be added.",
                            Toast.LENGTH_SHORT
                    ).show();
                    return;
                }
            }
        }

我确保提供的食物是真实的,已经将Eventtype和Camp设置为sessiontype,但仍然没有任何反应-甚至没有错误消息,或者吐司说什么也找不到。

任何帮助将不胜感激。另外,我对这些语言,此IDE和此站点还很陌生,因此,如果需要更多信息,请随时询问,以便我改善问题。

谢谢。

0 个答案:

没有答案