重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
以前学习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.打字太累了,比较匆忙,但是主要靠你自己领会。哪里不清楚再提出来。
class Fruit{
private String fruitName;
private float price;
public Fruit(String fruitName, float price) {
super();
this.fruitName = fruitName;
this.price = price;
}
public String getFruitName() {
return fruitName;
}
public void setFruitName(String fruitName) {
this.fruitName = fruitName;
}
public float getPrice() {
return price;
}
public void setPrice(float privce) {
this.price = privce;
}
@Override
public String toString() {
return "水果名称:" + getFruitName() + " " + "价格:" + getPrice();
}
}
class Apple extends Fruit{
public Apple(String fruitName, float price) {
super(fruitName, price);
}
}
class Banana extends Fruit{
public Banana(String fruitName, float price) {
super(fruitName, price);
}
}
public class FruitShop {
ListFruit list;
public FruitShop(){
list = new ArrayListFruit();
list.add(new Apple("苹果",3));
list.add(new Banana("香蕉",2));
}
public void show(){
for (Fruit f : list) {
System.out.println(f);
}
}
public static void main(String[] args) {
new FruitShop().show();
}
}
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; i times; i++) {
System.out.print(" ");
}
System.out.print(row2);
for (int i = 0; i 10; 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¥