Poprawiona klasa Cart
This commit is contained in:
parent
4474519d08
commit
998bc1f5b0
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -2,25 +2,17 @@ package com.example.pizzujemyv3;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
|
||||
|
||||
public class Cart {
|
||||
private Pizza pizza;
|
||||
public ArrayList<Pizza> cartLista;
|
||||
private int sum;
|
||||
|
||||
public Cart(){
|
||||
private int totalPrice;
|
||||
public Cart(ArrayList<Pizza> menuList){
|
||||
|
||||
setCartLista(menuList);
|
||||
}
|
||||
|
||||
|
||||
public void setCartLista(ArrayList<Pizza> menuLista)
|
||||
public void setCartLista(ArrayList<Pizza> menuList)
|
||||
{
|
||||
menuLista.forEach((pizza) -> addToCart(getPizza()));
|
||||
|
||||
menuList.forEach((pizza) -> addToCart(pizza));
|
||||
}
|
||||
|
||||
public void addToCart(Pizza pizza){
|
||||
if(pizza.getOnOrder()>0)
|
||||
{
|
||||
@ -29,15 +21,11 @@ public class Cart {
|
||||
}
|
||||
|
||||
|
||||
public void setSum()
|
||||
public void setTotalPrice()
|
||||
{
|
||||
cartLista.forEach(pizza -> sum+= pizza.getPrice());
|
||||
cartLista.forEach(pizza -> totalPrice+= pizza.getSumPrice());
|
||||
}
|
||||
|
||||
public int getSum(){return sum;}
|
||||
|
||||
public Pizza getPizza() {
|
||||
return pizza;
|
||||
}
|
||||
public int getTotalPrice(){return totalPrice;}
|
||||
}
|
||||
|
||||
|
@ -1,37 +1,57 @@
|
||||
package com.example.pizzujemyv3;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class CartActivity extends AppCompatActivity {
|
||||
ListView cartListView;
|
||||
ArrayList<Pizza> pizzaArrayList;
|
||||
ListView cLView;
|
||||
ArrayList<Pizza> pizzaArrayList = new ArrayList<>();
|
||||
Switch oSwitch;
|
||||
Button oButton;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_cart);
|
||||
cLView = findViewById(R.id.cartListView);
|
||||
|
||||
pizzaArrayList.add(new Pizza(1, "Margarita", 24, R.drawable.img1 , "sos pomidorowy\nmozzarella fior di latte"));
|
||||
pizzaArrayList.add(new Pizza(2, "Marinara", 22, R.drawable.img2, "sos pomidorowy\noregano\nczosnek\nbazylia"));
|
||||
pizzaArrayList.add(new Pizza(3, "Napoli", 26, R.drawable.img3 , "sos pomidorowy\nmozzarella fior di latte\nsalami napoli"));
|
||||
pizzaArrayList.add(new Pizza(4, "Parma", 26, R.drawable.img4 ,"sos pomidorowy\nmozzarella fior di latte\ncebula czerwona\nszynka parmenska"));
|
||||
pizzaArrayList.add(new Pizza(5, "Diavola", 28, R.drawable.img5 ,"sos pomidorowy\nmozzarella fior di latte\ncebula czerwona\nsalami spianata piccante"));
|
||||
pizzaArrayList.get(3).addOnOrder();
|
||||
pizzaArrayList.get(1).addOnOrder();
|
||||
ArrayList<Pizza> cartArrayList = new ArrayList<>();
|
||||
Cart cart = new Cart(pizzaArrayList);
|
||||
TextView totalPrice = (TextView)findViewById(R.id.totalPrice);
|
||||
totalPrice.setText(cart.getTotalPrice());
|
||||
PizzaAdapter pizzaAdapter = new PizzaAdapter(this,R.layout.list_row,cart.cartLista);
|
||||
cLView.setAdapter(pizzaAdapter);
|
||||
/*
|
||||
cartListView = findViewById(R.id.listView);
|
||||
pizzaArrayList = new ArrayList<>();
|
||||
pizzaArrayList = getIntent().getParcelableArrayListExtra("pizzas");
|
||||
PizzaAdapter pizzaAdapter = new PizzaAdapter(this,R.layout.list_row,pizzaArrayList);
|
||||
cartListView.setAdapter(pizzaAdapter);
|
||||
*/
|
||||
/* cartListView = findViewById(R.id.cartListView);
|
||||
ArrayList<Pizza> pizzaArrayList = new ArrayList<>();
|
||||
pizzaArrayList = getIntent().getParcelableArrayListExtra("pizzas");
|
||||
|
||||
|
||||
Bundle bundleObject = getIntent().getExtras();
|
||||
pizzaArrayList = (ArrayList<Pizza>) bundleObject.getSerializable("pizzas");
|
||||
|
||||
|
||||
Cart cart = new Cart();
|
||||
|
||||
cartListView = findViewById(R.id.cartListView);
|
||||
cart.setCartLista(pizzaArrayList);
|
||||
PizzaAdapter cartAdapter = new PizzaAdapter(this,R.layout.cart_list_row,cart.cartLista);
|
||||
cartListView.setAdapter(cartAdapter);
|
||||
Cart cart = new Cart(pizzaArrayList);
|
||||
PizzaAdapter pizzaAdapter = new PizzaAdapter(this,R.layout.cart_list_row,cart.cartLista);
|
||||
cartListView.setAdapter(pizzaAdapter);*/
|
||||
|
||||
oButton = findViewById(R.id.zamow);
|
||||
oButton.setOnClickListener(new View.OnClickListener() {
|
||||
|
85
app/src/main/java/com/example/pizzujemyv3/CartAdapter.java
Normal file
85
app/src/main/java/com/example/pizzujemyv3/CartAdapter.java
Normal file
@ -0,0 +1,85 @@
|
||||
package com.example.pizzujemyv3;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CartAdapter extends ArrayAdapter<Pizza> {
|
||||
private Context mContext;
|
||||
private int mResource;
|
||||
public CartAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Pizza> objects) {
|
||||
super(context, resource, objects);
|
||||
this.mContext = context;
|
||||
this.mResource = resource;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
|
||||
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
|
||||
|
||||
convertView = layoutInflater.inflate(mResource,parent,false);
|
||||
|
||||
TextView txtSumPrice = convertView.findViewById(R.id.sumTxtPrice);
|
||||
|
||||
TextView addCounter = convertView.findViewById(R.id.addCounter);
|
||||
|
||||
ImageView imageView = convertView.findViewById(R.id.image);
|
||||
|
||||
TextView txtName = convertView.findViewById(R.id.txtName);
|
||||
|
||||
TextView txtPrice = convertView.findViewById(R.id.txtPrice);
|
||||
|
||||
TextView txtContent = convertView.findViewById(R.id.txtContent);
|
||||
|
||||
Button minButton = convertView.findViewById(R.id.minButton);
|
||||
|
||||
Button addButton = convertView.findViewById(R.id.addButton);
|
||||
|
||||
imageView.setImageResource(getItem(position).getResID());
|
||||
addCounter.setText((getItem(position).getStringOnOrder()));
|
||||
|
||||
txtName.setText(getItem(position).getName());
|
||||
|
||||
txtSumPrice.setText(getItem(position).getStringSumPrice());
|
||||
|
||||
txtPrice.setText(getItem(position).getStringPrice());
|
||||
|
||||
txtContent.setText(getItem(position).getDes());
|
||||
|
||||
minButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
getItem(position).minOnOrder();
|
||||
addCounter.setText(((getItem(position).getStringOnOrder())));
|
||||
}
|
||||
});
|
||||
addButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
|
||||
|
||||
public void onClick(View v) {
|
||||
Toast.makeText(getContext(), "Dodano do koszyka!", Toast.LENGTH_SHORT).show();
|
||||
getItem(position).addOnOrder();
|
||||
addCounter.setText((getItem(position).getStringOnOrder()));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return convertView;
|
||||
}
|
||||
|
||||
}
|
@ -23,7 +23,7 @@ public class Menu extends AppCompatActivity {
|
||||
menuListView = findViewById(R.id.listView);
|
||||
ArrayList<Pizza> arrayList = new ArrayList<>();
|
||||
|
||||
arrayList.add(new Pizza(1, "Margarita", 24, R.drawable.img1 , "sos pomidorowy\nmozzarella fior di latte"));
|
||||
arrayList.add(new Pizza(1, "Margarita", 24, R.drawable.img1 , "sos pomidorowy\nmozzarella fior di latte"));
|
||||
arrayList.add(new Pizza(2, "Marinara", 22, R.drawable.img2, "sos pomidorowy\noregano\nczosnek\nbazylia"));
|
||||
arrayList.add(new Pizza(3, "Napoli", 26, R.drawable.img3 , "sos pomidorowy\nmozzarella fior di latte\nsalami napoli"));
|
||||
arrayList.add(new Pizza(4, "Parma", 26, R.drawable.img4 ,"sos pomidorowy\nmozzarella fior di latte\ncebula czerwona\nszynka parmenska"));
|
||||
@ -31,13 +31,14 @@ public class Menu extends AppCompatActivity {
|
||||
|
||||
PizzaAdapter pizzaAdapter = new PizzaAdapter(this,R.layout.list_row,arrayList);
|
||||
menuListView.setAdapter(pizzaAdapter);
|
||||
cartButton = findViewById(R.id.cart);
|
||||
|
||||
|
||||
cartButton = findViewById(R.id.cart);
|
||||
|
||||
cartButton.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
Intent intent = new Intent(Menu.this, CartActivity.class);
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putSerializable("pizzas",arrayList);
|
||||
intent.putExtras(bundle);
|
||||
intent.putParcelableArrayListExtra("pizzas",arrayList);
|
||||
startActivity(intent);
|
||||
};
|
||||
});
|
||||
|
@ -1,8 +1,11 @@
|
||||
package com.example.pizzujemyv3;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Pizza implements Serializable{
|
||||
public class Pizza implements Parcelable {
|
||||
private int id;
|
||||
private String name;
|
||||
private int price;
|
||||
@ -21,6 +24,29 @@ public class Pizza implements Serializable{
|
||||
}
|
||||
|
||||
|
||||
protected Pizza(Parcel in) {
|
||||
id = in.readInt();
|
||||
name = in.readString();
|
||||
price = in.readInt();
|
||||
resID = in.readInt();
|
||||
des = in.readString();
|
||||
onOrder = in.readInt();
|
||||
sumPrice = in.readInt();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static final Parcelable.Creator<Pizza> CREATOR = new Parcelable.Creator<Pizza>() {
|
||||
@Override
|
||||
public Pizza createFromParcel(Parcel in) {
|
||||
return new Pizza(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pizza[] newArray(int size) {
|
||||
return new Pizza[size];
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pizza{" +
|
||||
@ -80,4 +106,20 @@ public class Pizza implements Serializable{
|
||||
public String getStringOnOrder(){return String.valueOf((onOrder));}
|
||||
public int getSumPrice(){return sumPrice;}
|
||||
public String getStringSumPrice(){return String.valueOf(sumPrice);}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeInt(id);
|
||||
dest.writeString(name);
|
||||
dest.writeInt(price);
|
||||
dest.writeInt(resID);
|
||||
dest.writeString(des);
|
||||
dest.writeInt(onOrder);
|
||||
dest.writeInt(sumPrice);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package com.example.pizzujemyv3;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.LayoutInflater;
|
||||
@ -15,7 +17,7 @@ import android.widget.Toast;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class PizzaAdapter extends ArrayAdapter<Pizza> implements Serializable {
|
||||
public class PizzaAdapter extends ArrayAdapter<Pizza> {
|
||||
private Context mContext;
|
||||
private int mResource;
|
||||
public PizzaAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Pizza> objects) {
|
||||
@ -32,7 +34,7 @@ public class PizzaAdapter extends ArrayAdapter<Pizza> implements Serializable {
|
||||
|
||||
convertView = layoutInflater.inflate(mResource,parent,false);
|
||||
|
||||
// TextView txtSumPrice = convertView.findViewById(R.id.sumTxtPrice);
|
||||
TextView txtSumPrice = convertView.findViewById(R.id.sumTxtPrice);
|
||||
|
||||
TextView addCounter = convertView.findViewById(R.id.addCounter);
|
||||
|
||||
@ -53,7 +55,7 @@ public class PizzaAdapter extends ArrayAdapter<Pizza> implements Serializable {
|
||||
|
||||
txtName.setText(getItem(position).getName());
|
||||
|
||||
// txtSumPrice.setText(getItem(position).getStringSumPrice());
|
||||
txtSumPrice.setText(getItem(position).getStringSumPrice());
|
||||
|
||||
txtPrice.setText(getItem(position).getStringPrice());
|
||||
|
||||
@ -81,5 +83,4 @@ public class PizzaAdapter extends ArrayAdapter<Pizza> implements Serializable {
|
||||
return convertView;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -39,15 +39,32 @@
|
||||
android:textColor = "#ffffff"
|
||||
android:divider="#000000"
|
||||
android:dividerHeight="2dp"
|
||||
android:layout_weight="0.1"
|
||||
>
|
||||
</ListView>
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="0.9"
|
||||
android:background="#20FFFFFF"
|
||||
android:textColor="@color/white"
|
||||
android:shadowColor="#55000000"
|
||||
android:shadowDx="6"
|
||||
android:shadowDy="6"
|
||||
android:shadowRadius="6"
|
||||
android:id="@+id/totalPrice"
|
||||
|
||||
></TextView>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1"
|
||||
android:orientation="horizontal" >
|
||||
android:orientation="horizontal"
|
||||
>
|
||||
|
||||
<Switch
|
||||
android:id="@+id/dowoz"
|
||||
|
@ -98,6 +98,7 @@
|
||||
android:layout_toRightOf="@id/image"
|
||||
android:layout_marginLeft="20dp"
|
||||
android:id="@+id/sumTxtPrice"
|
||||
android:text="0"
|
||||
|
||||
/>
|
||||
</RelativeLayout>
|
@ -89,5 +89,15 @@
|
||||
android:layout_marginTop="5dp"
|
||||
android:layout_toRightOf="@id/addCounter"
|
||||
android:background="@drawable/ic_minus" />
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/addCounter"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_toRightOf="@id/image"
|
||||
android:layout_marginLeft="20dp"
|
||||
android:id="@+id/sumTxtPrice"
|
||||
android:text="0"
|
||||
|
||||
/>
|
||||
</RelativeLayout>
|
Loading…
Reference in New Issue
Block a user