vendredi 30 octobre 2015

how to show added items in a new line when button is clicked

I'm new to android development I have been trying to create a Point Of Sale application Android Studio ..I'm trying to make the app show the items that the user clicked in a separate line instead of adding it to the same text view giving me the total of it.

Any other helpful tips to improve the app are also welcome

Thanks!

public class MainActivity extends AppCompatActivity {

TextView proutput, poutput, subtotal, totalitems, discounts, tax, total, qoutput;
ListView listView;
Button remove1;
double price = 0.00;
double pricef = 0.00;
double totalPrice = 0.00;
double taxes = 0.05;
int quantity = 0;
ButtonClickListener btnClick;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    poutput = (TextView) findViewById(R.id.POutput);
    qoutput = (TextView) findViewById(R.id.QOutput);
    proutput = (TextView) findViewById(R.id.PROutput);
    subtotal= (TextView) findViewById(R.id.STotal);
    totalitems = (TextView) findViewById(R.id.Total_Items);
    discounts = (TextView) findViewById(R.id.Discount);
    tax = (TextView) findViewById(R.id.Tax);
    total = (TextView) findViewById(R.id.FTotal);
    remove1 = (Button) findViewById(R.id.Remove1);




    btnClick= new ButtonClickListener();

    //array of id's
    int buttonid[]={ R.id.Remove1, R.id.Chicken, R.id.Beef,R.id.Pork, R.id.Fish,R.id.Milk,
            R.id.Eggs, R.id.Potato, R.id.Onions, R.id.Cereal, R.id.Chips,
            R.id.Tomato, R.id.Cookies, R.id.Hair_Gel, R.id.Shower_Gel, R.id.Soap,
            R.id.Apple, R.id.Avocado, R.id.Lettuce, R.id.Garlic, R.id.Banana,
            R.id.Total, R.id.Clear};

    for(int id:buttonid){
        View v= findViewById(id);
        v.setOnClickListener(btnClick);
    }
}

