class?Fruit{
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都網(wǎng)站建設、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的湘鄉(xiāng)網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!
private?String?fruitName;
private?float?price;
public?Fruit(String?fruitName,?float?price)?{
super();
this.fruitName?=?fruitName;
this.price?=?price;
}
public?String?getFruitName()?{
return?fruitName;
}
public?void?setFruitName(String?fruitName)?{
this.fruitName?=?fruitName;
}
public?float?getPrice()?{
return?price;
}
public?void?setPrice(float?privce)?{
this.price?=?privce;
}
@Override
public?String?toString()?{
return?"水果名稱:"?+?getFruitName()?+?"???????"?+?"價格:"?+?getPrice();
}
}
class?Apple?extends?Fruit{
public?Apple(String?fruitName,?float?price)?{
super(fruitName,?price);
}
}
class?Banana?extends?Fruit{
public?Banana(String?fruitName,?float?price)?{
super(fruitName,?price);
}
}
public?class?FruitShop?{
ListFruit?list;
public?FruitShop(){
list?=?new?ArrayListFruit();
list.add(new?Apple("蘋果",3));
list.add(new?Banana("香蕉",2));
}
public?void?show(){
for?(Fruit?f?:?list)?{
System.out.println(f);
}
}
public?static?void?main(String[]?args)?{
new?FruitShop().show();
}
}
只寫個demo級的例程很好寫,但用到生產(chǎn)環(huán)境中還得具體分析設計再編碼。這種代碼網(wǎng)上太多了內(nèi),你隨便搜下就有了。
package?entity;
public?class?Market?{
private?int?id;//id
private?int?num;//數(shù)量
private?String?goods;//商品
private?double?price;//價格
public?Market(int?id,?int?num,?String?goods,?double?price)?{
super();
this.id?=?id;
this.num?=?num;
this.goods?=?goods;
this.price?=?price;
}
public?int?getId()?{
return?id;
}
public?void?setId(int?id)?{
this.id?=?id;
}
public?int?getNum()?{
return?num;
}
public?void?setNum(int?num)?{
this.num?=?num;
}
public?String?getGoods()?{
return?goods;
}
public?void?setGoods(String?goods)?{
this.goods?=?goods;
}
public?double?getPrice()?{
return?price;
}
public?void?setPrice(double?price)?{
this.price?=?price;
}
public?double?calc(?){
double?sum=price*num;
System.out.println("您消費共計:"+sum+"¥");
return?sum;
}
}
package?test;
import?java.util.HashMap;
import?java.util.Map;
import?java.util.Scanner;
import?entity.Market;
public?class?Test?{
private?static?MapInteger,Market?goods=new?HashMapInteger,?Market();
public?static?void?main(String[]?args)?{
System.out.println("-------超市計價系統(tǒng)-------");
String?goods1="可口可樂";
String?goods2="爆米花";
String?goods3="益達";
printTable("編號","商品","價格");
printTable("1",goods1,"3.0¥");
printTable("2",goods2,"5.0¥");
printTable("3",goods3,"10.0¥");
goods.put(1,?new?Market(1,?1,?goods1,?3.0));
goods.put(2,?new?Market(2,?1,??goods2,?5.0));
goods.put(3,?new?Market(3,?1,?goods3,?10.0));
Scanner?input?=?new?Scanner(System.in);
System.out.println("請輸入商品的編號:");
int?num?=?input.nextInt();
System.out.println("請輸入商品的數(shù)量");
int?amount?=?input.nextInt();
Market?market?=?goods.get(num);
market.setNum(amount);
market.calc();
}
private?static?void?printTable(String?row1,String?row2,String?row3?)?{
System.out.print(row1);
int?times=12;
if?(row2!="商品")?{
times=5;
}
for?(int?i?=?0;?i??times;?i++)?{
System.out.print("?");
}
System.out.print(row2);
for?(int?i?=?0;?i??10;?i++)?{
System.out.print("?");
}
System.out.print(row3);
System.out.println("\n");
}
}
//測試結果:
-------超市計價系統(tǒng)-------
編號????????????商品??????????價格
1?????可口可樂??????????3.0¥
2?????爆米花??????????5.0¥
3?????益達??????????10.0¥
請輸入商品的編號:
3
請輸入商品的數(shù)量
5
您消費共計:50.0¥
那很簡單啊,界面用dreamweaver直接做就行 ,代碼就是JAVA訪問數(shù)據(jù)庫,就那幾行
以前學習java又做個實例,挺值得學習的。
1.首先我先列出我們所需要的java類結構。
1)Database.java --------- 模擬存儲商品的數(shù)據(jù)庫。
2)McBean.java ------------ 商品實體類,一個普通的javabean,里面有商品的基本屬性。
3)OrderItemBean.java --- 訂單表。
4)ShoppingCar.java ------ 這個就是最主要的購物車,當然比較簡單。
5)TestShoppingCar.java --- 這個是測試類。
2.下面貼出具體代碼并帶關鍵注釋。
---Database.java
public class Database {
/*采用Map存儲商品數(shù)據(jù),為什么呢?我覺得這個問題你自己需要想下。
* Integer 為Map的key值,McBean為Map的value值。
*/
private static MapInteger, McBean data = new HashMapInteger, McBean();
public Database() {
McBean bean = new McBean();
bean.setId(1);
bean.setName("地瓜");
bean.setPrice(2.0);
bean.setInstuction("新鮮的地瓜");
data.put(1, bean);//把商品放入Map
bean = new McBean();
bean.setId(2);
bean.setName("土豆");
bean.setPrice(1.2);
bean.setInstuction("又好又大的土豆");
data.put(2, bean);//把商品放入Map
bean = new McBean();
bean.setId(3);
bean.setName("絲瓜");
bean.setPrice(1.5);
bean.setInstuction("本地絲瓜");
data.put(3, bean);//把商品放入Map
}
public void setMcBean(McBean mcBean){
data.put(mcBean.getId(),mcBean);
}
public McBean getMcBean(int nid) {
return data.get(nid);
}
}
---McBean.java
public class McBean {
private int id;//商品編號
private String name;//商品名
private double price;//商品價格
private String instuction;//商品說明
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getInstuction() {
return instuction;
}
public void setInstuction(String instuction) {
this.instuction = instuction;
}
}
---ShoppingCar.java
public class ShoppingCar {
private double totalPrice; // 購物車所有商品總價格
private int totalCount; // 購物車所有商品數(shù)量
private MapInteger, OrderItemBean itemMap; // 商品編號與訂單項的鍵值對
public ShoppingCar() {
itemMap = new HashMapInteger, OrderItemBean();
}
public void buy(int nid) {
OrderItemBean order = itemMap.get(nid);
McBean mb;
if (order == null) {
mb = new Database().getMcBean(nid);
order = new OrderItemBean(mb, 1);
itemMap.put(nid, order);
update(nid, 1);
} else {
order.setCount(order.getCount() + 1);
update(nid, 1);
}
}
public void delete(int nid) {
OrderItemBean delorder = itemMap.remove(nid);
totalCount = totalCount - delorder.getCount();
totalPrice = totalPrice - delorder.getThing().getPrice() * delorder.getCount();
}
public void update(int nid, int count) {
OrderItemBean updorder = itemMap.get(nid);
totalCount = totalCount + count;
totalPrice = totalPrice + updorder.getThing().getPrice() * count;
}
public void clear() {
itemMap.clear();
totalCount = 0;
totalPrice = 0.0;
}
public void show() {
DecimalFormat df = new DecimalFormat("¤#.##");
System.out.println("商品編號\t商品名稱\t單價\t購買數(shù)量\t總價");
Set set = itemMap.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
OrderItemBean order = itemMap.get(it.next());
System.out.println(order.getThing().getId() + "\t"
+ order.getThing().getName() + "\t"
+ df.format(order.getThing().getPrice()) + "\t" + order.getCount()
+ "\t" + df.format(order.getCount() * order.getThing().getPrice()));
}
System.out.println("合計: 總數(shù)量: " + df.format(totalCount) + " 總價格: " + df.format(totalPrice));
System.out.println("**********************************************");
}
}
---OrderItemBean.java
public class OrderItemBean {
private McBean thing;//商品的實體
private int count;//商品的數(shù)量
public OrderItemBean(McBean thing, int count) {
super();
this.thing = thing;
this.count = count;
}
public McBean getThing() {
return thing;
}
public void setThing(McBean thing) {
this.thing = thing;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
---TestShoppingCar.java
package com.shop;
public class TestShoppingCar {
public static void main(String[] args) {
ShoppingCar s = new ShoppingCar();
s.buy(1);//購買商品編號1的商品
s.buy(1);
s.buy(2);
s.buy(3);
s.buy(1);
s.show();//顯示購物車的信息
s.delete(1);//刪除商品編號為1的商品
s.show();
s.clear();
s.show();
}
}
3.打印輸出結果
商品編號 商品名稱 單價 購買數(shù)量 總價
1 地瓜 ¥2 3 ¥6
2 土豆 ¥1.2 1 ¥1.2
3 絲瓜 ¥1.5 1 ¥1.5
合計: 總數(shù)量: ¥5 總價格: ¥8.7
**********************************************
商品編號 商品名稱 單價 購買數(shù)量 總價
2 土豆 ¥1.2 1 ¥1.2
3 絲瓜 ¥1.5 1 ¥1.5
合計: 總數(shù)量: ¥2 總價格: ¥2.7
**********************************************
商品編號 商品名稱 單價 購買數(shù)量 總價
合計: 總數(shù)量: ¥0 總價格: ¥0
**********************************************
4.打字太累了,比較匆忙,但是主要靠你自己領會。哪里不清楚再提出來。