I'm working in Java. So what I'm trying to do is, take the user input from subscriberPackage
public static String subscriberPackage() { //Method to get the customers name
Scanner sc = new Scanner(System.in);
System.out.println("\nPlease choose a package the customer would like:");
System.out.println("Enter Bronze, Silver or Gold to select a package.");
String subscriptionPackage = sc.nextLine(); //Get subscription package from the user
while (!(subscriptionPackage.equalsIgnoreCase("Bronze") || subscriptionPackage.equalsIgnoreCase("Silver") || subscriptionPackage.equalsIgnoreCase("Gold"))) {
System.err.println("This is not a valid package!");
System.err.println("Please try again!");
System.out.println("Please choose a package the customer would like:");
System.out.println("Bronze, Silver or Gold.");
subscriptionPackage = sc.nextLine();
//If the package entered is not Bronze, Silver or Gold, the user is asked to re-enter till the while condition is met
}
return subscriptionPackage; //Returns the subscription package to the newSubscription method
}
and subscriberDuration
public static int subscriberDuration() { //Method to get the subscription duration
Scanner sc = new Scanner(System.in);
System.out.println("\nPlease choose the subscription duration");
System.out.println("It must be either 1, 3, 6 or 12 months.");
System.out.println("Please now enter the subscription duration the customer would like");
int subscriptionDuration = sc.nextInt(); //Get the subscription duration from the user
while (subscriptionDuration != 1 && subscriptionDuration != 3 && subscriptionDuration != 6 && subscriptionDuration != 12) {
System.err.println("This is not a valid duration, please try again!");
System.err.println("It must be either 1, 3, 6 or 12 months.");
subscriptionDuration = sc.nextInt();
//If the duration entered is not 1, 3, 6 or 12, the user is asked to re-enter till the while condition is met
}
return subscriptionDuration; //Returns the subscription duration to the newSubscription method
}
and calculate a cost in subscriberBill.
private static int subscriberBill(String subscriptionPackage, int subscriptionDuration) {
int subscriptionCost = 0;
int[] subsBronzePackageCosts = new int[1000];
subsBronzePackageCosts[0] = 600;
subsBronzePackageCosts[1] = 500;
subsBronzePackageCosts[2] = 400;
subsBronzePackageCosts[3] = 300;
int count = 0;
for(int i = 1; i <= 12; i =3){
if (subscriptionPackage.equals(subscriptionPackage) && (subscriptionDuration == subscriptionDuration)) {
subscriptionCost = (subsBronzePackageCosts[i] i count);
count ;
System.out.println("\nThe cost of this subscription is: " subscriptionCost);
return subscriptionCost;
}
}
return subscriptionCost;
}
}
I have managed to pass through this information and started adding the costs of the Bronze tier subscriptionPackage. I have done this using an array:
int[] subsBronzePackageCosts = new int[1000];
subsBronzePackageCosts[0] = 600;
subsBronzePackageCosts[1] = 500;
subsBronzePackageCosts[2] = 400;
subsBronzePackageCosts[3] = 300;
I'm trying to loop through said array so that for example, a user has set the duration as 3, it'll iterate using the for loop through my array and set subscriptionCost to the appropriate value. In this instance, that being 400.
The part I'm stuck with, is iterating through the array with the for loop. I have it so it's only adding 1 and reporting 500 every time. I did have a solution working for if else statements for each different package type and duration, but obviously, this isn't ideal practice and causes repetitive code, which I'm trying to avoid.
Thanks in advance!
EDIT: To add a bit more context. I have three tiers of subscriptionPackage to work with: Bronze, Silver and Gold. All of these packages have different pricings and the pricings change with the length of subscriptionDuration that the user enters.
The packages/pricings are: Bronze prices are 600 for 1 month, 500 for 3 months, 400 for 6 months and 300 for 12 months.
Silver prices are 800 for 1 month, 700 for 3 months, 600 for 6 months and 500 for 12 months.
Gold prices are 999 for 1 month, 899 for 3 months, 799 for 6 months and 699 for 12 months.
CodePudding user response:
Your code here is a bit of a mess to look at. But I guess I would point you to the logic in your if statement ...
subscriptionPackage.equals(subscriptionPackage) ... this is always true, right? So you can just delete this.
subscriptionDuraction == suscriptionDuration ... thius is also always true, right? So your whole if statement disappears.
So this if statement ALWAYS evaluates to true, which means you will always hit the return statement in the if block, with i=1, and in your array, [1] = 500.
CodePudding user response:
I would suggest attempting to work with classes, for example:
// Going this far could be up for debate
public enum SubscriptionType { GOLD, SILVER, BRONZE }
public enum SubscriptionDuration { MONTHLY, QUARTERLY, BI_ANNUALLY, ANNUALLY }
public class SubscriptionFee {
SubscriptionType type; // Replace with String
SubscriptionDuration duration; // Replace with Integer
int cost;
}
Both type and duration are up for debate if you'd rather use String and Integer respectively.
You can try to implement these in your current code and see if it's easier to loop through all the options comparing:
- subscriptionFee.type
- subscriptionFee.duration
to the information you've collected from the "user".
