2016-04-25 90 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

感謝您的幫助,再次感謝 –