Given the attached class named Item with fields for holding the item's name and description, extend Item with a class named SaleItem. The SaleItem class should have double fields for price and shipping cost, and an int field for the number of items in stock. Write a copy constructor and a constructor that takes data to instantiate all fields. Write a boolean method purchase that takes a quantity as a parameter and returns true if the purchase can be completed (if theres enough stock), and false otherwise. The purchase method should appropriately deduct from the field that holds the number of items in stock. Write a restock method that takes a quantity and adds it to the stock. Write accessors for price and shipping cost, and write a toString() method for SaleItem. Write exactly the methods described here, and no others. Include exactly the fields described here, and no others.
A SaleItem object's toString should produce a string that looks like this example:
Lovely shirt
Blue shirt that is lovely.
Price: $9.99
Shipping: $4.95
Write another class called AuctionItem that extends the Item class. AuctionItem should have one constant, accessible from outside the class - NONE, of type String, representing the high bidder when there are no bids in the auction. It should have three private fields: one for the account name of the high bidder (a String) and one for the high bid (a double). It should also have a field for the ending time of the auction, which will be an instance of the Instant class. (See the appendix at the end of this assignment for help with the Instant class.)
The AuctionItem class should have two constructors - a copy constructor and an argument constructor. In the copy constructor, all fields should be copied from the object passed in. In the argument constructor, the arguments passed in will be those needed to instantiate the superclass object and one other -- an integer duration, which is the number of hours that the auction should last. The high bid field will be initialized to 0, the high bidder field will be initialized to NONE, and the ending time field will be initialized to a time that is duration hours from the current time.
The AuctionItem class should have a method isActive, which returns true if the auction is still active and false otherwise. An auction is active if it hasn't yet ended. Whether it has ended depends on the current time and the ending time.
It should have a bid method, which takes an amount (double) and the bidder (String) as parameters and returns a boolean - true if the bid was successful (auction still active and bid higher than high bid), and false otherwise.
It should have two accessors - getTimeLeft, which returns the amount of time left in seconds, and getHighBidder. It should have a toString method.
An AuctionItem object's toString should produce a String that looks like this example:
Disney pin
Cute stylized Pluto pin!
High bid: $1.00
High Bidder: Princess4Evar
Time Left: 23 hours, 59 minutes
Ends at: 2018-01-14T00:22:58.662903800Z
Focus on inheritance first and getting the parts of the class that use the Instant object working second, after you're confident youve written a correct inheritance hierarchy.
You've been provided with an ItemClient java program that should work with your classes, if your classes are implemented to specifications. The output of this program should be self explanatory. Do not edit this program.
Please create a class diagram showing the three classes and the inheritance relationship.
Use only techniques that are covered in chapters 1-8 and 10 of Gaddis/Muganda or covered in this course. Output to the console should be from the ItemClient program only (your classes should not interact with the end user in any way). Do not, under any circumstances, use System.exit, or any other code (e.g. return, break) that artificially breaks out of a logically controlled block of code (conditional statement or loop).
Item.java
/**
* Item is an abstract base class for all items for sale.
**/
public class Item {
//Fields:
private String name; //the name of the item
private String description; //the person's address
//Methods:
//CONSTRUCTORS
/**
* Item, constructor that takes arguments for all fields
* @param name -- String, name of the item
* @param description -- String, description of the item
*/
public Item(String name, String description){
this.name = name;
this.description = description;
}
/**
* Item, copy constructor
* @param toCopy -- an Item object to copy into the current object
*/
public Item(Item toCopy){
this.name = toCopy.name;
this.description = toCopy.description;
}
//ACCESSORS
/**
* getName
* @return name of the item, String
*/
public String getName(){
return this.name;
}
/**
* getDescription
* @return description of the item, String
*/
public String getDescription(){
return this.description;
}
/**
* toString, return a String representing the data of the object.
* @return String with name and description on separate lines.
*/
public String toString(){
return this.name + "\n" + this.description;
}
}
ItemClient.java
/**
* Create some SaleItem and AuctionItem objects, send messages, compare results.
*/
public class ItemClient {
private static String NAME = "Lovely shirt";
private static String DESCRIPTION = "Blue shirt that is lovely.";
private static double PRICE = 9.99;
private static double SHIPPING = 4.95;
private static int NUM_IN_STOCK = 50;
private static String AUCTION_ITEM_NAME = "Disney pin";
private static String AUCTION_ITEM_DESCRIPTION = "Cute stylized Disney pin.";
private static double DEFAULT_HIGH_BID = 0.0;
private static String DEFAULT_HIGH_BIDDER = AuctionItem.NONE;
private static String BIDDER = "Princess4Evar";
private static String BIDDER2 = "StoleYourAuction";
private static double BID = 1.0;
private static double SECOND_BID = 1.5;
/**
* Display what the SaleItem output should look like for constant values.
*/
public static void printExpectedSaleItem() {
String str = NAME + "\n" + DESCRIPTION;
str += "\nPrice:\t" + PRICE + "\nShipping:\t" + SHIPPING;
System.out.println(str);
}
/**
* Display what the AuctionItem output should look like for constant values.
*/
public static void printExpectedAuctionItem() {
String str = AUCTION_ITEM_NAME + "\n" + AUCTION_ITEM_DESCRIPTION + "\nHigh bid:\t" + DEFAULT_HIGH_BID
+ "\nHigh bidder:\t" + DEFAULT_HIGH_BIDDER + "\nTime Left:\t1 hours, 0 minutes\nEnds at:\t(current time + 1 hour) \n";
System.out.println(str);
}
/**
* Display what the AuctionItem output should look like after one bid.
*/
public static void printExpectedAuctionItemOneBid() {
String str = AUCTION_ITEM_NAME + "\n" + AUCTION_ITEM_DESCRIPTION + "\nHigh bid:\t" + BID
+ "\nHigh bidder:\t" + BIDDER + "\nTime Left:\t0 hours, 59 minutes\nEnds at:\t(current time + 1 hour) \n";
System.out.println(str);
}
/**
* Display what the AuctionItem output should look like after second successful bid.
*/
public static void printExpectedAuctionItemSecondBid() {
String str = AUCTION_ITEM_NAME + "\n" + AUCTION_ITEM_DESCRIPTION + "\nHigh bid:\t" + SECOND_BID
+ "\nHigh bidder:\t" + BIDDER2 + "\nTime Left:\t0 hours, 59 minutes\nEnds at:\t(current time + 1 hour) \n";
System.out.println(str);
}
/**
* Display what the AuctionItem output should look like for constant values and zero days.
*/
public static void printExpectedAuctionItemExpired() {
String str = AUCTION_ITEM_NAME + "\n" + AUCTION_ITEM_DESCRIPTION + "\nHigh bid:\t" + DEFAULT_HIGH_BID
+ "\nHigh bidder:\t" + DEFAULT_HIGH_BIDDER + "\nTime Left:\t0 hours, 0 minutes\nEnds at:\t(current time) \n";
System.out.println(str);
}
/**
* @param args
*/
public static void main(String[] args) {
// Create a sale item and send it messages
System.out.println("**********************************************************************");
System.out.println("SaleItem Tests");
System.out.println("**********************************************************************\n");
System.out.println("**********************************************************************");
System.out.println("Test 1: Create a sale item and test output.");
System.out.println("Expected output:");
printExpectedSaleItem();
System.out.println("Actual output:");
SaleItem shirt = new SaleItem(NAME, DESCRIPTION, PRICE, SHIPPING, NUM_IN_STOCK);
System.out.println(shirt);
System.out.println("**********************************************************************\n");
System.out.println("**********************************************************************");
System.out.println("Test 2: Attempt to purchase more of the item than available.");
System.out.println("Expected output: false / Failure");
System.out.print("Actual output: ");
if (shirt.purchase(NUM_IN_STOCK+1)) System.out.println("Success");
else System.out.println("Failure");
System.out.println("**********************************************************************\n");
System.out.println("**********************************************************************");
System.out.println("Test 3: Attempt to purchase exact amount of the item available.");
System.out.println("Expected output: true / Success");
System.out.print("Actual output: ");
if (shirt.purchase(NUM_IN_STOCK)) System.out.println("Success");
else System.out.println("Failure");
System.out.println("**********************************************************************\n");
System.out.println("**********************************************************************");
System.out.println("Test 4: Create a new sale item with same parameters, ");
System.out.println(" and attempt to purchase less than amount available.");
System.out.println("Expected output: true / Success");
System.out.print("Actual output: ");
shirt = new SaleItem(NAME, DESCRIPTION, PRICE, SHIPPING, NUM_IN_STOCK);
if (shirt.purchase(NUM_IN_STOCK-1)) System.out.println("Success");
else System.out.println("Failure");
System.out.println("**********************************************************************\n");
System.out.println("**********************************************************************");
System.out.println("Test 5: Create a new sale item with same parameters, ");
System.out.println(" and attempt to add to the stock, then attempt to");
System.out.println(" remove full stock.");
System.out.println("Expected output: true / Success");
System.out.print("Actual output: ");
shirt = new SaleItem(NAME, DESCRIPTION, PRICE, SHIPPING, NUM_IN_STOCK);
shirt.restock(NUM_IN_STOCK);
if (shirt.purchase(NUM_IN_STOCK * 2)) System.out.println("Success");
else System.out.println("Failure");
System.out.println("**********************************************************************\n");
System.out.println("**********************************************************************");
System.out.println("Test 5: Create a new sale item with same parameters, ");
System.out.println(" and attempt to add to the stock, then attempt to");
System.out.println(" remove full stock.");
System.out.println("Expected output: true / Success");
System.out.print("Actual output: ");
shirt = new SaleItem(NAME, DESCRIPTION, PRICE, SHIPPING, NUM_IN_STOCK);
shirt.restock(NUM_IN_STOCK);
if (shirt.purchase(NUM_IN_STOCK * 2)) System.out.println("Success");
else System.out.println("Failure");
System.out.println("**********************************************************************\n");
System.out.println("**********************************************************************");
System.out.println("AuctionItem Tests");
System.out.println("**********************************************************************\n");
System.out.println("(Note: durations might be off by nanoseconds, showing 1 hours 0 minutes");
System.out.println(" instead of 0 hours, 59 minutes.)");
System.out.println("**********************************************************************");
System.out.println("Test 1: Create an auction item active for 1 hour and view");
System.out.println("Expected output: ");
printExpectedAuctionItem();
System.out.println("Actual output: ");
AuctionItem disneyPin = new AuctionItem(AUCTION_ITEM_NAME, AUCTION_ITEM_DESCRIPTION, 1);
System.out.println(disneyPin);
System.out.println("**********************************************************************\n");
System.out.println("**********************************************************************");
System.out.println("Test 2: Bid on auction and view");
System.out.println("Expected output: Success");
printExpectedAuctionItemOneBid();
System.out.print("Actual output: ");
if (disneyPin.bid(BID, BIDDER)) System.out.println("Success");
else System.out.println("Failure");
System.out.println(disneyPin);
System.out.println("**********************************************************************\n");
System.out.println("**********************************************************************");
System.out.println("Test 3: A bid at the current high bid");
System.out.println("Expected output: Failure");
printExpectedAuctionItemOneBid();
System.out.print("Actual output: ");
if (disneyPin.bid(BID, BIDDER2)) System.out.println("Success");
else System.out.println("Failure");
System.out.println(disneyPin);
System.out.println("**********************************************************************\n");
System.out.println("**********************************************************************");
System.out.println("Test 4: A bid below the current high bid");
System.out.println("Expected output: Failure");
printExpectedAuctionItemOneBid();
System.out.print("Actual output: ");
if (disneyPin.bid(BID-.5, BIDDER2)) System.out.println("Success");
else System.out.println("Failure");
System.out.println(disneyPin);
System.out.println("**********************************************************************\n");
System.out.println("**********************************************************************");
System.out.println("Test 5: A bid above the current high bid");
System.out.println("Expected output: Success");
printExpectedAuctionItemSecondBid();
System.out.print("Actual output: ");
if (disneyPin.bid(SECOND_BID, BIDDER2)) System.out.println("Success");
else System.out.println("Failure");
System.out.println(disneyPin);
System.out.println("**********************************************************************\n");
System.out.println("**********************************************************************");
System.out.println("Test 6: Create an auction item active for 0 days and view");
System.out.println("Expected output: ");
printExpectedAuctionItemExpired();
System.out.println("Actual output: ");
disneyPin = new AuctionItem(AUCTION_ITEM_NAME, AUCTION_ITEM_DESCRIPTION, 0);
System.out.println(disneyPin);
System.out.println("**********************************************************************\n");
System.out.println("**********************************************************************");
System.out.println("Test 7: Attempt to bid on an inactive auction");
System.out.println("Expected output: Failure");
printExpectedAuctionItemExpired();
System.out.print("Actual output: ");
if (disneyPin.bid(BID, BIDDER)) System.out.println("Success");
else System.out.println("Failure");
System.out.println(disneyPin);
System.out.println("**********************************************************************\n");
}
}