curiostore2.java
来自「Java经典例程 从外国一大学计算机教授出版物下载的代码 经典」· Java 代码 · 共 95 行
JAVA
95 行
class CurioStore2 {
/* Polelo Curio Store No. 2 by J M Bishop April 2000
* ------------------------
* The shop with stock levels and a sell option
* Illustrates methods in an object-oriented program
*/
public static void main (String [ ] args) {
// creates the class's (i.e. program's) object
new CurioStore2 ();
}
// declare three objects
Curio mugs, tshirts, carvings;
// The constructor for the program
// where the initialising and main work gets done
CurioStore2 () {
// instantiate the objects with different initial values
mugs = new Curio("Traditional mugs", 6, "beaded in Ndebele style");
tshirts = new Curio("T-shirts", 30, "sizes M to XL");
carvings = new Curio("Masks", 80, "carved in wood");
// print out a report
report ();
// Now stock the shop for each curio
mugs.addToStock(10);
tshirts.addToStock(50);
carvings.addToStock(5);
// Report on stock levels
available ();
// Sell something. The sell method returns a string with
// a "receipt" we can print.
System.out.println(tshirts.sell(8));
// Report on stock levels again
available ();
}
void report () {
System.out.println("The Polelo Curio Store sells\n");
// use the objects' access to toString to print their contents
mugs.write();
tshirts.write();
carvings.write();
}
void available () {
System.out.println("Available are "+mugs.stockLevel()+" "+
tshirts.stockLevel()+" "+carvings.stockLevel()+
" curios respectively\n");
}
}
class Curio {
// An inner class giving details of and actions on a curio.
// Will be expanded as the project progresses.
// Declare the fields, including a new one for stock
String item;
int price;
String description;
int stock = 0;
// The constructor copies the initialisation into the fields
Curio (String n, int p, String d) {
item = n;
price = p;
description = d;
}
// Three new actions available on curios
void addToStock (int n) {
stock += n;
}
String sell (int n) {
stock -= n;
return "Sold " + n + " " + item;
}
int stockLevel () {
return stock;
}
void write () {
System.out.println(item + " " + description + " for G" + price);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?