Rewards App Text-Based Starter Code
Everything in a Sandwich Rewards App Text-Based Starter Code
Set up a project in the IDE of your choice and add the following classes to your project:
- RewardsApps: This class can be used to create a RewardsApps object, which includes a list of subscribers and available offers.
- RewardsAppStarter: This class contains your main method and will run the app.
- Reward: This class represents a reward that might be offered to a customer. Reward objects have a name, discount amount, and an expiration date. Once a reward is created it cannot be chnaged.
- Address: This class represents a mailing address. Address objects have street 1, street 2, city, state, and zip code.
- CustomerContactInfo: This class represents a customer's first name, last name, email address, and mailing address
- Customer: This class represents a customer in the app and has the CustomerContactInfo, customer id number, and date joined. This class has a static variable that keeps track of the customer count in order to generate the customer id number.
Return to Missions
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Creates customer profiles with the following information:
* - Customer Contact Information which contains the person’s first name, last name, email address, and mailing address.
* - The customer’s unique customer identification number that is assigned to them when they become a member.
* - The date in which the customer has joined.
* Rewards are available for all customers that subscribe to the app.
* Rewards contain the name of the offer, the amount of discount the customer will receive, and an expiration date for using the offer.
* @author Crystal Sheldon
*/
public class RewardsApps {
private ArrayList<Customer> subscribers;
private ArrayList<Reward> offers;
/**
* Creates a RewardsApps object and initializing both the subscribers and offers ArrayList
*/
public RewardsApps() {
subscribers = new ArrayList<>();
offers = new ArrayList<>();
}
/**
* Creates a RewardsApps object and initializes the subscribers list to the values in list
* initializes offers to an empty ArrayList
* @param list - An ArrayList of Customer objects
*/
public RewardsApps (ArrayList<Customer> list) {
subscribers = new ArrayList<> ();
for (Customer c: list) {
subscribers.add (c);
}
offers = new ArrayList<> ();
}
/**
* Adds a new Customer to the subscriber list
* @param newCustomer - The new customer that is being added to the subscriber list
*/
public void addCustomer (Customer newCustomer) {
subscribers.add(newCustomer);
}
/**
* Adds a new offer to the offers list.
* @param newOffer - A Reward object to be added to the offers list.
*/
public void addOffers (Reward newOffer) {
offers.add(newOffer);
}
/**
* Removes any expired Reward objects from the offers list.
*/
public void updateOffers() {
for (int x = offers.size() - 1; x >= 0; x--) {
Reward reward = offers.get(x);
if (reward.expirationDate().isBefore(LocalDate.now())) {
offers.remove(x);
}
}
}
/**
* Removes a Customer from the subscribers ArrayList
* @param oldCustomer - the Customer to be removed from subscribers
*/
public void removeSubscriber (Customer oldCustomer) {
for (int x = 0; x < subscribers.size(); x++) {
Customer current = subscribers.get(x);
if (current.getCustomerId() == oldCustomer.getCustomerId())
{
subscribers.remove(x);
break;
}
}
}
/**
* Displays the offers as long as a correct id is provided
* @param id - An int value for the id
* @return - Returns a random offer that hasn't expired as long as the id matches
*/
public Reward viewOffers (int id) {
boolean found = false;
for (Customer customer : subscribers) {
if (customer.getCustomerId() == id) {
found = true;
}
}
Reward offer = null;
if (found) {
if (!offers.isEmpty()) { // offers.size() > 0
int index = (int)(Math.random() * offers.size());
offer = offers.get(index);
}
else {
System.out.println ("No offers at this time");
}
}
return offer;
}
/**
* Collects the customer information for a new subscribers, adds them to the subscribers list, and returns the created Customer
* @return the newly collected customer information.
*/
public Customer newCustomerSignUp() {
Scanner input = new Scanner (System.in);
String firstName;
String lastName;
String email;
Address custAddress;
System.out.println ("Welcome to Everything on a Sandwich! Let me get a little information from you");
System.out.println ("First Name: ");
firstName = input.nextLine();
System.out.println ("Last Name: ");
lastName = input.nextLine();
System.out.println ("Email Address: ");
email = input.nextLine();
custAddress = getAddress();
CustomerContactInfo customerContact = new CustomerContactInfo (firstName, lastName, email, custAddress);
Customer customer = new Customer (customerContact, LocalDate.now());
this.addCustomer (customer);
return customer;
}
/**
* Collects the address information for a new subscriber
* @return - an Address object that represents the address of the subscriber
*/
public static Address getAddress() {
String streetAddress1;
String streetAddress2;
String city;
String state;
String zip;
Scanner input = new Scanner (System.in);
System.out.println ("Street Address: ");
streetAddress1 = input.nextLine();
System.out.println ("Enter a second line to your street address. If you don't have a second line, type NA: ");
streetAddress2 = input.nextLine();
System.out.println ("City: ");
city = input.nextLine();
System.out.println ("State: ");
state = input.nextLine();
System.out.println ("Zip Code: ");
zip = input.nextLine();
if (streetAddress2.equalsIgnoreCase ("NA")) {
streetAddress2 = "";
}
return new Address(streetAddress1, streetAddress2, city, state, zip);
}
@Override
public String toString () {
String rewardsandcustomers = "";
for (Reward reward: offers){
rewardsandcustomers += reward + "\n";
}
rewardsandcustomers += "\n";
for (Customer customer: subscribers) {
rewardsandcustomers += customer + "\n";
}
return rewardsandcustomers;
}
}
import java.time.LocalDate;
import java.util.Scanner;
/**
* A text representation of the Rewards App. Includes a menu to register customers as subscribers and
* to view the rewards.
* @author Crystal Sheldon
*/
public class RewardAppStarter {
public static void main(String[] args) {
System.out.println("Welcome to Everything On a Sandwich Rewards Program!");
Scanner input = new Scanner (System.in);
int options = 1;
RewardsApps everythingSandwich = new RewardsApps ();
LocalDate expiration = LocalDate.parse("2024-10-13");
LocalDate expiration2 = LocalDate.parse ("2024-10-15");
Reward tenOff = new Reward ("10% off", 0.1, expiration);
Reward twentyOff = new Reward ("20% off", 0.2, expiration2);
everythingSandwich.addOffers(tenOff);
everythingSandwich.addOffers(twentyOff);
do {
//re-write using a text block
System.out.println ("How can we help you? Enter your selection: \n1 - Sign up for rewards\n2 - View my rewards\n0 - exit");
options = input.nextInt();
//Because you might add more options and to increase reability, re-write this if statement with a switch statement
if (options == 1) {
Customer customer = everythingSandwich.newCustomerSignUp();
System.out.println ("Your account is successfully set up.");
System.out.println ("Your customer id number is " + customer.getCustomerId());
}
else {
if (options == 2) {
int id;
System.out.print ("Enter your Customer Id number: ");
id = input.nextInt();
Reward reward = everythingSandwich.viewOffers(id);
System.out.println ("Your rewards!");
System.out.println (reward);
}
else {
System.out.println ("Thank you for using Everything On a Sandwich Rewards!");
options = 0;
}
}
} while (options != 0);
System.out.println (everythingSandwich);
}
}
import java.time.LocalDate;
/**
* Represents a reward that is available to subscribing customers.
* Rewards contain the name of the offer, the amount of discount the customer will receive, and an expiration date for using the offer.
* @author Crystal Sheldon
*/
public class Reward {
private String rewardName;
private double discount;
private LocalDate expirationDate;
/**
* Creates a Reward with a name, discount amount, and an expiration date.
* @param name - The name of the reward
* @param disc - the amount of discount
* @param eDate - the expiration date
*/
public Reward (String name, double disc, LocalDate eDate) {
rewardName = name;
discount = disc;
expirationDate = eDate;
}
/**
* Returns the name of the reward
* @return - returns rewardName
*/
public String rewardName() {
return rewardName;
}
/**
* Returns the discount amount of the reward
* @return - returns discount
*/
public double discount() {
return discount;
}
/**
* Returns the expiration date of the reward
* @return - returns expirationDate
*/
public LocalDate expirationDate() {
return expirationDate;
}
/**
* Returns a string that represents the reward including the name, discount, and expiration date
* @return a string with rewardName, discount, and expirationDate
*/
public String toString() {
return "Reward[rewardName=" + rewardName + ", discount=" + discount + ", expirationDate=" + expirationDate + "]";
}
/**
* Returns whether two Reward objects are equal
* @param reward - the Reward being compared
* @return true if these two rewards have the same name, discount amount, and expiration date; false otherwise
*/
public boolean equals (Reward reward) {
return reward.rewardName().equals(this.rewardName()) && reward.discount() == this.discount() && reward.expirationDate().equals(this.expirationDate());
}
}
/**
* Represents a mailing address.
* Address contains the following information:
* - First line of the street address for the mailing address
* - Second line of the street address for the mailing address
* - city of the mailing address
* - state of the mailing address
* - zip of the mailing address
* @author Crystal Sheldon
*/
public class Address {
private String streetAddress1;
private String streetAddress2;
private String city;
private String state;
private String zip;
/**
*
* @param street1 - the value of streetAddress1, which is the first line of the street address for the mailing address.
* @param street2 - the value of streetAddress2, which is the second line of the street address for the mailing address.
* @param myCity - the value of city, which is the city of the mailing address.
* @param myState - the value of state, which is the state of the mailing address.
* @param myZip - the value of zip, which is the zip of the mailing address.
*/
public Address (String street1, String street2, String myCity, String myState, String myZip) {
streetAddress1 = street1;
streetAddress2 = street2;
city = myCity;
state = myState;
zip = myZip;
}
/**
* Returns the value of the first line of the street address for the mailing address.
* @return - the value of streetAddress1
*/
public String streetAddress1() {
return streetAddress1;
}
/**
* Returns the value of the second line of the street address for the mailing address.
* @return - the value of streetAddress2
*/
public String streetAddress2() {
return streetAddress2;
}
/**
* Returns the value of the city for the mailing address.
* @return - the value of city
*/
public String city() {
return city;
}
/**
* Returns the value of the state for the mailing address.
* @return - the value of state
*/
public String state() {
return state;
}
/**
* Returns the value of the zip for the mailing address.
* @return - the value of zip
*/
public String zip() {
return zip;
}
/**
* Returns a string that represents the mailing address.
* @return - a string that contains the value of the mailing address.
*/
public String toString () {
return "Address[streetAddress1="+streetAddress1+", streetAddress2="+streetAddress2+", city="+city+", state="+state+", zip="+zip+"]";
}
/**
* Returns whether or not two Address objects are equal to each other.
* @param address - The Address this Address is being compared to
* @return - true if the values in Address a are the same as this Address; false otherwise
*/
public boolean equals(Address address) {
return (address.streetAddress1().equals(this.streetAddress1()) && address.streetAddress2().equals(this.streetAddress2()) &&
address.city().equals(this.city()) && address.state().equals(this.state()));
}
}
/**
* Represents the customer contact information.
* CustomerContactInfo contains the following information:
* - The first name of the customer
* - The last name of the customer
* - The email address for this customer
* - The mailing address for this customer
* @author Crystal Sheldon
*/
public class CustomerContactInfo {
private String firstName;
private String lastName;
private String emailAddress;
private Address mailingAddress;
/**
* Creates a CustomerContactInfo object
* @param first - the value of firstName which is the first name of the customer
* @param last - the value of lastName which is the last name of the customer
* @param email - the value of emailAddress which is the customer's email address
* @param mailing - the value of mailingAddress which is the customer's mailing address
*/
public CustomerContactInfo(String first, String last, String email, Address mailing) {
firstName = first;
lastName = last;
emailAddress = email;
mailingAddress = new Address (mailing.streetAddress1(), mailing.streetAddress2(), mailing.city(), mailing.state(), mailing.zip());
}
/**
* Returns the first name of the customer
* @return - returns firstName
*/
public String firstName() {
return firstName;
}
/**
* Returns the last name of the customer
* @return - returns lastName
*/
public String lastName() {
return lastName;
}
/**
* Returns the email address of the customer
* @return - returns emailAddress
*/
public String emailAddress() {
return emailAddress;
}
/**
* Returns the mailing address of the customer
* @return - returns mailingAddress
*/
public Address mailingAddress() {
return mailingAddress;
}
/**
* Returns a string that contains the values of the CustomerContactInfo
* @return - String that represents the value of CustomerContactInfo
*/
public String toString() {
return "CustomerContactInfo[firstName=" + firstName + ", lastName=" + lastName + ", emailAddress=" + emailAddress + ", mailingAddress=" + mailingAddress + "]";
}
/**
* Checks to see if two CustomerContactInfo objects are equal
* @param customerContact - The CustomerContactInfo that is being compared to this CustomerContactInfo
* @return - returns true if both CustomerContactInfo objects have the same first name, last name, email address,
* and mailing address; returns false otherwise.
*/
public boolean equals(CustomerContactInfo customerContact) {
return (customerContact.firstName().equals(this.firstName()) && customerContact.lastName().equals(this.lastName()) &&
customerContact.emailAddress().equals(this.emailAddress()) && customerContact.mailingAddress().equals(this.mailingAddress()));
}
}
import java.time.LocalDate;
import java.util.Scanner;
/**
* Represents the customer profiles for subscribers of the app.
* Customer contains the following information:
* - Customer Contact Information which contains the person’s first name, last name, email address, and mailing address.
* - The customer’s unique customer identification number that is assigned to them when they become a member.
* - The date in which the customer has joined.
* @author Crystal Sheldon
*/
public class Customer {
private CustomerContactInfo contactInfo;
private static int customerCount = 0;
private final int customerId;
private final LocalDate dateJoined;
/**
* Creates a Customer object.
* Customer id numbers are managed by the RewardsApp class, but store with the Customer object.
* @param contact - A CustomerContactInfo object that has the first name, last name, mailing address, and email address for a Customer
* @param date - The date that the subscriber joined the app
*/
public Customer (CustomerContactInfo contact, LocalDate date) {
contactInfo = contact;
customerCount++;
customerId = customerCount;
dateJoined = date;
}
/**
* Creates a Customer object.
* @param contact - A CustomerContactInfo object that has the first name, last name, mailing address, and email address for a Customer
*
*/
public Customer (CustomerContactInfo contact) {
contactInfo = contact;
dateJoined = LocalDate.now();
customerCount++;
customerId = customerCount;
}
/**
* Returns the contact information for a customer
* @return - the CustomerContactInfo which includes their first name, last name, mailing address, and email address
*/
public CustomerContactInfo getContactInfo () {
return contactInfo;
}
/**
* Returns the customer id
* @return - this subscriber's customer id number
*/
public int getCustomerId() {
return customerId;
}
/**
* Returns the date you joined the app
* @return - the date this subscriber joined the app
*/
public LocalDate getDateJoined() {
return dateJoined;
}
/**
* Returns true after the contact information of the customer has been updated.
* @param contact - the new contact information
* @return - returns true
*/
public boolean updateContactInfo (CustomerContactInfo contact) {
contactInfo = contact;
return true;
}
@Override
public String toString ()
{
return contactInfo + "\n" + customerId + " " + dateJoined;
}
}