使用链表添加多个项目,JAVA

Adding multiple items using linkedlist, JAVA

This is the Question:

假设卡有一个有效的 PIN 开始,并且银行账户中有 100 个单位。 每个视频都有租金“成本”。您需要扫描购物篮列表并计算 ATM 交易的总额。您可以假设银行账户中始终有足够的单位来支付本练习的租金。 Video Kiosk 代码的一个版本将包含以下步骤:

• 为视频标题创建链接列表并填充 5 个项目

• 创建一个空链表(购物篮)来存储客户选择

• 提供菜单驱动的选择过程

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()+"]";
  }
}

我只购买一件商品成功,需要通过Linked List Basket购买多件商品。

只有一次购买的原因是您没有尝试购买(或租用;术语有点混乱)额外选择的循环。还有一些 static class 变量使程序变得有点复杂。这里有一些建议。

主要方法
添加一个循环以收集额外的输入。此外,删除 static double rentCost 并移至 main 方法(另外,将 buyVideo 更改为 return 成本而不是更新实例变量)。添加方法以继续收集输入。

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!");
}

购买视频方法
修改为return租金金额;不更新实例变量

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 方法
删除 VideoList() 调用——只在 main() 方法中初始化一次出租列表。另外,考虑稍微清理一下输出格式。

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();
}

此更改将提供如下演示文稿:

********************************************************************
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]