В дополнение к проблемам с кодом, указанным DevilsHnd, я хотел направить вас в другом направлении.
- Использование потоков для получения отчетов, которые вы искали, и
- Вместо использования строк для представления таких вещей, как Пункт назначения, Тип посетителя и День посещения, использование перечислений для представления этих концепций можетнемного очистите ваш код и проясните, как работает схема дисконтирования.
Я соответствующим образом реорганизовал ваш код
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.summingDouble;
public class Visitor {
public enum VisitorType {
Child, Senior, Other
}
public enum VisitType {
Weekday,Weekend
}
// represents each Destination as well as all related discount rates.
public enum Destination {
Disneyland {
{
visitorDiscount.put(VisitorType.Child, 5.0);
visitorDiscount.put(VisitorType.Senior, 10.0);
visitTypeDiscount.put(VisitType.Weekday, 15.0);
}
}
, UniversalStudios {
{
visitorDiscount.put(VisitorType.Child, 10.0);
visitorDiscount.put(VisitorType.Senior, 15.0);
visitTypeDiscount.put(VisitType.Weekday, 20.0);
}
};
protected Map<VisitorType,Double> visitorDiscount= new HashMap();
protected Map<VisitType,Double> visitTypeDiscount= new HashMap();
public double getVisitorTypeDiscount(VisitorType visitorType) {
Double discount = visitorDiscount.get(visitorType);
return discount == null ? 0.0 : discount;
}
public double getVisitTypeDiscount(VisitType visitType) {
Double discount = visitTypeDiscount.get(visitType);
return discount == null ? 0.0 : discount;
}
};
public static class VisitorPackage {
private final String visitorName;
private final VisitorType visitorType;
private final VisitType visitDay;
private final Destination destination;
private final double totalCost;
public VisitorPackage(String newVisitorName, VisitorType newVisitorType,
VisitType newVisitDay, Destination newDestination, double newTotalCost) {
visitorName = newVisitorName;
visitorType = newVisitorType;
visitDay = newVisitDay;
totalCost = newTotalCost;
destination = newDestination;
}
public String getDestinationPark() {
return destination.toString();
}
public double getTotalCost() {
return totalCost;
}
public double getDiscountedCost() {
double visitorTypeDiscount = totalCost * destination.getVisitorTypeDiscount(visitorType)/100;
return totalCost - visitorTypeDiscount - destination.getVisitTypeDiscount(visitDay);
}
public Destination getDestination() { return destination; }
public void printInfo() {
System.out.print(visitorName + " - ");
System.out.print(destination + " - ");
System.out.printf("$%.2f -> ", getTotalCost());
System.out.print("$");
System.out.printf("%.2f", getDiscountedCost());
if (visitorType == VisitorType.Other) {
System.out.print(" ***** Discount cannot be applied to "
+ visitorName);
}
System.out.println();
}
}
public static void main(String args[]) {
double totalAttendeeCost = 0.0;
//Using 1 array
VisitorPackage[] attendee = new VisitorPackage[7];
attendee[0] = new VisitorPackage("Mickey", VisitorType.Senior, VisitType.Weekday,
Destination.Disneyland, 75.0);
attendee[1] = new VisitorPackage("Donald", VisitorType.Child, VisitType.Weekday,
Destination.Disneyland, 75.0);
attendee[2] = new VisitorPackage("Minnie", VisitorType.Other, VisitType.Weekend,
Destination.Disneyland, 75.0);
attendee[3] = new VisitorPackage("Goofie", VisitorType.Other, VisitType.Weekend,
Destination.Disneyland, 75.0);
attendee[4] = new VisitorPackage("Harry", VisitorType.Child, VisitType.Weekend, Destination.UniversalStudios, 60.0);
attendee[5] = new VisitorPackage("Hermoine", VisitorType.Senior, VisitType.Weekend,
Destination.UniversalStudios, 60.0);
attendee[6] = new VisitorPackage("Ron", VisitorType.Other, VisitType.Weekday, Destination.UniversalStudios, 60.0);
// Print a report grouped by Destination showing all VisitoerPackages and their discounted costs with subtotals
Arrays.stream(attendee)
.collect(groupingBy(VisitorPackage::getDestination))
.entrySet().stream()
.forEach(e->{
System.out.println("Summary for "+e.getKey());
e.getValue().stream().forEach(VisitorPackage::printInfo);
Double total = e.getValue().stream().collect(summingDouble(VisitorPackage::getDiscountedCost));
System.out.printf("Total Discounted Cost for %s = $%.2f\n",e.getKey(),total);
System.out.println("------------------------------------------------------------");
});
// Here's a way to reduce the dataset to map of sub-totals keyed by destination.
Map<Destination,Double> discountedCostByDest = Arrays.stream(attendee)
.collect(groupingBy(
VisitorPackage::getDestination,
summingDouble(VisitorPackage::getDiscountedCost)));
System.out.println(discountedCostByDest);
// compute and display the total cost.
Double totalDiscountedCost = Arrays.stream(attendee)
.collect(summingDouble(VisitorPackage::getDiscountedCost));
System.out.printf("Grand Total = $%.2f\n", totalDiscountedCost);
}
}
Этот код выдает следующий вывод:
Summary for UniversalStudios
Harry - UniversalStudios - $60.00 -> $54.00
Hermoine - UniversalStudios - $60.00 -> $51.00
Ron - UniversalStudios - $60.00 -> $40.00 ***** Discount cannot be applied to Ron
Total Discounted Cost for UniversalStudios = $145.00
------------------------------------------------------------
Summary for Disneyland
Mickey - Disneyland - $75.00 -> $52.50
Donald - Disneyland - $75.00 -> $56.25
Minnie - Disneyland - $75.00 -> $75.00 ***** Discount cannot be applied to Minnie
Goofie - Disneyland - $75.00 -> $75.00 ***** Discount cannot be applied to Goofie
Total Discounted Cost for Disneyland = $258.75
------------------------------------------------------------
{UniversalStudios=145.0, Disneyland=258.75}
Grand Total = $403.75