我正在尝试设置Java代码,以分析数组中的元素是否曾参加迪士尼乐园或环球影业。我的数组有7个参与者的7个元素。参与者可以享受到他们参加哪个公园的折扣,并且我创建了两个for循环来代表两个公园。
如何在main方法中设置代码以确定哪些与会者去了哪里?
public class Visitor
{
public static void main( String args[] )
{
double totalAttendeeCost = 0.0;
//Using 1 array
VisitorPackage[] attendee = new VisitorPackage[7];
attendee[0] = new VisitorPackage("Mickey", 'S', "weekday",
"Disneyland", 75.0);
attendee[1] = new VisitorPackage("Donald", 'C', "weekday",
"Disneyland", 75.0);
attendee[2] = new VisitorPackage("Minnie", 'E', "weekend",
"Disneyland", 75.0);
attendee[3] = new VisitorPackage("Goofie", 'E', "weekend",
"Disneyland", 75.0);
attendee[4] = new VisitorPackage("Harry", 'C', "weekend", "Universal
Studios", 60.0);
attendee[5] = new VisitorPackage("Hermoine", 'S', "weekend",
"Universal Studios", 60.0);
attendee[6] = new VisitorPackage("Ron", 'E', "weekday", "Universal
Studios", 60.0);
//This is where I am looking for help
//if attendee == disneyland
for(int i=0; i < attendee.length; i++)
{
{
attendee[i].applyDiscount(attendee[i], 5.0, 10.0);
attendee[i].applyWeekdayRate(attendee[i], 15.0);
attendee[i].printInfo(attendee[i]);
totalAttendeeCost += attendee[i].getTotalCost();
}
}
//else if attendee == universal
for(int i=0; i < attendee.length; i++)
{
attendee[i].applyDiscount(attendee[i], 10.0, 15.0);
attendee[i].applyWeekdayRate(attendee[i], 20.0);
attendee[i].printInfo(attendee[i]);
totalAttendeeCost += attendee[i].getTotalCost();
}
//System.out.println("Total: $" + totalCostDisney + "\n");
System.out.println("--------------");
System.out.printf("Total: $%.2f\n", totalAttendeeCost);
}
}
这是另一个包含方法的类:
public class VisitorPackage
{
private String visitorName;
private char visitorType;
private String visitDay;
private String destination;
private double totalCost;
public VisitorPackage()
{
visitorName ="N/A";
visitorType = '-';
visitDay = "-";
destination = "N/A";
totalCost = 0.0;
}
public VisitorPackage(String newVisitorName, char newVisitorType,
String newVisitDay, String newDestination, double newTotalCost)
{
visitorName = newVisitorName;
visitorType = newVisitorType;
visitDay = newVisitDay;
totalCost = newTotalCost;
destination = newDestination;
}
public String getDestinationPark(VisitorPackage arrayInstance)
{
if (arrayInstance.destination.equalsIgnoreCase("disneyland"))
return destination;
else
return destination;
}
public double getTotalCost()
{
return totalCost;
}
public void applyDiscount(VisitorPackage arrayInstance, double
childRate, double seniorRate)
{
if (arrayInstance.visitorType == 'C')
totalCost -= ((totalCost * childRate) / 100);
else if (arrayInstance.visitorType == 'S')
totalCost -= ((totalCost * seniorRate) / 100);
}
public void applyWeekdayRate(VisitorPackage arrayInstance, double
weekdayDiscount)
{
if (arrayInstance.visitDay.equalsIgnoreCase("weekday"))
totalCost -= weekdayDiscount;
}
public void printInfo(VisitorPackage arrayInstance)
{
System.out.print(arrayInstance.visitorName + " - ");
System.out.print(arrayInstance.destination + " - ");
System.out.print("$");
System.out.printf("%.2f", arrayInstance.totalCost);
if (arrayInstance.visitorType == 'E')
System.out.print(" ***** Discount cannot be applied to " +
arrayInstance.visitorName);
System.out.println();
}
}
最佳答案
请记住,您的与会者数组的每个元素都是VisitorPackage的特定实例。因此,请在VisitorPackage类中利用您的Getter方法,但首先要删除该方法的参数,并像这样简化它:
public String getDestinationPark() {
return this.destination;
}
对VisitorPackage类中的所有其他方法执行相同的操作,摆脱VisitorPackage arrayInstance参数以及您的类方法中与它的任何关系,例如:
public void applyDiscount(double childRate, double seniorRate) {
if (visitorType == 'C') {
totalCost -= ((totalCost * childRate) / 100);
}
else if (visitorType == 'S')
totalCost -= ((totalCost * seniorRate) / 100);
}
public void applyWeekdayRate(double weekdayDiscount) {
if (visitDay.equalsIgnoreCase("weekday")) {
totalCost -= weekdayDiscount;
}
}
public void printInfo() {
System.out.print(visitorName + " - ");
System.out.print(destination + " - ");
System.out.print("$");
System.out.printf("%.2f", totalCost);
if (visitorType == 'E') {
System.out.print(" ***** Discount cannot be applied to " + visitorName);
}
System.out.println();
}
因为与会者数组的每个元素都是VisitorPackage类的实例,所以您无需将该实例发送到类方法。提供索引号时,它已经知道您要指的是哪个实例(米奇,唐纳德,罗恩等)。由于Minnie位于与会者数组的索引2,因此对VisitorPackage类(如
attendee[2].applyWeekdayRate(15.0)
)的任何调用将仅适用于Minnie。现在,当您要处理与会者的数组时:
// iterate through each attendee and
// apply necessary discounts, etc.
for (int i = 0; i < attendee.length; i++) {
// Disneyland
if (attendee[i].getDestinationPark().equalsIgnoreCase("disneyland")) {
attendee[i].applyDiscount(5.0, 10.0);
attendee[i].applyWeekdayRate(15.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
// Universal Studios
else if (attendee[i].getDestinationPark().equalsIgnoreCase("universal studios")) {
attendee[i].applyDiscount(10.0, 15.0);
attendee[i].applyWeekdayRate(20.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
}
或者您可以这样做:
for (int i = 0; i < attendee.length; i++) {
String dest = attendee[i].getDestinationPark();
if (dest.equalsIgnoreCase("disneyland")) {
attendee[i].applyDiscount(5.0, 10.0);
attendee[i].applyWeekdayRate(15.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
else if (dest.equalsIgnoreCase("universal studios")) {
attendee[i].applyDiscount(10.0, 15.0);
attendee[i].applyWeekdayRate(20.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
}
}
或者您可以这样做:
for (int i = 0; i < attendee.length; i++) {
String dest = attendee[i].getDestinationPark();
switch (dest.toLowerCase()) {
case "disneyland":
attendee[i].applyDiscount(5.0, 10.0);
attendee[i].applyWeekdayRate(15.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
break;
case "universal studios": // if the string has a space
case "universalstudios": // OR if the string has no space
attendee[i].applyDiscount(10.0, 15.0);
attendee[i].applyWeekdayRate(20.0);
attendee[i].printInfo();
totalAttendeeCost += attendee[i].getTotalCost();
break;
}
}
关于java - 如何在Java中设置if-else以解析数组元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53642262/