私はアイテムをスキャンするマシンのシミュレーションを作成しました。 MachineはクラスでItemは別のクラスです。私は現在、アイテムを価格に設定してから、マシンにそれをスキャンさせ、合計価格とスキャンしたアイテムの数を合計させることができます。最後にスキャンしたアイテムを削除する最も簡単な方法は何ですか?それはarraylistまたはそれのようなものを含める必要がありますか? Btw私は非常にjavaに新しいですので、どんな助けも素晴らしいでしょう!ここに私のコードは、これまでのところです:javaで最後に入力した値を削除する方法は?
public class Machine
{
// Both balance and count feilds below.
private double balance;
private int count;
// This is the constructor method, it sets both the balance and count to 0.
public Machine()
{
balance = 0;
count = 0;
}
// This simply returns the value of balance.
public double getBalance()
{
return balance;
}
// This simply returns the value of count.
public int getItemCount()
{
return count;
}
/* This simulates an item being scanned and is added to total item count,
* as well as adding its price to the total balance.
*/
public double scanItem (Item item)
{
balance = balance + item.price;
count = count + 1;
return item.price;
}
// This prints the receipt, showing the balance and total item count
public void printReciept()
{
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(" -- ");
System.out.println(" -- ");
System.out.println(" -- ");
System.out.println(" -- ");
System.out.println("##########################");
System.out.println(" Purchase summary ");
System.out.println(" ");
System.out.println(" Total balance: $" + balance + " ");
System.out.println("Total item count: " + count + " items");
System.out.println(" ");
System.out.println(" Thank you for shopping ");
System.out.println(" with us! ");
System.out.println("##########################");
System.out.println(" -- ");
System.out.println(" -- ");
System.out.println(" -- ");
System.out.println(" -- ");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~");
balance = 0;
count = 0;
}
// This method clears the balance and count
public void clearAll()
{
balance = 0;
count = 0;
}
}
と私Itemクラス:
public class Item
{
public double price;
public Item(double paramPrice)
{
price = paramPrice;
}
public double getPrice()
{
return price;
}
}
リストを使用し、トップ、ニーズに応じて、それを管理する...追加、deleteat(...)など –