现有一台自动贩卖机,贩卖机中有如下商品,所有商品都有商品名称,价格,自动贩卖机有出售商品的方法,用户可以输入想要购买的商品的编号,根据商品编号售卖不同的商品
奶茶:
红牛
冰红茶
package oracleday15;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import java.util.Random;
import java.util.Scanner;
/**
*
*/
public class Traffic {
public static void main(String[] args) {
VendMechine vmechine = new VendMechine();//创建贩卖机
vmechine.foundgoods();//生成商品
Colo colo = new Colo();
RedBull redBull = new RedBull();
IceRedTea iceRedTea = new IceRedTea();
Scanner scan = new Scanner(System.in);
System.out.println("请输入想要的商品编号");
System.out.println("1.colo 2.红牛 3.茶 4.退出");
OUT:
while (true){
int systemIn = scan.nextInt();
if (systemIn == colo.goodsnum){
for (int i = 0; i < vmechine.commditys.length; i++) {
if (vmechine.commditys[i] instanceof Colo){
System.out.println(vmechine.commditys[i].name+"价格是"+vmechine.commditys[i].price);
break;}
}
}else if (systemIn == redBull.goodsnum) {
for (int i = 0; i < vmechine.commditys.length; i++) {
if (vmechine.commditys[i] instanceof RedBull){
System.out.println(vmechine.commditys[i].name+"价格是"+vmechine.commditys[i].price);
break;}
}
}else if (systemIn == iceRedTea.goodsnum){
for (int i = 0; i < vmechine.commditys.length; i++) {
if (vmechine.commditys[i] instanceof IceRedTea){
System.out.println(vmechine.commditys[i].name+"价格是"+vmechine.commditys[i].price);
break;}
}
}else if (systemIn == 4){
System.out.println("成功退出");
break OUT;
}
else {
System.out.println("fault");
}
}
}
}
class VendMechine {
public VendMechine() {
}
// public VendMechine(Colo colo){
// System.out.println();
// }
//创建多个商品对象
Commditys commditys[] = new Commditys[15];
Random random = new Random();
public Commditys[] foundgoods() {
for (int i = 0; i < commditys.length; i++) {
int a = random.nextInt(15);
if (a < 5) {
commditys[i] = new Colo();
}else if(a < 10){
commditys[i] = new RedBull();
}else if (a < 15){
commditys[i] = new IceRedTea();
}
}
//生成随机的所有商品对象
return commditys;
}//贩卖机的生成商品方法
}
class Commditys {
protected String name;
protected double price;
protected int goodsnum;
public Commditys() {
}
public Commditys(String name, double price, int goodsnum) {
this.name = name;
this.price = price;
this.goodsnum = goodsnum;
}
}
class Colo extends Commditys {
public Colo() {
name = "可乐";
price = 3;
goodsnum = 1;
}
}
class RedBull extends Commditys {
public RedBull() {
name = "红牛";
price = 6;
goodsnum = 2;
}
}
class IceRedTea extends Commditys {
public IceRedTea() {
name = "冰红茶";
price = 3;
goodsnum = 3;
}
}
定时器定时输出Pet的子类对象
package com.poloymorphic.extendsx;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
/**
* 多态
* 向上造型
*/
public class ExtendsDemo {
private Pet pets[] = {}; //自定义数组长度
// Pet pets[] = new Pet[20];
public Pet[] creatPets(){
pets = Arrays.copyOf(pets,pets.length+1);
//每次调用creatPet数组 数组长度加1 创造一个新的pet对象
Random random = new Random();
for (int i = 0; i < pets.length; i++) {
int n = random.nextInt(20);
if (n <= 4){
pets[i] = new Dog();
}else if( n<= 12){
pets[i] = new Rabbits();
}else if (n <= 19){
pets[i] = new Cat();
}
}
//遍历for循环给声明的pet类型创建一个子类对象
return pets;
//creatPets给定一个返回值
}
/**
* 遍历循环输出数组
*/
public void printPet(Pet petss[]){
for (int i = 0; i < petss.length; i++) {
System.out.println(petss);
}
}
public static void main(String[] args) {
ExtendsDemo exdemo = new ExtendsDemo();
Timer timer = new Timer();//创建定时器对象
timer.schedule(new TimerTask(){
@Override
public void run(){
exdemo.creatPets();
exdemo.printPet(exdemo.pets);
}
},10,1000);
}
}