实体类1(商品):
package mall.model;
public class goods {
private int shoppingID; // 商品编号
private String shoppingName;// 商品名
private int price; // 商品价格
public goods(int shoppingID, String shoppingName, int price) {
this.shoppingID = shoppingID;
this.shoppingName = shoppingName;
this.price = price;
}
public goods() {
super();
}
public int getShoppingID() {
return shoppingID;
}
public void setShoppingID(int shoppingID) {
this.shoppingID = shoppingID;
}
public String getShoppingName() {
return shoppingName;
}
public void setShoppingName(String shoppingName) {
this.shoppingName = shoppingName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
实体类2(购物车)
package mall.model;
public class shoppingCart {
private int shoppingID; // 商品编号
private String shoppingName; // 商品名
private int price; // 商品价格
public shoppingCart() {
super();
}
public shoppingCart(int shoppingID, String shoppingName, int price) {
super();
this.shoppingID = shoppingID;
this.shoppingName = shoppingName;
this.price = price;
}
public int getShoppingID() {
return shoppingID;
}
public void setShoppingID(int shoppingID) {
this.shoppingID = shoppingID;
}
public String getShoppingName() {
return shoppingName;
}
public void setShoppingName(String shoppingName) {
this.shoppingName = shoppingName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
实体类3(用户)
package mall.model;
/**
* @author shixi1809 用户类
*/
public class user {
private String userName; // 用户名
private String passWord; // 用户密码
private int type; // 用户类型,1位管理员,2为会员
public user(String userName, String passWord, int type) {
super();
this.userName = userName;
this.passWord = passWord;
this.type = type;
}
public user() {
super();
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public void show1() {
String ttt = null;
if (getType() == 1) {
ttt = "管理员";
} else if (getType() == 2) {
ttt = "会员";
}
System.out.println("用户名:" + getUserName() + ",密码:" + getPassWord()
+ ",用户类型:" + ttt);
}
}
测试类
package mall.controller;
import mall.function.service;
public class test {
public static void main(String[] args) {
System.out.println("请登录:");
service service = new service();
service.login();
}
}
功能实现
package mall.function;
import java.util.Scanner;
import mall.model.goods;
import mall.model.shoppingCart;
import mall.model.user;
public class service {
private static user user[] = new user[5];
private static goods good[] = new goods[5];
private static shoppingCart cart[] = new shoppingCart[5];
private static Integer num; // 登录的用户id
static {
for (int i = 0; i < good.length; i++) {
good[i] = new goods();
}
for (int i = 0; i < good.length; i++) {
cart[i] = new shoppingCart();
}
}
// 登录
@SuppressWarnings("resource")
public void login() {
// 给用户添加两个值,1为管理员,2位用户
user[0] = new user("admin", "admin", 1);
user[1] = new user("zhangsan", "zhangsan", 2);
Scanner scanner1 = new Scanner(System.in);
System.out.println("请输入用户名:");
String str1 = scanner1.next();
Scanner scanner