Назад | Начало урока | Вперед
Содержание

Глава 5

Задача 5.11 "Курсы обмена."

Усовершенствуйте программу из примера 5.5 (из книги Джулии Бишоп) так, чтобы курсы можно было обновлять каждый день перед началом торговли.

Решение

Добавим в функцию openTheStore () четыре новых поля, в которые можно будет вводить курсы четырех валют по отношению к валюте G (грацы):

void openTheStore () {

display.prompt("Kind of curio sold"," ");
display.prompt("Number sold",0);
display.prompt("Currency used", "G");

display.prompt("Y",0.0378);
display.prompt("$",4.8845);
display.prompt("D",2.7361);
display.prompt("J",8.047);
display.prompt("The shop is","Open");

open = true;

}

И добавим код, в котором эти переменные будут инициализироваться.

void displayReceipt (Curio curio, String curioName, int curiosSold) {

double yenExchange = display.getDouble("Y");
double dollarExchange = display.getDouble("$");
double markExchange = display.getDouble("D");
double poundExchange = display.getDouble("J");

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 'Ј': factor = poundExchange; break;
case 'G': factor = 1; break;
default : factor = 1;
symbolRead = true;
display.println("Invalid symbol, try again");
}
}
while (!symbolRead);

display.println("RECEIPT: for "+ curiosSold + " "
+ curioName +" at G"+ curio.price+" each = G" +
curio.price*curiosSold);

if (currencySymbol != 'G') {

display.print("which is "+ currencySymbol +
Stream.format(curio.price*curiosSold/factor,10,2));
}
}

Теперь мы можем изменять курсы валют интерактивно!

Код программы:


import javagently.*;

class 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");
}//end constructor

public static void main (String [ ] args) {

new CurioStore3b ();
}

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("Y",0.0378);
display.prompt("$",4.8845);
display.prompt("D",2.7361);
display.prompt("J",8.047);
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 yenExchange = display.getDouble("Y");
double dollarExchange = display.getDouble("$");
double markExchange = display.getDouble("D");
double poundExchange = display.getDouble("J");

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 'Ј': factor = poundExchange; break;
case 'G': factor = 1; break;
default : factor = 1;
symbolRead = true;
display.println("Invalid symbol, try again");
}
}
while (!symbolRead);

display.println("RECEIPT: for "+ curiosSold + " "
+ curioName +" at G"+ curio.price+" each = G" +
curio.price*curiosSold);

if (currencySymbol != 'G') {

display.print("which is "+ currencySymbol +
Stream.format(curio.price*curiosSold/factor,10,2));
}
}

void available () {

display.println("\nAvailable are "+mugs.stockLevel()+" "+ tshirts.stockLevel()+"
"+carvings.stockLevel()+ " curios respectively\n");
}
}
class Curio {
String name;
int price;
String description;
int stock;

Curio (String n, int p, String d, int t, Display display) {

name = n;
price = p;
description = d;
display.prompt(name,t);
}
void addStock (int n) {
stock += n;
}

void sell (int n) {

// stock could go negative
stock -= n;
}
int stockLevel () {
return stock;
}
// This method is accessed by println to turn
// a Curio object into a String.
public String toString () {
return name + " "+description+" for G" + price;
}
}

Результат :

Подсказка


Назад | Начало урока | Вверх | Вперед
Содержание

Hosted by uCoz