📄 basket.java
字号:
package Catalogue;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Formatter;
import java.util.Currency;
public class Basket implements Serializable
{
private static final long serialVersionUID = 1;
private ArrayList<Product> theContents = new ArrayList<Product>(10);
/**
* Adds a product to the Basket
*/
public void add( Product pr ) // Add a product to the
{ // product list
theContents.add( pr ); //
}
/**
* Returns the number of products in the basket.
* @return number of products
*/
public int number() // Return the number of
{ // entries in the
return theContents.size(); // product list
}
/**
*
*/
public Product remove() // Remove and return last item
{
int items = theContents.size();
if ( items < 1 ){
throw new Error("EMPTY: Basket.remove()");
}
return theContents.remove( items-1 );
}
/**
* Clear the list. All items stored are removed.
*/
public void clear() // Clear the
{ // the product list
theContents.clear();
}
/**
* Returns an array of the held products.
* @return array of held products
*/
public Product[] products() // Return array of products
{
Product results[] = new Product[theContents.size()]; // Result array
return theContents.toArray( results ); // return
}
/**
* Returns a string containing a description of all the products held.
* @return string description of products
*/
public String details() // Return list of products as string
{
Locale uk = Locale.UK;
StringBuilder sb = new StringBuilder(256);
Formatter fr = new Formatter(sb, uk);
String csign = (Currency.getInstance( uk )).getSymbol();
double total = 0.00;
for ( Product pr: theContents )
{
int no = pr.getQuantity();
fr.format("%-7s", pr.getProductNo() );
fr.format("%-14.14s ", pr.getDescription() );
fr.format("(%d) ", no );
fr.format("%s%7.2f", csign, pr.getPrice()*no );
fr.format("\n");
total += pr.getPrice() * no;
}
fr.format("--------------------------\n");
fr.format("Total ");
fr.format("%s%7.2f\n", csign, total );
return sb.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -