這是一個問題:添加使用鏈表多個項目,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()+"]";
}
}
我成功只買一個項目,需要購買通過鏈表籃多個項目。
當我嘗試另購的視頻列表中顯示Repeteadly更多....兩次 –
@AnilShivakoti,不知道你的意思。如果您說當您循環回去並顯示視頻的兩個副本時,這是因爲未在'showVideoList()'方法中刪除VideoList()調用並將其放在'main'方法中。您只需要一次初始化庫存,並且應該使用'main'方法完成。 – KevinO
感謝您的幫助,再次感謝 –