Java-based online shopping cart program and java-based online shopping cart

Source: Internet
Author: User

Java-based online shopping cart program and java-based online shopping cart

Use java code to write a simple online shopping cart program for your reference. The specific content is as follows:

Requirements:

1. Write a product category, which includes the product ID, product name, product category, and unit price.

2. Write a commodity item information class with two attributes: commodity and quantity, and the total price method of the commodity.

3. Write a shopping cart class, including adding a commodity method, viewing the order information, deleting a commodity, modifying the commodity, clearing the shopping cart, and calculating the total amount of all commodities in the shopping cart. 4. Write a test class and test the above method.

Product Type:

Public class Product {private int productId; // Product no. private String productName; // Product name private String category; // Product category private double price; // unit price public Product () {// super ();} public Product (int productId, String productName, String category, double price) {super (); this. productId = productId; this. productName = productName; this. category = category; this. price = price;} public String toString () {return "Product [productId =" + productId + ", productName =" + productName + ", category =" + category + ", price = "+ price +"] ";} public int getProductId () {return productId;} public void setProductId (int productId) {this. productId = productId;} public String getProductName () {return productName;} public void setProductName (String productName) {this. productName = productName;} public String getCategory () {return category;} public void setCategory (String category) {this. category = category;} public double getPrice () {return price;} public void setPrice (double price) {this. price = price ;}}

Item information:

Public class ProductItem {private Product product; // private int count of the purchased item; // number of items public double totalMoney () {// subtotal double price = product. getPrice (); // returns price * count;} public ProductItem () {super ();} public ProductItem (Product product, int count) {super (); this. product = product; this. count = count;} public Product getProduct () {return product;} public void setProduct (Product product) {this. product = product;} public int getCount () {return count;} public void setCount (int count) {this. count = count ;}}

Shopping cart type:

Import java. util. collection; import java. util. iterator; import java. util. linkedHashMap; import java. util. map; public class ShoppingCart {// shopping cart // key: item No. value: item entry private Map <Integer, ProductItem> map = new LinkedHashMap <Integer, ProductItem> (); public void addProduct (Product p) {// Add Product int productId = p. getProductId (); if (map. containsKey (productId) {ProductItem productItem = map. get (productId); productItem. setCount (productItem. getCount () + 1);} else {map. put (productId, new ProductItem (p, 1) ;}} public void showAll () {// view order information Collection <ProductItem> productItems = map. values (); Iterator <ProductItem> iterator = productItems. iterator (); while (iterator. hasNext () {ProductItem productItem = iterator. next (); Product product = productItem. getProduct (); System. out. println ("product No.:" + product. getProductId () + ", product name:" + product. getProductName () + ", unit price:" + product. getPrice () + ", quantity:" + productItem. getCount () + ", subtotal:" + productItem. totalMoney () ;}} public boolean deleteProduct (int productId) {// delete a product if (map. containsKey (productId) {map. remove (productId); return true;} return false;} public boolean modifyProduct (int productId, int count) {// modify if (map. containsKey (productId) {if (count> = 1) {ProductItem productItem = map. get (productId); productItem. setCount (count); return true;} else if (count = 0) {// Delete the eproduct (productId); return true ;}} return false ;} public void clearCart () {// clear the map of the shopping cart. clear ();} public double totalAllMoney () {// total item money count double total = 0; Collection <ProductItem> productItems = map. values (); Iterator <ProductItem> iterator = productItems. iterator (); while (iterator. hasNext () {ProductItem productItem = iterator. next (); double money = productItem. totalMoney (); total + = money;} return total ;}}

Test class:

Public class ShoppingCartTest {public static void main (String [] args) {ShoppingCart cart = new ShoppingCart (); Product p1 = new Product (101, "Asus Notebook", "Notebook ", 4599); Product p2 = new Product (102, "apple", "Fruit", 5.9); Product p3 = new Product (103, "Color TV", "Household Appliances ", 2799); Product p4 = new Product (104, "Autumn pants", "clothing", 128); Product p5 = new Product (105, " mobile phone", "Mobile Phone ", 2998); Product p6 = new Product (101, "Asus Notebook", "Notebook", 4599); // test the cart used to buy two products. a DdProduct (p1); cart. addProduct (p2); cart. addProduct (p3); cart. addProduct (p4); cart. addProduct (p5); cart. addProduct (p6); cart. showAll (); System. out. println ("###########"); boolean flag = cart. deleteProduct (p2.getProductId (); if (flag) {System. out. println ("product number:" + p2.getProductId () + "is deleted successfully! ");} Else {System. out. println ("failed to delete");} cart. showAll (); System. out. println ("###########"); boolean flag2 = cart. modifyProduct (p3.getProductId (), 2); if (flag2) {System. out. println ("the product number is:" + p3.getProductId () + "is successfully modified! ");} Else {System. out. println ("failed to modify");} cart. showAll (); // cart. clearCart (); // cart. showAll (); System. out. println ("total price:" + cart. totalAllMoney ());}}

Run:


The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.