题目描述:疯狂购物节,许多用户需要购买多种家用小电器,考虑的家用小电器包括电风扇、电水壶、空气净化器和电吹风。设计并实现一个用户挑选、购买家用小电器的应用程序。(可视化界面实现在下一个博客噢)
|・ω・`)
package oo;
//goods.class
public class goods {
public int stock;
public double price;
public int number;//需要购买的数量
goods(int sto,double pri,int num)
{
stock=sto;
price=pri;
number=num;
}
public boolean not_empty ()
{
if (this.stock!=0) return true;
else return false;
}
}
package oo;
import javax.swing.JOptionPane;
/*
shopping.class
*/
//电风扇20元库存(实例变量)10个,电水壶10.5元库存15个,净化器30元库存16个,电吹风20.3元库存30个
public class shopping {
public static double payment;
static goods fanner=new goods(10,20,1);
static goods kettle=new goods(15,10.5,1);
static goods purifier =new goods(16,30,1);
static goods hair_drier=new goods(30,20.3,1);
public static void main(String args[])
{
payment=0;
int num_fanner=0,num_kettle=0,num_purifier=0,num_hair_drier=0;
if(fanner.stock>0){
String number_fanner=JOptionPane.showInputDialog("电风扇,每个20.0rmb;选购则输入想购买的商品数目,不选按0");
num_fanner=Integer.parseInt(number_fanner);
if(num_fanner!=0)
{
if(fanner.stock>=num_fanner){
payment+=num_fanner*20.0;
}
}
}
if(kettle.stock>0){
String number_kettle=JOptionPane.showInputDialog("电水壶,每个10.5rmb;选购则输入想购买的商品数目,不选按0");
num_kettle=Integer.parseInt(number_kettle);
if(num_kettle!=0)
{
if(kettle.stock>=num_kettle){
payment+=num_kettle*10.5;
}
}
}
if(purifier.stock>0){
String number_purifier=JOptionPane.showInputDialog("净化器,每个30.0rmb;选购则输入想购买的商品数目,不选按0");
num_purifier=Integer.parseInt(number_purifier);
if(num_purifier!=0)
{
if(purifier.stock>=num_purifier){
payment+=num_purifier*30.0;
}
}
}
if(hair_drier.stock>0){
String number_hair_drier=JOptionPane.showInputDialog("电吹风,每个20.3rmb;选购则输入想购买的商品数目,不选按0");
num_hair_drier=Integer.parseInt(number_hair_drier);
if(num_hair_drier!=0)
{
if(hair_drier.stock>=num_hair_drier){
payment+=num_hair_drier*20.3;
}
}
}
System.out.println("尊敬的顾客您好!"+"\n"+"您选购的商品有"
+" 电风扇"+num_fanner+"个"
+" 电水壶"+num_kettle+"个"
+" 净化器"+num_purifier+"个"
+" 电吹风"+num_hair_drier+"个"+"\n"
+"共需支付"+payment+"元");
}
}
运行结果是弹出的框框,让用户 输入每种商品的个数,然后控制台输出所购商品和总价格。
如果本篇博客对您有用的话点个赞呗盆友~~嘻嘻