Tuesday, July 5, 2016

ưng dụng quản lý tính tiền bán sách đơn giản - lập trinh android

 


Code Class KhachHang


package com.example.macbookpro.dtsondctinhtienbansach;
public class KhachHang {
    public String tenKh;
    public Integer slMua;
    public Boolean laVip;
    public final Integer GiaBan=20000;

    public KhachHang() {
    }

    public KhachHang(Boolean laVip, Integer slMua, String tenKh) {
        this.laVip = laVip;
        this.slMua = slMua;
        this.tenKh = tenKh;
    }

    public Integer getGiaBan() {
        return GiaBan;
    }

    public Boolean getLaVip() {
        return laVip;
    }

    public Integer getSlMua() {
        return slMua;
    }

    public String getTenKh() {
        return tenKh;
    }

    public void setLaVip(Boolean laVip) {
        this.laVip = laVip;
    }

    public void setSlMua(Integer slMua) {
        this.slMua = slMua;
    }

    public void setTenKh(String tenKh) {
        this.tenKh = tenKh;
    }
    public double tinhThanhTien()
    {
        return (laVip?slMua*GiaBan*0.9:slMua*GiaBan);
    }
}

Code Class DanhSachKhachHang

package com.example.macbookpro.dtsondctinhtienbansach;

import java.util.ArrayList;
public class DanhSachKhachHang {
     ArrayList<KhachHang>dsKhachHang = new ArrayList<KhachHang>();
    // tao ham them khach hang vao dsKhachHang    public void themKh(KhachHang kh)
    {
        dsKhachHang.add(kh);
    }
    //Tao ham tinh tong doanh thu    public Double tongDoanhThu()
    {
        double tdt=0.0;
        for (KhachHang kh:dsKhachHang)
        {
            tdt += kh.tinhThanhTien();
        }
        return tdt;
    }
    // Tao ham tinh tong khach hang    public Integer tongKhachHang()
    {
        return dsKhachHang.size();
    }
    // Tao ham tinh tong khach VIP    public Integer tongKhachVip()
    {
        int tong =0;
        for (KhachHang kh:dsKhachHang)
        {
            if(kh.laVip)tong++;
        }
        return tong;
    }
}

Code hàm MainActivity

package com.example.macbookpro.dtsondctinhtienbansach;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
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.ImageButton;

public class MainActivity extends AppCompatActivity {
    Button btnThanhTien, btnTiep, btnThongKe;
    EditText edtTenKh,edtSoLuongSach, edtTongKh,edtThanhTien, edtTongVip,edtTongDoanhThu;
    CheckBox chkVip;
    ImageButton imgThoat;
    DanhSachKhachHang dsKh = new DanhSachKhachHang();

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


