List集合综合练习
设计电商类,其中包含商品类,商品类属性(名称、价格、介绍、厂家、生产地址),继承类有食品类、电气类、服装类、玩具类。
食品类属性:有效时长、重量
电气类属性:电压、功率、重量
服装类属性:材质、面料、成分
玩具类属性:材质、分类、适用年龄
要求:
1、设计商品类,封装商品信息
2、设计继承类,继承商品类,封装继承商品信息
3、设计商城,商城中有一个各类商品的集合,客户可以看到所有商品,也可以根据不同继承类查询商品,每类商品不少以五种
4、查询商城中价格超过100元的商品
5、查询生产地是“北京”的商品
6、根据商品价格给商城排序,价格从高到低
7、设计购物车,购物车包括商品名称和商品数量,输入商品名称,显示商品信息,输入数量,加入购物车,完成添加后,输入“结账”,计算所有商品价格。
8、设计抽奖功能,查询所有商品,生成随机数,随机数范围0到商品集合长度,生成随机数后,通过下标获取商品,显示商品信息
注意:只允许是有目前学习的内容进行练习
1、设计商品类,封装商品信息
依次为:食品类、电气类、服装类、玩具类。
package Day1817pm;
public class EC {
private String name;//名称、
private double price;//价格
private String intro;//、介绍、
private String factory;//厂家、
private String proAddr;//生产地址
public EC(String name, double price, String intro, String factory, String proAddr) {
super();
this.name = name;
this.price = price;
this.intro = intro;
this.factory = factory;
this.proAddr = proAddr;
}
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 getIntro() {
return intro;
}
public void setIntro(String intro) {
this.intro = intro;
}
public String getFactory() {
return factory;
}
public void setFactory(String factory) {
this.factory = factory;
}
public String getProAddr() {
return proAddr;
}
public void setProAddr(String proAddr) {
this.proAddr = proAddr;
}
@Override
public String toString() {
return "EC [名称=" + name + ",价格=" + price + ", 介绍=" + intro + ", 厂家=" + factory + ", 生产地址="
+ proAddr + "]";
}
}
2、设计继承类,继承商品类,封装继承商品信息
Food类
package Day1817pm;
public class Food extends EC{
private double time;//有效时长、
private double weight;//重量
public Food(String name, double price, String intro, String factory, String proAddr, double time, double weight) {
super(name, price, intro, factory, proAddr);
this.time = time;
this.weight = weight;
}
public double getTime() {
return time;
}
public void setTime(double time) {
this.time = time;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Food [有效时长=" + time + ", 重量=" + weight + ", toString()=" + super.toString() + "]";
}
}
package Day1817pm;
public class Electric extends EC{
private double v;//电压、
private double p;//功率、
private double Eweight;//重量
public Electric(String name, double price, String intro, String factory, String proAddr, double v, double p,
double eweight) {
super(name, price, intro, factory, proAddr);
this.v = v;
this.p = p;
Eweight = eweight;
}
public double getV() {
return v;
}
public void setV(double v) {
this.v = v;
}
public double getP() {
return p;
}
public void setP(double p) {
this.p = p;
}
public double getEweight() {
return Eweight;
}
public void setEweight(double eweight) {
Eweight = eweight;
}
@Override
public String toString() {
return "Electric [电压=" + v + ", 功率=" + p + ", 重量=" + Eweight + ", toString()=" + super.toString() + "]";
}
}
package Day1817pm;
public class Clothing extends EC{
private String texture;//材质、
private String lining;//面料、
private String element;//成分
public Clothing(String name, double price, String intro, String factory, String proAddr, String texture,
String lining, String element) {
super(name, price, intro, factory, proAddr);
this.texture = texture;
this.lining = lining;
this.element = element;
}
public String getTexture() {
return texture;
}
public void setTexture(String texture) {
this.texture = texture;
}
public String getLining() {
return lining;
}
public void setLining(String lining) {
this.lining = lining;
}
public String getElement() {
return element;
}
public void setElement(String element) {
this.element = element;
}
@Override
public String toString() {
return "Clothing [材质=" + texture + ", 面料=" + lining + ", 成分=" + element + ", toString()="
+ super.toString() + "]";
}
}
package Day1817pm;
public class Tool extends EC{
private String Ttexture;//材质、
private String Ttype;//分类
private String perAge;//、适用年龄
public Tool(String name, double price, String intro, String factory, String proAddr, String ttexture, String ttype,
String perAge) {
super(name, price, intro, factory, proAddr);
Ttexture = ttexture;
Ttype = ttype;
this.perAge = perAge;
}
public String getTtexture() {
return Ttexture;
}
public void setTtexture(String ttexture) {
Ttexture = ttexture;
}
public String getTtype() {
return Ttype;
}
public void setTtype(String ttype) {
Ttype = ttype;
}
public String getPerAge() {
return perAge;
}
public void setPerAge(String perAge) {
this.perAge = perAge;
}
@Override
public String toString() {
return "Tool [材质=" + Ttexture + ", 分类=" + Ttype + ", 适用年龄=" + perAge + ", toString()="
+ super.toString() + "]";
}
}
3、设计商城,商城中有一个各类商品的集合,客户可以看到所有商品,也可以根据不同继承类查询商品,每类商品不少以五种
4、查询商城中价格超过100元的商品
5、查询生产地是“北京”的商品
6、根据商品价格给商城排序,价格从高到低
7、设计购物车,购物车包括商品名称和商品数量,输入商品名称,显示商品信息,输入数量,加入购物车,完成添加后,输入“结账”,计算所有商品价格。
8、设计抽奖功能,查询所有商品,生成随机数,随机数范围0到商品集合长度,生成随机数后,通过下标获取商品,显示商品信息
注意:只允许是有目前学习的内容进行练习
package Day1817pm;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import org.junit.Test;
public class Store {
private static List<String> type = null;
private static List<Food> foods = null;
private static List<Electric> electrics = null;
private static List<Clothing> clothings = null;
private static List<Tool> tools = null;
private static List<EC> ecs = null;
Scanner sc = new Scanner(System.in);
public void init(){
type = new ArrayList<String>();
Collections.addAll(type, "食品类","电气类","服装类","玩具类");
foods = new ArrayList<Food>();
electrics = new ArrayList<Electric>();
clothings = new ArrayList<Clothing>();
tools = new ArrayList<Tool>();
ecs = new ArrayList<EC>();
foods.add(new Food("食品1",12.8,"这是食品1","食品厂家1","食品北京1111",21,10.8));
foods.add(new Food("食品2",112.8,"这是食品2","食品厂家2","食品1112",22,12.8));
foods.add(new Food("食品3",102.8,"这是食品3","食品厂家3","食品北京1113",23,14.8));
foods.add(new Food("食品4",10.8,"这是食品4","食品厂家4","食品1114",24,16.8));
foods.add(new Food("食品5",120.8,"这是食品5","食品厂家5","食品1115",25,18.8));
electrics.add(new Electric("电气1",22.8,"这是电气1","电气厂家1","电气北京1112",221,1001.0,19.8));
electrics.add(new Electric("电气2",42.8,"这是电气2","电气厂家2","电气1113",222,1002.0,49.8));
electrics.add(new Electric("电气3",62.8,"这是电气3","电气北京厂家3","电气1114",223,1003.0,59.8));
electrics.add(new Electric("电气4",82.8,"这是电气4","电气厂家4","电气1115",224,1004.0,69.8));
electrics.add(new Electric("电气5",102.8,"这是电气5","电气厂家5","电气北京1116",225,1005.0,79.8));
clothings.add(new Clothing("服装1",22.8,"这是服装1","服装厂家1","服装北京1112","尼龙0","优秀1","羊毛1"));
clothings.add(new Clothing("服装2",42.8,"这是服装2","服装厂家2","服装1113","尼龙1","优秀2","羊毛2"));
clothings.add(new Clothing("服装3",62.8,"这是服装3","服装厂家3","服装北京1114","尼龙2","优秀3","羊毛3"));
clothings.add(new Clothing("服装4",82.8,"这是服装4","服装厂家4","服装1115","尼龙3","优秀4","羊毛4"));
clothings.add(new Clothing("服装5",102.8,"这是服装5","服装厂家5","服装1116","尼龙4","优秀5","羊毛5"));
tools.add(new Tool("玩具1",22.8,"这是玩具1","玩具厂家1","玩具北京1112","尼龙01","玩具11","1~8岁"));
tools.add(new Tool("玩具2",42.8,"这是玩具2","玩具厂家2","玩具北京1114","尼龙02","玩具12","2~8岁"));
tools.add(new Tool("玩具3",62.8,"这是玩具3","玩具厂家3","玩具1116","尼龙03","玩具13","3~8岁"));
tools.add(new Tool("玩具4",82.8,"这是玩具4","玩具厂家4","玩具1118","尼龙04","玩具14","4~8岁"));
tools.add(new Tool("玩具5",212.8,"这是玩具5","玩具厂家5","玩具1110","尼龙05","玩具15","5~8岁"));
for(Food s : foods){
ecs.add(s);
}
for(Electric s : electrics){
ecs.add(s);
}
for(Clothing s : clothings){
ecs.add(s);
}
for(Tool s : tools){
ecs.add(s);
}
select();
shopCar();
System.out.println("是否要抽奖?1:抽奖;2:不抽奖");
int s = sc.nextInt();
if(s == 1){
chojiang();
}else{
System.out.println("很遗憾,你没有抽奖!!");
}
}
/*
8、设计抽奖功能,查询所有商品,生成随机数,随机数范围0到商品集合长度,
生成随机数后,通过下标获取商品,显示商品信息
*/
private void chojiang() {
System.out.println("开始抽奖了!!");
Random r = new Random();
int num = r.nextInt(ecs.size());
System.out.println("恭喜你抽中了:");
System.out.println(ecs.get(num));
}
/*
* 7、设计购物车,购物车包括商品名称和商品数量,
* 输入商品名称,显示商品信息,输入数量,加入购物车,
* 完成添加后,输入“结账”,计算所有商品价格。
*/
//这个有问题,因为我这个只能添加一个,虽然知道怎么改,但是我不想改了,我就说一下实现思路:
//1.创建一个新的List集合private static List<EC> gwc = null;,
//2.大概逻辑还是下面那样,如果有的话,就往gwc这个集合里面添加元素,判断没有的函数使用gwc.isEmpty()返回false,就可以判断了
//3.剩下的又都是逻辑判断,选择价格,然后相乘
private void shopCar() {
String n = null;
double s = 0.0;
System.out.println("请输入你想要查询的商品名称:");
while(true){
n = sc.next();
for(EC e : ecs){
if(n.equals(e.getName())){
System.out.println(e);
s = e.getPrice();
}
}
if(s == 0.0){
System.out.println("没有此商品,请重新输入你想要查询的商品名称:");
}else{
break;
}
}
System.out.println("请输入你想要购买的数量:");
int n1 = sc.nextInt();
double sum = s * n1;
System.out.println("是否要结账?结账 OR 不结账");
String in = sc.next();
if("结账".equals(in)){
System.out.println("所以商品价格为:"+sum);
}else{
System.out.println("欢迎光临!");
}
}
/*
3、设计商城,商城中有一个各类商品的集合,客户可以看到所有商品,也可以根据不同继承类查询商品,每类商品不少以五种
4、查询商城中价格超过100元的商品
5、查询生产地是“北京”的商品
6、根据商品价格给商城排序,价格从高到低
*/
private void select() {
System.out.println("请选择你想要查看的商品分类:0全部商品,1食品类;2电气类;3服装类;4玩具类");
int which = -1;
loop:
while(true){
which =sc.nextInt();
switch(which){
case 0:
Collections.sort(ecs, new Comparator<EC>(){
@Override
public int compare(EC o1, EC o2) {
return (int) (o2.getPrice()-o1.getPrice());
}
});
for(EC e : ecs){
System.out.println(e);
}
System.out.println("是否要对商品进行挑选价格大于100?true OR false");
boolean isOk = sc.nextBoolean();
if(isOk){
System.out.println("商品价格大于100的有:");
for(EC e : ecs){
if(e.getPrice()>100.0){
System.out.println(e);
}
}
}
System.out.println("是否要查询生产地是“北京”的商品?true OR false");
boolean is = sc.nextBoolean();
if(is){
System.out.println("查询生产地是“北京”的商品有:");
for(EC e : ecs){
if(e.getProAddr().contains("北京")){
System.out.println(e);
}
}
}
break loop;
case 1:
Collections.sort(foods, new Comparator<Food>(){
@Override
public int compare(Food o1, Food o2) {
return (int) (o2.getPrice()-o1.getPrice());
}
});
for(Food s : foods){
System.out.println(s);
}
System.out.println("是否要对商品进行挑选价格大于100?true OR false");
boolean isOk1 = sc.nextBoolean();
if(isOk1){
System.out.println("商品价格大于100的有:");
for(Food s : foods){
if(s.getPrice()>100.0){
System.out.println(s);
}
}
}
System.out.println("是否要查询生产地是“北京”的商品?true OR false");
boolean is1 = sc.nextBoolean();
if(is1){
System.out.println("查询生产地是“北京”的商品有:");
for(Food s : foods){
if(s.getProAddr().contains("北京")){
System.out.println(s);
}
}
}
break loop;
case 2:
Collections.sort(electrics, new Comparator<Electric>(){
@Override
public int compare(Electric o1, Electric o2) {
return (int) (o2.getPrice()-o1.getPrice());
}
});
for(Electric s : electrics){
System.out.println(s);
}
System.out.println("是否要对商品进行挑选价格大于100?true OR false");
boolean isOk2 = sc.nextBoolean();
if(isOk2){
System.out.println("商品价格大于100的有:");
for(Electric s : electrics){
if(s.getPrice()>100.0){
System.out.println(s);
}
}
}
System.out.println("是否要查询生产地是“北京”的商品?true OR false");
boolean is2 = sc.nextBoolean();
if(is2){
System.out.println("查询生产地是“北京”的商品有:");
for(Electric s : electrics){
if(s.getProAddr().contains("北京")){
System.out.println(s);
}
}
}
break loop;
case 3:
Collections.sort(clothings, new Comparator<Clothing>(){
@Override
public int compare(Clothing o1, Clothing o2) {
return (int) (o2.getPrice()-o1.getPrice());
}
});
for(Clothing s : clothings){
System.out.println(s);
}
System.out.println("是否要对商品进行挑选价格大于100?true OR false");
boolean isOk3 = sc.nextBoolean();
if(isOk3){
System.out.println("商品价格大于100的有:");
for(Clothing s : clothings){
if(s.getPrice()>100.0){
System.out.println(s);
}
}
}
System.out.println("是否要查询生产地是“北京”的商品?true OR false");
boolean is3 = sc.nextBoolean();
if(is3){
System.out.println("查询生产地是“北京”的商品有:");
for(Clothing s : clothings){
if(s.getProAddr().contains("北京")){
System.out.println(s);
}
}
}
break loop;
case 4:
Collections.sort(tools, new Comparator<Tool>(){
@Override
public int compare(Tool o1, Tool o2) {
return (int) (o2.getPrice()-o1.getPrice());
}
});
for(Tool s : tools){
System.out.println(s);
}
System.out.println("是否要对商品进行挑选价格大于100?true OR false");
boolean isOk4 = sc.nextBoolean();
if(isOk4){
System.out.println("商品价格大于100的有:");
for(Tool s : tools){
if(s.getPrice()>100.0){
System.out.println(s);
}
}
}
System.out.println("是否要查询生产地是“北京”的商品?true OR false");
boolean is4 = sc.nextBoolean();
if(is4){
System.out.println("查询生产地是“北京”的商品有:");
for(Tool s : tools){
if(s.getProAddr().contains("北京")){
System.out.println(s);
}
}
}
break loop;
default:
System.out.println("输入错误!请重新输入:");
}
}
}
}
测试类
package Day1817pm;
public class Test {
public static void main(String[] args) {
Store s = new Store();
s.init();
}
}