public class ButtonClickListener implements OnClickListener{
    public void onClick(View v){

        switch(v.getId()) {

            case R.id.Chicken:

                remove1.setText("X");
                poutput.setText("Chicken");
                ++quantity;
                pricef = 10.25;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Beef:

                remove1.setText("X");
                poutput.setText("Beef");
                ++quantity;
                pricef = 12.45;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Pork:

                remove1.setText("X");
                poutput.setText("Pork");
                ++quantity;
                pricef = 8.35;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Fish:

                remove1.setText("X");
                poutput.setText("Fish");
                ++quantity;
                pricef = 14.15;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Apple:

                remove1.setText("X");
                poutput.setText("Apple");
                ++quantity;
                pricef = 1.23;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Tomato:

                remove1.setText("X");
                poutput.setText("Tomato");
                ++quantity;
                pricef = 1.53;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Onions:

                remove1.setText("X");
                poutput.setText("Onions");
                ++quantity;
                pricef = 0.85;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Garlic:

                remove1.setText("X");
                poutput.setText("Garlic");
                ++quantity;
                pricef = 0.54;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Avocado:

                remove1.setText("X");
                poutput.setText("Avocado");
                ++quantity;
                pricef = 3.25;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Lettuce:

                remove1.setText("X");
                poutput.setText("Lettuce");
                ++quantity;
                pricef = 2.35;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Banana:

                remove1.setText("X");
                poutput.setText("Banana");
                ++quantity;
                pricef = 2.05;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Cereal:

                remove1.setText("X");
                poutput.setText("Cereal");
                ++quantity;
                pricef =  8.10;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Cookies:

                remove1.setText("X");
                poutput.setText("Cookies");
                ++quantity;
                pricef = 4.99;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Chips:

                remove1.setText("X");
                poutput.setText("Chips");
                ++quantity;
                pricef = 1.85;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Milk:

                remove1.setText("X");
                poutput.setText("Milk");
                ++quantity;
                pricef = 2.75;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Eggs:

                remove1.setText("X");
                poutput.setText("Eggs");
                ++quantity;
                pricef = 4.25;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Hair_Gel:

                remove1.setText("X");
                poutput.setText("Hair Gel");
                ++quantity;
                pricef = 5.34;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Shower_Gel:

                remove1.setText("X");
                poutput.setText("Shower Gel");
                ++quantity;
                pricef = 4.88;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Soap:

                remove1.setText("X");
                poutput.setText("Soap");
                ++quantity;
                pricef = 3.85;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;

            case R.id.Potato:

                remove1.setText("X");
                poutput.setText("Potato");
                ++quantity;
                pricef = 2.55;
                price = quantity * pricef;
                qoutput.setText(String.valueOf(quantity));
                proutput.setText(String.format("%.2f", price));
                break;




            case R.id.Clear:
                quantity = 0;
                taxes = 0.0;
                totalPrice = 0.0;
                price = 0;
                remove1.setText("");
                poutput.setText("");
                qoutput.setText("");
                proutput.setText("");
                subtotal.setText("Sub Total  ");
                totalitems.setText("Total Items  ");
                discounts.setText("Discount  ");
                total.setText("Total  ");
                tax.setText("Tax(%5)  ");
                break;

            case R.id.Total:
                //taxes prices incremented error
                double tax2 = 0.05;
                taxes = tax2 * price;
                totalPrice = tax2 + price;
                subtotal.setText(String.format( "    Sub Total  %.2f", price ));
                totalitems.setText("Total Items  " + String.valueOf(quantity));
                discounts.setText("Discount  ");
                tax.setText(String.format( "Tax(%%5) %.2f", taxes));
                total.setText(String.format( "Total  %.2f", totalPrice ));
                break;

            case R.id.Remove1:
                --quantity;
                if (quantity <= 0)
                {
                    quantity = 0;
                    remove1.setText("");
                    poutput.setText("");
                    qoutput.setText("");
                    proutput.setText("");
                }
                else
                {
                    price =  pricef * quantity;
                    qoutput.setText(String.valueOf(quantity));
                    proutput.setText(String.format("%.2f", price));
                }
                break;

        }
    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="horizontal"
android:weightSum="2"
android:background="#43378dff">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="20"
    android:layout_weight="1">

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product1"
            android:id="@+id/Chicken"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#90ff2030" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product5"
            android:id="@+id/Milk"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#92fffa34" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product9"
            android:id="@+id/Apple"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#622aff00" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product13"
            android:id="@+id/Avocado"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#622aff00" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product17"
            android:id="@+id/Cereal"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#9bffb103" />

    </LinearLayout>

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product2"
            android:id="@+id/Beef"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#90ff2030" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product6"
            android:id="@+id/Eggs"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#92fffa34" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product10"
            android:id="@+id/Lettuce"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#622aff00" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product14"
            android:id="@+id/Potato"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#622aff00" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product18"
            android:id="@+id/Soap"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#4eff00dc" />
    </LinearLayout>

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product3"
            android:id="@+id/Pork"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#90ff2030" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product7"
            android:id="@+id/Tomato"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#622aff00" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product11"
            android:id="@+id/Onions"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#622aff00" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product15"
            android:id="@+id/Chips"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#9bffb103" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product19"
            android:id="@+id/Shower_Gel"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#4eff00dc" />
    </LinearLayout>

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product4"
            android:id="@+id/Fish"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#90ff2030" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product8"
            android:id="@+id/Banana"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#622aff00" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product12"
            android:id="@+id/Garlic"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#622aff00" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product16"
            android:id="@+id/Cookies"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#9bffb103" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Product20"
            android:id="@+id/Hair_Gel"
            android:layout_weight="1"
            android:textSize="9sp"
            android:textStyle="bold|italic"
            android:background="#4eff00dc" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layout_marginLeft="10dp"
    android:weightSum="8">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:weightSum="4"
        android:background="#fffc84">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="        X"
            android:id="@+id/Remove"
            android:layout_weight="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Product"
            android:id="@+id/Product"
            android:layout_weight="1"
            android:layout_marginLeft="5dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Quantity"
            android:id="@+id/Quantity"
            android:layout_weight="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Price "
            android:id="@+id/Price"
            android:layout_weight="1" />
    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="6"
        android:background="#ffffff">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/Remove1"
            android:background="#ffffff"
            android:layout_weight="1"
            android:textStyle="bold" />
    </ScrollView>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:weightSum="2"
        android:background="#49ffff07">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Total Items"
            android:id="@+id/Total_Items"
            android:layout_weight="1"
            android:layout_marginLeft="6dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sub Total"
            android:id="@+id/STotal"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:weightSum="2"
        android:background="#49ffff07">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Discount"
            android:id="@+id/Discount"
            android:layout_weight="1"
            android:layout_marginLeft="6dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tax(%5)"
            android:id="@+id/Tax"
            android:layout_weight="1"
            android:layout_marginLeft="10dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:weightSum="1"
        android:background="#49ffff07">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Total"
            android:id="@+id/FTotal"
            android:layout_weight="1"
            android:layout_marginLeft="6dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:layout_weight=".5">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Clear"
            android:id="@+id/Clear"
            android:layout_weight="1"
            android:textStyle="bold|italic"
            android:background="#dfff000d" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Total_Result"
            android:id="@+id/Total"
            android:layout_weight="1"
            android:textStyle="bold|italic"
            android:background="#bc38ff00" />

    </LinearLayout>

</LinearLayout>

Aucun commentaire:

Enregistrer un commentaire