The user who has not yet purchased any item should get the discount and if the user visits for the second time then the discount should not be given.
Loading
The user who has not yet purchased any item should get the discount and if the user visits for the second time then the discount should not be given.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Abhishek ChadhaRank #402 · 6.2K reputationPosted May 28, 2024, 2:55 PM EDT
1. Initialize a variable to track the number of purchases made by the user.
2. Prompt the user if they have made any purchases before.
3. If the user has not made any purchases:
- Grant the discount.
- Increment the purchase count.
4. If the user has made purchases before:
- Check if it's the user's second visit.
- If it's the user's second visit:
- Do not grant the discount.
- If it's not the user's second visit:
- Do not grant the discount again.
5. End.
Tuhin PaulRank #7 · 71.9K reputationPosted May 30, 2024, 10:26 PM EDT
// Define a class to represent the user public class User { private int purchaseCount = 0; private bool secondVisit = false; // Method to check if the user has made any purchases before public bool HasMadePurchases() { return purchaseCount > 0; } // Method to check if it's the user's second visit public bool IsSecondVisit() { return secondVisit; } // Method to grant the discount and increment the purchase count public void GrantDiscount() { Console.WriteLine("Discount granted!"); purchaseCount++; } // Method to indicate that the discount should not be granted public void NoDiscount() { Console.WriteLine("Discount not granted."); } // Method to simulate a user visiting the store public void VisitStore() { // Prompt the user if they have made any purchases before if (!HasMadePurchases()) { // If the user has not made any purchases: // Grant the discount. // Increment the purchase count. GrantDiscount(); } else { // If the user has made purchases before: // Check if it's the user's second visit. if (IsSecondVisit()) { // If it's the user's second visit: // Do not grant the discount. NoDiscount(); } else { // If it's not the user's second visit: // Do not grant the discount again. NoDiscount(); } } } } // Usage public class Program { public static void Main(string[] args) { // Create a new instance of the user User user = new User(); // Simulate the user visiting the store user.VisitStore(); // Simulate the user visiting the store again user.VisitStore(); } }here the
Userclass encapsulates the behavior of a user, and theVisitStoremethod simulates the user visiting the store.