package candyMachine;
public class Dispenser
{
private int numberOfltems;
private int cost;
public Dispenser()
{ numberOfltems = 50;
cost = 50;
}
public Dispenser( int setNoOfltems, int setCost )
{ if( setNoOfltems >= 0 )
numberOfltems = setNoOfltems;
else
numberOfltems = 50;
if( setCost >= 0 )
cost = setCost;
else
cost = 50;
}
public int getCount()
{ return numberOfltems;
}
public int getProductCost()
{ return cost;
}
public void makeSale()
{ numberOfltems --;
}
}
package candyMachine;
public class CashRegister
{private int cashOnHand;
public CashRegister( int cashIn )
{if(cashIn >= 0)
cashOnHand = cashIn;
else
cashOnHand = 500;
}
public CashRegister()
{cashOnHand = 500;
}
public int currentBalance()
{return cashOnHand;
}
public void acceptAmount( int amountIn )
{cashOnHand = cashOnHand + amountIn;
}
}
import java.io.* ;
import candyMachine.* ;
public class CandyMachine
{ static BufferedReader keyboard = new BufferedReader( new InputStreamReader( System.in ) );
public static void main( String[] args ) throws IOException
{
CashRegister cashRegister = new CashRegister();
Dispenser andy = new Dispenser(100, 50 );
Dispenser chips = new Dispenser( 100,65 );
Dispenser gum = new Dispenser( 75,45 );
Dispenser cookies = new Dispenser( 100,85 );
int choice;
showSelection();
choice = Integer.parseInt( keyboard.readLine() );
while( choice != 9 )
{ switch( choice )
{ case 1: sellProduct( candy, cashRegister );
break; case 2: sellProduct( chips, cashRegister );
break;
case 3: sellProduct( gum, cashRegister );
break; case 4: sellProduct( cookies, cashRegister );
break;
default: System.out.println( "Invalid Selection " );
}
showSelection();
choice = Integer.parseInt( keyboard.readLine() );
}
}
public static void showSelection()
{ System.out.println( "*** Welcome to shelly's Candy Shop ***" );
System.out.println( "To select an item,enter" );
System.out.println( "1 for Candy" );
System.out.println( "2 for Chip" );
System.out.println( "3 for Gum " );
System.out.println( "4 for Cookies" );
System.out.println( "9 to exit");
}
public static void sellProduct( Dispenser product, CashRegister cRegister ) throws IOException
{ int amount;
int amount2;
if( product.getCount() > 0 )
{ System.out.println( "Please deposit" + product.getProductCost() + "cents" );
amount = Integer.parseInt( keyboard.readLine());
if(amount < product.getProductCost())
{ System.out.println( "Please deposit another" +(product.getProductCost() - amount) + "cents");
amount2 = Integer.parseInt( keyboard.readLine() );
amount = amount + amount2;
}
if( amount >= product.getProductCost() )
{ cRegister.acceptAmount( amount );
product.makeSale();
System.out.println( "Collect your item at the bottom" + " and enjoy." ) ;
}
else
System.out.println( "The amount is not enough. Collect what you deposited.");
System.out.println( "*_*_*_*_*_*_*_*_*_*_*_*_*_");
}
else
System.out.println( "Sorry this item is sold out" );
}
}
public class Dispenser
{
private int numberOfltems;
private int cost;
public Dispenser()
{ numberOfltems = 50;
cost = 50;
}
public Dispenser( int setNoOfltems, int setCost )
{ if( setNoOfltems >= 0 )
numberOfltems = setNoOfltems;
else
numberOfltems = 50;
if( setCost >= 0 )
cost = setCost;
else
cost = 50;
}
public int getCount()
{ return numberOfltems;
}
public int getProductCost()
{ return cost;
}
public void makeSale()
{ numberOfltems --;
}
}
package candyMachine;
public class CashRegister
{private int cashOnHand;
public CashRegister( int cashIn )
{if(cashIn >= 0)
cashOnHand = cashIn;
else
cashOnHand = 500;
}
public CashRegister()
{cashOnHand = 500;
}
public int currentBalance()
{return cashOnHand;
}
public void acceptAmount( int amountIn )
{cashOnHand = cashOnHand + amountIn;
}
}
import java.io.* ;
import candyMachine.* ;
public class CandyMachine
{ static BufferedReader keyboard = new BufferedReader( new InputStreamReader( System.in ) );
public static void main( String[] args ) throws IOException
{
CashRegister cashRegister = new CashRegister();
Dispenser andy = new Dispenser(100, 50 );
Dispenser chips = new Dispenser( 100,65 );
Dispenser gum = new Dispenser( 75,45 );
Dispenser cookies = new Dispenser( 100,85 );
int choice;
showSelection();
choice = Integer.parseInt( keyboard.readLine() );
while( choice != 9 )
{ switch( choice )
{ case 1: sellProduct( candy, cashRegister );
break; case 2: sellProduct( chips, cashRegister );
break;
case 3: sellProduct( gum, cashRegister );
break; case 4: sellProduct( cookies, cashRegister );
break;
default: System.out.println( "Invalid Selection " );
}
showSelection();
choice = Integer.parseInt( keyboard.readLine() );
}
}
public static void showSelection()
{ System.out.println( "*** Welcome to shelly's Candy Shop ***" );
System.out.println( "To select an item,enter" );
System.out.println( "1 for Candy" );
System.out.println( "2 for Chip" );
System.out.println( "3 for Gum " );
System.out.println( "4 for Cookies" );
System.out.println( "9 to exit");
}
public static void sellProduct( Dispenser product, CashRegister cRegister ) throws IOException
{ int amount;
int amount2;
if( product.getCount() > 0 )
{ System.out.println( "Please deposit" + product.getProductCost() + "cents" );
amount = Integer.parseInt( keyboard.readLine());
if(amount < product.getProductCost())
{ System.out.println( "Please deposit another" +(product.getProductCost() - amount) + "cents");
amount2 = Integer.parseInt( keyboard.readLine() );
amount = amount + amount2;
}
if( amount >= product.getProductCost() )
{ cRegister.acceptAmount( amount );
product.makeSale();
System.out.println( "Collect your item at the bottom" + " and enjoy." ) ;
}
else
System.out.println( "The amount is not enough. Collect what you deposited.");
System.out.println( "*_*_*_*_*_*_*_*_*_*_*_*_*_");
}
else
System.out.println( "Sorry this item is sold out" );
}
}
解决方案 »
- 读txt文档一个小问题.
- 求教一个用java编写二进制加法器问题
- 简述一下VeriftEvent和ModifyEvent事件的区别
- 请教一个棘手的问题。
- 我用java的udp协议模拟本机发出大量数据,但是不行。
- 请大家看看这段程序,谢谢了
- 请问java里面有没有类似C里面memcopy()一样的函数........................
- 非常急的问一下:.class文件怎么才能看见?
- 怎么把JAVA的标题栏换成XP的啊
- jcreator pro 2.5,jdk1.4rc,这个接口怎么编译不了?
- 一个字符匹配的问题。正 则得不知道有没有人会!头都想疼了啊
- 急啊,请问大家我这程序为什么输入学号后,密码就直接跳过去了啊
public class Dispenser
{
private int numberOfltems;
private int cost;
public Dispenser()
{ numberOfltems = 50;
cost = 50;
}
public Dispenser( int setNoOfltems, int setCost )
{ if( setNoOfltems >= 0 )
numberOfltems = setNoOfltems;
else
numberOfltems = 50;
if( setCost >= 0 )
cost = setCost;
else
cost = 50;
}
public int getCount()
{ return numberOfltems;
}
public int getProductCost()
{ return cost;
}
public void makeSale()
{ numberOfltems --;
}
}
package candyMachine;
public class CashRegister
{private int cashOnHand;
public CashRegister( int cashIn )
{if(cashIn >= 0)
cashOnHand = cashIn;
else
cashOnHand = 500;
}
public CashRegister()
{cashOnHand = 500;
}
public int currentBalance()
{return cashOnHand;
}
public void acceptAmount( int amountIn )
{cashOnHand = cashOnHand + amountIn;
}
}
import java.io.* ;
import candyMachine.* ;
public class CandyMachine
{ static BufferedReader keyboard = new BufferedReader( new InputStreamReader( System.in ) );
public static void main( String[] args ) throws IOException
{
CashRegister cashRegister = new CashRegister();
Dispenser andy = new Dispenser(100, 50 );
Dispenser chips = new Dispenser( 100,65 );
Dispenser gum = new Dispenser( 75,45 );
Dispenser cookies = new Dispenser( 100,85 );
int choice;
showSelection();
choice = Integer.parseInt( keyboard.readLine() );
while( choice != 9 )
{ switch( choice )
{ case 1: sellProduct( candy, cashRegister );
break; case 2: sellProduct( chips, cashRegister );
break;
case 3: sellProduct( gum, cashRegister );
break; case 4: sellProduct( cookies, cashRegister );
break;
default: System.out.println( "Invalid Selection " );
}
showSelection();
choice = Integer.parseInt( keyboard.readLine() );
}
}
public static void showSelection()
{ System.out.println( "*** Welcome to shelly's Candy Shop ***" );
System.out.println( "To select an item,enter" );
System.out.println( "1 for Candy" );
System.out.println( "2 for Chip" );
System.out.println( "3 for Gum " );
System.out.println( "4 for Cookies" );
System.out.println( "9 to exit");
}
public static void sellProduct( Dispenser product, CashRegister cRegister ) throws IOException
{ int amount;
int amount2;
if( product.getCount() > 0 )
{ System.out.println( "Please deposit" + product.getProductCost() + "cents" );
amount = Integer.parseInt( keyboard.readLine());
if(amount < product.getProductCost())
{ System.out.println( "Please deposit another" +(product.getProductCost() - amount) + "cents");
amount2 = Integer.parseInt( keyboard.readLine() );
amount = amount + amount2;
}
if( amount >= product.getProductCost() )
{ cRegister.acceptAmount( amount );
product.makeSale();
System.out.println( "Collect your item at the bottom" + " and enjoy." ) ;
}
else
System.out.println( "The amount is not enough. Collect what you deposited.");
System.out.println( "*_*_*_*_*_*_*_*_*_*_*_*_*_");
}
else
System.out.println( "Sorry this item is sold out" );
}
}
case 1: sellProduct(candy, cashRegister );变量名不一致
然后填写一些相关的东西,还有导出的JAR文件要存放的位置,最后点"finish"就OK了!