Instructions
Requirements and Specifications
Source Code
GOLDEN TICKET
/**
* Represents a ticket used for discounts.
* @author NAME
* @version 1.0
*/
public class GoldenTicket {
/**
* String variable for catchphrase
*/
private String catchphrase;
/**
* Discount variable
*/
private double discount;
/**
* Constructor that creates a GoldenTicket object given the catchphrase and the discount.
* The constructor checks that the given parameters are valid
* @param catchphrase String
* @param discount double
*/
public GoldenTicket(String catchphrase, double discount) {
// If catchphrase is empty, set to "Congrats!"
this.catchphrase = (catchphrase == null || catchphrase.length() == 0) ? "Congrats!" : catchphrase;
// if discount is not in (0, 25] set to 15.0
this.discount = (discount <= 0 || discount > 25) ? 15.0 : discount;
}
/**
* Copy constructor for golden ticket.
* @param other GoldenTicket
*/
public GoldenTicket(GoldenTicket other) {
this(other.getCatchphrase(), other.getDiscount());
}
/**
* Getter method for catch phrase.
* @return String
*/
public String getCatchphrase() {
return catchphrase;
}
/**
* Setter method for catch phrase.
* @param newCatchphrase String
*/
public void setCatchphrase(String newCatchphrase) {
this.catchphrase = (newCatchphrase == null || newCatchphrase.length() == 0) ? "Congrats!" : newCatchphrase;
}
/**
* Getter method for discount.
* @return double
*/
public double getDiscount() {
return discount;
}
/**
* Setter method for discount.
* @param newDiscount
*/
public void setDiscount(double newDiscount) {
this.discount = (newDiscount <= 0 || newDiscount > 25) ? 15.0 : newDiscount;
}
/**
* Mthod that returns a String representation of the Golden Ticket object.
* @return String
*/
public String ticketString() {
return String.format("Golden Ticket with a %.2f%% discount! %s",
getDiscount(),
getCatchphrase());
}
}
DISCOUNTED PAPER
/**
* Represents a Discounted Paper product.
* Paper product that has number of sheets, weight per unit sheet, discount and ticket
* @author NAME
* @version 1.0
*/
public class DiscountedPaper extends PaperProduct {
/**
* Discount variable
*/
private double discount;
/**
* GoldenTicket variable
*/
private GoldenTicket ticket;
/**
* Constructor that creates a DiscountedPaper object.
* Creates object given the name, numberOfSheets, weightOfUnitSheet, discount and ticket object
* @param name String
* @param numberOfSheets int
* @param weightOfUnitSheet double
* @param discount double
* @param ticket GoldenTicket
*/
public DiscountedPaper(String name, int numberOfSheets,
double weightOfUnitSheet, double discount, GoldenTicket ticket) {
// Call super
super(name, numberOfSheets, weightOfUnitSheet);
// Check that discount is in the correct range
this.discount = (discount <= 0 || discount > 50) ? 15.0 : discount;
// set ticket
this.ticket = ticket;
}
/**
* Constructor that creates a DiscountedPaper object given the name, numberOfSheets.
* @param name String
* @param numberOfSheets int
*/
public DiscountedPaper(String name, int numberOfSheets) {
// Call super
super(name, numberOfSheets);
// Set other variables
this.discount = 15.0;
this.ticket = null;
}
/**
* Constructor that creates a DiscountedPaper object given the name.
* @param name String
*/
public DiscountedPaper(String name) {
// Call super
super(name);
// Set other variables
this.discount = 15.0;
this.ticket = null;
}
/**
* Copy constructor that copies all variables from a given DiscountedPaper object.
* @param other DiscountedPaper object
*/
public DiscountedPaper(DiscountedPaper other) {
this(other.getName(),
other.getNumberOfSheets(),
other.getWeightOfUnitSheet(),
other.getDiscount(),
new GoldenTicket(other.getTicket()));
}
/**
* No argument constrcutor.
*/
public DiscountedPaper() {
this("A4");
}
/**
* Getter method for discount value.
* @return discount
*/
public double getDiscount() {
return discount;
}
/**
* Getter method for ticket object.
* @return ticket object
*/
public GoldenTicket getTicket() {
return ticket;
}
/**
* Setter for discount variable.
* @param newDiscount double
*/
public void setDiscount(double newDiscount) {
this.discount = (newDiscount <= 0 || newDiscount > 50) ? 15.0 : newDiscount;
}
/**
* Setter for ticket object.
* @param newTicket GoldenTicket
*/
public void setTicket(GoldenTicket newTicket) {
this.ticket = newTicket;
}
/**
* Calculates the final cost.
* @return double
*/
public double discountedCost() {
// Calculate total cost
double totalCost = totalCost();
// Apply discount
double finalCost = totalCost * (1.0 - getDiscount() / 100.0);
// If there is a golden ticket, apply the discount of the golden ticket
if (getTicket() != null) {
finalCost = finalCost * (1.0 - getTicket().getDiscount() / 100.0);
}
return finalCost;
}
/**
* Return a String representation detailing if the product was shipped or not.
* @param companyName String
* @return String
*/
public String shipDiscounted(String companyName) {
return super.ship(companyName)
+ String.format(" The total cost after the discount is $%.2f", discountedCost());
}
/**
* Return a String representation of all the details about prices.
* It also returns details about discounts of the product
* @return String
*/
public String botherAccounting() {
// Get original cost
double origCost = totalCost();
// Calculate final cost
double finalCost = discountedCost();
// get original discount
double origDiscount = getDiscount();
// get ticket discount (if present)
double ticketDiscount = 0.0;
if (getTicket() != null) {
ticketDiscount = getTicket().getDiscount();
}
// Return String
return String.format("Discounted Paper Product\nOriginal Cost: $%.2f\n"
+ "Final Cost: $%.2f\nOriginal Discount: %.2f%%\n"
+ "Golden Ticket Discount: %.2f%%",
origCost,
finalCost,
origDiscount,
ticketDiscount);
}
}
PAPER PRODUCT
/**
* Represents a Paper product that has number of sheets and weight per unit sheet.
* @author NAME
* @version 1.0
*/
public class PaperProduct {
/**
* Product name.
*/
private final String name;
/**
* Number of sheets.
*/
private int numberOfSheets;
/**
* Weight of each sheet.
*/
private final double weightOfUnitSheet;
/**
* Variable to be shared between instances.
*/
private static int totalProductsToShip = 10;
/**
* Constant.
*/
public static final double COST_PER_GRAM = 0.025;
/**
* All-args constructor to create a PaperProduct object.
* Given the name, number of sheets and weight
* @param name String
* @param numberOfSheets int
* @param weightOfUnitSheet double
*/
public PaperProduct(String name, int numberOfSheets, double weightOfUnitSheet) {
// If name is empty or null set to A4
this.name = (name == null || name.length() == 0) ? "A4" : name;
// Checking if number of sheets is positive or zero
this.numberOfSheets = (numberOfSheets < 0) ? 500 : numberOfSheets;
// if weight is invalid, set to 0.25
this.weightOfUnitSheet = (weightOfUnitSheet < 0) ? 0.25 : weightOfUnitSheet;
}
/**
* Constructor to create a PaperProduct object given the name and number of sheets.
* @param name String
* @param numberOfSheets int
*/
public PaperProduct(String name, int numberOfSheets) {
// If name is empty or null set to A4
this.name = (name == null || name.length() == 0) ? "A4" : name;
// Checking if number of sheets is positive or zero
this.numberOfSheets = (numberOfSheets < 0) ? 500 : numberOfSheets;
this.weightOfUnitSheet = 0.25;
}
/**
* Constructor to create a PaperProduct object given the name.
* @param name String
*/
public PaperProduct(String name) {
// If name is empty or null set to A4
this.name = (name == null || name.length() == 0) ? "A4" : name;
this.numberOfSheets = 500;
this.weightOfUnitSheet = 0.25;
}
/**
* Copy constructor that copies all variables from a given PaperProduct object.
* @param other PaperProduct object
*/
public PaperProduct(PaperProduct other) {
this(other.getName(), other.getNumberOfSheets(), other.getWeightOfUnitSheet());
}
/**
* No argument constructor that sets all variables to its default values.
* @return None
*/
public PaperProduct() {
// Calling constrcutor only with name
this("A4");
}
/**
* Getter method for name.
* @return name
*/
public String getName() {
return name;
}
/**
* Getter method for number of sheets.
* @return int
*/
public int getNumberOfSheets() {
return numberOfSheets;
}
/**
* Getter method for weight of unit sheet.
* @return double
*/
public double getWeightOfUnitSheet() {
return weightOfUnitSheet;
}
/**
* Getter method for total products to ship.
* @return int
*/
public static int getTotalProductsToShip() {
return PaperProduct.totalProductsToShip;
}
/**
* Setter for number of sheets.
* @param newNumberOfSheets int
*/
public void setNumberOfSheets(int newNumberOfSheets) {
this.numberOfSheets = (newNumberOfSheets < 0) ? 500 : newNumberOfSheets;
}
/**
* This function returns the total weight of the product.
* @return double
*/
public double totalWeight() {
return numberOfSheets * weightOfUnitSheet;
}
/**
* Returns the total cost of the product.
* @return double
*/
public double totalCost() {
return COST_PER_GRAM * totalWeight();
}
/**
* Returns a string representing the object.
* @return String
*/
public String paperString() {
return String.format("%.2fg of %s for $%.2f", totalWeight(), getName(), totalCost());
}
/**
* Returns a String depending on if the item is shipped or not.
* Receives the company name as parameter
* @param companyName String representing the company name
* @return String
*/
public String ship(String companyName) {
if (getTotalProductsToShip() > 0) {
totalProductsToShip -= 1;
return "Shipped " + this.paperString() + " to " + companyName + ".";
} else {
return "Cannot ship any items, Warehouse is empty!";
}
}
}
WAREHOUSE
/**
* This is the Driver of the program.
* @author NAME
* @version 1.0
*/
public class Warehouse {
/**
* Main method for the driver application.
* @param args String array with arguments
*/
public static void main(String[] args) throws Exception {
// Create 2 paper products
PaperProduct pp1 = new PaperProduct("Paper Product 1", 450, 0.01);
PaperProduct pp2 = new PaperProduct("Paper Product 2", 900, 0.17);
// Create 1 Golden Ticket
GoldenTicket gt = new GoldenTicket("Super Ticket!", 30); // with 30% discount
// Create 2 DiscountedPaper products
PaperProduct dp1 = new DiscountedPaper("Discounted Paper 1", 1500, 0.01, 13.0, null); // without golden ticket
PaperProduct dp2 = new DiscountedPaper("Discounted Paper 2", 1500, 0.01, 13.0, gt); // with golden ticket
// Create 2 PhotoPaper objects
PaperProduct photo1 = new PhotoPaper("Photo Paper 1", 2500, 0.005, 35.0);
PaperProduct photo2 = new PhotoPaper("Photo Paper 2", 400, 0.20, 25.0);
System.out.println(((PhotoPaper) photo1).photoString());
// Let's ship
System.out.println(pp1.ship("IBM"));
System.out.println(pp2.ship("IBM"));
System.out.println(dp1.ship("IBM"));
System.out.println(dp2.ship("IBM"));
System.out.println(photo1.ship("IBM"));
System.out.println(photo2.ship("IBM"));
for (int i = 0; i < 10; i++) {
System.out.println(pp1.ship("Apple"));
}