更新時(shí)間:2022-09-26 10:15:42 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1222次
使用您喜歡的任何名稱(chēng)創(chuàng)建一個(gè)新的 Java 項(xiàng)目。
在您的 src 文件夾下創(chuàng)建以下包,
收藏品.shoppingcart
為簡(jiǎn)單起見(jiàn),所有代碼都將駐留在此包中。
產(chǎn)品.java
Products.java
購(gòu)物車(chē).java
UI.java
主.java
對(duì)于這個(gè)示例項(xiàng)目,確保所有上述類(lèi)都位于同一個(gè)包下,即collections.shoppingcart。
將class一一解釋它的意義。
通過(guò)下面的代碼,
package collections.shoppingcart;
import java.util.Objects;
/**
*
* @author Varun Shrivastava (vslala)
*/
class Product {
private Integer pid;
private String name;
private Double price;
private Integer stock;
public Product () {
}
public Product (Integer pid, String name, Double price, Integer stock) {
this.pid = pid;
this.name = name;
this.price = price;
this.stock = stock;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the price
*/
public Double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(Double price) {
this.price = price;
}
/**
* @return the stock
*/
public Integer getStock() {
return stock;
}
/**
* @param stock the stock to set
*/
public void setStock(Integer stock) {
this.stock = stock;
}
/**
* @return the pid
*/
public Integer getPid() {
return pid;
}
@Override
public int hashCode() {
int hash = 7;
hash = 29 * hash + Objects.hashCode(this.pid);
hash = 29 * hash + Objects.hashCode(this.name);
hash = 29 * hash + Objects.hashCode(this.price);
hash = 29 * hash + Objects.hashCode(this.stock);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Product other = (Product) obj;
if (!Objects.equals(this.name, other.name)) {
return false;
}
if (!Objects.equals(this.pid, other.pid)) {
return false;
}
if (!Objects.equals(this.price, other.price)) {
return false;
}
if (!Objects.equals(this.stock, other.stock)) {
return false;
}
return true;
}
/**
* @param pid the pid to set
*/
public void setPid(Integer pid) {
this.pid = pid;
}
}
這是一個(gè)包含Product屬性并為其提供 setter 和 getter 的具體類(lèi)。還覆蓋了 Hash 和 Equals 方法。
equals()方法用于根據(jù)屬性比較兩個(gè)對(duì)象。
hashCode()是在創(chuàng)建對(duì)象時(shí)附加到每個(gè)對(duì)象的唯一哈希/數(shù)字。
因此,每當(dāng)比較兩個(gè)對(duì)象時(shí)。比較它們的哈希碼和屬性。如果兩者(哈希碼和屬性值)相同,則認(rèn)為對(duì)象相等,否則不相等。
因此,重寫(xiě)對(duì)象的 hascode() 和 equals() 方法非常重要。
通過(guò)下面的代碼,
package collections.shoppingcart;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Varun Shrivastava (vslala)
*/
public class Products {
private final List<Product> products = new ArrayList<Product>();
public Products () {
this.initStoreItems();
}
public List<Product> getProducts() {
return products;
}
public void initStoreItems() {
String [] productNames = {"Lux Soap", "Fair n Lovely", "MTR"};
Double [] productPrice = {40.00d, 60.00d, 30.00d};
Integer [] stock = {10, 6, 10};
for (int i=0; i < productNames.length; i++) {
this.products.add(new Product(i+1, productNames[i], productPrice[i], stock[i]));
}
}
}
此類(lèi)在您的購(gòu)物車(chē)應(yīng)用程序中的作用是為您提供商店產(chǎn)品。
換句話(huà)說(shuō),這個(gè)類(lèi)用于在應(yīng)用程序啟動(dòng)后立即初始化您的商店。在實(shí)際應(yīng)用中,項(xiàng)目將從用戶(hù)會(huì)話(huà)或數(shù)據(jù)庫(kù)中檢索。為簡(jiǎn)單起見(jiàn),使用 Products 類(lèi)來(lái)初始化對(duì)象。
如您所見(jiàn),該initStoreItems()方法用于將產(chǎn)品添加到Product類(lèi)型的新ArrayList 中 。
你已經(jīng)成功了一半。
package collections.shoppingcart;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Varun Shrivastava
*/
class Cart {
List<Product> cartItems = new ArrayList<Product>();
public void addProductToCartByPID(int pid) {
Product product = getProductByProductID(pid);
addToCart(product);
}
private Product getProductByProductID(int pid) {
Product product = null;
List<Product> products = new Products().getProducts();
for (Product prod: products) {
if (prod.getPid() == pid) {
product = prod;
break;
}
}
return product;
}
private void addToCart(Product product) {
cartItems.add(product);
}
public void removeProductByPID(int pid) {
Product prod = getProductByProductID(pid);
cartItems.remove(prod);
}
void printCartItems() {
for (Product prod: cartItems) {
System.out.println(prod.getName());
}
}
}
這是一個(gè)具體的類(lèi),它充當(dāng)虛擬購(gòu)物車(chē)來(lái)臨時(shí)存儲(chǔ)物品。
它提供了購(gòu)物車(chē)應(yīng)具有的所有必需操作,例如:
添加到購(gòu)物車(chē)()
removeFromCart()
getProduct(), 等等……
它還維護(hù)一個(gè)具有 Product 類(lèi)型的cartItems 列表,類(lèi)似于 products 類(lèi)中的產(chǎn)品列表。
list的唯一目的cartItems是將購(gòu)買(mǎi)的商品存儲(chǔ)在購(gòu)物車(chē)中或從購(gòu)物車(chē)中刪除商品。它只是維護(hù)用戶(hù)添加到購(gòu)物車(chē)的項(xiàng)目列表。
在 Web 應(yīng)用程序中,您將擁有一個(gè)漂亮的 HTML 頁(yè)面,并支持 Javascript。因此,用戶(hù)只需查找產(chǎn)品并單擊“添加到購(gòu)物車(chē)” 按鈕即可。
在控制臺(tái)應(yīng)用程序中(比如這個(gè)),UI 部分變得有點(diǎn)棘手。但是,有一個(gè)很酷的方法可以讓它變得簡(jiǎn)單。
看:
在控制臺(tái)中,用戶(hù)一次只能輸入一個(gè)輸入。這是關(guān)鍵概念,可用于簡(jiǎn)化 UI。
人們經(jīng)常為不同的輸入取不同的輸入變量。如果要處理大量輸入,這可能會(huì)令人頭疼。
這就是為什么最簡(jiǎn)單的方法是使用單個(gè)輸入變量并在填充后立即處理它。
在這個(gè)應(yīng)用程序中,使用了一個(gè)類(lèi)變量來(lái)獲取用戶(hù)輸入。并根據(jù)輸入值執(zhí)行相應(yīng)的過(guò)程。
瀏覽下面的代碼并理解這個(gè)概念。
package collections.shoppingcart;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Varun Shrivastava (vslala)
*/
public class UI {
Cart cart = new Cart();
private int ch = 0;
public UI () {
menu();
}
public void startScreen () {
System.out.println("1. Display Store Products");
System.out.println("2. Display Cart");
System.out.println("0. Exit");
}
public void storeProductsMenu() {
System.out.println("1. Add to Cart");
System.out.println("2. Remove From Cart");
System.out.println("0. Exit");
}
public void menu () {
do {
startScreen();
getUserInput();
switch (ch) {
case 1:
displayStoreProducts();
storeProductsMenu();
getUserInput();
innerChoice1();
break;
case 2:
showCart();
break;
case 0:
System.exit(0);
break;
default:
break;
}
} while (ch != 0);
}
private void innerChoice1() {
switch (ch) {
case 1:
addProductToCart();
showCart();
break;
case 2:
removeProductFromCart();
break;
default:
break;
}
}
private int getUserInput() throws NumberFormatException {
Scanner in = new Scanner (System.in);
ch = Integer.parseInt(in.nextLine());
return ch;
}
private void displayStoreProducts() {
List<Product> products = new Products().getProducts();
for (Product prod: products) {
System.out.println(
prod.getPid() + "- " +
prod.getName() + " " +
prod.getPrice() + " " +
prod.getStock()
);
}
}
private void addProductToCart() {
int pid = getUserInput();
cart.addProductToCartByPID(pid);
}
private void showCart() {
cart.printCartItems();
}
private void removeProductFromCart() {
int pid = getUserInput();
cart.removeProductByPID(pid);
}
}
這是用戶(hù)和應(yīng)用程序之間發(fā)生主要交互的類(lèi)。
它是應(yīng)用程序的控制點(diǎn)。
您在控制臺(tái)中看到的所有內(nèi)容都是此頁(yè)面的結(jié)果。
它接受用戶(hù)輸入并從您之前創(chuàng)建的類(lèi)中調(diào)用相應(yīng)的方法來(lái)執(zhí)行所需的任務(wù)。
/*
* Create a fully functional program to store and delete objects from the cart
*/
package collections.shoppingcart;
/**
*
* @author Varun Shrivastava
*/
public class Main {
public static void main (String [] args) {
new UI();
}
}
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話(huà)與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743