gpt4 book ai didi

Java - 使用 void 返回类型允许客户获得 Activity 入场券

转载 作者:行者123 更新时间:2023-12-02 10:12:34 26 4
gpt4 key购买 nike

我正在尝试制定一种方法,允许客户获得参加 Activity 的入场券。作为要求,payAdmission() 方法应具有 void 返回类型。我的方法也应该是一行长,利用已经编写的computeFee()和spend()方法。我不太明白当返回类型为空并且我无法减去任何值或返回任何内容时,我应该如何确定客户应该如何支付入场费?

客户类别代码:

public class Customer {
String name;
int age;
float money;

public Customer(String initName){
name = initName;
}

public Customer(String initName, int initAge){
name = initName;
age = initAge;
}

public Customer(String initName, int initAge, float initMoney){
name = initName;
age = initAge;
money = initMoney;
}

public Customer(){
}

public float computeFee(){
if(age >= 18 && age <65){
return 12.75f;
}
if(age >= 65){
return 0.5f;
}
if(age >= 4 && age <= 17){
return 8.50f;
}
return 0.0f;
}

public boolean spend(float amount){
if(amount <= money){
money -= amount;
return true;
}else{
return false;
}
}

public boolean hasMoreMoneyThan(Customer c){
if(this.money > c.money){
return true;
}
return false;
}

public void payAdmission(){
float comF = computeFee();
float spe = spend();
float moneyLeft -= (comF + spe);
}
}

CustomerAdmissionTestProgram 代码:​​

public class CustomerAdmissionTestProgram {
public static void main(String args[]) {
Customer c1, c2, c3, c4;
c1 = new Customer("Bob", 17, 100);
c2 = new Customer("Dottie", 3, 10);
c3 = new Customer("Jane", 24, 40);
c4 = new Customer("Sam", 72, 5);
System.out.println("Here is the money before going into the circus:");
System.out.println(" Bob has $" + c1.money);
System.out.println(" Dottie has $" + c2.money);
System.out.println(" Jane has $" + c3.money);
System.out.println(" Sam has $" + c4.money);
// Simulate people going into the circus

c1.payAdmission();
c2.payAdmission();
c3.payAdmission();
c4.payAdmission();

System.out.println("Here is the money after going into the circus:");
System.out.println(" Bob has $" + c1.money);
System.out.println(" Dottie has $" + c2.money);
System.out.println(" Jane has $" + c3.money);
System.out.println(" Sam has $" + c4.money);


}
}

最佳答案

仔细查看 spendcomputeFee 方法的作用。 spend 获取一定金额并从金钱中减去它(当然,如果这个人有足够的钱可以花的话)。 computeFee 方法计算并返回费用。

那么payAdmission应该做什么呢?自然而然就应该花这笔费用。你如何获得费用?通过调用computeFee。你怎么花它?通过调用spend

所以你的方法应该像这样实现:

spend(computeFee());

关于Java - 使用 void 返回类型允许客户获得 Activity 入场券,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54899668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com