【问题标题】:Generating a file to store a receipt in android studios生成文件以在 android studios 中存储收据
【发布时间】:2022-01-07 16:56:40
【问题描述】:

您好,我是安卓工作室的新手。我正在做一个航空公司预订项目。该项目的要求之一是在每次客户付款时生成存储在外部文件中的收据。我试图环顾四周,试图弄清楚一个人如何在没有成功的情况下做到这一点。在应用程序中,我使用 SQLite 作为数据库,每个用户在创建帐户后都会分配一个余额。

这是我的 DBHelper 类:

package com.example.shashank.fffffffffffffffffffffffffff;


import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class DBHelper extends SQLiteOpenHelper {
    public static final String DBNAME = "Login.db";
    public static final String FLIGHTS = "FLIGHTS";
    public static final String COLUMN_ID = "ID";
    public static final String COLUMN_DESTINATION = "DESTINATION";
    public static final String COLUMN_PRICE = "PRICE";
    public static final String COLUMN_DEPARTURE_TIME = "DEPARTURE_TIME";
    public static final String COLUMN_ARRIVAL_TIME = "ARRIVAL_TIME";
    public static final String COLUMN_DURATION = "DURATION";
    public static final String COLUMN_AVAILABLE_SEATS = "AVAILABLE_SEATS";
    public static final String USERS = "users";
    public static final String USERNAME = "username";
    public static final String PASSWORD = "password";
    public static final String EMAIL = "email";
    public static final String BALANCE = "balance";
    public static final String BOOKING = "booking";

    public DBHelper(Context context) {
        super(context, "Login.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase MyDB) {

        String createTable1 = ("create Table " + USERS + "(" + USERNAME + " TEXT primary key, " + PASSWORD + " TEXT, " + EMAIL + " TEXT UNIQUE, " + BALANCE + " REAL, " + BOOKING + " INTEGER)");
        MyDB.execSQL(createTable1);

        MyDB.execSQL("CREATE TABLE " + FLIGHTS + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_DESTINATION + " TEXT, " + COLUMN_PRICE + " REAL, " + COLUMN_DEPARTURE_TIME + " TEXT, " + COLUMN_ARRIVAL_TIME + " TEXT, " + COLUMN_DURATION + " TEXT, " + COLUMN_AVAILABLE_SEATS + " INTEGER)");


        ContentValues insertValues = new ContentValues();
        insertValues.put(COLUMN_DESTINATION, "Cape Town");
        insertValues.put(COLUMN_PRICE, 2000);
        insertValues.put(COLUMN_DEPARTURE_TIME, "1200");
        insertValues.put(COLUMN_ARRIVAL_TIME, "1400");
        insertValues.put(COLUMN_DURATION, "2");
        insertValues.put(COLUMN_AVAILABLE_SEATS, 10);
        MyDB.insert(FLIGHTS, null, insertValues);

        ContentValues insertValues2 = new ContentValues();
        insertValues2.put(COLUMN_DESTINATION, "Johannesburg");
        insertValues2.put(COLUMN_PRICE, 1000);
        insertValues2.put(COLUMN_DEPARTURE_TIME, "1400");
        insertValues2.put(COLUMN_ARRIVAL_TIME, "1600");
        insertValues2.put(COLUMN_DURATION, "2");
        insertValues2.put(COLUMN_AVAILABLE_SEATS, 22);
        MyDB.insert(FLIGHTS, null, insertValues2);

        ContentValues insertValues3 = new ContentValues();
        insertValues3.put(COLUMN_DESTINATION, "Cape Town");
        insertValues3.put(COLUMN_PRICE, 500);
        insertValues3.put(COLUMN_DEPARTURE_TIME, "1200");
        insertValues3.put(COLUMN_ARRIVAL_TIME, "1400");
        insertValues3.put(COLUMN_DURATION, "2");
        insertValues3.put(COLUMN_AVAILABLE_SEATS, 0);
        MyDB.insert(FLIGHTS, null, insertValues3);

        

    }


    @Override
    public void onUpgrade(SQLiteDatabase MyDB, int i, int i1) {
        MyDB.execSQL("drop Table if exists " + USERS);
        MyDB.execSQL("drop Table if exists " + FLIGHTS);

        onCreate(MyDB);
    }

    public Boolean insertData(String username, String password, String email, Double balance){
        SQLiteDatabase MyDB = this.getWritableDatabase();
        ContentValues contentValues= new ContentValues();
        contentValues.put(USERNAME, username);
        contentValues.put(PASSWORD, password);
        contentValues.put(EMAIL, email);
        contentValues.put(BALANCE, balance);
        long result = MyDB.insert(USERS, null, contentValues);
        if(result==-1) return false;
        else
            return true;
    }

    public Boolean checkusername(String username) {
        SQLiteDatabase MyDB = this.getWritableDatabase();
        Cursor cursor = MyDB.rawQuery("Select * from " + USERS + " where " + USERNAME + " = ?", new String[]{username});
        if (cursor.getCount() > 0)
            return true;
        else
            return false;
    }

    public Boolean checkusernamepassword(String username, String password){
        SQLiteDatabase MyDB = this.getWritableDatabase();
        Cursor cursor = MyDB.rawQuery("Select * from " + USERS + " where " + USERNAME + " = ? and " + PASSWORD + " = ?", new String[] {username,password});
        if(cursor.getCount()>0)
            return true;
        else
            return false;
    }


    public List<FlightsModel> getEveryone(){

        List<FlightsModel> returnList = new ArrayList<>();

        String queryString = "SELECT * FROM " + FLIGHTS;

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.rawQuery(queryString, null);

        if(cursor.moveToFirst()){
            do {
                int id = cursor.getInt(0);
                String destination = cursor.getString(1);
                double price = cursor.getDouble(2);
                String departure = cursor.getString(3);
                String arrival = cursor.getString(4);
                String duration = cursor.getString(5);
                int space = cursor.getInt(6);

                FlightsModel newFlight = new FlightsModel(id, destination, price, departure, arrival, duration, space);
                returnList.add(newFlight);


            }while (cursor.moveToNext());
        }
        else{

        }
        cursor.close();
        db.close();
        return returnList;
    }



    @SuppressLint("Range") // suppress Bug/issue with getColumnIndex
    public FlightsModel getFlightById(int id) {
        FlightsModel rv;
        SQLiteDatabase db = this.getWritableDatabase();
        // Uses the query convenience method rather than raw query
        Cursor csr = db.query(FLIGHTS,null,COLUMN_ID+"=?",new String[]{String.valueOf(id)},null,null,null);
        if (csr.moveToFirst()) {
            rv = new FlightsModel(
                    csr.getInt(csr.getColumnIndex(COLUMN_ID)),
                    csr.getString(csr.getColumnIndex(COLUMN_DESTINATION)),
                    csr.getDouble(csr.getColumnIndex(COLUMN_PRICE)),
                    csr.getString(csr.getColumnIndex(COLUMN_DEPARTURE_TIME)),
                    csr.getString(csr.getColumnIndex(COLUMN_ARRIVAL_TIME)),
                    csr.getString(csr.getColumnIndex(COLUMN_DURATION)),
                    csr.getInt(csr.getColumnIndex(COLUMN_AVAILABLE_SEATS))
            );
        }  else {
            rv = new FlightsModel();
        }
        csr.close();
        // No need to close the database (inefficient to keep opening and closing db)
        return rv;
    }



    @SuppressLint("Range")
    public UsersModel getPasswordByName(String name){

        UsersModel rv;
        SQLiteDatabase db = this.getWritableDatabase();

        Cursor cr = db.query(USERS, null, USERNAME+"=?", new String[]{name}, null, null, null);

        if (cr.moveToFirst()) {
            rv = new UsersModel(
                    cr.getString(cr.getColumnIndex(USERNAME)),
                    cr.getString(cr.getColumnIndex(PASSWORD)),
                    cr.getString(cr.getColumnIndex(EMAIL)),
                    cr.getDouble(cr.getColumnIndex(BALANCE)),
                    cr.getInt(cr.getColumnIndex(BOOKING))

            );
        }  else rv = new UsersModel();

        cr.close();
        return rv;
    }

    public int setBookingByUserName(int bookingAmount, String userName) {
        ContentValues cv = new ContentValues();
        cv.put(BOOKING,bookingAmount);
        return this.getWritableDatabase().update(USERS,cv,USERNAME+"=?",new String[]{userName});
    }

    public double makingPayment(double balance, String userName){
        ContentValues cv = new ContentValues();
        cv.put(BALANCE,balance);
        return this.getWritableDatabase().update(USERS,cv,USERNAME+"=?",new String[]{userName});
    }


    public int setAvailableSeatsAfterPayment(int seats, int flightID){
        ContentValues cv = new ContentValues();
        cv.put(COLUMN_AVAILABLE_SEATS,seats);
        return this.getWritableDatabase().update(FLIGHTS,cv,COLUMN_ID+"=?",new String[]{String.valueOf(flightID)});
    }


    public int cancelBooking(int bookingAmount, String userName){
        ContentValues cv = new ContentValues();
        cv.put(BOOKING,bookingAmount);
        return this.getWritableDatabase().update(USERS,cv,USERNAME+"=?",new String[]{String.valueOf(userName)});
    }


}

付款方式用于在客户预订航班时使用新余额更新数据库。

这是预订活动:

package com.example.shashank.fffffffffffffffffffffffffff;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class BookingActivity extends AppCompatActivity {


    TextView textView;
    TextView departure, arrival, duration, price, seats;

    DBHelper dbHelper; //<<<<< ADDED
    Button book, accountBtn;
    FlightsModel flightsModel; //<<<<< ADDED
    UsersModel userModel;


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

        dbHelper = new DBHelper(this); //<<<<< ADDED

        textView = findViewById(R.id.textView);
        departure = findViewById(R.id.departure);
        arrival = findViewById(R.id.arrival);
        duration = findViewById(R.id.duration);
        price = findViewById(R.id.price);
        seats = findViewById(R.id.seats);
        book = findViewById(R.id.button2);
        accountBtn = findViewById(R.id.accountBtn);


        Intent mIntent = getIntent();
        int intValue = mIntent.getIntExtra("intVariableName", 0);
        Intent nameIntent = getIntent();
        String name = nameIntent.getStringExtra("userName");
        flightsModel = dbHelper.getFlightById(intValue + 1);


        intValue = intValue + 1;
        textView.setText(flightsModel.getDestination());
        departure.setText(flightsModel.getDeparture_time());
        arrival.setText(flightsModel.getArrival_time());
        duration.setText(flightsModel.getDuration());
        price.setText("R" +  Double.toString(flightsModel.getPrice()));
        seats.setText(Integer.toString(flightsModel.getAvailable_space()));







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

                int intValue, seats;
                Intent mIntent = getIntent();
                intValue = mIntent.getIntExtra("intVariableName", 0);
                intValue = intValue + 1;

                dbHelper = new DBHelper(BookingActivity.this);
                flightsModel = dbHelper.getFlightById(intValue);
                double price;
                price = flightsModel.getPrice();

                userModel = dbHelper.getPasswordByName(name);
                double balance;
                balance = userModel.getBalance();

                String name = nameIntent.getStringExtra("userName");

                seats = flightsModel.getAvailable_space();



                if(seats == 0){
                    Toast.makeText(BookingActivity.this, "Booking unsuccessful, no available seats", Toast.LENGTH_SHORT).show();
                }else{
                    if(price <= balance){
                        dbHelper.setBookingByUserName(intValue, name);
                        Toast.makeText(BookingActivity.this, "Payment successful, booking has been made", Toast.LENGTH_SHORT).show();
                        double newBalance;
                        newBalance = balance - price;
                        dbHelper.makingPayment(newBalance, name);
                        seats = seats - 1;
                        dbHelper.setAvailableSeatsAfterPayment(seats, intValue);
                    }else{
                        Toast.makeText(BookingActivity.this, "Payment unsuccessful, not enough funds", Toast.LENGTH_SHORT).show();
                    }
                }





            }
        });


        accountBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BookingActivity.this, AccountActivity.class);
                intent.putExtra("userName", name);
                startActivity(intent);
            }
        });




    }


}

