Last active
May 14, 2021 07:57
-
-
Save junsuk5/91b1075c81efec2232d1 to your computer and use it in GitHub Desktop.
커피 주문 앱 소스
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| tools:context=".MainActivity"> | |
| <LinearLayout | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:orientation="vertical" | |
| android:paddingBottom="@dimen/activity_vertical_margin" | |
| android:paddingLeft="@dimen/activity_horizontal_margin" | |
| android:paddingRight="@dimen/activity_horizontal_margin" | |
| android:paddingTop="@dimen/activity_vertical_margin"> | |
| <EditText | |
| android:id="@+id/name_edit_text" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:layout_marginBottom="16dp" | |
| android:hint="Name" | |
| android:singleLine="true" /> | |
| <TextView | |
| style="@style/TextCategory" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="Toppings" /> | |
| <CheckBox | |
| android:id="@+id/whip_checkbox" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:paddingLeft="24dp" | |
| android:text="Whipped cream" | |
| android:textSize="16sp" /> | |
| <CheckBox | |
| android:id="@+id/chocolate_checkbox" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_marginBottom="16dp" | |
| android:paddingLeft="24dp" | |
| android:text="Chocolate" | |
| android:textSize="16sp" /> | |
| <TextView | |
| style="@style/TextCategory" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="@string/quantity" /> | |
| <LinearLayout | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content"> | |
| <Button | |
| android:layout_width="48dp" | |
| android:layout_height="48dp" | |
| android:layout_marginBottom="16dp" | |
| android:onClick="decrement" | |
| android:text="-" /> | |
| <TextView | |
| android:id="@+id/quantity_text_view" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_marginLeft="8dp" | |
| android:layout_marginRight="8dp" | |
| android:text="0" | |
| android:textColor="#000000" | |
| android:textSize="16sp" /> | |
| <Button | |
| android:layout_width="48dp" | |
| android:layout_height="48dp" | |
| android:onClick="increment" | |
| android:text="+" /> | |
| </LinearLayout> | |
| <TextView | |
| style="@style/TextCategory" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="Order Summary" /> | |
| <TextView | |
| android:id="@+id/price_text_view" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_marginBottom="16dp" | |
| android:textColor="#000000" | |
| android:textSize="16sp" /> | |
| <Button | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:onClick="submitOrder" | |
| android:text="@string/order" /> | |
| </LinearLayout> | |
| </ScrollView> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainActivity extends AppCompatActivity { | |
| private static final String TAG = MainActivity.class.getSimpleName(); | |
| private final int PRICE_COFFEE = 2500; | |
| private final int PRICE_WHIP = 500; | |
| private final int PRICE_CHOCOLATE = 300; | |
| private int mQuantity = 0; | |
| private TextView mPriceTextView; | |
| private TextView mQuantityTextView; | |
| private CheckBox mWhipCheckBox; | |
| private CheckBox mChocolateCheckBox; | |
| private EditText mNameEditText; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| mPriceTextView = (TextView) findViewById(R.id.price_text_view); | |
| mQuantityTextView = (TextView) findViewById(R.id.quantity_text_view); | |
| mWhipCheckBox = (CheckBox) findViewById(R.id.whip_checkbox); | |
| mChocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox); | |
| mNameEditText = (EditText) findViewById(R.id.name_edit_text); | |
| } | |
| /** | |
| * 주문 버튼 이벤트 처리 | |
| * | |
| * @param view 이벤트 처리 할 view | |
| */ | |
| public void submitOrder(View view) { | |
| String name = "Name : " + mNameEditText.getText(); | |
| String whip = "Add whipped cream? " + mWhipCheckBox.isChecked(); | |
| String chocolate = "Add chocolate? " + mChocolateCheckBox.isChecked(); | |
| String quantity = "Quantity : " + mQuantity; | |
| int price = PRICE_COFFEE * mQuantity; | |
| if (mWhipCheckBox.isChecked()) { | |
| price += mQuantity * PRICE_WHIP; | |
| } | |
| if (mChocolateCheckBox.isChecked()) { | |
| price += mQuantity * PRICE_CHOCOLATE; | |
| } | |
| String formattedPrice = "Total : " | |
| + NumberFormat.getCurrencyInstance().format(price); | |
| String message = name + "\n" | |
| + whip + "\n" | |
| + chocolate + "\n" | |
| + quantity + "\n" | |
| + formattedPrice + "\n" | |
| + "Thank you!"; | |
| displayMessage(message); | |
| // 전화걸기 예제 | |
| // Uri uri = Uri.parse("tel:01048993729"); | |
| // Intent intent = new Intent(Intent.ACTION_DIAL, uri); | |
| // startActivity(intent); | |
| String[] addresses = new String[] { | |
| "master@suwonsmartapp.com" | |
| }; | |
| composeEmail(addresses, "커피 주문 메일", null, message); | |
| } | |
| /** | |
| * 메일 보내기 | |
| * | |
| * @param addresses 받을 사람 | |
| * @param subject 제목 | |
| * @param attachment 첨부 | |
| * @param text 내용 | |
| */ | |
| public void composeEmail(String[] addresses, String subject, Uri attachment, String text) { | |
| Intent intent = new Intent(Intent.ACTION_SEND); | |
| intent.setType("text/plain"); | |
| intent.putExtra(Intent.EXTRA_EMAIL, addresses); | |
| intent.putExtra(Intent.EXTRA_SUBJECT, subject); | |
| intent.putExtra(Intent.EXTRA_STREAM, attachment); | |
| intent.putExtra(Intent.EXTRA_TEXT, text); | |
| if (intent.resolveActivity(getPackageManager()) != null) { | |
| startActivity(intent); | |
| } | |
| } | |
| public void increment(View view) { | |
| mQuantity++; | |
| if (mQuantity > 100) { | |
| mQuantity = 100; | |
| Toast.makeText(getApplicationContext(), "100 개 이상 주문 안 되요", Toast.LENGTH_SHORT).show(); | |
| } | |
| display(mQuantity); | |
| displayPrice(PRICE_COFFEE * mQuantity); | |
| } | |
| public void decrement(View view) { | |
| mQuantity--; | |
| if (mQuantity < 0) { | |
| mQuantity = 0; | |
| Toast.makeText(MainActivity.this, "0 개 이하 주문 안 되요", Toast.LENGTH_SHORT).show(); | |
| } | |
| display(mQuantity); | |
| displayPrice(PRICE_COFFEE * mQuantity); | |
| } | |
| /** | |
| * 수량을 화면에 표시 | |
| * | |
| * @param number 표시 할 수량 | |
| */ | |
| private void display(int number) { | |
| mQuantityTextView.setText("" + number); | |
| } | |
| private void displayPrice(int number) { | |
| mPriceTextView.setText(NumberFormat.getCurrencyInstance().format(number)); | |
| } | |
| private void displayMessage(String message) { | |
| mPriceTextView.setText(message); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <resources> | |
| <!-- Base application theme. --> | |
| <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> | |
| <!-- Customize your theme here. --> | |
| </style> | |
| <!-- 카테고리 스타일 정의 --> | |
| <style name="TextCategory"> | |
| <item name="android:layout_marginBottom">16dp</item> | |
| <item name="android:textAllCaps">true</item> | |
| </style> | |
| </resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment