java开发实战经典(第二版)P217 6
6.6 使用面向对象的概念表示出下面的生活场景:
小明去超市买东西,所有买到的东西都放在了购物车之中,最后到收银台一起结账。
package book;
interface Goods {
public double getPrice();
public String getName();
}
class Toys implements Goods {
private String name;
private double price;
static String goodName = "玩具";
public double getPrice() {
return this.price;
}
public String getName() {
return this.name;
}
Toys(String name, double price) {
this.name = name;
this.price = price;
}
};
class Clothes implements Goods {
private String name;
private double price;
static String goodName = "服装";
public double getPrice() {
return this.price;
}
public String getName() {
return this.name;
}
Clothes(String name, double price) {
this.name = name;
this.price = price;
}
};
class Drinks implements Goods {
private String name;
private double price;
static String goodName = "饮品";
public double getPrice() {
return this.price;
}
public String getName() {
return this.name;
}
Drinks(String name, double price) {
this.name = name;
this.price = price;
}
};
class Foods implements Goods {
private String name;
private double price;
static String goodName = "食品";
public double getPrice() {
return this.price;
}
public String getName() {
return this.name;
}
Foods(String name, double price) {
this.name = name;
this.price = price;
}
};
class ShopTotal {
private Goods[] Good;
private int foot;
private double total;
static int len = 5;
ShopTotal() {
this.Good = new Goods[len];
}
public void add(Goods good) {
if (this.foot >= this.Good.length) {
Goods[] goods = new Goods[this.Good.length + len];
System.arraycopy(this.Good, 0, goods, 0, this.Good.length);
this.Good = goods;
}
this.Good[this.foot] = good;
this.foot++;
}
public double totalMoney() {
for (int i = 0; i < this.foot; i++) {
total += Good[i].getPrice();
}
return this.total;
}
public int getNumber() {
return this.foot;
}
};
public class JiOu {
public static void main(String args[]) {
ShopTotal st = new ShopTotal();
st.add(new Toys("手枪", 25.5));
st.add(new Toys("飞机", 84.3));
st.add(new Drinks("伊利(箱)", 73.5));
st.add(new Foods("乐事薯片", 8.5));
st.add(new Clothes("牛仔裤", 120));
st.add(new Clothes("白T", 60));
System.out.println("共购买" + st.getNumber() + "件商品,合计" + String.format("%.2f", st.totalMoney()) + "元");
}
}
运行结果:
共购买6件商品,合计371.80元
相关知识
JAVA编程不得不看的几本经典书籍
【Java项目】java实战宠物领养系统项目
基于Java宠物领养救助平台(源码+LW+调试文档)
今天我开始学习:PETSHOP3.0宠物商店(经典案例)
基于java的“爱心”宠物管理系统的设计与实现
JAVA模式电子宠物系统的开发 app开发
案例27:基于Java宠物领养系统开题报告设计
基于java springboot vue 可爱多宠物店管理系统
宠物服务平台设计与实现:Java后端与Vue前端结合
基于Java技术的宠物医院预约就医系统设计与开发(Springboot框架)研究背景和意义、国内外现状
网址: java开发实战经典(第二版)P217 6 https://www.mcbbbk.com/newsview356345.html
上一篇: 数万网友围观直播 “萌宠”投票仍 |
下一篇: 线上投票活动用什么平台制作?全民 |
推荐分享

- 1我的狗老公李淑敏33——如何 5096
- 2南京宠物粮食薄荷饼宠物食品包 4363
- 3家养水獭多少钱一只正常 3825
- 4豆柴犬为什么不建议养?可爱的 3668
- 5自制狗狗辅食:棉花面纱犬的美 3615
- 6狗交配为什么会锁住?从狗狗生 3601
- 7广州哪里卖宠物猫狗的选择性多 3535
- 8湖南隆飞尔动物药业有限公司宠 3477
- 9黄金蟒的价格 3396
- 10益和 MATCHWELL 狗 3352