因此,一旦客户进行预订,就需要生成收据文件,例如包含航班详细信息、时间、日期和航班价格。任何帮助将不胜感激。

【问题讨论】:

    标签: java android sqlite


    【解决方案1】:

    下面是一个将文件保存在 App 的 Files 目录下的收据子目录中的示例。文件名由值组成,例如 弗雷德-开普敦-1200-1400

    存储在 /data/user/0/the_package_name/files/receipts/Fred-Cape Town-1200-1400

    文件本身,在示例中包含:-

    Fred
    Cape Town
    1200
    1400
    

    DBHelper 中添加了一些支持方法:-

    public UsersModel getUsersModelByName(String userName) {
        UsersModel rv = new UsersModel();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor csr = db.query(USERS,null,USERNAME+"=?", new String[]{userName},null,null,null);
        if (csr.moveToFirst()) {
            rv = new UsersModel(
                    csr.getString(csr.getColumnIndex(USERNAME)),
                    csr.getString(csr.getColumnIndex(PASSWORD)),
                    csr.getString(csr.getColumnIndex(EMAIL)),
                    csr.getDouble(csr.getColumnIndex(BALANCE)),
                    csr.getInt(csr.getColumnIndex(BOOKING))
            );
        }
        csr.close();
        return rv;
    }
    
    public boolean makeBooking(String userName, int bookingId) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(BOOKING,bookingId);
        return db.update(USERS,cv,USERNAME + "=?", new String[]{userName}) > 0;
    }
    

    现在ma​​keBookReceiptFile方法,也放在DBHelper类中:-

    public String makeBookReceiptFile(String userName, Context context) {
        String rcpts_directory = "receipts";
        String rv = "";
        SQLiteDatabase db = this.getWritableDatabase();
        UsersModel currentUser = getUsersModelByName(userName);
        FlightsModel currentFlightsModel = new FlightsModel();
        if (currentUser.booking > 0) {
            currentFlightsModel = getFlightById(currentUser.booking);
            if (currentFlightsModel.id < 1) {
                rv = "INVALID - unable to extract booking for id " + currentUser.booking;
            }
        } else {
            rv = "INVALID - unable to extract user who's name is " + userName;
        }
        if (rv.length() > 0) return rv;
        String rcpt_filename =
                currentUser.username + "-" +
                        currentFlightsModel.destination + "-" +
                        currentFlightsModel.departure + "-" +
                        currentFlightsModel.arrival
                ;
        File rcpt = new File(context.getFilesDir().getPath() + File.separatorChar + rcpts_directory + File.separatorChar + rcpt_filename);
        rcpt.getParentFile().mkdirs();
        try {
            FileWriter fw = new FileWriter(rcpt);
            fw.write(userName +
                    "\n" + currentFlightsModel.destination+
                    "\n" + currentFlightsModel.departure +
                    "\n" + currentFlightsModel.arrival
            );
            fw.flush();
            fw.close();
            rv = rcpt.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
            rv = "IOERROR - " + e.getMessage();
        }
        return rv;
    }
    

    首先根据 UserName 检索 UsersModel,然后根据 booking id 检索 FlightModel。

    如果没有得到,则返回一个以 INVALID 开头的字符串

    然后它通过 Context 获取 Files 目录。 生成文件的路径。 制作任何缺失的目录。 将数据写入文件,刷新并关闭文件,返回路径或如果有 io 错误,则返回错误消息。

    以上测试使用:-

        db = new DBHelper(this);
    
        db.insertData("Fred","password","fed@email",0.00);
        UsersModel fred = db.getUsersModelByName("Fred");
        db.makeBooking(fred.username,1);
        Log.d("RESULTINFO",db.makeBookReceiptFile("Fred",this));
    

    导致(通过设备资源管理器):-

    如果您对文件的位置有疑问,则必须阅读 https://developer.android.com/training/data-storage/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 2012-09-05
      相关资源
      最近更新 更多