老师上课演示了一个程序,让我们课下补完,过程中出现了一些问题,我java菜鸟一只,求~
Product.java:
id,name,description变量,以及对应的get和set函数Store.java:
void addProduct(...){...}
void removeProduct(...){...}
void delProduct(...){...}
void printProductList(...){...}Seller.java
Store store;
String sellerName;BuyList.java:
public void addProduct(String name, int number){...}Buyer.java:
BuyList buylist;
String buyerName;Test.java:
测试的实现。
问题出现啦:main函数必然是static的,可是main引用的一些sellerRun(运行seller操作的函数)和buyerRun也必须设置为static,否则报错。
运行的时候选择seller之后设置完产品列表,返回到上一级菜单(选择buyer还是seller的菜单)的时候没法保存产品列表,无论再进哪个用户都会显示产品列表为空(这个问题和static有关么)
刚从c转过来,封装性和面向对象让人很头大,总感觉这程序开始构思的时候就有一些混乱……
Product.java:
id,name,description变量,以及对应的get和set函数Store.java:
void addProduct(...){...}
void removeProduct(...){...}
void delProduct(...){...}
void printProductList(...){...}Seller.java
Store store;
String sellerName;BuyList.java:
public void addProduct(String name, int number){...}Buyer.java:
BuyList buylist;
String buyerName;Test.java:
测试的实现。
问题出现啦:main函数必然是static的,可是main引用的一些sellerRun(运行seller操作的函数)和buyerRun也必须设置为static,否则报错。
运行的时候选择seller之后设置完产品列表,返回到上一级菜单(选择buyer还是seller的菜单)的时候没法保存产品列表,无论再进哪个用户都会显示产品列表为空(这个问题和static有关么)
刚从c转过来,封装性和面向对象让人很头大,总感觉这程序开始构思的时候就有一些混乱……
这些都是java基础,还是需要自己慢慢练,慢慢理解的!
public static void main(String[] args){
...
userRun()
...}
接下来的userRun()函数和其他main引用的函数也要求是static的,感觉这样都声明为static不对吧
纠结~
package shopping;
import java.util.*;public class TestShopping {
static ArrayList<Seller> allSellers;
static ArrayList<Buyer> allBuyers;
static Intermediary intermediary;//增加中间人这个角色,方便商家和用户之间的交易
static Set<Product> allProducts; public TestShopping() {
allSellers = new ArrayList<Seller>();
allBuyers = new ArrayList<Buyer>();
allProducts = new HashSet<Product>();
init();
} private void init() {
// only use one Intermediary as test
intermediary = Intermediary.createIntermediary(allSellers,
allBuyers); // use three Seller as test
for (int i = 0; i < 3; i++) {
allSellers.add(Seller.createSeller(i)); }
// use three Buyer as test
for (int i = 0; i < 3; i++) {
allBuyers.add(Buyer.createBuyer(i));
}
// generate ten Product as test
while (allProducts.size() < 10) {
int id = (int) ((Math.random() * 100) + 1);
Product product = Product.createProduct(id, "name" + id,
"description" + id);
// System.out.println(id);
if (allProducts.add(product))
allSellers.get(id % 3).add(product); } println("system start .........."); } public static void println(Object obj) {
System.out.println(obj);
} public static void println() {
System.out.println();
} public static void main(String[] args) {
new TestShopping();//initialization //display system information
/*
* println("products from each seller :"); for (Seller seller :
* allSellers) { println(); System.out.println(seller);
* System.out.println("-----------------------------------");
* seller.displayProductInStore(); } println();
* System.out.println("products from each buyer :"); for (Buyer
* buyer : allBuyers) { println(); System.out.println(buyer);
* System.out.println("-----------------------------------");
* buyer.displayProductInShoppingCar(); } println();
*///------------------------------------------------------------------------------------------------------
Scanner scanner = new Scanner(System.in);
while (true) {
Seller seller = intermediary.getSeller();//选择用户
Buyer buyer = intermediary.getBuyer();//选择商家 ;
while (true) {
intermediary.hint();
int operationNumber = scanner.nextInt();
switch (operationNumber) {
case 1:
seller.displayProductInStore();//显示商家的商品
break;
case 2:
buyer.displayProductInShoppingCar();//显示购物车的商品
break;
case 3:
intermediary.addProductToShoppingCar(buyer, seller);//把商品添加到购物车
break;
case 4:
intermediary.deleteProductFromShoppingCar(buyer,//删除购物车中的商品
seller);
break;
case 5:
intermediary.buyerBuyProductFromSeller(buyer,//用户购买此买商品
seller);
break;
case 6:
buyer = intermediary.getBuyer();//新用户
break;
case 7:
seller = intermediary.getSeller();//新商家
break;
default://系统退出
println("bye");
System.exit(0);
}
} }
}
}/** ***************************************Intermediary*************************************************** */
class Intermediary {// medium : deal interactive between Buyer and Seller
private int id;
private ArrayList<Seller> sellers = null;
private ArrayList<Buyer> buyers = null; private Intermediary(int id) {
this.id = id;
init();
} private Intermediary(ArrayList<Seller> sellers,
ArrayList<Buyer> buyers) {
this.sellers = sellers;
this.buyers = buyers;
init();
} private void init() {
if (sellers == null)
sellers = new ArrayList<Seller>();
if (buyers == null)
buyers = new ArrayList<Buyer>();
} public static Intermediary createIntermediary(int id) {
return new Intermediary(id);
} public static Intermediary createIntermediary(
ArrayList<Seller> sellers, ArrayList<Buyer> buyers) {
return new Intermediary(sellers, buyers);
} public boolean addSeller(Seller seller) {
return sellers.add(seller);
} public boolean addBuyer(Buyer buyer) {
return buyers.add(buyer);
} public void addProductToShoppingCar(Buyer buyer, Seller seller) {
Scanner scanner = new Scanner(System.in);
int productId = 0;
println("please input product id:");
seller.displayProductInStore();
productId = scanner.nextInt();
Product product = seller.getProduct(productId);
if (product != null)
buyer.add(product);
else
println("in store no exisit product id:");
} public boolean removeSeller(Seller seller) {
return sellers.remove(seller);
} public boolean removeBuyer(Buyer buyer) {
return buyers.remove(buyer);
} public void deleteProductFromShoppingCar(Buyer buyer,
Seller seller) {
Scanner scanner = new Scanner(System.in);
int productId = 0;
println("please input product id:");
buyer.displayProductInShoppingCar();
productId = scanner.nextInt();
Product product = buyer.getProduct((int) productId);
if (product != null)
buyer.remove(product);
else
println("in shopping car no exisit product id:"); } public Buyer getBuyer() {
Buyer buyer = null;
int buyerId = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
println("please input buyer id(1 to " + buyers.size() + ")");
buyerId = scanner.nextInt();
if (buyerId > buyers.size() || buyerId <= 0) {
println("error! resumeload");
} else {
buyer = buyers.get(buyerId - 1);
break;
}
}
return buyer;
} public Seller getSeller() {
Seller seller = null;
int sellerId = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
println("please input seller id(1 to " + sellers.size() + ")");
sellerId = scanner.nextInt();
if (sellerId > sellers.size() || sellerId <= 0) {
println("error! resumeload");
} else {
seller = sellers.get(sellerId - 1);
break;
}
}
return seller;
} public void hint() {
println("please input operation number");
println("1:display seller products");
println("2:display shopping car products");
println("3:add product to shopping car");
println("4:delete products from shopping car");
println("5:buy one product:");
println("6:switch buyer :");
println("7:switch seller:");
println("other:exit system:"); } public void displayBuyer() {// display buyer
System.out.println("the sellers that can be managed :");
for (Buyer buyer : buyers) {
System.out.println(buyer);
}
} public void displaySeller() {// display seller
System.out.println("the sellers that can be managed :");
for (Seller seller : sellers) {
System.out.println(seller);
}
} public boolean trade(Seller seller, Buyer buyer, Product product) {
if (!sellers.contains(seller))
return false;
if (!sellers.contains(seller))
return false;
if (!buyers.contains(buyer))
return false;
if (!buyers.contains(buyer))
return false;
if (!seller.remove(product))
return false;
if (seller.remove(product))
return false;
if (buyer.remove(product))
return false;
System.out.println("now " + buyer + " successfully buy " + product);
return true;
} public void buyerBuyProductFromSeller(Buyer buyer,
Seller seller) {
Scanner scanner = new Scanner(System.in);
println("please input product id:");
buyer.displayProductInShoppingCar();
id = scanner.nextInt();
System.out.println("input id = " + id);
Product product = buyer.getProduct((int) id);
if (product != null) {
buyer.setIntermediary(this);
buyer.setSeller(seller);
buyer.buy(product);
} else
println("in shopping car no exisit product id:");
} public ArrayList<Seller> getSellers() {
return sellers;
} public void setSellers(ArrayList<Seller> sellers) {
this.sellers = sellers;
} public ArrayList<Buyer> getBuyers() {
return buyers;
} public void setBuyers(ArrayList<Buyer> buyers) {
this.buyers = buyers;
} public void println(Object obj) {
System.out.println(obj);
} public void println() {
System.out.println();
} public String toString() {
return getClass().getSimpleName() + id;
}
}
/** ***************************************Seller*************************************************** */
class Seller implements Comparable<Seller> {
private int id;
private ArrayList<Product> products;
private Intermediary intermediary;// private Seller(int id) {
this.id = id;
init();
} private Seller(int id, ArrayList<Product> products,
Intermediary intermediary) {
this.id = id;
this.products = products;
this.intermediary = intermediary;
init();
} private void init() {
if (products == null)
products = new ArrayList<Product>();
if (intermediary == null)
intermediary = Intermediary.createIntermediary(0);
} public static Seller createSeller(int id) {
return new Seller(id);
} public static Seller createSeller(int id, ArrayList<Product> products,
Intermediary intermediary) {
return new Seller(id, products, intermediary);
} boolean add(Product product) {// new arrivals
return products.add(product);
} boolean remove(Product product) {// take out of
return products.remove(product);
} public Product getProduct(int id) {// display product
for (int i = 0; i < products.size(); i++) {
if (products.get(i).getId() == id)
return products.get(i);
}
return null;
} public void displayProductInStore() {// display product
for (int i = 0; i < products.size(); i++) {
System.out.println(products.get(i));
}
} public int compareTo(Seller seller) {
int temp = id > seller.id ? 1 : (id == seller.id ? 0 : -1);
return temp;
} public Intermediary getIntermediary() {
return intermediary;
} public void setIntermediary(Intermediary intermediary) {
this.intermediary = intermediary;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public ArrayList<Product> getProducts() {
return products;
} public void setProducts(ArrayList<Product> products) {
this.products = products;
} public String toString() {
return getClass().getSimpleName() + id;
}}/** ***************************************Buyer*************************************************** */class Buyer implements Comparable<Buyer> {
private int id;
private ArrayList<Product> products;
private Seller seller;//
private Intermediary intermediary;// private Buyer(int id) {
this.id = id;
init();
} private Buyer(int id, ArrayList<Product> products, Seller seller,
Intermediary intermediary) {
super();
this.id = id;
this.products = products;
this.seller = seller;
this.intermediary = intermediary;
init();
} private void init() {
if (products == null)
products = new ArrayList<Product>();
if (seller == null)
seller = Seller.createSeller(0);
if (intermediary == null)
intermediary = Intermediary.createIntermediary(0);
} public static Buyer createBuyer(int id) {
return new Buyer(id);
} public static Buyer createBuyer(int id, ArrayList<Product> products,
Seller seller, Intermediary intermediary) {
return new Buyer(id, products, seller, intermediary);
} public void add(int id, String name, String descreption) {// add
products.add(Product.createProduct(id, name, descreption));
} public void add(Product product) {// add seller's product to shoppincar
if (product != null)
products.add(product);
else
throw new UnsupportedOperationException();
} public boolean remove(int id) {// remove product from shopping car
for (int i = 0; i < products.size(); i++) {
if (products.get(i).getId() == id) {
products.remove(id);
return true;
}
}
return false;
} boolean remove(Product product) {// remove product from shopping car
return products.remove(product);
} public Product getProduct(int id) {
for (Product product : products) {
if (product.getId() == id)
return product;
}
return null;
} public void displayProductInShoppingCar() {// display products in`shopping
// car
for (Product product : products) {
System.out.println(product);
}
} public void displayProductFromSeller() {
seller.displayProductInStore();
} public void displaySellerFromIntermediary() {
intermediary.displaySeller();
} public void buy(Product product) {// buy thing
intermediary.trade(seller, this, product);
} public int compareTo(Buyer buyer) {
int temp = id > buyer.id ? 1 : (id == buyer.id ? 0 : -1);
return temp;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public Seller getSeller() {
return seller;
} public void setSeller(Seller seller) {
this.seller = seller;
} public Intermediary getIntermediary() {
return intermediary;
} public void setIntermediary(Intermediary intermediary) {
this.intermediary = intermediary;
} public ArrayList<Product> getProducts() {
return products;
} public void setProducts(ArrayList<Product> products) {
this.products = products;
} public String toString() {
return getClass().getSimpleName() + id;
}
}/** ***************************************Product*************************************************** */
class Product {
private final int id;// 每件商品ID是唯一的
private String name;
private String description; private Product(int id) {
super();
this.id = id;
init();
} private Product(int id, String name, String description) {
super();
this.id = id;
this.name = name;
this.description = description;
init();
} private void init() {
if (name == null)
name = "name" + id;
if (description == null)
description = "description" + id;
} public static Product createProduct(int id) {
return new Product(id);
} public static Product createProduct(int id, String name, String description) {
return new Product(id, name, description);
} public int getId() {
return id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
} public String toString() {
return getClass().getSimpleName() + id;
}
}
system start ..........
please input seller id(1 to 3)
2
please input buyer id(1 to 3)
3
please input operation number
1:display seller products
2:display shopping car products
3:add product to shopping car
4:delete products from shopping car
5:buy one product:
6:switch buyer :
7:switch seller:
other:exit system:
3
please input product id:
Product88
Product37
Product55
Product22
88
please input operation number
1:display seller products
2:display shopping car products
3:add product to shopping car
4:delete products from shopping car
5:buy one product:
6:switch buyer :
7:switch seller:
other:exit system:
4
please input product id:
Product88
88
please input operation number
1:display seller products
2:display shopping car products
3:add product to shopping car
4:delete products from shopping car
5:buy one product:
6:switch buyer :
7:switch seller:
other:exit system:
2
please input operation number
1:display seller products
2:display shopping car products
3:add product to shopping car
4:delete products from shopping car
5:buy one product:
6:switch buyer :
7:switch seller:
other:exit system:
3
please input product id:
Product88
Product37
Product55
Product22
37
please input operation number
1:display seller products
2:display shopping car products
3:add product to shopping car
4:delete products from shopping car
5:buy one product:
6:switch buyer :
7:switch seller:
other:exit system:
5
please input product id:
Product37
37
input id = 37
please input operation number
1:display seller products
2:display shopping car products
3:add product to shopping car
4:delete products from shopping car
5:buy one product:
6:switch buyer :
7:switch seller:
other:exit system:
1
Product88
Product55
Product22
please input operation number
1:display seller products
2:display shopping car products
3:add product to shopping car
4:delete products from shopping car
5:buy one product:
6:switch buyer :
7:switch seller:
other:exit system:
2
please input operation number
1:display seller products
2:display shopping car products
3:add product to shopping car
4:delete products from shopping car
5:buy one product:
6:switch buyer :
7:switch seller:
other:exit system:
*/