gpt4 book ai didi

java - 如何调用一个类,以便方法根据该类中的变量执行特定操作

转载 作者:行者123 更新时间:2023-12-01 16:43:40 25 4
gpt4 key购买 nike

我对这个标题深表歉意,我愿意接受建议,将其重命名为更清晰的名称

我的问题是我有一个任务,其中有 3 张单独的卡片代表特定商店内的不同成员(member)状态。有基本卡、银卡和金卡。根据卡的不同,年底会向客户发放不同的优惠券。现在我在主类中创建优惠券方法。我一直在尝试让该方法适用于每张卡的不同操作方式。

本质上,该方法会查看客户拥有什么卡并应用适当的折扣,例如,如果客户使用基本卡并且余额(总支出)超过 2000 美元,则优惠券为 3%

下面是我目前拥有的(请注意,我并不担心创建 3 个卡类,我只想让其中一个工作)

    public class storeCard {

public static void main(String[] args) {

cardDetails detailsObject1 = new cardDetails(0001, "Adam Gong", 7000);
Date date = new Date(04, 8, 2019);
storeCard store = new storeCard();
basicCard card = new basicCard();

System.out.println("***** Customer Details ******");
System.out.println("Customers name: " + detailsObject1.getName());
System.out.println("Customers ID: " + detailsObject1.getID());
System.out.println("Customers balance: " + detailsObject1.getBalance());
System.out.println("Created Date: " + date.getDay() + "/" + date.getMonth() + "/" + date.getYear());
Address addressObject1 = new Address(63, "Boyd Street", "Eagle Vale", "Campbelltown", "NSW", 2558);

}

public void calCoupon(double balance) {

if(balance < 2000);
System.out.println("Coupon is 2%");


}

}


import java.util.*;

class cardDetails {

private int ID;
private String name;
private double balance;

public cardDetails(int ID, String name, double balance) {

this.ID = ID;
this.name = name;
this.balance = balance;

}

public int getID() {

return this.ID;
}

public void setID(int customerID) {

this.ID = customerID;

}

public String getName() {
return this.name;
}

public void setName(String customerName) {

this.name = customerName;
}

public double getBalance() {
return this.balance;
}

public void setBalance() {
this.balance = balance;
}

}




import java.util.*;

class Date {

private int day, month, year;

public Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}

public int getDay() {

return this.day;
}

public void seDay(int Day) {

this.day = day;

}

public int getMonth() {
return this.month;
}

public void setMonth(int Month) {

this.month = Month;
}

public int getYear() {
return this.year;
}

public void setYear(int Year) {
this.year = Year;
}

public String toString() {
return new String("date=" + day + ",month=" + month + ",year=" + year);

}
}

import java.util.*;

class Address {

private int streetNumber;
private String streetName;
private String suburb;
private String city;
private String state;
private int postcode;

public Address(int streetNumber, String streetName, String suburb, String city, String state, int postcode) {
this.streetNumber = streetNumber;
this.streetName = streetName;
this.suburb = suburb;
this.city = city;
this.state = state;
this.postcode = postcode;

System.out.println("Customers Address: " + this);

}

public String toString() {
return new String( streetNumber + " " +streetName + " " + suburb + " " + city + " " + state + " " + postcode);

}
}

class basicCard { 
final double discount = 0.03;
double annuelFee = 10;
}

最佳答案

尚不完全清楚这是否是您所要求的,但您可能希望为卡的功能创建一个接口(interface),并让具体的卡实现它。

interface Card {
double getDiscount(double moneySpent);
}

class BasicCard implements Card {
public double getDiscount(double spent) {
return spent < 2000d ? 0 : 3;
}
}

class SilverCard implements Card {
public double getDiscount(double spent) {
return spent < 2000d ? 1 : 5;
}
}

class PremiumCard implements Card {
public double getDiscount(double spent) {
return spent < 2000d ? 3 : 8;
}
}

那么总共花费的钱是

double baseAmount = 2500; // for example
double total = baseAmount - (customer.getCard().getDiscount(baseAmount) * baseAmount);

您可能并不真正需要子类,但可以使用工厂方法。

class Cards {
public static Card basicCard() {
return amount -> amount < 2000d ? 0 : 3;
}
public static Card silverCard() {
return amount -> amount < 2000d ? 1 : 5;
}
public static Card basicCard() {
return amount -> amount < 2000d ? 5 : 8;
}
}

关于java - 如何调用一个类,以便方法根据该类中的变量执行特定操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57346521/

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