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 '£': 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;
    }
  }