超市購物,連結串列
interface ILink<E>{
public void add(E e);
public int size();
public boolean isEmpty();
public Object [] toArray();
public E get(int index);
public void set(int index,E data);
public boolean contains(E data);
public void remove(E data);
public void clean();
}
class ILinkImpl<E> implements ILink<E>{
private class Node{
private Node next;
private E data;
public Node(E data){
this.data = data;
}
public void addNode(Node newNode){
if(this.next == null){
this.next = newNode;
}else{
this.next.addNode(newNode);//儲存節點
}
}
public void toArrayNode(){
ILinkImpl.this.returnData[ILinkImpl.this.foot ++] = this.data;
if(this.next != null){
this.next.toArrayNode();
}
}
public E getNode(int index){
if(ILinkImpl.this.foot ++ == index){
return this.data;
}else{
return this.next.getNode(index);
}
}
public void setNode(int index,E data){
if(ILinkImpl.this.foot ++ == index){
this.data = data;
}else{
this.next.setNode(index,data);
}
}
public boolean containsNode(E data){
if(data.equals(this.data)){
return true;
}else{
if(this.next != null){
return this.next.containsNode(data);
}else{
return false;
}
}
}
public void removeNode(Node previous,E data){
if(this.data.equals(data)){
previous = this.next;
}else{
this.next.removeNode(this,data);
}
}
}
private Node root;
private int count;
private int foot;
private Object [] returnData;
public void add(E e){
if(e == null){
return ;
}
Node newNode = new Node(e);
if(this.root == null){
this.root = newNode;
}else{
this.root.addNode(newNode);//儲存資料
}
this.count ++;
}
public int size(){
return this.count;
}
public boolean isEmpty(){
return this.root == null;
}
public Object [] toArray(){
if(this.root == null){
return null;
}
this.foot = 0;
this.returnData = new Object[this.count];
this.root.toArrayNode();
return this.returnData;
}
public E get(int index){
if(index >= this.count){
return null;
}
this.foot = 0;
return this.root.getNode(index);
}
public void set(int index,E data){
if(data == null){
return ;
}
this.foot = 0;
this.root.setNode(index,data);
}
public boolean contains(E data){
if(data == null){
return false;
}
return this.root.containsNode(data);
}
public void remove(E data){
if(this.contains(data)){
if(this.root.data.equals(data)){
this.root = this.root.next;
}else{
this.root.removeNode(this.root,data);
}
this.count --;
}
}
public void clean(){
this.count = 0;
this.root = null;
}
}
interface IGoods{//商品標準
public String getName();
public double getPrice();
}
interface IShopCar{
public void add(IGoods goods);
public void delete(IGoods goods);
public Object [] getAll();
}
class ShopCar implements IShopCar{
private ILink<IGoods> allGoods = new ILinkImpl<IGoods>();
public void add(IGoods goods){
this.allGoods.add(goods);
}
public void delete(IGoods goods){
this.allGoods.remove(goods);
}
public Object [] getAll(){
return this.allGoods.toArray();
}
}
class Cashier {
private IShopCar shopCar;
public Cashier(IShopCar shopCar){
this.shopCar = shopCar;
}
public int getAllCount(){
return this.shopCar.getAll().length;
}
public double getAllPrice(){
double all = 0.0;
Object result [] = this.shopCar.getAll();
for(Object obj : result){
IGoods goods = (IGoods) obj;
all += goods.getPrice();
}
return all;
}
}
class Ball implements IGoods{
private String name;
private double price;
public Ball(String name,double price){
this.name = name;
this.price = price;
}
public String getName(){
return this.name;
}
public double getPrice(){
return this.price;
}
public boolean equals(Object obj){
if(obj == null){
return false;
}
if(obj == this){
return true;
}
if(!(obj instanceof Ball)){
return false;
}
Ball ball = (Ball) obj;
return this.name.equals(ball.getName()) && this.price == ball.getPrice();
}
public String toString(){
return "【商品資訊:】名稱:" + this.name + "、價格:" + this.price;
}
}
class Shoes implements IGoods{
private String name;
private double price;
public Shoes(String name,double price){
this.name = name;
this.price = price;
}
public String getName(){
return this.name;
}
public double getPrice(){
return this.price;
}
public boolean equals(Object obj){
if(obj == null){
return false;
}
if(obj == this){
return true;
}
if(!(obj instanceof Shoes)){
return false;
}
Shoes shoes = (Shoes) obj;
return this.name.equals(shoes.getName()) && this.price == shoes.getPrice();
}
public String toString(){
return "【商品資訊:】名稱:" + this.name + "、價格:" + this.price;
}
}
public class JavaDemo{
public static void main(String args []){
IShopCar car = new ShopCar();
car.add(new Ball("NBA籃球",298.00));
car.add(new Ball("CBA籃球",99.8));
car.add(new Shoes("NBA籃球鞋",498.00));
car.add(new Shoes("李寧籃球鞋",198.00));
Cashier cas = new Cashier(car);
System.out.println("購買商品數量:" + cas.getAllCount());
System.out.println("購買商品總價:" + cas.getAllPrice());
}
}
思路:
1,商品標準;
2,購物車標準;
3,購物車將商品、前臺、連結串列的關係關聯起來。