Java-使用void返回类型允许客户进入活动
我正在尝试一种方法,允许客户进入某个活动。作为一项要求,payaccession()方法应该具有void返回类型。我的方法也应该是一行长,利用已经编写的computeFee()和spend()方法。我不太明白,当返回类型为void且我无法减去任何值或返回任何内容时,我应该如何确定客户应该如何支付入场费 客户类别代码:Java-使用void返回类型允许客户进入活动,java,constructor,Java,Constructor,我正在尝试一种方法,允许客户进入某个活动。作为一项要求,payaccession()方法应该具有void返回类型。我的方法也应该是一行长,利用已经编写的computeFee()和spend()方法。我不太明白,当返回类型为void且我无法减去任何值或返回任何内容时,我应该如何确定客户应该如何支付入场费 客户类别代码: public class Customer { String name; int age; float money; public Custome
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);
}
}
publicstaticvoidmain(字符串…参数){
List customers=Arrays.asList(
新客户(“Bob”,17100),
新客户(“Dottie”,3,10),
新客户(“Jane”,24岁,40岁),
新客户(“Sam”,72,5));
System.out.println(“这是去马戏团之前的钱:”);
customers.forEach(customer->System.out.format(“%s有%.2f\n”、customer.name、customer.money));
customers.forEach(Customer::paycommission);
System.out.println(“这是进入马戏团后的钱:”);
customers.forEach(customer->System.out.format(“%s有%.2f\n”、customer.name、customer.money));
}
公共静态类客户{
私有最终字符串名;
私人最终年龄;
私人双重货币;
公共客户(字符串名称){
这个(名字,0);
}
公共客户(字符串名称,整数){
这(姓名、年龄、0);
}
公共客户(字符串名称、整数、双倍货币){
this.name=名称;
this.age=Math.max(0,age);
this.money=Math.max(0,money);
}
公共双计算机{
如果(年龄>=65岁)
返回0.5;
如果(年龄>=18岁)
回报率12.75;
如果(年龄>=4岁)
回报率8.50;
返回0;
}
公共支出(双倍金额){
金额=数学最大值(0,金额);
如果(双倍比较(金额、金钱)>0)
返回false;
货币-=金额;
返回true;
}
公共布尔值hasMoreMoneyThan(客户){
返回双倍。比较(money,customer.money)>0;
}
公众入场券(){
如果(!expense(computeFee()))
抛出新的RuntimeException();
}
}
仔细查看方法和computeFee方法的花费<代码>支出
接受一个金额,然后从钱中减去它(当然,如果这个人有足够的钱来支出的话)。computeFee
方法计算并返回费用
那么,paycommission
应该做什么呢?当然,它应该花掉这笔费用。你怎么拿到费用的?通过调用computeFee
。你是怎么花的?通过调用花费
因此,您的方法应按如下方式实现:
spend(computeFee());
使用和更新成员变量,而不是使用局部变量,并检查方法返回的类型,布尔值不是浮点值。float spe=spend()代码>不正确-它返回布尔值。如果返回FALSE,则不应继续。您现在发布的代码将无法编译。在hasMoreMoneyThan方法之后,结束括号太多了。为什么要在payaccession()
方法中计算剩余的钱?
public static void main(String... args) {
List<Customer> customers = Arrays.asList(
new Customer("Bob", 17, 100),
new Customer("Dottie", 3, 10),
new Customer("Jane", 24, 40),
new Customer("Sam", 72, 5));
System.out.println("Here is the money before going into the circus:");
customers.forEach(customer -> System.out.format(" %s has $%.2f\n", customer.name, customer.money));
customers.forEach(Customer::payAdmission);
System.out.println("Here is the money after going into the circus:");
customers.forEach(customer -> System.out.format(" %s has $%.2f\n", customer.name, customer.money));
}
public static class Customer {
private final String name;
private final int age;
private double money;
public Customer(String name) {
this(name, 0);
}
public Customer(String name, int age) {
this(name, age, 0);
}
public Customer(String name, int age, double money) {
this.name = name;
this.age = Math.max(0, age);
this.money = Math.max(0, money);
}
public double computeFee() {
if (age >= 65)
return 0.5;
if (age >= 18)
return 12.75;
if (age >= 4)
return 8.50;
return 0;
}
public boolean spend(double amount) {
amount = Math.max(0, amount);
if (Double.compare(amount, money) > 0)
return false;
money -= amount;
return true;
}
public boolean hasMoreMoneyThan(Customer customer) {
return Double.compare(money, customer.money) > 0;
}
public void payAdmission() {
if (!spend(computeFee()))
throw new RuntimeException();
}
}
spend(computeFee());