Instructions
Requirements and Specifications
Source Code
GOLDEN TICKET
public class GoldenTicket {
private String catchphrase;
private double discount;
/**
* Constrcutor 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!"
if(catchphrase.length() == 0) {
catchphrase = "Congrats!";
}
// if discount is not in (0, 25] set to 15.0
if(discount <= 0 ||discount > 25) {
discount = 15.0;
}
this.catchphrase = catchphrase;
this.discount = discount;
}
// Getters and setters
public String getCatchPhrase() {return catchphrase;}
public void setCatchPhrase(String newCatchPhrase) {catchphrase = newCatchPhrase;}
public double getDiscount() {return discount;}
public void setDiscount(double newDiscount) {discount = newDiscount;}
/**
* Methods
*/
/**
* 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
public class DiscountedPaper extends PaperProduct {
private double discount;
private GoldenTicket ticket;
/**
* Constructor that creates a DiscountedPaper 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 valid
if(discount <= 0 || discount > 50) {
discount = 15.0;
}
this.discount = discount;
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);
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);
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) {
super(other.getName(), other.getNumberfSheets(), other.getWeightOfUnitSheet());
this.discount = other.getDiscount();
this.ticket = other.getTicket();
}
// getters and setts
public double getDiscount() {return discount;}
public GoldenTicket getTicket() {return ticket;}
public void setDiscount(double newDiscount) {discount = newDiscount;}
public void setTicket(GoldenTicket newTicket) {ticket = newTicket;}
/**
* Methods
*/
/**
* Calculates the final cost given the discount and the GoldenTicket discount (if present)
* @return double
*/
public double discountedCost() {
// Calculate total cost
double total_cost = totalCost();
// Apply discount
double final_cost = total_cost*(1.0-getDiscount()/100.0);
// If there is a golden ticket, apply the discount of the golden ticket
if(getTicket() != null) {
final_cost = final_cost *(1.0 - getTicket().getDiscount()/100.0);
}
return final_cost;
}
/**
* Return a String representation detailing if the product was shipped or not
* @param companyName String
* @return String
*/
public String shipDiscount(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 and discounts of the product
* @returN String
*/
public String botherAccounting()
{
// Get original cost
double orig_cost = totalCost();
// Calculate final cost
double final_cost = discountedCost();
// get original discount
double orig_discount = getDiscount();
// get ticket discount (if present)
double ticket_discount = 0.0;
if(getTicket() != null) {
ticket_discount = getTicket().getDiscount();
}
// Return String
return String.format("Discounted Paper Product\nOriginal Cost: $%.2f\nFinal Cost: $%.2f\nOriginal Discount: %.2f%%\nGolden Ticket Discount: %.2f%%", orig_cost, final_cost, orig_discount, ticket_discount);
}
}
PAPER PRODUCT
class PaperProduct
{
private String name;
private int numberOfSheets;
private double weightOfUnitSheet;
static int totalProductsToShip = 10;
public final double COST_PER_GRAM = 0.025;
/**
* Constructor to create a PaperProduct object given the name, number of sheets and weight
* @param name
* @param numberOfSheets
* @param weightOfUnitSheet
*/
public PaperProduct(String name, int numberOfSheets, double weightOfUnitSheet) {
this.name = name;
// if numberOfSheets is invalid, set to 500
if(numberOfSheets < 0) {
numberOfSheets = 500;
}
this.numberOfSheets = numberOfSheets;
// if weight is invalid, set to 0.25
if(weightOfUnitSheet < 0) {
weightOfUnitSheet = 0.25;
}
this.weightOfUnitSheet = weightOfUnitSheet;
}
/**
* Constructor to create a PaperProduct object given the name and number of sheets
;* @param name
* @param numberOfSheets
*/
public PaperProduct(String name, int numberOfSheets) {
this.name = name;
// if numberOfSheets is invalid, set to 500
if(numberOfSheets < 0) {
numberOfSheets = 500;
}
this.numberOfSheets = numberOfSheets;
this.weightOfUnitSheet = 0.25;
}
/**
* Constructor to create a PaperProduct object given the name
* @param name
*/
public PaperProduct(String name) {
this.name = name;
this.numberOfSheets = 500;
this.weightOfUnitSheet = 0.25;
}
/**
* Copy constructor that copies all variables from a given PaperProduct object
* @param other
*/
public PaperProduct(PaperProduct other) {
this.name = other.name;
this.numberOfSheets = other.numberOfSheets;
this.weightOfUnitSheet = other.weightOfUnitSheet;
}
// Add getters
public String getName() {return name;}
public int getNumberfSheets() {return numberOfSheets;}
public double getWeightOfUnitSheet() {return weightOfUnitSheet;}
public int getTotalProductsToShip() {return PaperProduct.totalProductsToShip;}
// Setter only for numberOfSheets
public void setNumberOfSheets(int newNumberOfSheets) {numberOfSheets = newNumberOfSheets;}
/**
* METHODS
*/
/**
* 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
public class Warehouse {
// Main method
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);
// 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"));
}
}
}