Итак, я создал симуляцию сканирования машины. Где Machine - класс, а Item - отдельный класс. В настоящее время я могу установить товар по цене, а затем сканировать его и добавить общую цену, а также количество проверенных элементов. Какой самый простой способ удалить последний отсканированный элемент? Нужно ли включать в себя аррайалиста или что-то в этом роде? 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 (...) и т.д. –