超市管理系统
public class Goods {
private String GoodsName;
public Goods(String goodsName) {
GoodsName = goodsName;
}
@Override
public String toString() {
return "Goods{" +
"GoodsName='" + GoodsName + '\'' +
'}';
}
public String getGoodsName() {
return GoodsName;
}
public void setGoodsName(String goodsName) {
GoodsName = goodsName;
}
}
public class Person {
private String name;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person(String name) {
this.name = name;
}
public Person() {
}
String[] strings = new String[3];
Shop shop;
public void buy(Shop shop1, Person person) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要去的超市");
String name = scanner.next();
shop = shop1;
if (!(name.equals(shop.getGoodsName()))) {
System.out.println("没有对应的超市");
return;
}
System.out.println("请输入要购买的商品");
String GoodsName = scanner.next();
boolean loop = shop.sell(name, GoodsName, shop);
if (loop) {
System.out.println("超市:" + name + "\t商品:" + GoodsName + "\t用户:" + person.getName());
return;
}
System.out.println("很遗憾,白跑一趟");
}
}
public class Shop extends Goods {
public Shop(String goodsName, String[] strings) {
super(goodsName);
this.strings = strings;
}
String[] strings;
public String[] getStrings() {
return strings;
}
public void setStrings(String[] strings) {
this.strings = strings;
}
public boolean sell(String name, String GoodsName, Shop shop) {
String[] str = shop.getStrings();
for (int i = 0; i < str.length; i++) {
if (GoodsName.equals(str[i])) {
return true;
}
}
return false;
}
}
public class Supermarket {
public static void main(String[] args) {
Goods goods1 = new Goods("苹果");
Goods goods2 = new Goods("荔枝");
Goods goods3 = new Goods("夏威夷果");
Goods goods4 = new Goods("开心果");
String[] str = {goods1.getGoodsName(), goods2.getGoodsName(), goods3.getGoodsName(), goods4.getGoodsName()};
Shop shop = new Shop("超市一", str);
Person person = new Person("李四");
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while (loop) {
System.out.println("1、购买");
System.out.println("2、退出");
System.out.println("请输入选择");
int choice = scanner.nextInt();
switch (choice) {
case 1:
person.buy(shop, person);
break;
case 2:
loop = false;
break;
case 3:
System.out.println("输入错误");
break;
}
}
}
}