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

279 lines
7.0 KiB
Java

/**
* Represents a user of a holiday website.
*
* @George Wilkinson
* @13/11/23
*/
import java.util.ArrayList;
public class Member
{
// Creates a String for variable @email.
private String email;
// Creates an integer for variable @membershipNumber.
private int membershipNumber;
// Creates a boolean for variable @loggedInStatus.
private boolean loggedInStatus;
//Creates a new pointer to a Holiday object.
private Holiday holiday;
// Creates a new pointer to a Website object.
private Website website;
//Holds a list of friends / companions a member has.
private ArrayList<Friend> companions = new ArrayList<Friend>();
// Stores the balance of the member.
private double money;
/**
* Default Constructor for objects of Class Member.
*/
public Member()
{
email = "john.doe@topstravel.co.uk";
membershipNumber = 1;
loggedInStatus = false;
money = 500.0;
}
/**
* Constructor for objects of class Member
*/
public Member( String email, int membershipNumber )
{
// initialise instance variables
this.email = email;
this.membershipNumber = membershipNumber;
loggedInStatus = false;
money = 0.0;
}
/**
* Returns the value of @email.
*/
public String getEmail()
{
return email;
}
/**
* Returns the value of @membershipNumber.
*/
public int getMembershipNumber()
{
return membershipNumber;
}
/**
* Returns the value of @loggedInStatus.
*/
public boolean getLoggedInStatus()
{
return loggedInStatus;
}
/**
* Returns the current selected holiday object.
*/
public Holiday getHoliday()
{
return holiday;
}
/**
* Returns the current logged in website object.
*/
public Website getWebsite()
{
return website;
}
/**
* Returns the current balance of the member
*/
public double getMoney()
{
return money;
}
/**
* Returns the companions arrayList
*/
public ArrayList<Friend> getCompanions()
{
return companions;
}
/**
* Get the size of the companions list
*/
public int getNumberOfCompanions()
{
return companions.size();
}
/**
* Replaces the value of @email with a new value.
*/
public void setEmail( String email )
{
this.email = email;
}
/**
* Replaces the value of @membershipNumber with new value.
*/
public void setMembershipNumber( int membershipNumber )
{
this.membershipNumber = membershipNumber;
}
/**
* Replaces the value of @loggedInStatus with new value.
*/
public void setLoggedInStatus( boolean loggedInStatus )
{
this.loggedInStatus = loggedInStatus;
}
/**
* Sets the object pointed to by the member.
*/
public void setWebsite( Website website )
{
this.website = website;
}
/**
* Sets the object pointed to by the member.
* Currently used to clear the member's checkout after paying.
*/
public void setHoliday( Holiday holiday )
{
this.holiday = holiday;
}
/**
* Sets the value of the money variable to a new value
*/
public void setMoney( double money )
{
this.money = money;
}
/**
* Stores a friend / companion in the companions arrayList.
*/
public void storeFriend( Friend friend )
{
companions.add( friend );
}
/**
* Lists the value of each friend object in the companions list.
*/
public void listFriends()
{
for( Friend friendMessage : companions )
{
System.out.println( friendMessage.toString() );
}
}
/**
* Allows member to select a holiday object, provided they are logged into a website.
*/
public void selectHoliday( Holiday holiday )
{
if( loggedInStatus )
{
if ( checkMoney( holiday.getPrice() ) )
{
this.holiday = holiday;
System.out.println( "member ID: " + membershipNumber );
System.out.println( "holiday ref number: " + holiday.getRefNumber() );
System.out.println( "holiday type: " + holiday.getType() );
System.out.println( "holiday price: £" + holiday.getPrice() );
}
else
whoCannotPay( holiday.getPrice() );
}
else {
System.out.println( "You are not logged in" );
}
}
/**
* Allows member to pay for a holiday, provided they are logged in, passing itself to website.checkout().
*/
public void payForHoliday()
{
if( loggedInStatus )
{
// Collect the cost of the holiday from each friend, and add to main member.
for( Friend friend : companions )
{
// Negate amount from friend.
friend.setMoney( friend.getMoney() - holiday.getPrice() );
// Add amount to member.
setMoney( getMoney() + holiday.getPrice() );
}
// Since the amount must be taken here, the member will be credited later.
money -= ( holiday.getPrice() * ( getNumberOfCompanions() + 1 ) );
//Pass current member to the checkout method of website
website.checkout( this );
}
else {
System.out.println( "You are not logged in" );
}
}
/**
* Returns the current state of field variables as a string, with newlines after each.
*/
public String toString()
{
return ( "membership number : " + this.membershipNumber + "\n" + "email : " + this.email + "\n" + "logged in status : " + this.loggedInStatus );
}
/**
* Checks if each person buying the holiday can afford it, returning a true or false.
*/
private boolean checkMoney( int holidayPrice )
{
if( money >= holidayPrice )
{
boolean allCanAfford = true;
for( Friend friend : companions )
{
if( friend.getMoney() <= holidayPrice )
{
allCanAfford = false;
break;
}
}
return allCanAfford;
}
else
return false;
}
public void whoCannotPay( int holidayPrice )
{
if( money < holidayPrice )
System.out.println( membershipNumber + " has insufficient money to afford this holiday");
for( Friend friend : companions )
{
if( friend.getMoney() < holidayPrice )
{
System.out.println( friend.getName() + " has insufficient money to afford this holiday");
}
}
}
}