永发信息网

java 商品 系统 代码

答案:1  悬赏:40  手机版
解决时间 2021-02-09 21:26
里面分为两个类,一个是Goods;类,用来实例化对象的;shopmanagement是存放主方法的类。Goods;类包括成员变量 (名称,价格,数量,编号,类型,供应商),每个成员变量都是private类型,都有相对应的get和set方法(好像用不到我就不写了);我加了个prt(也就是print)的方法,当主方法调用查询功能的时候可以使用实例化对象的prt方法输出商品信息。

shopmanagement类的主方法包括两个循环,一个循环用来录入商品信息,另一个循环来查询商品信息,总报错无法运行,大神帮我看看怎么处理,是在解决不掉

package shopmanagement;
import java.util.Scanner;
class Goods {
private String name;
private int price;
private int sum;
private int code;
private String type;
private String supplier;
Goods(String name, int price, int sum, int code, String type, String supplier) {
this.name = name;
this.price = price;
this.sum = sum;
this.code = code;
this.type = type;
this.supplier = supplier;
}

public void prt() {
System.out.print("商品名称:" + this.name + "\t");
System.out.print("价格" + this.name + "\t");
System.out.print("库存:" + this.name + "\t");
System.out.print("编号:" + this.name + "\t");
System.out.print("类型:" + this.name + "\t");
System.out.print("供应商:" + this.name + "\t");
}

}

public class shopManagement {

public static void main(String[] args) {

System.out.println("********************商品管理系统********************");
System.out.println("********是否执行录入功能?Y执行,N切换到查询功能;********");

Scanner sc = new Scanner(System.in);


String function = sc.nextLine();
int i = 0;
Goods[] goods = null;
for (;;) {

if (function.equals("N")) {
System.out.println("切换到查询功能");
break;
}

System.out.println("商品名称(enter键结束):");
String name = sc.nextLine();

System.out.println("价格(enter键结束):");
int price = sc.nextInt();

System.out.println("存量(enter键结束):");
int sum = sc.nextInt();

System.out.println("编号(enter键结束):");
int code = sc.nextInt();

System.out.println("类型(enter键结束):");
String type = sc.nextLine();

System.out.println("供应商(enter键结束):");
String supplier = sc.nextLine();

goods[i] = new Goods(name, price, sum, code, type, supplier);
i++;

sc.close();
}


int j = 0;
System.out.println("是否继续操作?Y继续或N退出");
String function2 = new Scanner(System.in).nextLine();
for (;;) {
if (function2.equals("N")) {
System.out.println("正在退出");
System.exit(0);
System.out.println("输入品名");
String find = new Scanner(System.in).nextLine();

for (int k = 0; k < goods.length; k++) {
if (find.equals(goods[k].getName())) {
goods[k].prt();
}

}

}
}

}

}
最佳答案
package entity;

public class Market {

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<Integer,Market> goods=new HashMap<Integer, Market>();

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 < times; i++) {

System.out.print(" ");

}

System.out.print(row2);

for (int i = 0; i < 10; 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¥
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
梁启超说:“我支那四千年之大梦唤醒,始自甲
手抓饭地址在什么地方,想过去办事
24×25025-25×24024 简算
去4s店修车,4s店可以不经车主同意,自行试车吗
当朋友与别人打架的时候,我们应当CA. 不顾一
家电维修中心怎么去啊,有知道地址的么
洁净干洗行(政务公开中心旁)地址好找么,我有
单选题TheplanewillarriveinBeijingat7o’
我家车撞到一个8岁小男孩,导致其轻伤,对方
如果一个已婚男人前后几个情人都怀了他的孩子
福建公务员考试考试地点可以自己选择在哪里考
佛山市稻香餐饮管理有限公司在哪里啊,我有事
沙发在二楼可以发在供桌的上方可以吗
勿欺善,永无怨,勿让恶,无不良风,如荷花出
无锡宜兴水中楼饭店怎么去啊,有知道地址的么
推荐资讯
(3分)回答下列有关神经冲动传导的问题:(1
昆明那里可以做脏辫?
清泉大众洗浴尧母园店在哪里啊,我有事要去这
此茶叶末釉弥勒佛造型有民国特征,其底部胎质
老人和儿子户口独立,老人是否可以申请住房?
销售费用、管理费用、财务费用在期末均要进行
机电一体化专业大二读完可以找工作不?电工证
单选题Isit________thathehasleftforLond
天缘国际酒店(伊犁伊宁)地址在什么地方,想过
甲公司为增值税一般纳税人,适用增值税税率为
直街地址有知道的么?有点事想过去
周幽王死时周朝统治了多少年
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?