Test.java
unknown
java
8 months ago
2.7 kB
12
Indexable
package lab2;
/**
* @author Art Freebrey
* @version 1.0
<br></br>
* - no copyright
<br></br>
* - created 11.2.2025
<br></br>
* - last modified 11.2.2025
<br></br>
* - purpose: runs various tests on vending machine instances
<br></br>
* - version history: 1.0 added code;
*/
public class Test {
public static void main(String[] args) {
// default constructor (limit of 10)
VendingMachine machine1 = new VendingMachine("Chips", 5, 9);
// custom constructor within bounds
VendingMachine machine2 = new VendingMachine("Drinks", 3, 7, 20);
// custom constructor exceeding max allowed (should stop at 50)
VendingMachine machine3 = new VendingMachine("Cookies", 1, 2, 60);
// initial item count exceeding machine's limit
VendingMachine machine4 = new VendingMachine("Candy", 2, 25, 15);
VendingMachine[] vendingMachines = new VendingMachine[4];
vendingMachines[0] = machine1;
vendingMachines[1] = machine2;
vendingMachines[2] = machine3;
vendingMachines[3] = machine4;
System.out.println("\n| serial | item type | price | quantity | limit |");
for (VendingMachine vm : vendingMachines) {
System.out.printf("| %6d | %9s | £%4.2f | %8d | %6d |\n",
vm.getSerialNumber(),
vm.getItemType(),
vm.getItemPrice(),
vm.getItemCount(),
vm.getItemQuantityLimit());
}
System.out.println();
// try while powered off
System.out.println("try add items while off:");
machine1.addItems(5);
System.out.println("try buy item while off:");
machine1.buyItem();
System.out.println();
// power on and add items
System.out.println("turning on machine");
machine1.togglePower();
System.out.println("add 5 items");
machine1.addItems(5);
System.out.println();
// test capacity
System.out.println("add 6 more items (should fail):");
machine1.addItems(6);
System.out.println("add -1 items (should fail):");
machine1.addItems(-1);
System.out.println();
// buying items
System.out.println("buy 3 items:");
for (int i = 0; i < 4; i++) {
machine1.buyItem();
}
System.out.println();
machine2.togglePower();
System.out.println("\nmachine 2 (custom limit 20):");
System.out.println("adding 15 items (should fail):");
machine2.addItems(15);
System.out.println("add 10 items");
machine2.addItems(10);
}
} Editor is loading...
Leave a Comment