/**
A class of public constants.
@author Frank M. Carrano
@version 4.0
*/
public class Constants
{
private Constants()
{
} // end private default constructor
public static final double INCHES_PER_CENTIMETER = 0.39370079;
public static final double FEET_PER_METER = 3.2808399;
public static final double MILES_PER_KILOMETER = 0.62137119;
} // end Constants
/**
@author Frank M. Carrano
@version 4.0
*/
public class Demo
{
public static void main(String[] args)
{
System.out.println(Constants.FEET_PER_METER);
System.out.println(Constants.MILES_PER_KILOMETER);
} // end main
} // end Demo
/**
An interface that describes the operations of a bag of objects.
@author Frank M. Carrano
@version 4.0
*/
public interface BagInterface<T>
{
/** Gets the current number of entries in this bag.
@return The integer number of entries currently in the bag. */
public int getCurrentSize();
/** Sees whether this bag is empty.
@return True if the bag is empty, or false if not. */
public boolean isEmpty();
/** Adds a new entry to this bag.
@param newEntry The object to be added as a new entry.
@return True if the addition is successful, or false if not. */
public boolean add(T newEntry);
/** Removes one unspecified entry from this bag, if possible.
@return Either the removed entry, if the removal.
was successful, or null. */
public T remove();
/** Removes one occurrence of a given entry from this bag.
@param anEntry The entry to be removed.
@return True if the removal was successful, or false if not. */
public boolean remove(T anEntry);
/** Removes all entries from this bag. */
public void clear();
/** Counts the number of times a given entry appears in this bag.
@param anEntry The entry to be counted.
@return The number of times anEntry appears in the bag. */
public int getFrequencyOf(T anEntry);
/** Tests whether this bag contains a given entry.
@param anEntry The entry to locate.
@return True if the bag contains anEntry, or false if not. */
public boolean contains(T anEntry);
/** Retrieves all entries that are in this bag.
@return A newly allocated array of all the entries in the bag.
Note: If the bag is empty, the returned array is empty. */
public T[] toArray();
} // end BagInterface
/** A class of items for sale.
@author Frank M. Carrano
@version 4.0
*/
public class Item
{
private String description;
private int price;
public Item(String productDescription, int productPrice)
{
description = productDescription;
price = productPrice;
} // end constructor
public String getDescription()
{
return description;
} // end getDescription
public int getPrice()
{
return price;
} // end getPrice
public String toString()
{
return description + "\t$" + price / 100 + "." + price % 100;
} // end toString
} // end Item
/** A class that maintains a shopping cart for an online store.
@author Frank M. Carrano
@version 4.0
*/
public class OnlineShopper
{
public static void main(String[] args)
{
Item[] items = {new Item("Bird feeder", 2050),
new Item("Squirrel guard", 1547),
new Item("Bird bath", 4499),
new Item("Sunflower seeds", 1295)};
BagInterface<Item> shoppingCart = new ArrayBag<>();
int totalCost = 0;
// Statements that add selected items to the shopping cart:
for (int index = 0; index < items.length; index++)
{
Item nextItem = items[index]; // Simulate getting item from shopper
shoppingCart.add(nextItem);
totalCost = totalCost + nextItem.getPrice();
} // end for
// Simulate checkout
while (!shoppingCart.isEmpty())
System.out.println(shoppingCart.remove());
System.out.println("Total cost: " + "\t$" + totalCost / 100 + "." +
totalCost % 100);
} // end main
} // end OnlineShopper
/*
Sunflower seeds $12.95
Bird bath $44.99
Squirrel guard $15.47
Bird feeder $20.50
Total cost: $93.91
*/
标签:,return,Item,bag,entry,end,public From: https://blog.51cto.com/u_15892225/11993479