里面分为两个类,一个是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();
}
}
}
}
}
}
java 商品 系统 代码
答案:1 悬赏:40 手机版
解决时间 2021-02-09 21:26
- 提问者网友:那叫心脏的地方装的都是你
- 2021-02-09 14:36
最佳答案
- 五星知识达人网友:雾月
- 2021-02-09 15:02
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¥
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¥
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