Entity Class 1 (product):
Package Mall.model;
public class Goods {
private int shoppingid; Product number
Private String shoppingname;//Product name
private int price; Commodity 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;
}
}
Entity Class 2 (Shopping cart)
Package Mall.model;
public class ShoppingCart {
private int shoppingid; Product number
Private String Shoppingname; Product Name
private int price; Commodity 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;
}
}
Entity Class 3 (user)
Package Mall.model;
/**
* @author shixi1809 User class
*/
public class User {
Private String UserName; User name
Private String PassWord; User password
private int type; User type, 1 admins, 2 members
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 = "Administrator";
} else if (getType () = = 2) {
TTT = "Member";
}
System.out.println ("Username:" + getusername () + ", Password:" + GetPassword ()
+ ", User type:" + TTT);
}
}
Test class
Package Mall.controller;
Import Mall.function.service;
public class Test {
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN ("Please login:");
Service service = new service ();
Service.login ();
}
}
function implementation
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; Logged-on User 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 ();
}
}
Login
@SuppressWarnings ("resource")
public void Login () {
Add two values to the user, 1 administrators, 2 users
USER[0] = new User ("admin", "admin", 1);
USER[1] = new User ("Zhangsan", "Zhangsan", 2);
Scanner scanner1 = new Scanner (system.in);
System.out.println ("Please enter user name:");
String str1 = Scanner1.next ();
Scanner scanner2 = new Scanner (system.in);
System.out.println ("Please enter password:");
String str2 = Scanner2.next ();
for (int i = 0; i < user.length; i++) {
if (user[i]! = null) {
if (Str1.equals (User[i].getusername ())
&& str2.equals (User[i].getpassword ())) {
if (user[i].gettype () = = 1) {
System.out.println ("Welcome to the Admin Interface!! ");
while (true) {
System.out
. println ("1. Add the item \ t 2. Delete the item \ t 3. Modify the item \ t 4. Product display \ t 5. Switch user \ t 0. Exit system");
Scanner Scanner = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Please select function:");
int sel = Scanner.nextint ();
Switch (SEL) {
Case 1:
Add ();
Break
Case 2:
Scanner scan1 = new Scanner (system.in);
System.out.println ("Please enter the item number to be deleted:");
int sttr1 = Scan1.nextint ();
System.out.println ("Are you sure you want to delete this item?") ");
Scanner scan2 = new Scanner (system.in);
System.out
. println ("Please enter the product name (confirm Please enter ' y ', cancel Please enter ' n '):");
String sttr2 = Scan2.next ();
if (sttr2.equals ("Y")) {
Remove (STTR1);
} else if (Sttr2.equals ("n")) {
Break
} else {
SYSTEM.OUT.PRINTLN ("Wrong input! ");
}
Break
Case 3:
Scanner scan3 = new Scanner (system.in);
System.out.println ("Please enter the product number:");
int STRR = Scan3.nextint ();
Modify (STRR);
Break
Case 4:
Show ();
Break
Case 5:
Login ();
Break
Case 0:
System.exit (0);
Break
Default
SYSTEM.OUT.PRINTLN ("Wrong input! ");
Break
}
}
} else {
System.out.println ("Welcome to the user interface!! ");
num = i; Logged-on User ID
SYSTEM.OUT.PRINTLN (num);
while (true) {
System.out
. println ("1. Buy goods \ t 2. Product search \ t 3. Product display \ t 4. Switch user \ t 0. Exit system");
Scanner Scanner = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Please select function:");
int sel = Scanner.nextint ();
Switch (SEL) {
Case 1:
Scanner scannerbuy = new Scanner (system.in);
System.out.println ("Please enter the product number:");
int buyno = Scannerbuy.nextint ();
Buy (Buyno);
Break
Case 2:
Scanner Sele = new Scanner (system.in);
System.out.println ("Please enter product Name:");
String selname = Sele.next ();
Select (Selname);
Break
Case 3:
Show ();
Break
Case 4:
Login ();
Break
Case 0:
System.exit (0);
Break
Default
SYSTEM.OUT.PRINTLN ("Wrong input! ");
Break
}
}
}
}
}
}
System.out.println ("username and password do not match, please re-enter!") ");
}
Add to Shopping Cart
@SuppressWarnings ("resource")
public void Cartadd (int no) {
for (int i = 0; i < cart.length; i++) {
if (cart[i].getshoppingid () = = 0) {
Cart[i] = new ShoppingCart (Good[no].getshoppingid (),
Good[no].getshoppingname (), Good[no].getprice ());
SYSTEM.OUT.PRINTLN ("Shopping cart added successfully! ");
SYSTEM.OUT.PRINTLN ("Please choose to continue shopping or go to the shopping cart: 1. Continue shopping \ t 2. Enter the shopping cart");
Scanner scanner1 = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Please select:");
int str1 = Scanner1.nextint ();
Switch (STR1) {
Case 1:
Show ();
Break
Case 2:
Cartshow ();
Break
Default
SYSTEM.OUT.PRINTLN ("Wrong input!! ");
Break
}
Break
}
}
}
Search Products
@SuppressWarnings ("resource")
public void Select (String name) {
for (int i = 0; i < good.length; i++) {
if (good[i].getshoppingname () = null
&& good[i].getshoppingname ()! = "") {
if (Good[i].getshoppingname (). INDEXOF (name)! =-1) {
for (int j = 0; J < Good.length; J + +) {
if (Good[i].getshoppingname (). Equals (
Good[j].getshoppingname ())) {
SYSTEM.OUT.PRINTLN ("Product Code:"
+ good[i].getshoppingid () + ", Product Name:"
+ good[i].getshoppingname () + ", Price:"
+ Good[i].getprice ());
System.out
. println ("Please select purchase item or add to Cart: 1. Buy this item \ t 2. Add to cart \ t 3. Continue shopping \ t 4. Go to Cart");
Scanner scanner1 = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Please select:");
int str1 = Scanner1.nextint ();
Switch (STR1) {
Case 1:
Buy (j);
Break
Case 2:
Cartadd (j);
Break
Case 3:
Show ();
Break
Case 4:
Cartshow ();
Break
Default
SYSTEM.OUT.PRINTLN ("Wrong input!! ");
Break
}
}
}
}
}
}
}
//Purchase goods
@SuppressWarnings ("resource")
public void Buy (int no) {
for (int i = 0; i < good.length; i++) {
if (no = = Good[i].getshoppingid ()) {
System.out.println ("Product Code:" + good[i].getshoppingid () + ", Product Name:"
+ good[i] . Getshoppingname () + ", Price:"
+ Good[i].getprice ());
System.out.println ("Do you want to confirm the purchase (reply to y/n)?" ");
Scanner scan2 = new Scanner (system.in);
String sttr2 = Scan2.next ();
if (sttr2.equals ("Y")) {
System.out.println ("The product was purchased successfully! ");
} else if (Sttr2.equals ("n")) {
System.out.println ("Product purchase failed! ");
} else {
System.out.println ("wrong input! ");
}
}
}
}
//Add Item
@SuppressWarnings ("resource")
public void Add () {
Scanner scanner1 = new Scanner (system.in);
S Ystem.out.println ("Please enter the product number:");
int str1 = Scanner1.nextint ();
Scanner scanner2 = new Scanner (system.in);
System.out.println ("Please enter the product name:");
String str2 = Scanner2.next ();
Scanner scanner3 = new Scanner (system.in);
System.out.println ("Please enter the product price:");
int str3 = Scanner3.nextint ();
for (int i = 0; i < good.length; i++) {
if (good[i].getshoppingid ()! = 0) {
if (good[i].getshoppingid () = = STR1) {
System.out.println ("The Product already exists!!! ");
return;
}
}
}
for (int i = 0; i < good.length; i++) {
if (good[i].getshoppingid () = = 0) {
Good[i] = NE W Goods (str1, str2, STR3);
System.out.println ("product added successfully! ");
break;
}
}
}
Remove a product
public void Remove (int no) {
for (int i = 0; i < good.length; i++) {
if (Good[i].getshoppingid ()! = 0) {
if (no = = Good[i].getshoppingid ()) {
Good[i] = new Goods ();
SYSTEM.OUT.PRINTLN ("The product has been removed! ");
} else {
System.out.println ("There is no such product!" ");
}
}
}
}
Product modification
@SuppressWarnings ("resource")
public void Modify (int no) {
for (int i = 0; i < good.length; i++) {
if (Good[i].getshoppingid ()! = 0) {
if (no = = Good[i].getshoppingid ()) {
Scanner scanner2 = new Scanner (system.in);
System.out.println ("Please enter product Name:");
String str2 = Scanner2.next ();
Scanner Scanner3 = new Scanner (system.in);
System.out.println ("Please enter the product price:");
int str3 = Scanner3.nextint ();
Good[i] = new Goods (no, str2, STR3);
SYSTEM.OUT.PRINTLN ("The product has been modified ... ");
Return
}
}
}
SYSTEM.OUT.PRINTLN ("Product not found ... ");
}
Show Products
public void Show () {
for (int i = 0; i < good.length; i++) {
if (Good[i].getshoppingid ()! = 0) {
SYSTEM.OUT.PRINTLN ("Product Code:" + good[i].getshoppingid () + ", Product Name:"
+ good[i].getshoppingname () + ", Price:"
+ Good[i].getprice ());
}
}
}
Go to Shopping cart
public void Cartshow () {
System.out.println (User[num].getusername () + "Shopping Cart:");
for (int i = 0; i < cart.length; i++) {
if (Cart[i].getshoppingid ()! = 0) {
SYSTEM.OUT.PRINTLN ("Product Code:" + cart[i].getshoppingid () + ", Product Name:"
+ cart[i].getshoppingname () + ", Price:"
+ Cart[i].getprice ());
}
}
}
}
Simulating the function and realization of shopping mall with Java array