curiostore3b.java
来自「Java经典例程 从外国一大学计算机教授出版物下载的代码 经典」· Java 代码 · 共 113 行
JAVA
113 行
import javagently.*;
class CurioStore3b {
/* Curio Store Version 3b by J M Bishop April 2000
* ----------------------
* This shop has stock levels and a sell option
* and can make sales in several currencies.
* Illustrates the switch-statement (Example 5.5)
*/
private static final double
dollarExchange = 4.8845,
poundExchange = 8.047,
yenExchange = 0.0378,
markExchange = 2.7361;
public static void main (String [ ] args) {
new CurioStore3b ();
}
// Declare objects relevant to all methods
Display display = new Display ("Polelo Curio Store");
Curio mugs, tshirts, carvings;
boolean open;
// The constructor where the initialising and main work gets done
CurioStore3b () {
mugs = new Curio("Traditional mugs", 6, "beaded in Ndebele style",20, display);
tshirts = new Curio("T-shirts", 30, "sizes M to XL", 50, display);
carvings = new Curio("Masks", 80, "carved in wood", 8, display);
// print out the initial shop details
report();
// Use methods to perform a sequence of actions
stockTheStore();
openTheStore();
while (open) {
sellCurios();
open = display.getString("The shop is").equals("Open");
available();
}
display.println("Store closed.\nHave a good day");
}
void report () {
display.println("The Polelo Curio Store sells\n");
// use the objects' access to toString to print their contents
display.println(""+mugs);
display.println(""+tshirts);
display.println(""+carvings);
}
void stockTheStore () {
display.ready("Press ready when new stock levels have been entered");
mugs.addStock(display.getInt(mugs.name));
tshirts.addStock(display.getInt(tshirts.name));
carvings.addStock(display.getInt(carvings.name));
}
void openTheStore () {
display.prompt("Kind of curio sold"," ");
display.prompt("Number sold",0);
display.prompt("Currency used", "G");
display.prompt("The shop is","Open");
open = true;
}
void sellCurios () {
Curio curio;
display.ready("Press ready when sale completed");
String curioName = display.getString("Kind of curio sold");
int CurioSold = display.getInt("Number sold");
if (curioName.equals(mugs.name)) {
curio = mugs;
} else
if (curioName.equals(tshirts.name)) {
curio = tshirts;
} else
if (curioName.equals(carvings.name)) {
curio = carvings;
} else {
display.println(curioName + " is an invalid curio name");
return;
}
display.println("\nOrder for "+CurioSold+" "+curioName);
if (curio.stockLevel() >= CurioSold) {
curio.sell(CurioSold);
displayReceipt(curio, curioName, CurioSold);
}
else {
display.println("Not enough stock. "+
curio.stockLevel() + " available.");
}
}
void displayReceipt (Curio curio, String curioName, int curiosSold) {
double factor;
char currencySymbol;
boolean symbolRead=true;
do {
currencySymbol = display.getString("Currency used").charAt(0);
switch (currencySymbol) {
case 'Y': factor = yenExchange; break;
case '$': factor = dollarExchange; break;
case 'D': factor = markExchange; break;
case '
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?