java超市购物成功代码,java超市购物程序代码

java简单的购物车代码

package Test;

创新互联公司是专业的枞阳网站建设公司,枞阳接单;提供做网站、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行枞阳网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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("还要继续购物吗?(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+"件,合计:¥"+Integer.parseInt(value)*3);

}else if(key.equals("2")){

hj += Integer.parseInt(value)*5;

System.out.println("康师傅方便面: "+value+"件,合计:¥"+Integer.parseInt(value)*5);

}else if(key.equals("3")){

hj += Integer.parseInt(value)*4;

System.out.println("可口可乐: "+value+"件,合计:¥"+Integer.parseInt(value)*4);

}

}

System.out.println("合计金额:¥"+hj);

}

public static void init(){

System.out.println("******************");

System.out.println("\t商品列表\n");

System.out.println("              商品名称                价格");

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("请输入购买数量:");

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;

}

}

用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;} } }} }

用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;

}

}

}

Java初学者,哪位友友能帮我设计一个简单的类似超市购物车的程序,参考一下~谢谢!

以前学习java又做个实例,挺值得学习的。

1.首先我先列出我们所需要的java类结构。

1)Database.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 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; // 购物车所有商品数量

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购买数量\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.打字太累了,比较匆忙,但是主要靠你自己领会。哪里不清楚再提出来。

名称栏目:java超市购物成功代码,java超市购物程序代码
分享地址:https://www.cdcxhl.com/article14/dscepge.html

成都网站建设公司_创新互联,为您提供动态网站品牌网站建设网站维护品牌网站制作用户体验电子商务

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

商城网站建设