From 998bc1f5b037b5c32f6a0ba56634cb112a823a2f Mon Sep 17 00:00:00 2001 From: PawelZielinski Date: Sun, 7 Feb 2021 21:16:10 +0100 Subject: [PATCH] Poprawiona klasa Cart --- .idea/vcs.xml | 6 ++ .../java/com/example/pizzujemyv3/Cart.java | 28 ++---- .../com/example/pizzujemyv3/CartActivity.java | 44 +++++++--- .../com/example/pizzujemyv3/CartAdapter.java | 85 +++++++++++++++++++ .../java/com/example/pizzujemyv3/Menu.java | 11 +-- .../java/com/example/pizzujemyv3/Pizza.java | 44 +++++++++- .../com/example/pizzujemyv3/PizzaAdapter.java | 9 +- app/src/main/res/layout/activity_cart.xml | 19 ++++- app/src/main/res/layout/cart_list_row.xml | 1 + app/src/main/res/layout/list_row.xml | 10 +++ 10 files changed, 214 insertions(+), 43 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 app/src/main/java/com/example/pizzujemyv3/CartAdapter.java diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/pizzujemyv3/Cart.java b/app/src/main/java/com/example/pizzujemyv3/Cart.java index 3a8677b..d7f3a1b 100644 --- a/app/src/main/java/com/example/pizzujemyv3/Cart.java +++ b/app/src/main/java/com/example/pizzujemyv3/Cart.java @@ -2,25 +2,17 @@ package com.example.pizzujemyv3; import java.util.ArrayList; - - - public class Cart { - private Pizza pizza; public ArrayList cartLista; - private int sum; - - public Cart(){ + private int totalPrice; + public Cart(ArrayList menuList){ + setCartLista(menuList); } - - - public void setCartLista(ArrayList menuLista) + public void setCartLista(ArrayList 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;} } diff --git a/app/src/main/java/com/example/pizzujemyv3/CartActivity.java b/app/src/main/java/com/example/pizzujemyv3/CartActivity.java index 4ffc82f..6ba4af1 100644 --- a/app/src/main/java/com/example/pizzujemyv3/CartActivity.java +++ b/app/src/main/java/com/example/pizzujemyv3/CartActivity.java @@ -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 pizzaArrayList; +ListView cLView; +ArrayList 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 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 pizzaArrayList = new ArrayList<>(); + pizzaArrayList = getIntent().getParcelableArrayListExtra("pizzas"); - Bundle bundleObject = getIntent().getExtras(); - pizzaArrayList = (ArrayList) 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() { diff --git a/app/src/main/java/com/example/pizzujemyv3/CartAdapter.java b/app/src/main/java/com/example/pizzujemyv3/CartAdapter.java new file mode 100644 index 0000000..cd04342 --- /dev/null +++ b/app/src/main/java/com/example/pizzujemyv3/CartAdapter.java @@ -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 { + private Context mContext; + private int mResource; + public CartAdapter(@NonNull Context context, int resource, @NonNull ArrayList 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; + } + +} diff --git a/app/src/main/java/com/example/pizzujemyv3/Menu.java b/app/src/main/java/com/example/pizzujemyv3/Menu.java index ed7d7d5..7c83f89 100644 --- a/app/src/main/java/com/example/pizzujemyv3/Menu.java +++ b/app/src/main/java/com/example/pizzujemyv3/Menu.java @@ -23,7 +23,7 @@ public class Menu extends AppCompatActivity { menuListView = findViewById(R.id.listView); ArrayList 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); }; }); diff --git a/app/src/main/java/com/example/pizzujemyv3/Pizza.java b/app/src/main/java/com/example/pizzujemyv3/Pizza.java index 7d06915..86ebb24 100644 --- a/app/src/main/java/com/example/pizzujemyv3/Pizza.java +++ b/app/src/main/java/com/example/pizzujemyv3/Pizza.java @@ -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 CREATOR = new Parcelable.Creator() { + @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); + } } diff --git a/app/src/main/java/com/example/pizzujemyv3/PizzaAdapter.java b/app/src/main/java/com/example/pizzujemyv3/PizzaAdapter.java index 405a0bd..990ab20 100644 --- a/app/src/main/java/com/example/pizzujemyv3/PizzaAdapter.java +++ b/app/src/main/java/com/example/pizzujemyv3/PizzaAdapter.java @@ -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 implements Serializable { +public class PizzaAdapter extends ArrayAdapter { private Context mContext; private int mResource; public PizzaAdapter(@NonNull Context context, int resource, @NonNull ArrayList objects) { @@ -32,7 +34,7 @@ public class PizzaAdapter extends ArrayAdapter 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 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 implements Serializable { return convertView; } - } diff --git a/app/src/main/res/layout/activity_cart.xml b/app/src/main/res/layout/activity_cart.xml index b6fce0c..e07afb2 100644 --- a/app/src/main/res/layout/activity_cart.xml +++ b/app/src/main/res/layout/activity_cart.xml @@ -39,15 +39,32 @@ android:textColor = "#ffffff" android:divider="#000000" android:dividerHeight="2dp" + android:layout_weight="0.1" > + + + + android:orientation="horizontal" + > \ No newline at end of file diff --git a/app/src/main/res/layout/list_row.xml b/app/src/main/res/layout/list_row.xml index 9857704..cabcc07 100644 --- a/app/src/main/res/layout/list_row.xml +++ b/app/src/main/res/layout/list_row.xml @@ -89,5 +89,15 @@ android:layout_marginTop="5dp" android:layout_toRightOf="@id/addCounter" android:background="@drawable/ic_minus" /> + \ No newline at end of file