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

时间:2016-04-25 23:28:10

标签: java linked-list shopping-cart cart kiosk

  

这是问题:

假设该卡具有有效的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购买多件商品。

1 个答案:

答案 0 :(得分:1)

只有一次购买的原因是你没有一个循环试图购买(或租赁;命名有点混乱)附加选择。还有一些static类变量使程序更复杂一些。以下是一些建议。

主要方法
添加循环以收集其他输入。此外,删除static double rentCost并移至main方法(同样,更改buyVideo以返回成本而不是更新实例变量)。添加方法以继续收集输入。

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方法
更改退还租金金额;不要更新实例变量

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]