For this homework, you will develop a class called VendingMachine that simulates
ID: 3792673 • Letter: F
Question
For this homework, you will develop a class called VendingMachine that simulates an imaginary
vending machine at CSUMB. In the program, a user can buy a bottle of water ($1.50), a coffee ($2.00), a bag of chips ($1.00),
and a chocolate bar ($2.50) from the machine. The user can buy several items if they are available
in the machine. For the payment, a user can use only cash (including coins). Additionally, an
administrator of the machine can reset or refill the machine.
1 Demo Program
The following code presents a demo program that uses the VendingMachine class.
public class VendingMachineDemo {
public static void main(String[] args) {
VendingMachine machine1 = new VendingMachine(100);
VendingMachine machine2 = new VendingMachine(200, "Library");
System.out.println("===== Welcome to the CSUMB Vending Machine =====");
System.out.println(machine1);
System.out.println("");
System.out.println(machine2);
System.out.println(" === Compare Two Machines ===");
System.out.println(machine1.equals(machine2));
machine1.setLocation("BIT104");
machine1.setName(50);
machine1.setName(100);
System.out.println(" === Machine Reset ===");
machine1.reset(4, 5, 0, 6); // In this method, we assume that a machine
// administrator resets the contents of
// a machine.
machine1.addItems(1,2,3,4); // A system admin can add items to the machine
System.out.println(machine1);
System.out.println("");
machine1.displayMenu();
System.out.println(" === Buy an Item ===");
machine1.buyItem();
System.out.println(" === Buy another Item ===");
1
if(machine1.buyItem(1, 4) == false) {
System.exit(1);
}
System.out.println(" === Return Two Items ===");
machine1.returned(1, 2);
machine1.returned(2, 3);
System.out.println(" === Buy another Item ===");
machine1.buyItem(3, 5);
System.out.println(" === Pay for items selected. ===");
if(machine1.payment()) {
System.out.println(" === Valid payment. ===");
machine1.displayReceipt();
} else {
System.out.println(" === Invalid payment. Try one more time. ===");
if(machine1.payment()) {
System.out.println(" === Print Receipt ===");
machine1.displayReceipt();
} else {
System.exit(1);
}
}
machine1.addItems(5,5,5,5);
System.out.println(" === Machine Status ===");
machine1.status();
System.out.println(" ===== Thank you! =====");
}
}
Sample Run of the Demo Program: The following presents a sample result of the demo program.
Read the result very carefully to identify the operations of the program.
===== Welcome to the CSUMB Vending Machine =====
Serial Number: 100
Location: UNKNOWN
Contents:
Water: 0
Coffee: 0
Sun Chips: 0
Chocolate Bar: 0
Serial Number: 200
Location: Library
Contents:
Water: 0
Coffee: 0
Sun Chips: 0
2
Chocolate Bar: 0
=== Compare Two Machines ===
false
=== Machine Reset ===
Serial Number: 100
Location: BIT104
Contents:
Water: 5
Coffee: 7
Sun Chips: 3
Chocolate Bar: 10
===== Vending Machine Menu =====
1. Water............$1.50
2. Regular Coffee...$2.00
3. Sun Chips........$1.00
4. Chocolate Bar....$2.50
=== Buy an Item ===
Select an item number: 2
How many do you want to buy? 3
You selected Regular Coffee. Quantity: 3
=== Buy another Item ===
Select an item number: 1
How many do you want to buy? 4
You selected Water. Quantity: 4
=== Return Two Items ===
You selected Water. Quantity: 2
You selected Regular Coffee. Quantity: 3
=== Buy another Item ===
Select an item number: 1
How many do you want to buy? 5
You selected Sun Chips. Quantity: 5
Selection Failed. We don’t have enough Sun Chips.
=== Pay for items selected. ===
Enter money amount: $2.00
Insufficient money. $2.00 returned
=== Invalid payment. Try one more time. ===
Enter money amount: $5.00
Sufficient money. $1.70 returned
3
=== Print Receipt ===
Water: $1.50 X 2 = $3.00
Tax (10.0%): $0.30
Total: $3.30
=== Machine Status ===
Serial Number: 100
Location: BIT104
Sold Items:
Water: 2
Coffee: 0
Sun Chips: 0
Chocolate Bar: 0
Current Contents:
Water: 8
Coffee: 12
Sun Chips: 8
Chocolate Bar: 15
Total Earning: $3.30
===== Thank you! =====
Explanation / Answer
VendingMachineDemo.java
public class VendingMachineDemo {
public static void main(String[] args) {
VendingMachine machine1 = new VendingMachine(100);
VendingMachine machine2 = new VendingMachine(200, "Library");
System.out.println("===== Welcome to the CSUMB Vending Machine =====");
System.out.println(machine1);
System.out.println("");
System.out.println(machine2);
System.out.println(" === Compare Two Machines ===");
System.out.println(machine1.equals(machine2));
machine1.setLocation("BIT104");
machine1.setName(50);
machine1.setName(100);
System.out.println(" === Machine Reset ===");
machine1.reset(4, 5, 0, 6); // In this method, we assume that a machine
// administrator resets the contents of
// a machine.
machine1.addItems(1,2,3,4); // A system admin can add items to the machine
System.out.println(machine1);
System.out.println("");
machine1.displayMenu();
System.out.println(" === Buy an Item ===");
machine1.buyItem();
System.out.println(" === Buy another Item ===");
if(machine1.buyItem(1, 4) == false) {
System.exit(1);
}
System.out.println(" === Return Two Items ===");
machine1.returned(1, 2);
machine1.returned(2, 3);
System.out.println(" === Buy another Item ===");
machine1.buyItem(3, 5);
System.out.println(" === Pay for items selected. ===");
if(machine1.payment()) {
System.out.println(" === Valid payment. ===");
machine1.displayReceipt();
} else {
System.out.println(" === Invalid payment. Try one more time. ===");
if(machine1.payment()) {
System.out.println(" === Print Receipt ===");
machine1.displayReceipt();
} else {
System.exit(1);
}
}
machine1.addItems(5,5,5,5);
System.out.println(" === Machine Status ===");
machine1.status();
System.out.println(" ===== Thank you! =====");
}
}
VendingMachine.java
import java.util.Scanner;
public class VendingMachine {
public final double WATER_PRICE = 1.50;
public final double COFFEE_PRICE = 2.00;
public final double CHIPS_PRICE = 1.00;
public final double CHOCOLATE_PRICE = 2.50;
private int name;
private String location;
private int noOfWater;
private int noOfCoffee;
private int noOfChips;
private int noOfChocolateBar;
private int noOfWaterBought;
private int noOfCoffeeBought;
private int noOfChipsBought;
private int noOfChocolateBarBought;
private double amount;
public VendingMachine(int name){
this.name = name;
this.location = "UNKNOWN";
this.noOfWater =0 ;
this.noOfCoffee =0 ;
this.noOfChips =0 ;
this.noOfChocolateBar =0 ;
this.amount = 0;
noOfWaterBought= 0;
noOfCoffeeBought =0 ;
noOfChipsBought =0;
noOfChocolateBarBought =0;
}
public VendingMachine(int name, String location){
this.name = name;
this.location = location;
this.noOfWater =0 ;
this.noOfCoffee =0 ;
this.noOfChips =0 ;
this.noOfChocolateBar =0 ;
this.amount = 0;
noOfWaterBought= 0;
noOfCoffeeBought =0 ;
noOfChipsBought =0;
noOfChocolateBarBought =0;
}
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getNoOfWater() {
return noOfWater;
}
public void setNoOfWater(int noOfWater) {
this.noOfWater = noOfWater;
}
public int getNoOfCoffee() {
return noOfCoffee;
}
public void setNoOfCoffee(int noOfCoffee) {
this.noOfCoffee = noOfCoffee;
}
public int getNoOfChips() {
return noOfChips;
}
public void setNoOfChips(int noOfChips) {
this.noOfChips = noOfChips;
}
public int getNoOfChocolateBar() {
return noOfChocolateBar;
}
public void setNoOfChocolateBar(int noOfChocolateBar) {
this.noOfChocolateBar = noOfChocolateBar;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String toString(){
String strVal ="Serial Number: "+this.getName()+
" " + "Location: "+this.getLocation() +
" " + "Contents: " +
" " + "Water: "+this.getNoOfWater() +
" " + "Coffee: "+this.getNoOfCoffee() +
" " + "Sun Chips: "+this.getNoOfChips() +
" " + "Chocolate Bar : "+this.getNoOfChocolateBar();
return strVal;
}
public boolean equals(Object obj){
VendingMachine vm = (VendingMachine) obj;
if(this.getName() == vm.getName()){
return true;
}
return false;
}
public void reset(int water, int coffee, int chips, int chocolate){
this.setNoOfWater(water);
this.setNoOfCoffee(coffee);
this.setNoOfChips(chips);
this.setNoOfChocolateBar(chocolate);
}
public void addItems(int water, int coffee, int chips, int chocolate){
this.setNoOfWater(this.getNoOfWater() + water);
this.setNoOfCoffee(this.getNoOfCoffee() + coffee);
this.setNoOfChips(this.getNoOfChips() + chips);
this.setNoOfChocolateBar(this.getNoOfChocolateBar() + chocolate);
}
public void displayMenu(){
System.out.println("===== Vending Machine Menu =====");
System.out.println("1. Water............$" + WATER_PRICE);
System.out.println("2. Regular Coffee...$" + COFFEE_PRICE);
System.out.println("3. Sun Chips........$" + CHIPS_PRICE);
System.out.println("4. Chocolate Bar....$" + CHOCOLATE_PRICE);
}
public void buyItem(){
String itemNumber = "";
String noOfItemsStr = "";
int noOfItems = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Select an item number: ");
itemNumber = scan.nextLine();
System.out.println("How many do you want to buy? ");
noOfItemsStr = scan.nextLine();
noOfItems = Integer.valueOf(noOfItemsStr);
processBuyItems( Integer.valueOf(itemNumber), noOfItems);
}
public boolean buyItem(int itemNumber , int noOfItems){
processBuyItems( itemNumber, noOfItems);
return true;
}
public void returned(int itemNumber , int noOfItems){
String itemName ="";
if(1 == itemNumber){
this.setNoOfWater(this.getNoOfWater()+noOfItems);
itemName = "Water";
}else if(2 == itemNumber){
this.setNoOfCoffee(this.getNoOfCoffee()+noOfItems);
itemName = "Regular Coffee";
}else if(3 == itemNumber){
this.setNoOfChips(this.getNoOfChips() +noOfItems);
itemName = "Sun Chips";
}else if(4 == itemNumber){
this.setNoOfChocolateBar(this.getNoOfChocolateBar() +noOfItems);
itemName = "Chocolate Bar";
}
System.out.println("You selected "+itemName+". Quantity: "+noOfItems);
}
public void processBuyItems(int itemNumber, int noOfItems){
String itemName = "";
int existintCount =0 ;
boolean itemShortage =false;
if(1 == itemNumber){
existintCount = this.getNoOfWater();
if(this.getNoOfWater() - existintCount >= 0){
this.setNoOfWater(this.getNoOfWater() - noOfItems);
amount = amount + Double.valueOf(WATER_PRICE);
noOfWaterBought =noOfWaterBought +1;
}
else{
itemShortage = true;
}
itemName = "Water";
}else if(2==itemNumber){
existintCount = this.getNoOfCoffee();
if(this.getNoOfCoffee() - existintCount >= 0){
this.setNoOfCoffee(this.getNoOfCoffee() - noOfItems);
amount = amount + Double.valueOf(COFFEE_PRICE);
noOfCoffeeBought =noOfCoffeeBought+1;
}
else{
itemShortage = true;
}
itemName = "Regular Coffee";
}else if(3 == itemNumber){
existintCount = this.getNoOfChips();
if(this.getNoOfChips() - existintCount >= 0){
this.setNoOfChips(this.getNoOfChips() - noOfItems);
amount = amount + Double.valueOf(CHIPS_PRICE);
noOfChipsBought =noOfChipsBought +1;
}
else{
itemShortage = true;
}
itemName = "Sun Chips";
}else if(4 == itemNumber){
existintCount = this.getNoOfChocolateBar();
if(this.getNoOfChocolateBar() - existintCount >= 0){
this.setNoOfChocolateBar(this.getNoOfChocolateBar() - noOfItems);
amount = amount + Double.valueOf(CHOCOLATE_PRICE);
noOfChocolateBarBought =noOfChocolateBarBought +1;
}
else{
itemShortage = true;
}
itemName = "Chocolate Bar";
}
if(itemShortage ){
System.out.println("Selection Failed. We don’t have enough "+itemName);
}
else{
System.out.println("You selected "+itemName+". Quantity: "+itemNumber);
}
}
public boolean payment(){
Scanner scan = new Scanner(System.in);
String userAmtStr = "";
double userAmount =0 ;
System.out.println("Enter money amount: $");
userAmtStr = scan.nextLine();
userAmount = Integer.valueOf(userAmtStr);
if(this.getAmount()> userAmount){
System.out.println("Insufficient money. $"+userAmount +" returned");
return false;
}
else{
double balance = userAmount - this.getAmount();
if(balance>0){
System.out.println("Sufficient money. $"+balance +" returned");
}else{
System.out.println("Sufficient money. ");
}
}
return true;
}
public void displayReceipt(){
double totAmount =0 ;
double tax=0;
double grandTotal=0;
if(noOfWaterBought>0){
System.out.println("Water : $"+WATER_PRICE +" X "+noOfWaterBought + " = $"+WATER_PRICE*noOfWaterBought);
totAmount = WATER_PRICE*noOfWaterBought;
}
if(noOfCoffeeBought>0){
System.out.println("Coffee : $"+COFFEE_PRICE +" X "+noOfCoffeeBought + " = $"+COFFEE_PRICE*noOfCoffeeBought);
totAmount = COFFEE_PRICE*noOfCoffeeBought;
}
if(noOfChipsBought>0){
System.out.println("Sun Chips : $"+CHIPS_PRICE +" X "+noOfChipsBought + " = $"+CHIPS_PRICE*noOfChipsBought);
totAmount = CHIPS_PRICE*noOfChipsBought;
}
if(noOfChocolateBarBought>0){
System.out.println("Chocolate : $"+CHOCOLATE_PRICE +" X "+noOfChocolateBarBought + " = $" +CHOCOLATE_PRICE*noOfChocolateBarBought);
totAmount = CHOCOLATE_PRICE*noOfChocolateBarBought;
}
tax= totAmount*.1;
System.out.println("Tax (10.0%) : $"+tax);
grandTotal= tax+totAmount;
System.out.println("Total : $"+grandTotal);
}
public void status(){
this.toString();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.