Cześć, mam pytanie odnośnie list. Do listy zakupów dodaję 4 wartości produktu takie jak nazwa, cena, ilość i waga. Muszę zrobić tak, aby suma wagi wszystkich produktów nie mogła przekroczyć np. 100kg. Jeśli użytkownik chciałby dodać kolejny produkt, program wyświetli komunikat o braku możliwości dodania kolejnego. I teraz pytanie, w którym miejscu i w jaki sposób ustawić maksymalną wartość wagi całej listy?
klasa Product
public class Product {
private String name;
private int weight;
private int count; //ilosc
private BigDecimal price; //cena
public Product(String name, int weight, BigDecimal price, int count) {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Product(String name, int weight, int count, BigDecimal price) {
this.name = name;
this.weight = weight;
this.count = count;
this.price = price;
}
}
oraz dodawanie do listy
public static Product getProduct()
{
try
{
System.out.println("Podaj nazwę produktu");
String name = getStringFromSystemIn();
System.out.println("Podaj wagę");
int weight = getIntFromSystemIn(1,20);
System.out.println("podaj ilość produktu");
int count = getIntFromSystemIn(1,200);
System.out.println("Podaj cenę");
BigDecimal price = getBigDecimalFromSystemIn(2);
return new Product(name, weight, count, price);
}
catch(Exception e)
{
System.err.println(e.getMessage());
return null;
}
}