java超市买东西代码_java超市计价系统代码

展开全部

package entity;

public class Market {

32313133353236313431303231363533e59b9ee7ad9431333363373130

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 Map goods=new HashMap();

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 

System.out.print(" ");

}

System.out.print(row2);

for (int i = 0; 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¥

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值