簡體   English   中英

如何為啟動烤面包對象的訂單量設置最大值和最小值?

[英]How to set a max and min value for an order quantity that initiates a toast object?

我的應用程序輸入客戶名稱,訂購的咖啡數量以及客戶是否要在上面加奶油和/或巧克力。 它提供基於客戶訂單的總價格的實時更新,一旦下訂單,它將為客戶提供訂單摘要。 客戶不應訂購負數或超過10杯咖啡。 我已經創建了數量計數器,但需要執行TOAST對象的幫助。 這是我的MainActivity Java文件:

public class MainActivity extends AppCompatActivity {

    ArrayList<String> selections = new ArrayList<String>();
    TextView order_summary;
    private Button _decrease;
    private Button _increase;
    private TextView _quantity;
    private static int _counter = 1;
    private String _stringVal;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        order_summary = (TextView)findViewById(R.id.textOrder);
        order_summary.setEnabled(false);

        _decrease = (Button) findViewById(R.id.btnSubtract);
        _increase = (Button) findViewById(R.id.btnAdd);
        _quantity = (TextView) findViewById(R.id.textQty);

        _decrease.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.d("src", "Decreasing value...");
                _counter--;
                _stringVal = Integer.toString(_counter);
                _quantity.setText(_stringVal);
            }
        });

        _increase.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d("src", "Decreasing value...");
                _counter--;
                _stringVal = Integer.toString(_counter);
                _quantity.setText(_stringVal);
            }
        });

        _increase.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.d("src", "Increasing value...");
                _counter++;
                _stringVal = Integer.toString(_counter);
                _quantity.setText(_stringVal);
            }
        });

    };

    // start Order class
    public class Order {
        // declare order variables
        private int quantity;
        private double price;
        private double total = quantity * price;
    }

    // declare price variables
    private double coffeePrice = 5.00;
    private double whippedPrice = 1.00;
    private double chocolatePrice = 1.00;

    // adds selected items to order summary
    public void selectItem (View v) {
        boolean checked = ((CheckBox) v).isChecked();
            switch (v.getId())
            {
                case R.id.chkWhipped:
                    if(checked)
                    {selections.add("Whipped Cream");}
                else
                    {
                        selections.remove("whipped Cream");
                    }
                break;
                case R.id.chkChocolate:
                if(checked)
                {selections.add("Chocolate");}
                else
                {
                    selections.remove("Chocolate");
                }
                break;
            }
    }

    // display order summary
    public void finalSelection (View v) {
        String final_order = "";
            for(String Selections : selections){
                final_order = order_summary + Selections + "\n";
            }
        order_summary.setText(final_order);
            order_summary.setEnabled(true);
    }

    //display in long period of time
    //Toast.makeText(getApplicationContext(), "You cannot order a negative number or more than 10 coffees",
    //Toast.LENGTH_LONG).show();

我在底部注釋了一個TOAST腳本,不確定是否正確實現它很熱。

您可以在按下按鈕時執行檢查:

    _decrease.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(_counter <= 0) {
                 Log.d("src", "Decreasing value...");
                 _counter--;
                 _stringVal = Integer.toString(_counter);
                 _quantity.setText(_stringVal);

                 Toast.makeText(getApplicationContext(), "You cannot order a negative number or more than 10 coffees",
                 Toast.LENGTH_LONG).show();
            }
        }
    });

可以在_increase按鈕上執行相同的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM