Somecuban
03-16-2010, 02:17 AM
Write a program that simulates a vending machine. Products can be purchased by inserting coins with a value at least equal to the cost of the product. A user selects a product from a list of available products, adds coins, and either gets the product or gets the coins returned if insufficient money was supplied or if the product is sold out. The machine does not give change if too much money was added. Products can be restocked and money removed by an operator. Follow the design process that was described in this chapter. Your solution should include a class VendingMachine that is not coupled with the Scanner or PrintStream classes.
Here is a sample program run:
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
A
Description:
Candy bar
Price:
0.55
Quantity:
10
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
A
Description:
Fruit drink
Price:
0.65
Quantity:
10
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
S
Candy bar @ $0.55
Fruit drink @ $0.65
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
I
A) nickel @ 0.05
B) dime @ 0.1
C) quarter @ 0.25
D) dollar @ 1.0
C
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
I
A) nickel @ 0.05
B) dime @ 0.1
C) quarter @ 0.25
D) dollar @ 1.0
C
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
I
A) nickel @ 0.05
B) dime @ 0.1
C) quarter @ 0.25
D) dollar @ 1.0
A
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
B
A) Candy bar @ $0.55
B) Fruit drink @ $0.65
A
Purchased: Candy bar @ $0.55
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
I
A) nickel @ 0.05
B) dime @ 0.1
C) quarter @ 0.25
D) dollar @ 1.0
D
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
B
A) Candy bar @ $0.55
B) Fruit drink @ $0.65
B
Purchased: Fruit drink @ $0.65
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
R
Removed: $1.55
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
Q
Your main class should be called VendingMachineSimulation.
This is what i have so far
public class VendingMachineSimulation {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
product p = new product("name",10.0,10);
vendingMachine v = new vendingMachine();
System.out.println("S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit");
String b = a.next();
char choice = b.charAt(0);
do{
switch(choice){
case 'S':
System.out.println(v);
break;
case 'I':
break;
case 'B':
break;
case 'A':
System.out.println("Description:");
String desc = a.next();
System.out.println("Price:");
double pri = a.nextDouble();
System.out.println("Quantity:");
int qty = a.nextInt();
product userMadeProduct = new product(desc, pri, qty);
v.newproduct(userMadeProduct);
break;
case 'R':
break;
}
}while(choice != ('Q')||choice != ('q'));
}
}
Separate Class
public class vendingMachine {
ArrayList<product> p = new ArrayList<product>();
public void newproduct(product p1){
p.add(p1);
}
//add new product to vending machine
//display information of a product
}
class product{
public product(String productname, double productprice, int productqty) {
this.productname = productname;
this.productprice = productprice;
this.productqty = productqty;
}
String productname;
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
this.productname = productname;
}
public double getProductprice() {
return productprice;
}
public void setProductprice(double productprice) {
this.productprice = productprice;
}
public int getProductqty() {
return productqty;
}
public void setProductqty(int productqty) {
this.productqty = productqty;
}
double productprice;
int productqty;
}
YOU CAN CHANGE MY CODE OR JUST DO YOUR OWN BUT I NEED HELP PLXX
Here is a sample program run:
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
A
Description:
Candy bar
Price:
0.55
Quantity:
10
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
A
Description:
Fruit drink
Price:
0.65
Quantity:
10
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
S
Candy bar @ $0.55
Fruit drink @ $0.65
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
I
A) nickel @ 0.05
B) dime @ 0.1
C) quarter @ 0.25
D) dollar @ 1.0
C
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
I
A) nickel @ 0.05
B) dime @ 0.1
C) quarter @ 0.25
D) dollar @ 1.0
C
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
I
A) nickel @ 0.05
B) dime @ 0.1
C) quarter @ 0.25
D) dollar @ 1.0
A
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
B
A) Candy bar @ $0.55
B) Fruit drink @ $0.65
A
Purchased: Candy bar @ $0.55
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
I
A) nickel @ 0.05
B) dime @ 0.1
C) quarter @ 0.25
D) dollar @ 1.0
D
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
B
A) Candy bar @ $0.55
B) Fruit drink @ $0.65
B
Purchased: Fruit drink @ $0.65
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
R
Removed: $1.55
S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit
Q
Your main class should be called VendingMachineSimulation.
This is what i have so far
public class VendingMachineSimulation {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
product p = new product("name",10.0,10);
vendingMachine v = new vendingMachine();
System.out.println("S)how products I)nsert coin B)uy A)dd product R)emove coins Q)uit");
String b = a.next();
char choice = b.charAt(0);
do{
switch(choice){
case 'S':
System.out.println(v);
break;
case 'I':
break;
case 'B':
break;
case 'A':
System.out.println("Description:");
String desc = a.next();
System.out.println("Price:");
double pri = a.nextDouble();
System.out.println("Quantity:");
int qty = a.nextInt();
product userMadeProduct = new product(desc, pri, qty);
v.newproduct(userMadeProduct);
break;
case 'R':
break;
}
}while(choice != ('Q')||choice != ('q'));
}
}
Separate Class
public class vendingMachine {
ArrayList<product> p = new ArrayList<product>();
public void newproduct(product p1){
p.add(p1);
}
//add new product to vending machine
//display information of a product
}
class product{
public product(String productname, double productprice, int productqty) {
this.productname = productname;
this.productprice = productprice;
this.productqty = productqty;
}
String productname;
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
this.productname = productname;
}
public double getProductprice() {
return productprice;
}
public void setProductprice(double productprice) {
this.productprice = productprice;
}
public int getProductqty() {
return productqty;
}
public void setProductqty(int productqty) {
this.productqty = productqty;
}
double productprice;
int productqty;
}
YOU CAN CHANGE MY CODE OR JUST DO YOUR OWN BUT I NEED HELP PLXX