結(jié)算聯(lián)系到好幾個(gè)文件哦。上上個(gè)星期我們剛把網(wǎng)上購物車系統(tǒng)做完。下面是Servlet里的一段,希望對你有用!
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、永新ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的永新網(wǎng)站制作公司
else if (command.equalsIgnoreCase("modifybookcount")) {
int bookCartID = (Integer) session.getAttribute("bookcartID");
int operBookID = Integer.valueOf((String) request
.getParameter("operBookID"));
// 獲得購物車中原有數(shù)量
int bookCount = Integer.valueOf((String) request
.getParameter("bookCount"));
// 獲得需要會員修改的數(shù)量
int operBookCount = Integer.valueOf((String) request
.getParameter("operBookCount"));
// 得到圖書數(shù)量增量
int bookCountIncrement = operBookCount - bookCount;
// 得到圖書價(jià)格的增量
double bookPriceIncrement = bookCountIncrement
* Double.valueOf((String) request.getParameter("disPrice"));
CartListDAO cartlist = new CartListDAO();
// 更新購物車中圖書的總數(shù)量和總價(jià)格
cartlist
.updateBookCount(bookCartID, operBookID, bookCountIncrement);
BookCartDAO bookcart = (BookCartDAO) session
.getAttribute("bookcart");
bookcart.updateCartData(bookCartID, bookPriceIncrement,
bookCountIncrement);
targetPage = "/cartlist.jsp";
RequestDispatcher dispatcher = request
.getRequestDispatcher(targetPage);
dispatcher.forward(request, response);
}
import java.awt.*;
import java.awt.event.*;
class ShopFrame extends Frame implements ActionListener
{ Label label1,label2,label3,label4;
Button button1,button2,button3,button4,button5;
TextArea text;
Panel panel1,panel2;
static float sum=0.0f;
ShopFrame(String s)
{ super(s);
setLayout(new BorderLayout());
label1=new Label("面紙:3元",Label.LEFT);
label2=new Label("鋼筆:5元",Label.LEFT);
label3=new Label("書:10元",Label.LEFT);
label4=new Label("襪子:8元",Label.LEFT);
button1=new Button("加入購物車");
button2=new Button("加入購物車");
button3=new Button("加入購物車");
button4=new Button("加入購物車");
button5=new Button("查看購物車");
text=new TextArea("商品有:"+"\n",5,10);
text.setEditable(false);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
panel1=new Panel();
panel2=new Panel();
panel1.add(label1);
panel1.add(button1);
panel1.add(label2);
panel1.add(button2);
panel1.add(label3);
panel1.add(button3);
panel1.add(label4);
panel1.add(button4);
panel2.setLayout(new BorderLayout());
panel2.add(button5,BorderLayout.NORTH);
panel2.add(text,BorderLayout.SOUTH);
this.add(panel1,BorderLayout.CENTER);
this.add(panel2,BorderLayout.SOUTH);
setBounds(100,100,350,250);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button1)
{ text.append("一個(gè)面紙、");
sum=sum+3;
}
else if(e.getSource()==button2)
{ text.append("一只鋼筆、");
sum=sum+5;
}
else if(e.getSource()==button3)
{ text.append("一本書、");
sum=sum+10;
}
else if(e.getSource()==button4)
{ text.append("一雙襪子、");
sum=sum+8;
}
else if(e.getSource()==button5)
{
text.append("\n"+"總價(jià)為:"+"\n"+sum);
}
}
}
public class Shopping {
public static void main(String[] args) {
new ShopFrame("購物車");
}
}
我沒用Swing可能顯示不出來你的效果。不滿意得話我在給你編一個(gè)。
要使用Java編寫超市購物程序,您需要使用Java數(shù)據(jù)庫編程技術(shù)來連接到您的數(shù)據(jù)庫并執(zhí)行相關(guān)的數(shù)據(jù)庫操作。在Java中,您可以使用JDBC(Java數(shù)據(jù)庫連接)API來連接到數(shù)據(jù)庫并執(zhí)行SQL語句。例如,您可以使用JDBC API來執(zhí)行以下操作:
創(chuàng)建數(shù)據(jù)庫連接
執(zhí)行增刪改查(CRUD)操作
提交或回滾事務(wù)
執(zhí)行存儲過程和函數(shù)
查詢數(shù)據(jù)庫元數(shù)據(jù)
此外,您還可以使用Java的面向?qū)ο缶幊碳夹g(shù)來封裝數(shù)據(jù)庫操作,以便更方便地在您的程序中使用。例如,您可以創(chuàng)建一個(gè)類來表示超市商品,并定義一些方法來執(zhí)行商品的增刪改查操作。這樣,您就可以在程序中通過調(diào)用這些方法來簡單地完成對數(shù)據(jù)庫的操作,而不需要編寫復(fù)雜的SQL語句。
/**
*?@description:?
*?@author?chenshiqiang?E-mail:csqwyyx@163.com
*?@date?2014年9月7日?下午2:51:50???
*?@version?1.0???
*/
package?com.example.baidumap;
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.HashSet;
import?java.util.List;
import?android.app.Activity;
import?android.os.Bundle;
import?android.support.v4.view.PagerAdapter;
import?android.support.v4.view.PagerTabStrip;
import?android.support.v4.view.ViewPager;
import?android.text.Editable;
import?android.util.Log;
import?android.view.LayoutInflater;
import?android.view.View;
import?android.view.ViewGroup;
import?android.widget.ExpandableListView;
import?android.widget.ListView;
import?com.baidu.mapapi.map.offline.MKOLSearchRecord;
import?com.baidu.mapapi.map.offline.MKOLUpdateElement;
import?com.baidu.mapapi.map.offline.MKOfflineMap;
import?com.baidu.mapapi.map.offline.MKOfflineMapListener;
import?com.example.baidumap.adapters.OfflineExpandableListAdapter;
import?com.example.baidumap.adapters.OfflineMapAdapter;
import?com.example.baidumap.adapters.OfflineMapManagerAdapter;
import?com.example.baidumap.interfaces.OnOfflineItemStatusChangeListener;
import?com.example.baidumap.models.OfflineMapItem;
import?com.example.baidumap.utils.CsqBackgroundTask;
import?com.example.baidumap.utils.ToastUtil;
import?com.example.system.R;
public?class?BaiduOfflineMapActivity?extends?Activity?implements?MKOfflineMapListener,?OnOfflineItemStatusChangeListener
{
//?------------------------?Constants?------------------------
//?-------------------------?Fields?--------------------------
private?ViewPager?viewpager;
private?PagerTabStrip?pagertab;
private?MySearchView?svDown;
private?ListView?lvDown;
private?MySearchView?svAll;
private?ExpandableListView?lvWholeCountry;
private?ListView?lvSearchResult;
private?ListView?views?=?new?ArrayListView(2);
private?ListString?titles?=?new?ArrayListString(2);
private?MKOfflineMap?mOffline?=?null;
private?OfflineMapManagerAdapter?downAdapter;
private?OfflineMapAdapter?allSearchAdapter;
private?OfflineExpandableListAdapter?allCountryAdapter;
private?ListOfflineMapItem?itemsDown;?//?下載或下載中城市
private?ListOfflineMapItem?itemsAll;?//?所有城市,與熱門城市及下載管理對象相同
private?ListOfflineMapItem?itemsProvince;
private?ListListOfflineMapItem?itemsProvinceCity;
//?-----------------------?Constructors?----------------------
//?--------?Methods?for/from?SuperClass/Interfaces?-----------
@Override
protected?void?onCreate(Bundle?savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_offline_map);
// final?String?packname?=?this.getPackageName();
// PackageInfo?packageInfo;
// try
// {
// packageInfo?=?this.getPackageManager().getPackageInfo(packname,?PackageManager.GET_SIGNATURES);
//
//
// if?(code?==?-00)
// {
//?初始化離線地圖管理
mOffline?=?new?MKOfflineMap();
mOffline.init(this);
initViews();
viewpager.setCurrentItem(1);
// }
// }
// catch?(NameNotFoundException?e)
// {
// e.printStackTrace();
// }
}
private?boolean?isResumed?=?false;
@Override
protected?void?onResume()
{
super.onResume();
if?(!isResumed)
{
isResumed?=?true;
loadData();
}
}
@Override
protected?void?onDestroy()
{
super.onDestroy();
mOffline.destroy();
}
/**
?*?
?*?@author?chenshiqiang?E-mail:csqwyyx@163.com
?*?@param?type
?*????????????事件類型:?MKOfflineMap.TYPE_NEW_OFFLINE,?MKOfflineMap.TYPE_DOWNLOAD_UPDATE,?MKOfflineMap.TYPE_VER_UPDATE.
?*?@param?state
?*????????????事件狀態(tài):?當(dāng)type為TYPE_NEW_OFFLINE時(shí),表示新安裝的離線地圖數(shù)目.?當(dāng)type為TYPE_DOWNLOAD_UPDATE時(shí),表示更新的城市ID.
?*/
@Override
public?void?onGetOfflineMapState(int?type,?int?state)
{
switch?(type)
{
case?MKOfflineMap.TYPE_DOWNLOAD_UPDATE:
MKOLUpdateElement?update?=?mOffline.getUpdateInfo(state);
if?(setElement(update,?true)?!=?null)
{
if?(itemsDown?!=?null??itemsDown.size()??1)
{
Collections.sort(itemsDown);
}
refreshDownList();
}
else
{
downAdapter.notifyDataSetChanged();
}
allSearchAdapter.notifyDataSetChanged();
allCountryAdapter.notifyDataSetChanged();
break;
case?MKOfflineMap.TYPE_NEW_OFFLINE:
//?有新離線地圖安裝
Log.d("OfflineDemo",?String.format("add?offlinemap?num:%d",?state));
break;
case?MKOfflineMap.TYPE_VER_UPDATE:
//?版本更新提示
break;
}
}
/**
?*?百度下載狀態(tài)改變(暫停--》恢復(fù))居然不回調(diào),所以改變狀態(tài)時(shí)自己得增加接口監(jiān)聽狀態(tài)改變刷新界面
?*?
?*?@author?chenshiqiang?E-mail:csqwyyx@163.com
?*?@param?item
?*????????????有狀態(tài)改變的item
?*?@param?removed
?*????????????item是否被刪除
?*/
@Override
public?void?statusChanged(OfflineMapItem?item,?boolean?removed)
{
if?(removed)
{
for?(int?i?=?itemsDown.size()?-?1;?i?=?0;?i--)
{
OfflineMapItem?temp?=?itemsDown.get(i);
if?(temp.getCityId()?==?item.getCityId())
{
itemsDown.remove(i);
}
}
refreshDownList();
}
else
{
loadData();
downAdapter.notifyDataSetChanged();
}
allSearchAdapter.notifyDataSetChanged();
allCountryAdapter.notifyDataSetChanged();
}
//?---------------------?Methods?public?----------------------
public?void?toDownloadPage()
{
viewpager.setCurrentItem(0);
}
//?---------------------?Methods?private?---------------------
private?void?initViews()
{
//?TODO
viewpager?=?(ViewPager)?findViewById(R.id.viewpager);
pagertab?=?(PagerTabStrip)?findViewById(R.id.pagertab);
LayoutInflater?inf?=?LayoutInflater.from(this);
View?v1?=?inf.inflate(R.layout.view_offline_download,?null,?false);
svDown?=?(MySearchView)?v1.findViewById(R.id.svDown);
lvDown?=?(ListView)?v1.findViewById(R.id.lvDown);
views.add(v1);
View?v2?=?inf.inflate(R.layout.view_offline_countrys,?null,?false);
svAll?=?(MySearchView)?v2.findViewById(R.id.svAll);
lvWholeCountry?=?(ExpandableListView)?v2.findViewById(R.id.lvWholeCountry);
lvSearchResult?=?(ListView)?v2.findViewById(R.id.lvSearchResult);
views.add(v2);
titles.add("下載管理");
titles.add("城市列表");
pagertab.setTabIndicatorColor(0xff00cccc);
pagertab.setDrawFullUnderline(false);
pagertab.setBackgroundColor(0xFF38B0DE);
pagertab.setTextSpacing(50);
viewpager.setOffscreenPageLimit(2);
viewpager.setAdapter(new?MyPagerAdapter());
svDown.setSearchListener(new?MySearchView.SearchListener()
{
@Override
public?void?afterTextChanged(Editable?text)
{
refreshDownList();
}
@Override
public?void?search(String?text)
{
}
});
svAll.setSearchListener(new?MySearchView.SearchListener()
{
@Override
public?void?afterTextChanged(Editable?text)
{
refreshAllSearchList();
}
@Override
public?void?search(String?text)
{
}
});
downAdapter?=?new?OfflineMapManagerAdapter(this,?mOffline,?this);
lvDown.setAdapter(downAdapter);
allSearchAdapter?=?new?OfflineMapAdapter(this,?mOffline,?this);
lvSearchResult.setAdapter(allSearchAdapter);
allCountryAdapter?=?new?OfflineExpandableListAdapter(this,?mOffline,?this);
lvWholeCountry.setAdapter(allCountryAdapter);
lvWholeCountry.setGroupIndicator(null);
}
/**
?*?刷新下載列表,?根據(jù)搜索關(guān)鍵字及itemsDown?下載管理數(shù)量變動時(shí)調(diào)用
?*/
private?void?refreshDownList()
{
String?key?=?svDown.getInputText();
if?(key?==?null?||?key.length()??1)
{
downAdapter.setDatas(itemsDown);
}
else
{
ListOfflineMapItem?filterList?=?new?ArrayListOfflineMapItem();
if?(itemsDown?!=?null??!itemsDown.isEmpty())
{
for?(OfflineMapItem?i?:?itemsDown)
{
if?(i.getCityName().contains(key))
{
filterList.add(i);
}
}
}
downAdapter.setDatas(filterList);
}
}
/**
?*?刷新所有城市搜索結(jié)果
?*/
private?void?refreshAllSearchList()
{
String?key?=?svAll.getInputText();
if?(key?==?null?||?key.length()??1)
{
lvSearchResult.setVisibility(View.GONE);
lvWholeCountry.setVisibility(View.VISIBLE);
allSearchAdapter.setDatas(null);
}
else
{
lvSearchResult.setVisibility(View.VISIBLE);
lvWholeCountry.setVisibility(View.GONE);
ListOfflineMapItem?filterList?=?new?ArrayListOfflineMapItem();
if?(itemsAll?!=?null??!itemsAll.isEmpty())
{
for?(OfflineMapItem?i?:?itemsAll)
{
if?(i.getCityName().contains(key))
{
filterList.add(i);
}
}
}
allSearchAdapter.setDatas(filterList);
}
}
private?void?loadData()
{
new?CsqBackgroundTaskVoid(this)
{
@Override
protected?Void?onRun()
{
//?TODO?Auto-generated?method?stub
//?導(dǎo)入離線地圖包
//?將從官網(wǎng)下載的離線包解壓,把vmp文件夾拷入SD卡根目錄下的BaiduMapSdk文件夾內(nèi)。
//?把網(wǎng)站上下載的文件解壓,將\BaiduMap\vmp\l里面的.dat_svc文件,拷貝到手機(jī)BaiduMapSDK/vmp/h目錄下
int?num?=?mOffline.importOfflineData();
if?(num??0)
{
ToastUtil.showToastInfo(BaiduOfflineMapActivity.this,?"成功導(dǎo)入"?+?num?+?"個(gè)離線包",?false);
}
ListMKOLSearchRecord?all?=?null;
try
{
all?=?mOffline.getOfflineCityList();
}
catch?(Exception?e)
{
e.printStackTrace();
}
if?(all?==?null?||?all.isEmpty())
{
ToastUtil.showToastInfo(BaiduOfflineMapActivity.this,?"未獲取到離線地圖城市數(shù)據(jù),可能有其他應(yīng)用正在使用百度離線地圖功能!",?false);
return?null;
}
ListMKOLSearchRecord?hotCity?=?mOffline.getHotCityList();
HashSetInteger?hotCityIds?=?new?HashSetInteger();
if?(!hotCity.isEmpty())
{
for?(MKOLSearchRecord?r?:?hotCity)
{
hotCityIds.add(r.cityID);
}
}
itemsAll?=?new?ArrayListOfflineMapItem();
itemsDown?=?new?ArrayListOfflineMapItem();
itemsProvince?=?new?ArrayListOfflineMapItem();
itemsProvinceCity?=?new?ArrayListListOfflineMapItem();
//?cityType?0:全國;1:省份;2:城市,如果是省份,可以通過childCities得到子城市列表
//?全國概略圖、直轄市、港澳?子城市列表
ArrayListMKOLSearchRecord?childMunicipalities?=?new?ArrayListMKOLSearchRecord();
proHot.cityName?=?"熱門城市";
proHot.childCities?=?cs;
ListMKOLUpdateElement?updates?=?mOffline.getAllUpdateInfo();
if?(updates?!=?null??updates.size()??0)
{
}
@Override
protected?void?onResult(Void?result)
{
//?TODO?Auto-generated?method?stub
refreshDownList();
refreshAllSearchList();
allCountryAdapter.setDatas(itemsProvince,?itemsProvinceCity);
}
}.execute();
}
有兩個(gè)地方錯(cuò)了:
主要的一個(gè)錯(cuò)誤是BookDetail類不存在,而你在ShopCar
類中引用了BookDetail,得寫一個(gè)這樣的類才行。
第二個(gè)錯(cuò)誤的地方為你在類中使用了包,如果使用包的話就不能像一般的類那樣直接使用javac 類名.java這樣的形式編譯了。
而要使用 javac -d . 類名.java才行,其中的-d .的意思是把輸出的.class文件放以包名稱的形式到當(dāng)前文件夾下,當(dāng)然你也可以輸入其它路徑也是可以的。
運(yùn)行的話使用java 包名+類名就可以了,其中是以.分開包和類名稱的。
看一下javac的幫助和java的幫助,里面說的很明白的。
編譯的時(shí)候使用javac -d . 類名.java
運(yùn)行的時(shí)候 java 包名.類名
注意如果包里又有子包的話運(yùn)行的時(shí)候包與子包使用.分隔開來
package?Test;
import?java.util.LinkedHashMap;
import?java.util.Map;
import?java.util.Map.Entry;
import?java.util.Scanner;
public?class?Test?{
public?static?void?main(String[]?args)?{
init();//初始化
MapString,String?map?=?new?LinkedHashMap();
while(true){
Scanner?in=?new?Scanner(System.in);
map?=?buy(in,map);//選擇
System.out.println();
System.out.println("還要繼續(xù)購物嗎?(Y/N)");
String?jx?=?in.nextLine();
if(jx.equals("N")){
break;
}
}
print(map);
}
public?static?void?print(MapString,?String?m){
System.out.println("\n\n\n******************");
System.out.println("???????購物車清單");
Integer?hj?=?0;
for?(EntryString,?String?entry?:?m.entrySet())?{
String?key?=?entry.getKey();
String?value?=?entry.getValue();
if(key.equals("1")){
hj?+=?Integer.parseInt(value)*3;
System.out.println("哇哈哈純凈水:?"+value+"件,合計(jì):¥"+Integer.parseInt(value)*3);
}else?if(key.equals("2")){
hj?+=?Integer.parseInt(value)*5;
System.out.println("康師傅方便面:?"+value+"件,合計(jì):¥"+Integer.parseInt(value)*5);
}else?if(key.equals("3")){
hj?+=?Integer.parseInt(value)*4;
System.out.println("可口可樂:?"+value+"件,合計(jì):¥"+Integer.parseInt(value)*4);
}
}
System.out.println("合計(jì)金額:¥"+hj);
}
public?static?void?init(){
System.out.println("******************");
System.out.println("\t商品列表\n");
System.out.println("??????????????商品名稱????????????????價(jià)格");
System.out.println("1.???哇哈哈純凈水????????¥3");
System.out.println("2.???康師傅方便面????????¥5");
System.out.println("3.???可口可樂????????????????¥4");
System.out.println("******************");
}
public?static?MapString,String?buy(Scanner?scan,MapString,String?m){
System.out.print("請輸入編號:");
String?bh?=?scan.nextLine();
System.out.print("請輸入購買數(shù)量:");
String?num?=?scan.nextLine();
if(m.size()0??m.keySet().contains(bh)){
m.put(bh,(Integer.parseInt(bh)+Integer.parseInt(num))+"");
}else{
m.put(bh,?num);
}
return?m;
}
}