由于教材答案上没有源代码,求求各位大佬给个源代码和说明文档,谢谢各位了!!!
https://wenku.baidu.com/view/44bf909f59eef8c75ebfb365.html
第一题:
public class MyStack implements StackExercise{
private int maxSize;
private String[] stackArray;
private int top;
public MyStack(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(String item) {
stackArray[++top] = item;
}
public long pop() throws StackEmptyException{
return stackArray[top--];
}//异常不处理,谁调用这个方法谁去处理。
public boolean isEmpty() {
return (top == -1);
}
public static void main(String[] args) {
try {
MyStack theStack = new MyStack(10);
theStack.push("商品1");
theStack.push("商品2");
theStack.push("商品3");
theStack.push("商品4");
theStack.push("商品5");
while (!theStack.isEmpty()) {
String value = theStack.pop();
System.out.println(value);
System.out.println(",");
}}
catch(StackEmptyException e){
e.printStack;
}
}
}
第二题:
public class Cart implements ShoppingCart{
private int count;
private double price;
private ArrayList<Item> list
public Cart(){
count=0;
price=0;
list=new ArrayList<Item>();
}
public void addItems(Item anItem,int quantity) throws NegativeCountException{
price+=quantity*anItem.value;//其中就是anItem物品的价格乘数量
count+=quantity;
while(quantity!=0){
list.add(anItem);
quantity--;
}
}
public void deleteItem(Item anItem,int quantity){
price-=quantity*anItem.value;
count+=quantity;
while(quantity!=0){
list.remove(anItem);
quantity--;
}
}
public int itemCount(){
return count;
}
public Iterator iterator(){
return list.iterator();//ArrayList中有迭代器的方法。
}
}