效果图:
转帐前:
转账后:
以下是代码:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".TransferActivity">
<EditText
android:id="@+id/from_account_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="转出方账号" />
<EditText
android:id="@+id/to_account_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="转入方账号" />
<EditText
android:id="@+id/transfer_amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="转账金额" />
<Button
android:id="@+id/query_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/from_account_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/from_account_balance"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/to_account_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/to_account_balance"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<Button
android:id="@+id/transfer_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转账" />
</LinearLayout>
两个JAVA:
MainActivity
package com.nytd.licheng_21250214_fifth_work_1;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private AccountDatabaseHelper dbHelper;
private EditText fromAccountNumberEditText;
private EditText toAccountNumberEditText;
private EditText transferAmountEditText;
private TextView fromAccountNameTextView;
private TextView fromAccountBalanceTextView;
private TextView toAccountNameTextView;
private TextView toAccountBalanceTextView;
private Button queryButton;
private Button transferButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new AccountDatabaseHelper(this);
fromAccountNumberEditText = findViewById(R.id.from_account_number);
toAccountNumberEditText = findViewById(R.id.to_account_number);
transferAmountEditText = findViewById(R.id.transfer_amount);
fromAccountNameTextView = findViewById(R.id.from_account_name);
fromAccountBalanceTextView = findViewById(R.id.from_account_balance);
toAccountNameTextView = findViewById(R.id.to_account_name);
toAccountBalanceTextView = findViewById(R.id.to_account_balance);
queryButton = findViewById(R.id.query_button);
transferButton = findViewById(R.id.transfer_button);
queryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
queryAccountInfo();
}
});
transferButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
transfer();
}
});
}
private void queryAccountInfo() {
String fromAccountNumber = fromAccountNumberEditText.getText().toString();
String toAccountNumber = toAccountNumberEditText.getText().toString();
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor fromAccountCursor = db.rawQuery("SELECT account_name, balance FROM accounts WHERE account_number=?", new String[]{fromAccountNumber});
Cursor toAccountCursor = db.rawQuery("SELECT account_name, balance FROM accounts WHERE account_number=?", new String[]{toAccountNumber});
if (fromAccountCursor.moveToFirst() && toAccountCursor.moveToFirst()) {
fromAccountNameTextView.setText(fromAccountCursor.getString(0));
fromAccountBalanceTextView.setText(String.valueOf(fromAccountCursor.getDouble(1)));
toAccountNameTextView.setText(toAccountCursor.getString(0));
toAccountBalanceTextView.setText(String.valueOf(toAccountCursor.getDouble(1)));
} else {
// 提示用户输入的账号有误
}
fromAccountCursor.close();
toAccountCursor.close();
db.close();
}
private void transfer() {
String fromAccountNumber = fromAccountNumberEditText.getText().toString();
String toAccountNumber = toAccountNumberEditText.getText().toString();
double transferAmount = Double.parseDouble(transferAmountEditText.getText().toString());
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.beginTransaction();
try {
Cursor fromAccountCursor = db.rawQuery("SELECT balance FROM accounts WHERE account_number=?", new String[]{fromAccountNumber});
Cursor toAccountCursor = db.rawQuery("SELECT balance FROM accounts WHERE account_number=?", new String[]{toAccountNumber});
if (fromAccountCursor.moveToFirst() && toAccountCursor.moveToFirst()) {
double fromAccountBalance = fromAccountCursor.getDouble(0);
double toAccountBalance = toAccountCursor.getDouble(0);
if (fromAccountBalance >= transferAmount) {
db.execSQL("UPDATE accounts SET balance=? WHERE account_number=?", new Object[]{fromAccountBalance - transferAmount, fromAccountNumber});
db.execSQL("UPDATE accounts SET balance=? WHERE account_number=?", new Object[]{toAccountBalance + transferAmount, toAccountNumber});
db.setTransactionSuccessful();
// 提示转账成功
} else {
// 提示余额不足
}
} else {
// 提示输入的账号有误
}
fromAccountCursor.close();
toAccountCursor.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
db.endTransaction();
db.close();
}
}
}
AccountDatabaseHelper
package com.nytd.licheng_21250214_fifth_work_1;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class AccountDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "account.db";
private static final int DATABASE_VERSION = 1;
public AccountDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE accounts (id INTEGER PRIMARY KEY AUTOINCREMENT, account_number TEXT, account_name TEXT, balance REAL)";
db.execSQL(createTable);
// 添加初始数据
String insertData = "INSERT INTO accounts (account_number, account_name, balance) VALUES ('123', 'zhangsan', 1000.0)";
db.execSQL(insertData);
// 添加9组初始数据
insertData = "INSERT INTO accounts (account_number, account_name, balance) VALUES ('456', 'lisi', 2000.0)";
db.execSQL(insertData);
insertData = "INSERT INTO accounts (account_number, account_name, balance) VALUES ('789', 'wangwu', 3000.0)";
db.execSQL(insertData);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS accounts");
onCreate(db);
}
}