Witam, mam problem gdyż tak jak w temacie nie rozumiem za bardzo dlaczego Funkcja nie przekazuje wartości do pola klasy nadrzędnej. Dokładniej chodzi o funkcje sell() w klasie Product, jest tam metoda, która powinna ustawiać wartość pola moneyInCashRegister w klasie nadrzędnej. Po wywołaniu metody sell(); do wartosc pola moneyInCashRegister powinna zostać dodana cena produktu(price) i ilosc produktu zmniejsza sie o jednen. Uczę się obiektówki, dlatego jeśli mam jakieś złe przyzwyczajenia lub robię coś nie tak bardzo proszę o wskazanie błedu/ów. Z góry dziękuję :)
Main
package com.product;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<Product> products = new LinkedList<>();
products.add(0, new Phone("Galaxy s7","Samsung",2300.5,2));
products.add(1, new Phone("Iphone 7","Apple",4000.2,4));
Shop shop = new Shop("Telefonix","Trzeciego maja",0.0);
shop.setProduct(products);
shop.setMoneyInCashRegister(products.get(0).getPrice()); // To dziala
System.out.println(shop.getMoneyInCashRegister());
shop.getProduct(products).get(0).sell(2); // A to nie dziala, nie rozumiem dlaczego :C
System.out.println(shop.getMoneyInCashRegister());
}
}
Shop
package com.product;
import java.util.LinkedList;
/**
* Created by Mateusz on 2017-06-25.
*/
public class Shop{
private String storeName;
private String address;
private double moneyInCashRegister;
private LinkedList<Product> product = new LinkedList<>();
public Shop(){
storeName="Nie podano nazwy sklepu";
address="Nie podano adressu";
moneyInCashRegister=0.0;
}
public Shop(String storeName, String address) {
this.storeName = storeName;
this.address = address;
moneyInCashRegister = -1.00;
}
public Shop(String storeName, String address, double moneyInCashRegister) {
this.storeName = storeName;
this.address = address;
this.moneyInCashRegister = moneyInCashRegister;
}
public LinkedList<Product> getProduct(LinkedList<Product> products) {
return product;
}
public void setProduct(LinkedList<Product> product) {
this.product = product;
}
public String getStoreName() {
return storeName;
}
public void setStoreName(String storeName) {
this.storeName = storeName;
}
public double getMoneyInCashRegister() {
return moneyInCashRegister;
}
public void setMoneyInCashRegister(double moneyInCashRegister) {
this.moneyInCashRegister = moneyInCashRegister;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Product
package com.product;
/**
* Created by Mateusz on 2017-06-23.
*/
public abstract class Product extends Shop implements Sellable {
private String name;
private String producer;
private double price;
private boolean isSold;
private int quantity;
public Product()
{
this.name="Nie podano";
this.producer="Nie podano";
this.price=-1.0;
}
public Product(String name, String producer, double price)
{
this.name=name;
this.producer=producer;
this.price=price;
}
public Product(String name, String producer, double price, int quantity)
{
this.name=name;
this.producer=producer;
this.price=price;
this.quantity=quantity;
}
/**
* Funkcja do sprzedawania, jeśli produkt istnieje jego ilosc zostanie zmniejszona o jeden.
* */
public final void sell() {
setSold(quantity);
if(isSold()==false)
{
System.out.println("Sprzedaje "+getProducer()+" "+getName());
setMoneyInCashRegister(getMoneyInCashRegister()+getPrice());
setQuantity(getQuantity()-1);
}else
System.out.println("Sprzedano juz wszystkie "+getProducer()+" "+getName());
}
/**
* Funkcja do sprzedawainia na ilosc, jako argumnet podajmeny ile produktow chcemy sprzedac, za kazdym razem, jesli liczba
* produktu jest >0 to oddejmuje od jego ilosci jeden (petla)*/
public final void sell(int quantity)
{
for(int i=0;i<quantity;i++) {
if (isSold == false)
sell();
else
break;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProducer() {
return producer;
}
public void setProducer(String producer) {
this.producer = producer;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
/**
* Jesli ilosc twoaru jest wieksza od zera to isSold wynosi false, jesli nie to true*/
public void setSold(int quantity)
{
if(quantity>0)
isSold=false;
else
isSold=true;
}
public boolean isSold() {
return isSold;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public String toString()
{
return "Model : "+getName()+"\nProducent : "+getProducer()+"\nCena : "+getPrice()+"\nIlosc : "+getQuantity();
}
}
Dorzucam dodatkowo Phone
package com.product;
import com.product.Product;
/**
* Created by Mateusz on 2017-06-23.
*/
public class Phone extends Product {
private String screen;
private String proccesor;
private String battery;
private String video;
private int ram;
private int space;
public Phone() {
}
public Phone(String name, String producer, double price, int quantity) {
super(name, producer, price, quantity);
}
public Phone(String name, String producer, double price, int quantity, String screen, String proccesor, String battery, String video, int ram, int space) {
super(name, producer, price, quantity);
this.screen = screen;
this.proccesor = proccesor;
this.battery = battery;
this.video = video;
this.ram = ram;
this.space = space;
}
/**
* Zwraca wartosci pol klasy\
* */
@Override
public String toString()
{
return "Model : "+getName()+"\nProducent : "+getProducer()+"\nCena : "+getPrice()+"\nEkran : "+getScreen()+"\nProcesor : "+getProccesor()+"\nBateria : "+getBattery()+"\nAparat : "+getVideo()+"\nRAM : "+getRam()+"\nDysk : "+getSpace();
}
public String getScreen() {
return screen;
}
public void setScreen(String screen) {
this.screen = screen;
}
public String getProccesor() {
return proccesor;
}
public void setProccesor(String proccesor) {
this.proccesor = proccesor;
}
public String getBattery() {
return battery;
}
public void setBattery(String battery) {
this.battery = battery;
}
public String getVideo() {
return video;
}
public void setVideo(String video) {
this.video = video;
}
public int getRam() {
return ram;
}
public void setRam(int ram) {
this.ram = ram;
}
public int getSpace() {
return space;
}
public void setSpace(int space) {
this.space = space;
}
}