    // Ham nay de anh xa cac control ngoai man hinh vao bien trong bo nho chinh    private void addControls() {
        btnThanhTien = (Button) findViewById(R.id.btntinhtt);
        btnTiep = (Button) findViewById(R.id.btntiep);
        btnThongKe= (Button) findViewById(R.id.btnthongke);
        edtTenKh = (EditText) findViewById(R.id.editTextTenKhachHang);
        edtSoLuongSach = (EditText) findViewById(R.id.editTextSoLuongSach);
        edtThanhTien = (EditText) findViewById(R.id.editTextThanhTien);
        edtTongKh = (EditText) findViewById(R.id.editTextTongKhachHang);
        edtTongVip = (EditText) findViewById(R.id.editTextTongKhachVip);
        edtTongDoanhThu = (EditText) findViewById(R.id.editTextTongDoanhThu);
        chkVip = (CheckBox) findViewById(R.id.checkBoxVip);
        imgThoat = (ImageButton) findViewById(R.id.imageButtonThoat);
    }
    // Goi cac xu ly da thiet lap ben duoi    private void addEvents() {
        btnThongKe.setOnClickListener(new myEvent());
        btnTiep.setOnClickListener(new myEvent());
        btnThanhTien.setOnClickListener(new myEvent());
        imgThoat.setOnClickListener(new myEvent());

    }
    // Bat cac id cua control ngoai man hinh, tuy vao id ma co cac xu ly khac nhau    // Ke thua interface View.OnClickListener va override method onClick    private class myEvent implements View.OnClickListener
    {

        @Override        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btntinhtt:
                    tinhTien();
                    break;
                case R.id.btntiep:
                    tiep();
                    break;
                case R.id.btnthongke:
                    thongKe();
                    break;
                case R.id.imageButtonThoat:
                    thoat();
                    break;
            }

        }
    }
    // Viet ham Button tinh tien    private void tinhTien(){
        KhachHang kh = new KhachHang();
        kh.setTenKh(edtTenKh.getText()+"");
        kh.setSlMua(Integer.parseInt(edtSoLuongSach.getText()+""));
        kh.setLaVip(chkVip.isChecked());
        // cap nhap khach hang moi vao dsKhachHang phuc vu cho cong viec thong ke        dsKh.themKh(kh);
        edtThanhTien.setText(kh.tinhThanhTien()+"");

    }
    // Viet ham Button tiep    private void tiep()
    {
        // reset cac editText cua muc ban hang ve rong        edtTenKh.setText("");
        edtThanhTien.setText("");
        edtSoLuongSach.setText("");
        // focus vao editText ho ten        edtTenKh.requestFocus();
    }
    // viet ham cho Button Thong ke    private void thongKe()
    {
        // goi ham trong class DanhSachKhachHang de set gia tri cho cac editText trong muc thong ke        edtTongKh.setText(dsKh.tongKhachHang()+"");
        edtTongVip.setText(dsKh.tongKhachVip()+"");
        edtTongDoanhThu.setText(dsKh.tongDoanhThu()+"");
    }
    //Viet ham khi click vao imageButton Thoat    private void thoat()
    {
        AlertDialog.Builder bd = new AlertDialog.Builder(this);
        // set Title cho Alert        bd.setTitle("Thoat chuong trinh");
        // set message cho Alert        bd.setMessage("Ban co muon thoat khong?");
        // Tao button va set su kien cho button tren alert        bd.setNegativeButton("Khong", new DialogInterface.OnClickListener() {
            @Override            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        bd.setPositiveButton("Co", new DialogInterface.OnClickListener() {
            @Override            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        bd.create().show();

    }

}



Code xml giao diện

<?xml version="1.0" encoding="utf-8"?><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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.macbookpro.dtsondctinhtienbansach.MainActivity"    android:fillViewport="false">


    <LinearLayout        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="match_parent">

        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:textAppearance="?android:attr/textAppearanceLarge"            android:text="Chuong trinh tinh tien ban hang"            android:id="@+id/textView"            android:textColor="#FFFFFF"            android:background="#FF0000"            android:layout_gravity="center_horizontal" />

        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textAppearance="?android:attr/textAppearanceLarge"            android:text="Ban hang:"            android:textColor="#FFFFFF"            android:background="#0008ff"            android:id="@+id/textView2" />

        <TableLayout            android:layout_width="match_parent"            android:orientation="horizontal"            android:layout_height="match_parent">

            <TableRow                android:layout_width="match_parent"                android:layout_height="match_parent">

                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:textAppearance="?android:attr/textAppearanceMedium"                    android:text="Ten khac hang"                    android:id="@+id/textView3" />

                <EditText                    android:layout_width="230dp"                    android:layout_height="wrap_content"                    android:id="@+id/editTextTenKhachHang"                    android:layout_column="13" />
            </TableRow>
            <TableRow                android:layout_width="match_parent"                android:layout_height="match_parent">

                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:textAppearance="?android:attr/textAppearanceMedium"                    android:text="So luong sach"                    android:id="@+id/textView4" />

                <EditText                    android:layout_width="230dp"                    android:layout_height="wrap_content"                    android:id="@+id/editTextSoLuongSach"                    android:layout_column="13" />
            </TableRow>
            <CheckBox                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Khach vip"                android:id="@+id/checkBoxVip"                android:checked="false" />
            <TableRow                android:layout_width="match_parent"                android:layout_height="match_parent">

                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:textAppearance="?android:attr/textAppearanceMedium"                    android:text="Thanh tien"                    android:id="@+id/txtThanhTien" />

                <EditText                    android:layout_width="230dp"                    android:layout_height="wrap_content"                    android:id="@+id/editTextThanhTien"                    android:layout_column="13" />
            </TableRow>

        </TableLayout>
        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content" >

            <Button                android:id="@+id/btntinhtt"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="Tính TT" />

            <Button                android:id="@+id/btntiep"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="Tiếp" />

            <Button                android:id="@+id/btnthongke"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="Thống kê" />
        </LinearLayout>

        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textAppearance="?android:attr/textAppearanceLarge"            android:text="Thong ke:"            android:textColor="#FFFFFF"            android:background="#000dff"            android:id="@+id/textView10" />

        <TableLayout            android:layout_width="match_parent"            android:orientation="horizontal"            android:layout_height="match_parent">
            <TableRow                android:layout_width="match_parent"                android:layout_height="match_parent">
            <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textAppearance="?android:attr/textAppearanceMedium"            android:text="Tong khach hang"            android:id="@+id/textView6" />

            <EditText                android:layout_width="150dp"                android:layout_height="wrap_content"                android:id="@+id/editTextTongKhachHang"                android:layout_column="13" />
</TableRow>
            <TableRow                android:layout_width="match_parent"                android:layout_height="match_parent">
            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:textAppearance="?android:attr/textAppearanceMedium"                android:text="Tong khach VIP"                android:id="@+id/textView7" />
            <EditText                android:layout_width="230dp"                android:layout_height="wrap_content"                android:id="@+id/editTextTongKhachVip"                android:layout_column="13" />
                </TableRow>
            <TableRow                android:layout_width="match_parent"                android:layout_height="match_parent">
            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:textAppearance="?android:attr/textAppearanceMedium"                android:text="Tong doanh thu"                android:id="@+id/textView8" />

            <EditText                android:layout_width="230dp"                android:layout_height="wrap_content"                android:id="@+id/editTextTongDoanhThu"                android:layout_column="13" />
                </TableRow>
        </TableLayout>

        <ImageButton            android:layout_width="69dp"            android:layout_height="67dp"            android:layout_gravity="right"            android:id="@+id/imageButtonThoat"            android:src="@drawable/exit" />


    </LinearLayout>
</ScrollView>

No comments:

Post a Comment