1. 購物車的Java代碼
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;public class ShoppingCartManager {
HashMap<String, String> hm=new HashMap<String, String>();
float totlePrice=0;
//添加book到購物車
public void addBook(String bookId,String bookQuantity){
if(hm.containsKey(bookId)){
int value=Integer.parseInt(hm.get(bookId));
value+=Integer.parseInt(bookQuantity);
hm.put(bookId, value+"");
}else{
hm.put(bookId, bookQuantity);
}
}
//修改數量
public void updateQuantity(String bookId,String bookQuantity){
hm.put(bookId, bookQuantity);
}
//獲取購物車的所有信息 並計算總價
public ArrayList<BookBean> getShoppingCart(){
ArrayList<BookBean> al=new ArrayList<BookBean>();
Iterator<String> i=hm.keySet().iterator();
String ids="";
BookTableManager btm=new BookTableManager();
while(i.hasNext()){
ids=ids+","+i.next();
}
al= btm.selectByBookIds(ids);
totlePrice=0; //清空總價,防止無限累計
for(int j=0;j<al.size();j++){
BookBean bb=al.get(j);
totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+""));
}
return al;
}
//獲取總價
public float getTotlePrice(){
return totlePrice;
}
//根據ID獲取數量
public String getQuantityById(String id){
String quantity=hm.get(id);
return quantity;
}
//清空購物車
public void clear(){
hm.clear();
}
//刪除購物車中的一本書
public void deleteById(String id){
hm.remove(id);
}
}
2. JAVA 購物車示例代碼
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("一個面紙、");
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"+"總價為:"+"\n"+sum);
}
}
}
public class Shopping {
public static void main(String[] args) {
new ShopFrame("購物車");
}
}
我沒用Swing可能顯示不出來你的效果。不滿意得話我在給你編一個。
3. 求一個Java語言的超市收銀系統源代碼
剛好我們做了一個,一個是本地pos,一個是遠程pos,遠程用oracle資料庫,本地用mysql資料庫。可以斷網用也可以連網用,別35金幣了,十萬就行了。
4. 用java編程實現網上超市購物結算功能代碼
哎,我還是自己代碼黏貼給你吧!免得總通不過!裝不下這么多字,詳情自己找我。 連接資料庫類(添加修改會員信息) package com.to.java; import java.util.Scanner; import java.sql.*; public class Vds { Scanner Input=new Scanner(System.in); ZhuCK1 zhuck1=new ZhuCK1(); public void x(){ try{ Connection con=null; Statement stmt=null; ResultSet rs=null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String ur1="jdbc:odbc:gg"; con=DriverManager.getConnection(ur1); stmt=con.createStatement(); String sqlstmt="select id,mm from gg"; rs=stmt.executeQuery(sqlstmt); System.out.println("----------查詢結果---------"); System.out.println(); System.out.println("會員號--密碼"); while(rs.next()){ String id=rs.getString("id"); String mm=rs.getString("mm"); System.out.println(id+" "+mm); } con.close(); stmt.close(); }catch(Exception e){ System.out.println(e);} System.out.println("是否返回上級菜單"); System.out.println("1.是"); System.out.println("2.否"); int m=Input.nextInt(); switch(m){ case 1: zhuck1.ZhucK1(); break; case 2: { System.out.println("歡迎下次再次使用");System.exit(-1);}; break; }} public void j(){ try{ Connection con=null; Statement stmt=null; ResultSet rs=null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String ur1="jdbc:odbc:gg"; con=DriverManager.getConnection(ur1); stmt=con.createStatement(); String sqlstmt="select id,mm from gg"; rs=stmt.executeQuery(sqlstmt); while(rs.next()){ String id=rs.getString("id"); String mm=rs.getString("mm"); System.out.println(id+" "+mm); } Scanner input=new Scanner(System.in); System.out.println("請輸入會員信息:"); System.out.println("請輸入會員號:"); String f=input.next(); System.out.println("請輸入密碼:"); String q=input.next(); String sqlst="insert into gg values('"+f+"','"+q+"')"; Statement tatement=con.createStatement(); int result=tatement.executeUpdate(sqlst); System.out.println("操作成功!"); con.close(); stmt.close(); }catch(Exception e){ System.out.println(e); } System.out.println("是否返回上級菜單"); System.out.println("1.是"); System.out.println("2.否"); int m=Input.nextInt(); switch(m){ case 1: zhuck1.ZhucK1(); break; case 2: { System.out.println("歡迎下次再次使用");System.exit(-1);}; break; } } public void xg(){ try{ Connection con=null; Statement stmt=null; ResultSet rs=null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String ur1="jdbc:odbc:gg"; con=DriverManager.getConnection(ur1); stmt=con.createStatement(); String sqlstmt="select id,mm from gg"; rs=stmt.executeQuery(sqlstmt); while(rs.next()){ String id=rs.getString("id"); String mm=rs.getString("mm"); System.out.println(id+" "+mm); } Scanner input=new Scanner(System.in); System.out.println("請輸入會員信息:"); System.out.println("請輸入要修改的會員號:"); String f=input.next(); System.out.println("請輸入修改的密碼:"); String q=input.next(); String sqlst="update gg set mm='"+q+"' where id='"+f+"'"; Statement tatement=con.createStatement(); int result=tatement.executeUpdate(sqlst); System.out.println("操作成功!"); con.close(); stmt.close(); }catch(Exception e){ System.out.println(e); } System.out.println("是否返回上級菜單"); System.out.println("1.是"); System.out.println("2.否"); int m=Input.nextInt(); switch(m){ case 1: zhuck1.ZhucK1(); break; case 2: { System.out.println("歡迎下次再次使用");System.exit(-1);}; break; } } } package com.to.java; import java.util.Scanner; public class KeHu { Vds sj=new Vds(); public void kehu1(){ Scanner Input=new Scanner(System.in); ZhuCK1 zhuck1=new ZhuCK1(); System.out.println("******************************************************"+"\n\n"); System.out.println("1.顯示所有客戶信息"); System.out.println("2.添加客戶信息"); System.out.println("3.修改客戶信息"); System.out.println("4.查詢客戶信息"); System.out.println("5.返回上級菜單"); System.out.println("******************************************************"+"\n\n"); System.out.println("請輸入數字:"); int a=Input.nextInt(); switch(a){ case 1: sj.x(); break; case 2: sj.j(); break; case 3: sj.xg(); break; case 4: sj.x();break; case 5: zhuck1.ZhucK1();break; } } } package com.to.java; public class GouWuXiTong { public static void main(String[] args){ ZhuCK zhuck=new ZhuCK(); zhuck.ZhucK(); } } package com.to.java; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; public class ZhuCK { Scanner Input=new Scanner(System.in); public void ZhucK(){ ZhuCK1 zhuck1=new ZhuCK1(); System.out.println("\t\t"+"歡迎使用我心我素購物系統"); System.out.println("******************************************************"+"\n\n"); System.out.println("\t\t"+"1.登陸系統"); System.out.println(); System.out.println("\t\t"+"2.退出"+"\n\n"); System.out.println("******************************************************"); int d=1; while(d!=2){ System.out.println("請輸入會員號:"); String p=Input.next(); System.out.println("請輸入密碼:"); String f=Input.next(); try{ Connection con=null; Statement stmt=null; ResultSet rs=null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String ur1="jdbc:odbc:gg"; con=DriverManager.getConnection(ur1); stmt=con.createStatement(); String sqlstmt="select * from gg"; rs=stmt.executeQuery(sqlstmt); while(rs.next()){ String id=rs.getString("id"); String mm=rs.getString("mm"); if(id.equals(p)&&mm.equals(f)){ { System.out.println("請輸入數字:"); int a=Input.nextInt(); switch(a){ case 1: zhuck1.ZhucK1(); break; case 2: break; }d=2; } } } }catch(Exception e){ System.out.println(e);} {System.out.println("輸入錯誤請再次輸入:");d=1;} } }} }
5. java簡單的購物車代碼
packageTest;
importjava.util.LinkedHashMap;
importjava.util.Map;
importjava.util.Map.Entry;
importjava.util.Scanner;
publicclassTest{
publicstaticvoidmain(String[]args){
init();//初始化
Map<String,String>map=newLinkedHashMap<>();
while(true){
Scannerin=newScanner(System.in);
map=buy(in,map);//選擇
System.out.println();
System.out.println("還要繼續購物嗎?(Y/N)");
Stringjx=in.nextLine();
if(jx.equals("N")){
break;
}
}
print(map);
}
publicstaticvoidprint(Map<String,String>m){
System.out.println("
******************");
System.out.println("購物車清單");
Integerhj=0;
for(Entry<String,String>entry:m.entrySet()){
Stringkey=entry.getKey();
Stringvalue=entry.getValue();
if(key.equals("1")){
hj+=Integer.parseInt(value)*3;
System.out.println("哇哈哈純凈水:"+value+"件,合計:¥"+Integer.parseInt(value)*3);
}elseif(key.equals("2")){
hj+=Integer.parseInt(value)*5;
System.out.println("康師傅方便麵:"+value+"件,合計:¥"+Integer.parseInt(value)*5);
}elseif(key.equals("3")){
hj+=Integer.parseInt(value)*4;
System.out.println("可口可樂:"+value+"件,合計:¥"+Integer.parseInt(value)*4);
}
}
System.out.println("合計金額:¥"+hj);
}
publicstaticvoidinit(){
System.out.println("******************");
System.out.println(" 商品列表
");
System.out.println("商品名稱價格");
System.out.println("1.哇哈哈純凈水¥3");
System.out.println("2.康師傅方便麵¥5");
System.out.println("3.可口可樂¥4");
System.out.println("******************");
}
publicstaticMap<String,String>buy(Scannerscan,Map<String,String>m){
System.out.print("請輸入編號:");
Stringbh=scan.nextLine();
System.out.print("請輸入購買數量:");
Stringnum=scan.nextLine();
if(m.size()>0&&m.keySet().contains(bh)){
m.put(bh,(Integer.parseInt(bh)+Integer.parseInt(num))+"");
}else{
m.put(bh,num);
}
returnm;
}
}
6. Java初學者,哪位友友能幫我設計一個簡單的類似超市購物車的程序,參考一下~謝謝!
以前學習java又做個實例,挺值得學習的。
1.首先我先列出我們所需要的java類結構。
1).java --------- 模擬存儲商品的資料庫。
2)McBean.java ------------ 商品實體類,一個普通的javabean,裡面有商品的基本屬性。
3)OrderItemBean.java --- 訂單表。
4)ShoppingCar.java ------ 這個就是最主要的購物車,當然比較簡單。
5)TestShoppingCar.java --- 這個是測試類。
2.下面貼出具體代碼並帶關鍵注釋。
---Database.java
public class Database {
/*採用Map存儲商品數據,為什麼呢?我覺得這個問題你自己需要想下。
* Integer 為Map的key值,McBean為Map的value值。
*/
private static Map<Integer, McBean> data = new HashMap<Integer, 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; // 購物車所有商品數量
private Map<Integer, OrderItemBean> itemMap; // 商品編號與訂單項的鍵值對
public ShoppingCar() {
itemMap = new HashMap<Integer, 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購買數量\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("合計: 總數量: " + df.format(totalCount) + " 總價格: " + df.format(totalPrice));
System.out.println("**********************************************");
}
}
---OrderItemBean.java
public class OrderItemBean {
private McBean thing;//商品的實體
private int count;//商品的數量
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.列印輸出結果
商品編號 商品名稱 單價 購買數量 總價
1 地瓜 ¥2 3 ¥6
2 土豆 ¥1.2 1 ¥1.2
3 絲瓜 ¥1.5 1 ¥1.5
合計: 總數量: ¥5 總價格: ¥8.7
**********************************************
商品編號 商品名稱 單價 購買數量 總價
2 土豆 ¥1.2 1 ¥1.2
3 絲瓜 ¥1.5 1 ¥1.5
合計: 總數量: ¥2 總價格: ¥2.7
**********************************************
商品編號 商品名稱 單價 購買數量 總價
合計: 總數量: ¥0 總價格: ¥0
**********************************************
4.打字太累了,比較匆忙,但是主要靠你自己領會。哪裡不清楚再提出來。
7. 用JAVA實現超市購物功能,購物時如果購物者所需商品在超市中有則提示購物者買
首先來這個需求很奇怪。
購物者買的東源西 應該在超市裡選的才對。
那麼意思就是 萬一選的東西 沒庫存了怎麼辦?
2種解決方法:
1:每次購買前或者選擇的時候,都去後台查詢庫存,然後提示,這種效率低。但也行就是了
2:在進入這個商品詳情頁時候查詢本商品庫存,為0 那就提示沒庫存了。
8. 用JAVA編寫購物系統的代碼是什麼(急)
算是最簡單的吧
package cn.job01;
import java.util.Scanner;
public class Lx07 {
public static void choice() {
System.out.println("登陸菜單 ");
System.out.println("1登陸系統");
System.out.println("2退出");
}
static void choice1() {
System.out.println("購物管理系統>客戶信息");
System.out.println("1顯示所有客戶信息");
System.out.println("2添加客戶信息");
System.out.println("3修改客戶信息");
System.out.println("4查詢客戶信息");
}
static void choice2() {
System.out.println("購物管理系統>真情回饋");
System.out.println("1幸運大放送");
System.out.println("2幸運抽獎");
System.out.println("3生日問候");
}
public static void main(String[] args) {
choice();
Scanner input = new Scanner(System.in);
System.out.println("請輸入1or2");
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("主菜單");
System.out.println("1客戶信息管理");
System.out.println("2購物結算");
System.out.println("3真情回饋");
System.out.println("4注銷");
break;
}
System.out.println("選擇輸入數字");
int num1 = input.nextInt();
switch (num1) {
case 1:
choice1();
break;
case 2:
System.out.println("購物結算");
break;
case 3:
choice2();
break;
case 4:
choice();
break;
}
}
}
9. 用java編一個超市銷售界面的代碼咋樣寫啊 !急 謝謝各位啦
原先我寫了個,你看看 可以直接運行,不過要先運行服務端,再運行客戶端 至於就這簡單,具體的代碼你不懂再發消息我 *服務端* import java.awt.*;
10. 你好,java購物車代碼
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("一個面紙、");
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"+"總價為:"+"\n"+sum);
}
}
}
public class Shopping {
public static void main(String[] args) {
new ShopFrame("購物車");
}
}
我沒用Swing可能顯示不出來你的效果。不滿意得話我在給你編一個。