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

本文目录
- Java初学者,哪位友友能帮我设计一个简单的类似超市购物车的程序,参考一下~谢谢!
- 编写一个超市购物系统 完成商品的增加 删除 修改bean javabean实体类(封装
- java编写程序实现某超市商品查价功能从键盘输入商品号,显示对应的商品价格,以“n”结束查询
- 请高手解答,关于超市购物小票Java,从控制台获取数据的问题
- 求大神帮忙,谢谢!!!!!(要Java代码)
- 用JAVA实现超市购物功能,购物时如果购物者所需商品在超市中有则提示购物者买
- java:小明去超市买东西,所有买到的东西都放在了购物车之中,最后到收银台一起结账你编出来了是吗求助
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 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地瓜¥23¥6
2土豆¥1.21¥1.2
3丝瓜¥1.51¥1.5
合计: 总数量: ¥5 总价格: ¥8.7
**********************************************
商品编号商品名称单价购买数量总价
2土豆¥1.21¥1.2
3丝瓜¥1.51¥1.5
合计: 总数量: ¥2 总价格: ¥2.7
**********************************************
商品编号商品名称单价购买数量总价
合计: 总数量: ¥0 总价格: ¥0
**********************************************
4.打字太累了,比较匆忙,但是主要靠你自己领会。哪里不清楚再提出来。
编写一个超市购物系统 完成商品的增加 删除 修改bean javabean实体类(封装
边是一个超市购物罩大清系统完成商品的增加,删除,修改,这个这个都是一些对电脑非物前常的,就是说对电脑非常仿瞎在行的人才能做得到,请一下电脑专家吧。
java编写程序实现某超市商品查价功能从键盘输入商品号,显示对应的商品价格,以“n”结束查询
package sum;
import java.util.ArrayList;
import java.util.Scanner;
public class TestDemo {
public static void main(String args){
ArrayList《Goods》 arr = new ArrayList《Goods》();
Goods g1 = new Goods(1, "羽毛球", 237);
Goods g2 = new Goods(2, "羽毛球拍", 113);
Goods g3 = new Goods(3, "护腕", 100);
arr.add(g1);
arr.add(g2);
arr.add(g3);
System.out.println("***********************商品查询系统*********************");
System.out.println("1.羽毛球 2.羽毛球拍 3.护腕");
System.out.println("*****************************************************");
Scanner sc = new Scanner(System.in);
outer: while(true){
System.out.print("请选择商品号:");
String num = sc.next();
for(int i = 0; i 《 arr.size(); i++){
if((arr.get(i).getId() 局慎+ "").equals(num)){
System.out.println(arr.get(i).getName() + " " + arr.get(i).getPrice() + "元" );
}else if(num.equals("n")){
System.out.println("谢谢使用");
break outer;
}
}
}
}
}
class Goods{
public Goods(int id, String name, int price){
this.id = id;
this.name = name;
this.price = price;
}
private int id;
private String name;
private int price;
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 int getPrice() {
槐腊戚铅陵return price;
}
public void setPrice(int price) {
this.price = price;
}
}
很辛苦写的希望能采纳
请高手解答,关于超市购物小票Java,从控制台获取数据的问题
package util;
import java.util.ArrayList;
import java.util.Scanner;
public class PrintTable {
public static void main(String args){
Scanner input = new Scanner(System.in);
int number = 0;//商品的数量
double price = 0.0;//商品的单价
String flag = "";//标识是否继续操作
ArrayList《Integer》 numbers = new ArrayList《Integer》();//所有商品数量的集合
ArrayList《Double》 prices = new ArrayList《Double》();//所有商品单价集合
ArrayList《Double》 totals = new ArrayList《Double》();//商品总价集合
System.out.println("--------欢迎进入购物系统商城---------\n\n");
do{
System.out.print("请输入商品的数量:");
number = input.nextInt();
System.out.print("冲逗请输入商品的单价:");
price = input.nextDouble();
numbers.add(number);
prices.add(price);
totals.add(number*price);
System.out.print("确定要继续购物吗?(yes/no):");
flag= input.next();
System.out.println("\n");
}while ("yes".equalsIgnoreCase(flag));
System.out.print("\n需要打印购物单吗?(yes/no):");
flag= input.next();
while ("yes".equalsIgnoreCase(flag)) {
System.out.println("————————————————————————————————————");
System.out.println("数量 金额 单价");
System.out.println("——————悉判野——————————————————————————————");
for(int i=0; i《totals.size(); i++){
System.out.println(" "+numbers.get(i)+" "+totals.get(i)+" "+prices.get(i));
System.out.println("————————————————————————————————————");
}
System.out.println("\n购物结束,欢迎下次睁喊光临!!");
break;
}
}
}
你直接复制,到程序内跑跑,看看是不是你要的效果吧
求大神帮忙,谢谢!!!!!(要Java代码)
//第一题的答案:
import java.util.Scanner;
public class test {
public static void main(String args)
{
System.out.println("输入购陵烂买金额:");
Scanner input=new Scanner(System.in);
double a=input.nextDouble();
System.out.println("输入顾客类型(会员或普通):");
String b=input.next();
if(b=="会员")
{
if(a》=100)
{
a=a*0.8;
System.out.println("需付款:"+a);
}
else
{
System.out.println("需付款:"+a);
}
}
if(b=="普通")
{
if(a》=200)
{
a=a*0.75;
System.out.println("需付款:"+a);
}
else
{
System.out.println("需付款:"+a);
}
}
}
}
//下面是第二题答案:
public class test {
public static void main(String args)
{
for(int i = 0; i 《 3; i++)
{
for(int x = i + 1; x 《 3; x++)
{
System.out.print(" ");
}
for(int y = 0; y 《 (i + 1) * 2 - 1; y++)
{
System.out.print("*");
尺隐漏 }
System.out.println();
}
for(int i = 0; i 《 4; i++)
{
for(int x = 0; x 《 i; x++)
携州 {
System.out.print(" ");
}
for(int y = i; y 《 2 * 4 - i - 1; y++)
{
System.out.print("*");
}
System.out.println();
}
}
}
用JAVA实现超市购物功能,购物时如果购物者所需商品在超市中有则提示购物者买
首先这个需求滑樱很奇怪。
购物者买的东西 应该在超市里选的才对。
那么意思就是 万一选的东西 没库存了怎么办?
2种解信消丛决方法:
1:每次购买前或者选择的时候,都去后台查询库存,然后提示,这种效率低。但也行就是了
2:在进入这个商品详情页时候查桥吵询本商品库存,为0 那就提示没库存了。
java:小明去超市买东西,所有买到的东西都放在了购物车之中,最后到收银台一起结账你编出来了是吗求助
public class ShopGoodsDemo {
public static void main(String args) {
ShopCar s1=new ShopCar(5);
s1.add(new EatFood("面包",12.1));
s1.add(new EatFood("辣条",2.4));
s1.add(new EatFood("饼干",22.3));
s1.add(new WashGoods("洗发水",32.5));
s1.add(new WashGoods("卫生纸",22.8));
print(s1.search("饼干"));
System.out.println("悄带=============");
print(s1.getGoods());
}
public static void print(Goods gs){
double sum=0;
for(int i=0;i《gs.length;i++){
if(gs!=null){
//System.out.println(p+",");
System.out.println(gs.getPrice());
sum=sum+gs.getPrice();
}
}
System.out.println("总价格为:"渣改+sum);
}
}
public interface Goods {
public String getName();
public double getPrice();
}
public class EatFood implements Goods{
private String name;
private double price;
public EatFood() {
}
public EatFood(String name, double price) {
super();
this.name = name;
this.price = price;
}
@Override
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
public class WashGoods implements Goods{
private String name;
private double price;
public WashGoods() {
}
public WashGoods(String name, double price) {
super();
this.name = name;
this.price = price;
}
@Override
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
public class ShopCar {
private Goods goods=null;
private int foot;
//数组的大小由程序外部决定
public ShopCar(int len) {
if(len》0){
goods=new Goods;
}else{
goods=new Goods;
}
}
//判断数组的内容是否已满,未满,则添加如运判
public boolean add(Goods g){
if(this.foot《this.goods.length){
this.goods=g;
foot++;
return true;
}else{
return false;
}
}
//关键字查找
public Goods search(String keyword){
Goods go=null;
int count=0;
for(int i=0;i《this.goods.length;i++){
if(goods!=null){
if(this.goods.getName().indexOf(keyword)!=-1){
count++;
}
}
}
go=new Goods;
int f=0;
for(int i=0;i《this.goods.length;i++){
if(goods!=null){
if(this.goods.getName().indexOf(keyword)!=-1){
go;
f++;
}
}
}
return go;
}
//得到全部信息
public Goods getGoods(){
return this.goods;
}
}

更多文章:
手机版excel表格怎么排序(excel表格怎么排序成自己想要的顺序)
2025年6月12日 13:30
spring festival讲解(Spring festival是什么意思)
2025年11月30日 01:30
深入分析linux内核源码(面试必问的epoll技术,从内核源码出发彻底搞懂epoll)
2026年5月6日 13:30
countif函数使用举例(excel 中countif函数的用法)
2025年11月28日 11:45
excel常用函数和使用要点(excel常用函数的使用方法)
2026年3月6日 02:00
js html 趣味(求一个用html和js做的一个简单摇奖,是那种列出几个图片,然后开始,速度很快,逐渐速度变缓最终留在一个)
2026年2月24日 11:30
turnon和open怎么区分(turn on和open的区别是什么)
2026年8月31日 20:45
黄页仓库767ck(贯通式货架哪家好黄页,贯通式货架哪家好)
2026年8月22日 03:45
微信js开发教程(如何使用Cocos2d-JS引擎快速开发一个微信游戏)
2026年7月19日 19:45
canvas游戏(冒险岛打不开游戏缺少canvas.dll)
2025年6月30日 14:45
jsp学生信息管理系统源码(基于Java web 学生管理系统 最简单的就行 要求实现数据库的增删改查 要求个源代码和包)
2025年7月10日 22:45













