2016-04-25 94 views
1

这是一个问题:添加使用链表多个项目,JAVA

假设卡具有正确的密码启动,并且有100个单位的银行账户。 每个视频都有租用“费用”。您需要扫描购物篮清单并计算ATM交易的总额。您可以假设银行账户中有足够的单位来支付此练习的租金成本。 的视频信息站代码一个版本将包含以下步骤:

•创建视频节目链接列表,并用5个项目

选择填写

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

•给出了一个菜单驱动的选择过程

  • 在屏幕上显示一个选择数
  • 逐项确定选择和确定它是否有效
  • 认沽选定视频一分为第二链表(篮)
  • 删除从可用标题列表先前选定的视频
  • 完整选择过程

    带来的内容购物篮清单到ATM并付款

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

我成功只买一个项目,需要购买通过链表篮多个项目。

回答

1

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

主要方法
添加一个循环,以收集更多的输入。此外,删除static double rentCost并移至主要方法(同时,更改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] 
+0

当我尝试另购的视频列表中显示Repeteadly更多....两次 –

+0

@AnilShivakoti,不知道你的意思。如果您说当您循环回去并显示视频的两个副本时,这是因为未在'showVideoList()'方法中删除VideoList()调用并将其放在'main'方法中。您只需要一次初始化库存,并且应该使用'main'方法完成。 – KevinO

+0

感谢您的帮助,再次感谢 –