Android Studio Java - 购物车应用程序,总计问题



对于我的uni任务,我必须调整一段代码,以创建一个应用程序,该应用程序将项目的总成本相加并在Java中显示它们,我对移动开发非常陌生。我们得到了除总计和显示成本之外的所有代码。我在顶部添加了一些变量和一个total()方法。

目前,当应用程序运行时,它将从项目中获取所有价格并根据需要添加/减去它们,但它的加减金额是错误的(所有加减 1.5 而不是创建对象时定义的价格(,我不确定为什么。有什么想法吗?

如果您需要其他课程的信息,请告诉我。

谢谢

编辑:我期望该应用程序发生的情况是它将有4行,每行包含与其中一个甜点对象关联的图像,文本说明它是哪种甜点,然后是一个按钮来增加和减少数量。我还想显示当前选择的所有甜点的总成本,例如,如果选择了 1 个甜甜圈的数量,选择了 2 个饼干的数量,则预期总数应显示为 3.88 英镑。每当添加或减少另一种甜点时,总数应更新

我收到的代码具有列表的功能以及递增和递减功能,因此我的任务是为对象添加价格,并使它们合计。

法典:

主要活动.java

package com.example.annascott.buttondemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// quantity of desert
int desertNumber;
Dessert currentDessert;
double totalAmount = 0;
// Create an ArrayList of Dessert objects
final ArrayList<Dessert> desserts = new ArrayList<Dessert>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//CREATE THE DESSERT OBJECTS
desserts.add(new Dessert("Donut", 0, R.drawable.doughnut, 1.99));
desserts.add(new Dessert("Cookie", 0, R.drawable.cookie, 0.99));
desserts.add(new Dessert("PieceOfCake", 0, R.drawable.piece_of_cake, 2.99));
desserts.add(new Dessert("Pastry", 0, R.drawable.pastry, 1.50));

// Create an {@link DessertAdapter}, whose data source is a list of
// {@link Dessert}s. The adapter knows how to create list item views for each item
// in the list.
DesertAdapter flavorAdapter = new DesertAdapter(this, desserts);
// Get a reference to the ListView, and attach the adapter to the listView.
ListView listView = (ListView) findViewById(R.id.listview_dessert);
listView.setAdapter(flavorAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch (i) {
case 0:
Intent donut = new Intent(MainActivity.this, Donut.class);
startActivity(donut);
break;
case 1:
Intent cookie = new Intent(MainActivity.this, Cookie.class);
startActivity(cookie);
break;
case 2:
Intent pieceOfCake = new Intent(MainActivity.this, PieceOfCake.class);
startActivity(pieceOfCake);
break;
case 3:
Intent pastry = new Intent(MainActivity.this, Pastry.class);
startActivity(pastry);
break;
}
}
});
}

public void Decrement(View view) {

LinearLayout parentRow = (LinearLayout) view.getParent();
TextView quantityView = (TextView) parentRow.findViewById(R.id.dessert_number);
String quantityString = quantityView.getText().toString();
desertNumber = Integer.parseInt(quantityString);
desertNumber -= 1;
if (desertNumber < 0) {
desertNumber = 0;
Toast.makeText(MainActivity.this, "Can not be less than 0",
Toast.LENGTH_SHORT).show();
}
quantityView.setText(String.valueOf(desertNumber));
total(false);
}
public void Increment(View view) {


// Set the dessert amount to the text view
LinearLayout parentRow = (LinearLayout) view.getParent();
TextView quantityView = (TextView) parentRow.findViewById(R.id.dessert_number);
String quantityString = quantityView.getText().toString();
desertNumber = Integer.parseInt(quantityString);
desertNumber += 1;
quantityView.setText(String.valueOf(desertNumber));
total(true);

}


// Add the total cost of the items to be bought
public void total(boolean change) {
//get all of the desserts
for (int i = 0; i < desserts.size(); i++) {
currentDessert = desserts.get(i);
}
// if increment add price else subtract
TextView shoppingCartView = (TextView) findViewById(R.id.shopping_cart);
if (change) {
totalAmount += currentDessert.getMyPrice();
} else {
totalAmount -= currentDessert.getMyPrice();
}

shoppingCartView.setText(Double.toString(totalAmount));
}
}

甜点.java

package com.example.annascott.buttondemo;
/**
* {@link Dessert} represents type of desert.
* Each object has 3 properties: name, number, and image resource ID.
*/

public class Dessert {
// Name of the desert
private String mDessertName;
// Number of desserts
private int mDessertNumber;
// Drawable resource ID
private int mImageResourceId;
// Price of desert
private double myPrice;

/*
* Create a new dessert object.
*
* @param vName is the name of the dessert
* @param vNumber is the corresponding number of desserts
* @param image is drawable reference ID that corresponds to the dessert
* */
public Dessert(String vName, int vNumber, int imageResourceId, double price)
{
mDessertName = vName;
mDessertNumber = vNumber;
mImageResourceId = imageResourceId;
myPrice = price;
}
/**
* Get the name of the dessert
*/
public String getDessertName() {
return mDessertName;
}
/**
* Get the  number of desserts
*/
public int getDessertNumber() {
return mDessertNumber;
}
/**
* Get the image resource ID
*/
public int getImageResourceId() {
return mImageResourceId;
}
public double getMyPrice() {
return myPrice ;
}
}

您的错误可能出在 total 函数中。

//get all of the desserts
for (int i = 0; i < desserts.size(); i++) {
currentDessert = desserts.get(i);
}

您将最后一个条目分配给变量 currentDessert,即值为 1.5 的糕点

像这样的事情怎么样:

// Add the total cost of the items to be bought
public void total(boolean change) {
//get all of the desserts
for (int i = 0; i < desserts.size(); i++) {
currentDessert = desserts.get(i);
if (change) {
totalAmount += currentDessert.getMyPrice();
} else {
totalAmount -= currentDessert.getMyPrice();
}
}
// if increment add price else subtract
TextView shoppingCartView = (TextView) findViewById(R.id.shopping_cart);
shoppingCartView.setText(Double.toString(totalAmount));
}

但我必须说实话,我不知道你在这里想做什么,你的问题是什么,或者某个输入的预期输出。

话虽如此,我不知道你的代码出了什么问题,因为我不知道它首先应该做什么。如果/在寻求调试帮助时,请为它们输入一些示例输入和预期输出。

您正在搜索整个函数中的所有甜点。

因此,您需要以某种方式找到一种方法来指定要添加价格的沙漠价格。

我会在代码前面的 switch case 语句中的每个按钮添加一个单击的布尔变量,但这不是唯一的方法。

最新更新