Search code examples
javalinked-listshopping-cartcartkiosk

Adding multiple items using linkedlist, JAVA


This is the Question:

Assume that card has a valid PIN to start, and that there is 100 units in the bank account. Each video has a rental ‘cost’. You will need to scan the Basket list and calculate the total for the ATM transaction. You can assume there is always enough units in the bank account to cover the cost of the rental for this exercise. A version of Video Kiosk code will contain the following steps:

• Create Linked List for Video Titles and fill with choice of 5 items

• Create an empty Linked List (Basket) to store the customer selection

• Present a menu driven selection process

  • Display on screen itemized with a selection number
  • Identify the selection and determine if it is valid
  • Put selected video into 2nd linked list (Basket)
  • Remove the previously selected video from the available Title list
  • Complete selection process

    Bring the contents of the basket list to the ATM and pay

Vkiosk.java

    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.Scanner;

    public class VKiosk {
        private static LinkedList VTable=new LinkedList();
        private static LinkedList Basket=new LinkedList();
        private static double rentCost=0;
        private static int j=1;
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
             // declaring scanner name as "typeNumber"
        Scanner typeNumber = new Scanner(System.in);    
        System.out.println("::: Welcome to My Java Program To model an ATM machine :::");

            showVideoList();
            System.out.print("Input the serial number of video to buy: ");
            buyVideo();
            System.out.println("Your total amount of Cost: "+ rentCost);

            MMachine buy = new MMachine();
            buy.Txn(rentCost);



        }
        public static double buyVideo(){
            Scanner typeNumber = new Scanner(System.in);
            String title=typeNumber.nextLine();
            String amount=null;
            for(int i=0;i<VTable.size();i++){
                String videoDetails=VTable.get(i).toString();

                if(videoDetails.toLowerCase().contains(title.toLowerCase())){
                    Basket.add(VTable.get(i));
                   for(int j=videoDetails.length()-2;j>=0;j--){
                       if(videoDetails.charAt(j)==' '){
                        break;
                       }
                        amount=videoDetails.substring(j, videoDetails.length()-1);

                   }
                    VTable.remove(i);
                }

            }
            rentCost=Double.parseDouble(amount);

            return rentCost;
        }


        public static void VideoList(){
           Video vTable1=new Video("BladeRunner", 1, 5);
           Video vTable2=new Video("Wages of Fear", 2, 4);
           Video vTable3=new Video("Grease", 3, 5);
           Video vTable4=new Video("Mama Mia", 4, 4);
           Video vTable5=new Video("L'Illusionniste", 5, 6);
           VTable.add(vTable1);
           VTable.add(vTable2);
           VTable.add(vTable3);
           VTable.add(vTable4);
           VTable.add(vTable5);
        }
        public static void showVideoList(){
                System.out.println();
                System.out.println("********************************************************************");
                System.out.println("List of the Videos are: ");
                System.out.println("********************************************************************");
                System.out.println("Serial No       Video Detetails");
                VideoList();

                for(int i=0; i<VTable.size(); i++){
                    System.out.println( j+"               "+VTable.get(i));
                    j++;
                }
                System.out.println();
        }


    }

Video.java

public class Video{
    private String  title;
    private int     serial;
    private double  cost;

    public Video(String title, int serial, double cost)
    {
        this.title  = title;
        this.serial = serial;
        this.cost   = cost;
    }

    public String getTitle()  {return title;}
    public int    getSerial() {return serial;}
    public double  getCost()   {return cost;}


    public String getVideo() {
    return "title:" + title + " Serial: " + serial + " Cost: " + cost;
  }
    //  Upgrade output of toString ()

    @Override
    public String toString() {
        return "["+getVideo()+"]";
  }
}

I am succeed to buy only one items, need to buy multiple items through Linked List Basket.


