2017-10-01 12 views
-3

コーヒーショップは1種類のコーヒーを5ドルで販売しており、ホイップクリームとチョコレートは1ドルごとに追加できます。私はホイップクリームとチョコレートのオプションをshow_totalに追加する方法を正しく理解する方法がわかりませんか?チェックボックスとボタンを使用して合計価格を更新Android Studio

たとえば、クリームとチョコレートを入れたコーヒーは7ドル、チョコレートだけを3枚、コーヒーを18ドルにする必要があります。

CreamClick、onChocClick、countIN、およびcountDEを1つの総額で正しく処理するには、別のメソッドを呼び出す必要がありますか?

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
import java.text.NumberFormat; 
import java.util.Locale; 

public class MainActivity extends AppCompatActivity { 

    TextView show_quantity, show_total, show_summary; 
    Button order, plus_one, negative_one; 
    EditText customer_name; 
    CheckBox whipped_cream, chocolate; 
    int counter = 0; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    show_quantity = (TextView) findViewById(R.id.amount); 
    show_total = (TextView) findViewById(R.id.total); 
    show_summary = (TextView) findViewById(R.id.summary); 
    customer_name = (EditText) findViewById(R.id.customer_name); 
    plus_one = (Button) findViewById(R.id.plus_one); 
    negative_one = (Button) findViewById(R.id.negative_one); 
    whipped_cream = (CheckBox) findViewById(R.id.whipped_cream); 
    chocolate = (CheckBox) findViewById(R.id.chocolate); 
    order = (Button) findViewById(R.id.order); 

    order.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (customer_name.getText().toString().isEmpty()) { 
       Toast.makeText(getApplicationContext(), "Please enter a name.", Toast.LENGTH_SHORT).show(); 
      } else { 
       show_summary.setText("ORDER SUMMARY" + " \n" + 
       "Customer - " + customer_name.getText().toString() + " \n" + 
       "Whipped Cream? " + whipped_cream.isChecked() + " \n" + 
       "Chocolate? " + chocolate.isChecked() + " \n" + 
       "Total - " + show_total.getText().toString() + " \n" + 
       "Thank you!"); 
      } 
     } 
    }); 
} 

public void onCreamClick(View c) { 

    boolean ch1 = whipped_cream.isChecked(); 

    if (!ch1) { 
     double coffee = 5; 
     NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US); 
     String cof = format.format(coffee); 
     show_total.setText(cof);} 
    else if (ch1) { 
     double cream = 1; 
     NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US); 
     String crm = format.format(cream); 
     show_total.setText(crm);} 
} 

public void onChocClick(View chc) { 

    boolean ch2 = chocolate.isChecked(); 

    if (!ch2) { 
     double coffee = 5; 
     NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US); 
     String cof = format.format(coffee); 
     show_total.setText(cof);} 
    else if (ch2){ 
     double chocolate = 1; 
     NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US); 
     String choc = format.format(chocolate); 
     show_total.setText(choc);} 
} 

public void countIN(View in) { 
    counter++; 
    double coffee = 5 * counter; 
    if (counter > 10) { 
     Toast.makeText(getApplicationContext(), "You can not order more than 10 coffees", Toast.LENGTH_SHORT).show(); 
    } else { 
     show_quantity.setText(Integer.toString(counter)); 
     NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US); 
     String cof = format.format(coffee); 
     show_total.setText(cof); 
    } 
} 

public void countDE(View de) { 
    counter--; 
    double coffee = 5 * counter; 
    if (counter < 0) { 
     Toast.makeText(getApplicationContext(), "Please order at least one coffee", Toast.LENGTH_SHORT).show(); 
    } else { 
     show_quantity.setText(Integer.toString(counter)); 
     NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US); 
     String cof = format.format(coffee); 
     show_total.setText(cof); 
    } 
} 

public static double display_total(double price, double total) { 
    return price; 
} 
} 

答えて

0

これらの操作に反応するには、チェックボックスとボタンにリスナーを登録する必要があります。

whipped_cream.setOnCheckedChangeListener((checkBox, isChecked) -> onCreamClick());  
関連する問題