Witam, mam pewien problem, posiadam dwie klasy Position, Order. W klasie Order mam stworzoną metodę calculateProduct(name), gdzie za pomocą Hashmapy obliczam wartość danej pozycji. Chciałbym wartość tej pozycji umieścić w metodzie toString w klasie Order. Niestety z racji tego że metoda toString nie przyjmuje argumentu, nie mam jak przekazać wartości z metody CalculateProduct. Poniżej kod:
package programming.com.pl;
import java.util.Objects;
public class Position {
private String name;
private double price = 0;
private int quantity = 0;
public Position(String name, double price, int quantity){
this.name = name;
this.price = price;
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public String toString(){
String str = String.format("%4s,%4s zł,%2s szt.", name,price,quantity);
return str;
}
@Override
public boolean equals(Object other){
if(this == other){
return true;
}
if( other instanceof Position){
Position p = (Position) other;
return name.equals(p.name) && price == p.price && quantity == p.quantity;
}
return false;
}
@Override
public int hashCode(){
return Objects.hash(name,price,quantity);
}
}
package programming.com.pl;
import java.util.ArrayList;
import java.util.HashMap;
public class Order {
//final private ArrayList<Position> positions = new ArrayList<>();
final private HashMap<String,Position> positions = new HashMap<String,Position>();
double calculateProduct(String name){
double value = positions.get(name).getPrice()*positions.get(name).getQuantity();
return value;
}
void addPosition(String name, Position p) {
positions.put(name,p);
}
void deletePosition(String name) {
positions.remove(name);
}
public String toString() {
return String.format("%f",calculateProduct(name));
}
}