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

169 lines
4.7 KiB
Java

/**
* Represents a holiday website, where members can purchase holidays.
*
* @George Wilkinson
* @13/11/23
*/
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;
/**
* 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;
this.hits = 0;
this.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;
}
/**
* 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;
}
/**
* 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 );
//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 );
//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();
if( checkHitDiscount() == true )
{
//Discounts price, hence why holidayPrice must be a double
holidayPrice = holidayPrice * 0.9;
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 );
}
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
*/
public boolean checkHitDiscount()
{
//hits mod 10, when hits is multiple of 10, return true.
return ( hits % 10 ) == 0;
}
}