Solution

  • The reason there is only a single purchase is that you do not have a loop to attempt to purchase (or rent; the nomenclature is a bit confused) additional selections. There are also some static class variables that make the procedure a bit more complex. Here are some suggestions.

    Main Method
    Add a loop to collect additional input. In addition, remove the static double rentCost and move to the main method (also, change the buyVideo to return the cost rather than updating the instance variable). Add a method to continue to collect input.

    private static boolean purchaseAnother(Scanner stdin)
    {
        while (true) {
            System.out.println();
            System.out.println("Purchase another (y/n): ");
            String chk = stdin.nextLine();
    
            if (chk != null && chk.trim().length() > 0) {
                if (chk.toLowerCase().equals("y")) { return true; }
                if (chk.toLowerCase().equals("n")) { return false; }
            }
        }
    }
    
    
    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args)
    {
        // declaring scanner name as "typeNumber"
        Scanner typeNumber = new Scanner(System.in);
        System.out.println(
                "::: Welcome to My Java Program To model an ATM machine :::");
    
        boolean buyMore = true;
    
        // **ADD: initialize the video list only once
        VideoList();
    
        // **USE LOCAL VARIABLE FOR COST
        double rentCost = 0.0d;
    
    
        while (buyMore) {
            showVideoList();
            System.out.print("Input the serial number of video to buy: ");
            //**ACCUMULATE THE TOTAL COST
            rentCost += buyVideo();
            System.out.printf("Current total $%.2f", rentCost);
    
            // SEE IF WISH TO KEEP GOING IF THERE IS ANYTHING LEFT TO RENT
            buyMore = (! VTable.isEmpty() && purchaseAnother(typeNumber));
        }
    
        // actually make a purchase
        // ADD STATEMENT, but this might be in the MMachine class; unknown
        System.out.printf("Charging $%.2f to the Debit Card%n", rentCost);
        MMachine buy = new MMachine();
        buy.Txn(rentCost);
    
    
        System.out.println("Have a nice day!");
    }
    

    BuyVideo method
    Change to return the rental amount; do not update an instance variable

    public static double buyVideo()
    {
        Scanner typeNumber = new Scanner(System.in);
        String title = typeNumber.nextLine();
        String amount = null;
        for (int i = 0; i < VTable.size(); i++) {
            String videoDetails = VTable.get(i).toString();
    
            if (videoDetails.toLowerCase().contains(title.toLowerCase())) {
                Basket.add(VTable.get(i));
                for (int j = videoDetails.length() - 2; j >= 0; j--) {
                    if (videoDetails.charAt(j) == ' ') {
                        break;
                    }
                    amount = videoDetails.substring(j,
                            videoDetails.length() - 1);
    
                }
    
                // ** ADD LINE TO INDICATE ACTION
                System.out.println("Purchasing: " + VTable.remove(i).getTitle());
            }
    
        }
        // ** CHANGE TO USE A LOCAL VARIABLE AND RETURN IT
        double videoRentCost = Double.parseDouble(amount);
    
        return videoRentCost;
    }
    

    showVideoList method
    Remove the VideoList() call -- only initialize the rental list once in the main() method. Also, consider cleaning up the output formatting a bit.

    public static void showVideoList()
    {
        System.out.println();
        System.out.println(
                "********************************************************************");
        System.out.println("List of the Videos are: ");
        System.out.println(
                "********************************************************************");
    
        // **USE PRINTF TO GIVE BETTER FORMATTING
        System.out.printf("%10s     %53s%n","Serial No","Video Details");
    
    
        for (int i = 0; i < VTable.size(); i++) {
            // **USE SIMILAR FORMATTING OUTPUT
            System.out.printf("%10d     %53s%n", VTable.get(i).getSerial(), VTable.get(i));
        }
        System.out.println();
    }
    

    This change will give a presentation like:

    ********************************************************************
    List of the Videos are: 
    ********************************************************************
     Serial No                                             Video Details
             1                   [title:BladeRunner Serial: 1 Cost: 5.0]
             2                 [title:Wages of Fear Serial: 2 Cost: 4.0]
             3                        [title:Grease Serial: 3 Cost: 5.0]
             4                      [title:Mama Mia Serial: 4 Cost: 4.0]
             5               [title:L'Illusionniste Serial: 5 Cost: 6.0]