商品购买成功java代码 用java编写购买商品

java 商品 系统 代码package entity;
public class Market {
private int id;//id
private int num;//数量
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("-------超市计价系统-------");
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("请输入商品的数量");
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; itimes; i++) {
System.out.print(" ");
}
System.out.print(row2);
for (int i = 0; i10; i++) {
System.out.print(" ");
}
System.out.print(row3);
System.out.println("\n");
}
}
//测试结果:
-------超市计价系统-------
编号商品价格
1可口可乐3.0¥
2爆米花5.0¥
3益达10.0¥
请输入商品的编号:
3
请输入商品的数量
5
您消费共计:50.0¥
急求java购物车代码package bean;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Administrator
* 购物车类:
* 为了方便将商品信息绑订到session上面而设计的一个
* 工具,提供了商品的添加 , 删除 , 列表,计价,清空 , 
* 修改功能 。
*/
public class Cart {
//items属性:用来保存商品
private ListCartItem items =
new ArrayListCartItem();
/**
* 将商品添加到购物车
*/
public boolean add(CartItem item){
for(int i=0;iitems.size();i++){
CartItem curr = items.get(i);
if(curr.getC().getId() == item.getC().getId()){
//该商品已经购买过
return false;
}
}
//没有购买过,则添加该商品
items.add(item);
return true;
}
/**
* 从购物车当中删除某件商品
*/
public void delete(int id){
for(int i=0;iitems.size();i++){
CartItem curr = items.get(i);
if(curr.getC().getId() == id){

推荐阅读