Bernd Schatz
Authored Comments
Are the classes Bought, Pack and Unit not 100% Boilercode !?
There is no business there.
The interesting part (logic) is in the big while loop.
package OOP;
public class Oop
{
public static void main(String[] args)
{
Units units = new Units();
units.add("Rice","Best Family",10,5650,1);
units.add("Spaghetti","Best Family",1,327,10);
units.add("Sardines","Fresh Caught",3,2727,3);
units.add("Chickpeas","Southern Style",2,2600,5);
units.add("Lentils","Southern Style",2,2378,5);
units.add("Vegetable oil","Crafco",12,10020,1);
units.add("UHT milk","Atlantic",6,4560,2);
units.add("Flour","Neighbor Mills",10,5200,1);
units.add("Tomato sauce","Best Family",1,190,10);
units.add("Sugar","Good Price",1,565,10);
units.add("Tea","Superior",5,2720,2);
units.add("Coffee","Colombia Select",2,4180,5);
units.add("Tofu","Gourmet Choice",1,1580,10);
units.add("Bleach","Blanchite",5,3550,2);
units.add("Soap","Sunny Day",6,1794,2);
var valueIdeal = 5000;
var valueMax = Math.round(valueIdeal * 1.1);
while (units.notEmpty())
{
Hamper hamper = new Hamper(valueMax);
while(units.tryToAddUnitsToHamper(hamper));
hamper.print();
System.out.printf("Remaining units %d\n\n",units.size());
}
}
}
****************
package OOP;
import java.util.ArrayList;
public class Units
{
private List units = new ArrayList<>();
private static Random rnd = new Random();
public void add(String item, String brand, int unitCount, int overalPrice, int packs)
{
for(int i=0; i0;
}
public boolean tryToAddUnitsToHamper(Hamper hamper)
{
boolean canAdd = false;
var u = rnd.nextInt(units.size());
for (int o = 0; o < units.size(); o++)
{
var uo = (u + o) % units.size();
var unit = units.get(uo);
if ((units.size() < 3) || unit.canAddToHamper(hamper))
{
unit.addToHamper(hamper);
units.remove(uo);
canAdd = units.size() > 0;
break;
}
}
return canAdd;
}
public int size()
{
return units.size();
}
}
*******************
package OOP;
class Unit
{
private String item, brand;
private int price;
public Unit(String item, String brand, int price)
{
this.item = item;
this.brand = brand;
this.price = price;
}
public void addToHamper(Hamper hamper)
{
hamper.addUnit(this, this.price);
}
@Override
public String toString() { return String.format("%-25s%-25s%7d",item,brand,price); }
public boolean canAddToHamper(Hamper hamper)
{
return hamper.canAdd(this,price);
}
}
*******************
package OOP;
import java.util.ArrayList;
public class Hamper
{
private static int number = 0;
private List units = new ArrayList();
private long value;
private long size;
public Hamper(long size)
{
this.size = size;
number++;
}
public void addUnit(Unit unit,int price)
{
units.add(unit);
value+=price;
}
public void print()
{
System.out.printf("Hamper %d value %d:\n\n",number,value);
for(Unit unit:units)
System.out.println(unit);
}
public boolean canAdd(Unit unit,int price)
{
return((!units.contains(unit)) && ((value + price) < size));
}
}
************************
IMHO more (OOP) readable code
(also not getter methods that would violate iformation hiding).
ok, so i have probably not understood the intention of your example.
Regarding getter and setters: the remark was not *how* you expose the internals of your class, but *that* you expose it.
e.g. --> https://www.yegor256.com/2014/09/16/getters-and-setters-are-evil.html