public class Good {
// Good类
private String name;
private double price;
public Good(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public class Goodgl {
//Goodgl 类
Good [] good1 =new Good[10];
{
good1[index]= new Good("海天酱油",1.5);
good1[++index]= new Good("山西陈醋",5.4);
}
static int index=0;
// 显示商品
public void xian(){
for (int i = 0; i <good1.length ; i++) {
// System.out.println(good1[0]);
if(good1[i]!=null){
System.out.println((i+1)+" "+good1[i].getName()+good1[i].getPrice());
}
}
}
//添加商品
public void add(String name,double price) {
System.out.println(index);
for (int i = 0; i <good1.length ; i++) {
if(good1[i]==null){
good1[i].setName(name);//!!!!!!!!这行添加商品的功能报错,Exception in thread "main" java.lang.NullPointerException
good1[i].setPrice(price);
System.out.println(good1[i].getName());
break;
}
}
}
}
//测试类
public class Test1 {
public static void main(String[] args) {
Goodgl goodgl=new Goodgl();
Scanner input=new Scanner(System.in);
fn:while (true){
System.out.println("欢迎光临商品信息管理系统");
System.out.println("1.查看全部商品信息");
System.out.println("2,添加商品信息");
System.out.println("3.根据商品名称查看商品价格");
System.out.print("请选择操作");
int number=input.nextInt();
switch (number){
case 1:
System.out.println("商品信息显示");
System.out.println("序号"+'\t'+"商品名称"+'\t'+"商品价格");
goodgl.xian();
System.out.print("是否继续操作");
String cao=input.next();
if(cao.equals("y")){
continue;
}else {
System.out.println("谢谢使用本系统,欢迎下次光临");
break fn;
}
case 2:
System.out.println("商品信息添加");
System.out.print("请输入商品名称");
String name=input.next();
System.out.print("请输入商品价格");
double price=input.nextDouble();
goodgl.add(name,price);
System.out.println("商品添加成功");
System.out.print("是否继续操作");
String cao1=input.next();
if(cao1.equals("y")){
continue;
}else {
System.out.println("谢谢使用本系统,欢迎下次光临");
break fn;
}
}
}
}
}
//如何使我这个添加商品的功能不报错,且能正常添加一个商品
import java.util.Scanner;
public class Good {
private String name;
private double price;
public Good(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public class Goodgl {
private Good[] goods;
private int index;
public Goodgl() {
goods = new Good[10];
index = 0;
goods[index] = new Good("海天酱油", 1.5);
goods[++index] = new Good("山西陈醋", 5.4);
}
public void xian() {
for (int i = 0; i < goods.length; i++) {
if (goods[i] != null) {
System.out.println((i + 1) + " " + goods[i].getName() + " " + goods[i].getPrice());
}
}
}
public void add(String name, double price) {
if (index < goods.length) {
goods[index] = new Good(name, price);
index++;
System.out.println("商品添加成功");
} else {
System.out.println("商品列表已满,无法添加更多商品");
}
}
}
public class Test1 {
public static void main(String[] args) {
Goodgl goodgl = new Goodgl();
Scanner input = new Scanner(System.in);
fn: while (true) {
System.out.println("欢迎光临商品信息管理系统");
System.out.println("1.查看全部商品信息");
System.out.println("2.添加商品信息");
System.out.println("3.根据商品名称查看商品价格");
System.out.print("请选择操作");
int number = input.nextInt();
switch (number) {
case 1:
System.out.println("商品信息显示");
System.out.println("序号\t商品名称\t商品价格");
goodgl.xian();
System.out.print("是否继续操作");
String cao = input.next();
if (cao.equals("y")) {
continue;
} else {
System.out.println("谢谢使用本系统,欢迎下次光临");
break fn;
}
case 2:
System.out.println("商品信息添加");
System.out.print("请输入商品名称");
String name = input.next();
System.out.print("请输入商品价格");
double price = input.nextDouble();
goodgl.add(name, price);
System.out.print("是否继续操作");
String cao1 = input.next();
if (cao1.equals("y")) {
continue;
} else {
System.out.println("谢谢使用本系统,欢迎下次光临");
break fn;
}
}
}
}
}