Files
G4G0-1/Semester 1/Programming 1/Homework 4/Website.java
2024-01-15 20:14:10 +00:00

211 lines
5.8 KiB
Java

/**
* Represents a holiday website, where members can purchase holidays.
*
* @George Wilkinson
* @13/11/23
*/
import java.util.ArrayList;
public class Website
{
// Creates a String variable to hold the name of the website.
private String websiteName;
// Creates an integer variable to hold the amount of logins (hits).
private int hits;
// Creates an integer variable to hold the total sales in pounds.
private double salesTotal;
// Creates an array for the logged in members.
private ArrayList<Member> loggedInList = new ArrayList<Member>();
/**
* Default constructor for objects of class Website
*/
public Website()
{
websiteName = "Club 18";
hits = 1257;
salesTotal = 10592.0;
}
/**
* Constructor for objects of class Website
*/
public Website( String websiteName )
{
this.websiteName = websiteName;
hits = 0;
salesTotal = 0.0;
}
/**
* Return website's name
*/
public String getWebsiteName()
{
return websiteName;
}
/**
* Return hit count of website
*/
public int getHits()
{
return hits;
}
/**
* Return total sales of holidays purchased through the website.
*/
public double getSalesTotal()
{
return salesTotal;
}
/**
* Return the logged in list ArrayList
*/
public ArrayList<Member> getLoggedInList()
{
return loggedInList;
}
/**
* Return the amount of users currently logged in
*/
public int getNumberOfUsers()
{
return loggedInList.size();
}
/**
* Set the website's name to a new value.
*/
public void setWebsiteName( String websiteName )
{
this.websiteName = websiteName;
}
/**
* Set the website's hit count to a new value.
*/
public void setHits( int hits )
{
this.hits = hits;
}
/**
* Set the total sales of the website to a new value.
*/
public void setSalesTotal( double salesTotal )
{
this.salesTotal = salesTotal;
}
/**
* Lists all members logged in by using a for each loop and accessing the member.toString() method.
*/
public void listMembersLoggedIn()
{
for ( Member memberMessage : loggedInList )
{
System.out.println( memberMessage.toString() );
}
}
/**
* Allows the website to be logged into, taking a Member object as a parameter.
*/
public void memberLogin( Member member )
{
if( !member.getLoggedInStatus() )
{
//User status to logged in
member.setLoggedInStatus( true );
loggedInList.add( member );
//Assign to a website
member.setWebsite( this );
System.out.println( websiteName + " welcome member " + member.getMembershipNumber() + ", you are now logged in" );
//Increment hits
hits++;
}
else {
System.out.println( "Current user is still logged in, please try again later." );
}
}
/**
* Allows the current user of the website to be logged out, as long as there is a user logged in.
* The if statement here allows us to avoid a null reference error.
*/
public void memberLogout( Member member )
{
if( member.getLoggedInStatus() )
{
//User status logged out
member.setLoggedInStatus( false );
loggedInList.remove( member );
//No longer registered to a website
member.setWebsite( null );
System.out.println( websiteName + ": goodbye member " + member.getMembershipNumber() + ", you are now logged out" );
}
else
{
System.out.println( "User not logged in" );
}
}
/**
* Provides a checkout to the member buying a holiday. This will also apply the hit discount,
* and amend the purchase to the sales total field variable
*/
public void checkout( Member member )
{
if( member.getHoliday() != null ) {
//Stores the integer price of holiday as a double so it can be discounted.
//This couldve been avoided by repeating redundant code but this is cleaner.
double holidayPrice = ( member.getHoliday().getPrice() * ( member.getNumberOfCompanions() + 1 ) );
if( checkHitDiscount() == true )
{
//Discounts price, hence why holidayPrice must be a double
double credit = holidayPrice * 0.1;
holidayPrice -= credit;
// Reimburse the user for discount, since money is taken in payForHoliday
member.setMoney( member.getMoney() + credit );
System.out.println( "Congratulations! You are the 10th user to book a holiday, you will recieve a 10% discount on this holiday." );
}
//Add holiday price to running total
salesTotal += holidayPrice;
System.out.println("Your purchase totaling £" + holidayPrice + " was sucessful, user " + member.getMembershipNumber() + ", thank you for using Tops Travel.");
member.setHoliday( null );
memberLogout( member );
}
else
{
System.out.println( "You have not selected a holiday!" );
}
}
/**
* Returns true or false, depending on the current state of hit count.
* Every 10th member, result is true
*/
private boolean checkHitDiscount()
{
//hits mod 10, when hits is multiple of 10, return true.
if ( hits > 0 )
return ( hits % 10 ) == 0;
else
return false;
}
}