2 Replies Latest reply: Mar 18, 2013 4:01 PM by 992059 RSS

    Java shopping basket

    992059
      I've started working on my uni assignment - an application that should be able to enable users to add items to a basket, calculate the amount spent, and apply any discounts that are relevant. I'm new to java and I am a bit confused and don't know where exactly should I start. I do know the basics. I was thinking of creating 3-4 classes. Please note i want to make the whole thing as simple as possible.

      1. An item catalogue class whith a list of items and description which we could add to the basket.
      2. A container class of shopping basket items.
      3. A class which could move items from the catalog list class to the basket class, this class could also apply discounts to items and add delivery charges etc.

      I just wanted to make sure i am on the right track at the moment. What do you think?
        • 1. Re: Java shopping basket
          gimbal2
          I don't even need to look at your proposed solution to know that it will work. You know why? Because I'm assuming you thought about it. What this post represents is a minor case of fearing to fail if you ask me. Why don't you go ahead and start? If you make a mistake, correct it. If you get stuck, present code and ask for more specific help here.

          A shopping cart is quite trivial in design and you've already seen them a million times. I wouldn't expect much trouble if I were you.
          • 2. Re: Java shopping basket
            992059
            Ok I ended up with a totally different structure of classes.
            This is what I have at the moment:

            --------------------------------------------------------------------------------------------------------------------------
            NewJavaBasket.java:

            package newjavabasket;
            import java.util.Scanner;

            public class NewJavaBasket {

            public static void main(String[] args) {
            // TODO code application logic
            //this asks user to choose a group they belong to
            System.out.println("Choose which group you belong to?");
            //Scanner reads user inputs
            Scanner input = new Scanner(System.in);
            Customers myCustomer = new Customers();
            String[] CustomerGroup = myCustomer.getCustomerGroup();
            FoodProducts myFood = new FoodProducts();
            TechnicalProducts myTechnicalProduct = new TechnicalProducts();
            OtherProducts myOtherProducts = new OtherProducts();

            String[] foodlistnames = myFood.getProductName();
            double[] foodlistprice = myFood.getProductPrice();

            String[] technicallistnames = myTechnicalProduct.getTProductName();
            double[] technicallistprice = myTechnicalProduct.getTProductPrice();

            String[] otherlistnames = myOtherProducts.getOtherProductName();
            double[] otherlistprice = myOtherProducts.getOtherProductPrice();

            /*Variable choice will be used to choose between groups of customers
            *each group will have a specific discount applied on some product types
            */
            int choice;
            for(int b = 0; b < CustomerGroup.length; b++){
            System.out.println(CustomerGroup);
            }
            choice = input.nextInt();
            System.out.println("Choose which type of product you want to buy?");
            {
            Products myProduct = new Products();
            String[] Products = myProduct.getProductType();

            for(int a = 0; a < Products.length; a++){
            System.out.println(Products[a]);
            }
            //Variable productchoice is used to choose between types of products
            int productchoice;
            productchoice = input.nextInt();
            System.out.println("Please choose one of the following products: ");


            if (productchoice == 0){ 
            {  
            for(int c = 0; c < foodlistnames.length; c++){
            System.out.print(foodlistnames[c]);
            System.out.println(foodlistprice[c]);
            }
            }
            //variable itemchoice is used to select an item
            int itemchoice;
            itemchoice = input.nextInt();

            //quantity is used to select quantity of items
            System.out.println("Please enter a quantity");
            int quantity = input.nextInt();
            //Total is used to calculate the amount that will be spent on that item
            double total = (Double.valueOf(foodlistprice[itemchoice])* quantity);
            System.out.print("You have added " +quantity);
            System.out.print(" Of item ");
            System.out.print((foodlistnames[itemchoice]));
            System.out.println(" You will pay " +total);
            }

            else if (productchoice == 1){      
            {  
            for(int d = 0; d < technicallistnames.length; d++){
            System.out.print(technicallistnames[d]);
            System.out.println(technicallistprice[d]);
            }
            }

            int itemchoice;
            itemchoice = input.nextInt();
            System.out.println("Please enter a quantity");

            int quantity = input.nextInt();
            double total = (Double.valueOf(technicallistprice[itemchoice])* quantity);
            System.out.print("You have added " +quantity);
            System.out.print(" Of item ");
            System.out.print((technicallistnames[itemchoice]));
            System.out.println(" You will pay " +total);
            }

            else {   
            for(int f = 0; f < otherlistnames.length; f++){
            System.out.print(otherlistnames[f]);
            System.out.println(otherlistprice[f]);
            }
            int itemchoice;
            itemchoice = input.nextInt();
            System.out.println("Please enter a quantity");

            int quantity = input.nextInt();
            double total = (Double.valueOf(otherlistprice[itemchoice])* quantity);
            System.out.print("You have added " +quantity);
            System.out.print(" Of item ");
            System.out.print((otherlistnames[itemchoice]));
            System.out.println(" You will pay " +total);
            }
            }
            }
            }

            --------------------------------------------------------------------------------------------------------------------------
            Customers.java:

            /* This is a Customers class which contains
            * getCustomerGroup method
            * and a string containing 3 groups of customers
            */
            package newjavabasket;

            public class Customers {  

            public String[] getCustomerGroup(){

            String[] CustomerGroup = new String[3];


            CustomerGroup[0] = "0.Staff ";
            CustomerGroup[1] = "1.Credit card customer ";
            CustomerGroup[2] = "2.Other Customers ";

            return CustomerGroup;
            }
            }

            --------------------------------------------------------------------------------------------------------------------------
            Products.java:

            /* This is a Products class which contains
            * getProductType method
            * Products string containing 3 product types
            */
            package newjavabasket;

            public class Products {   

            public String[] getProductType(){

            String[] Products = new String[3];


            Products[0] = "0.Food ";
            Products[1] = "1.Technical ";
            Products[2] = "2.Other Product ";

            return Products;
            }
            }

            --------------------------------------------------------------------------------------------------------------------------
            FoodProducts.java :

            /* This is a FoodProducts class which contains
            * 3 methods getProductName, GetProductPrice, getTquantity
            * this class also contains food products prices names and quantity
            */
            package newjavabasket;

            public class FoodProducts
            {

            double[] productPrice = {5,10,20};
            String[] productName = {"0.Sandwich ", "1.Pizza ", "2.Fruits "};
            int productQuantity;


            public String[] getProductName() {
            return productName;
            }

            public double[] getProductPrice() {
            return productPrice;
            }
            public int getTquantity() {
            return productQuantity;
            }

            }

            --------------------------------------------------------------------------------------------------------------------------
            OtherProducts.java

            /* This is an OtherProducts class which contains
            * 3 methods getOtherProductName, GetOtherProductPrice, getTquantity
            * this class also contains other products prices names and quantity
            */
            package newjavabasket;

            public class OtherProducts {  
            // attributes
            double[] productPrice = {10,25,100};
            String[] productName = {"0.T-Shirt ", "1.Hamster ", "2.Tent "};
            int productQuantity;

            public String[] getOtherProductName() {
            return productName;
            }

            public double[] getOtherProductPrice() {
            return productPrice;
            }

            public int getTquantity() {
            return productQuantity;
            }

            }

            --------------------------------------------------------------------------------------------------------------------------
            TechnicalProducts.java

            /* This is a TechnicalProducts class which contains
            * 3 methods getTProductName, GetTProductPrice, getTquantity
            * this class also contains technical products prices names and quantity
            */
            package newjavabasket;

            public class TechnicalProducts {

            double[] productPrice = {9000,200,400};
            String[] productName = {"0.Car ", "1.Mobile phone ", "2.Laptop "};
            int productQuantity;

            public String[] getTProductName() {
            return productName;
            }

            public double[] getTProductPrice() {
            return productPrice;
            }

            public int getTquantity() {
            return productQuantity;
            }

            }
            --------------------------------------------------------------------------------------------------------------------------
            What I need is to have a specific discount applied to every group of customers depending on what type of products they are getting.
            -------------     Technical Food Other
            Staff     15%     20% 25%
            Credit Card Customers     0%     0% 5%
            Customers     0%     0% 0%

            Also I need to apply a delivery charge if total item cost is over 30. Any ideas how should I do that?

            Edited by: 989056 on Mar 18, 2013 9:00 PM