查看: 933|回复: 1
|
Java 问题...急
[复制链接]
|
|
Write a complete Java application using a one-dimensional array to keep track of the toys Acme has in its warehouse. It can enter or remove items from the inventory list, change the number of toys, and display the remaining amount. The default is up to 100 toys.
请问要怎样开始...我真的一点也不会... 求求了 |
|
|
|
|
|
|
|

楼主 |
发表于 25-7-2007 11:22 PM
|
显示全部楼层
import java.lang.*;
import java.io.*;
public class AcmeToys
{
public static void main(String[] args)
{
AcmeToys acmeToyCompany = new AcmeToys();
try
{
acmeToyCompany.addToy("Pokemon... 500);
acmeToyCompany.addToy("Barbie"... 350);
acmeToyCompany.addToy("Cycle", 76);
System.out.println("Inventory of Pokemon: " + acmeToyCompany.getToyInventory...
System.out.println("Inventory of Barbie: " + acmeToyCompany.getToyInventory...
System.out.println("Inventory of Cycle: " + acmeToyCompany.getToyInventory...
acmeToyCompany.updateToy("Barb... 1000);
System.out.println("Inventory of Barbie: " + acmeToyCompany.getToyInventory...
acmeToyCompany.removeToy("Cycl...
System.out.println("Inventory of Cycle: " + acmeToyCompany.getToyInventory...
}
catch (ToyNotFoundException ex)
{
System.out.println("Toy not found at Acme");
}
}
public AcmeToys()
{
listOfToys = new Toy[100];
}
public void addToy(String toyName, int toyQuantity)
{
listOfToys[numberOfToys] = new Toy(toyName, toyQuantity);
//Increment the number of toys at Acme so that we
//know where to add any new toys
numberOfToys++;
}
public void removeToy(String toyName)
throws ToyNotFoundException
{
int index = searchToy(toyName);
listOfToys[index].toyName = null;
listOfToys[index].quantity = 0;
numberOfToys--;
}
public void updateToy(String toyName, int toyQuantity)
throws ToyNotFoundException
{
int index = searchToy(toyName);
listOfToys[index].quantity = toyQuantity;
}
public int getToyInventory(String toyName)
throws ToyNotFoundException
{
int index = searchToy(toyName);
return listOfToys[index].quantity;
}
private int searchToy(String toyName)
throws ToyNotFoundException
{
for (int index=0; index<numberOfToys; index++)
{
if (toyName.equals(listOfToys[ind...
return index;
}
throw new ToyNotFoundException();
}
//One dimensional array of toys
//Each element is an object of type Toy
private Toy[] listOfToys;
//Initial number of toys at Acme
private static int numberOfToys = 0;
};
class ToyNotFoundException extends Exception
{
}
class Toy
{
public Toy(String name, int inventory)
{
toyName = name;
quantity = inventory;
}
public String toyName;
public int quantity;
};
是不是这样....这个好像有问题...可是不知道问题在哪里...有人知道吗? |
|
|
|
|
|
|
| |
本周最热论坛帖子
|