This program calculates the amount you should tip based on the bill amount and the user's rating of the waiter using different methods.
import java.util.*;
import java.text.*;
public class Arithmetic {
public static void main(String[] args) {
double price = getPrice();
int rating = rateWaiter();
double tipAmount = calculateTip(price, rating);
System.out.println("You should tip " + currencyFormatter(tipAmount)
+ " for a total of " + currencyFormatter(price + tipAmount));
}
public static int rateWaiter() {
Scanner keyboard = new Scanner(System.in);
int choice;
do {
System.out.print("Rate your waiter from 0-10: ");
choice = keyboard.nextInt();
} while (!(choice >= 0 && choice <= 10));
return choice;
}
public static String currencyFormatter(double amount) {
DecimalFormat myFormatter = new DecimalFormat("$###,###.00");
return myFormatter.format(amount);
}
public static double getPrice() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter bill amount: ");
return keyboard.nextDouble();
}
public static double calculateTip(double price, int rating) {
return price * ((10 + rating) / 100.0);
}
}
Thursday, August 7, 2014
Trading game
import java.util.*;
import java.text.*;
public class Arithmetic {
static final int NUMBER_OF_DAYS=10;
static final double BASE_PRICE = 10;
static final double VARIATION = 5 ;
static final double INITIAL_CASH=100;
static double cash = INITIAL_CASH;
static int nyAppleInventory;
static int nyPearInventory;
static int laAppleInventory;
static int laPearInventory;
static int appleInventory = 0;
static int pearInventory = 0;
static double nyApplePrice , nyPearPrice ;
static double laApplePrice , laPearPrice ;
static double applePrice , pearPrice ;
static int city;
public static void main(String[] args) {
for (int day = 1; day <= NUMBER_OF_DAYS; day++){
nyApplePrice = computePrice(BASE_PRICE, VARIATION);
nyPearPrice = computePrice(BASE_PRICE, VARIATION);
laApplePrice = computePrice(BASE_PRICE, VARIATION);
laPearPrice = computePrice(BASE_PRICE, VARIATION);
System.out.println("Day: "+day+" out of 10");
chooseCity();
int choice;
do{
printMenu();
choice = getChoice();
switch (choice){
case 1://Print cash balance and inventory
System.out.println("Cash: "+currencyFormatter(cash));
System.out.println("Apple inventory: " + appleInventory);
System.out.println("Pear inventory: "+pearInventory);
break;
case 2: //Print today's prices
System.out.println("Price of apples is: "+currencyFormatter(applePrice));
System.out.println("Price of pears is: "+currencyFormatter(pearPrice));
break;
case 3: //Buy apples
System.out.println("You can afford to buy "+((int) (cash/applePrice))+" apples.");
int amount = getQuantity("apples","buy");
if (!buyApples(amount)){
System.out.println("You don't have enough money");
}
break;
case 4: //Sell apples
System.out.println("You have "+ appleInventory +" apples.");
int amountx = getQuantity("apples","sell");
if (!sellApples(amountx)){
System.out.println("You don't have enough apples.");
}
break;
case 5: //buy pears
System.out.println("You can afford to buy "+((int) (cash/pearPrice))+" pears.");
int amounty = getQuantity("pears","buy");
if (!buyPears(amounty)){
System.out.println("You don't have enough money");
}
break;
case 6: //sell pears
System.out.println("You have "+ pearInventory +" pears.");
int amountz = getQuantity("pears","sell");
if (!sellPears(amountz)){
System.out.println("You don't have enough pears");
}
break;
}
System.out.println();
} while (choice !=7);
}
System.out.println("You finished with:"+currencyFormatter(cash));
if (cash>INITIAL_CASH){
System.out.println("Congratulations, you increased your initial investment "+ (int)(cash/INITIAL_CASH) +" times");
}else{
System.out.println("Unfortunately you only have "+ (cash/INITIAL_CASH*100) +"% of your initial investment remaining.");
}
}
public static void printMenu(){
System.out.println("1. Print cash balance and inventory");
System.out.println("2. Print today's prices");
System.out.println("3. Buy apples");
System.out.println("4. Sell apples");
System.out.println("5. Buy pears");
System.out.println("6. Sell pears");
System.out.println("7. I am done for today");
}
public static int getChoice(){
Scanner keyboard = new Scanner(System.in);
int choice;
do{
System.out.print("Your choice: ");
choice = keyboard.nextInt();
} while (choice > 7 || choice <1);
return choice;
}
public static String currencyFormatter(double amount){
DecimalFormat myFormatter = new DecimalFormat("$###,###.00");
return myFormatter.format(amount);
}
public static double computePrice(double basePrice, double variation){
Random random = new Random();
double y = random.nextDouble()*variation;
if(random.nextBoolean()){
basePrice = basePrice+y;
}else{
basePrice = basePrice-y;
}
return (((int)(basePrice*100))/100.0);
}
public static int getQuantity(String product, String action){
System.out.print("How many "+ product+" do you want to "+action+"? ");
Scanner keyboard = new Scanner(System.in);
return keyboard.nextInt();
}
public static boolean sellApples(int amount){
if (amount>appleInventory){
return false;
}
cash += amount * applePrice;
appleInventory-= amount;
return true;
}
public static boolean sellPears(int amount){
if(amount>pearInventory){
return false;
}
cash += amount * pearPrice;
pearInventory-= amount;
return true;
}
public static boolean buyApples(int amount){
if(amount*applePrice<cash){
cash -= amount * applePrice;
appleInventory += amount;
return true;
}
return false;
}
public static boolean buyPears(int amount){
if (amount * pearPrice < cash){
cash -= amount * pearPrice;
pearInventory += amount;
return true;
}
return false;
}
public static void chooseCity(){
System.out.println("Cash: "+currencyFormatter(cash));
System.out.println("Apple inventory: " + appleInventory);
System.out.println("Pear inventory: "+pearInventory);
System.out.println("Price of apples in NY: "+currencyFormatter(nyApplePrice));
System.out.println("Price of pears in NY: "+currencyFormatter(nyPearPrice));
System.out.println("Price of apples in LA: "+currencyFormatter(laApplePrice));
System.out.println("Price of pears in LA: "+currencyFormatter(laPearPrice));
System.out.println("1. Go to New York");
System.out.println("2. Go to Los Angeles");
Scanner keyboard = new Scanner(System.in);
int choice;
do{
System.out.print("Your choice: ");
choice = keyboard.nextInt();
} while (choice > 2 || choice <1);
if(choice == 1){//if NY
applePrice = nyApplePrice;
pearPrice = nyPearPrice;
city = 1;
}else{//if LA
applePrice = laApplePrice;
pearPrice = laPearPrice;
city = 2;
}
}
}
import java.text.*;
public class Arithmetic {
static final int NUMBER_OF_DAYS=10;
static final double BASE_PRICE = 10;
static final double VARIATION = 5 ;
static final double INITIAL_CASH=100;
static double cash = INITIAL_CASH;
static int nyAppleInventory;
static int nyPearInventory;
static int laAppleInventory;
static int laPearInventory;
static int appleInventory = 0;
static int pearInventory = 0;
static double nyApplePrice , nyPearPrice ;
static double laApplePrice , laPearPrice ;
static double applePrice , pearPrice ;
static int city;
public static void main(String[] args) {
for (int day = 1; day <= NUMBER_OF_DAYS; day++){
nyApplePrice = computePrice(BASE_PRICE, VARIATION);
nyPearPrice = computePrice(BASE_PRICE, VARIATION);
laApplePrice = computePrice(BASE_PRICE, VARIATION);
laPearPrice = computePrice(BASE_PRICE, VARIATION);
System.out.println("Day: "+day+" out of 10");
chooseCity();
int choice;
do{
printMenu();
choice = getChoice();
switch (choice){
case 1://Print cash balance and inventory
System.out.println("Cash: "+currencyFormatter(cash));
System.out.println("Apple inventory: " + appleInventory);
System.out.println("Pear inventory: "+pearInventory);
break;
case 2: //Print today's prices
System.out.println("Price of apples is: "+currencyFormatter(applePrice));
System.out.println("Price of pears is: "+currencyFormatter(pearPrice));
break;
case 3: //Buy apples
System.out.println("You can afford to buy "+((int) (cash/applePrice))+" apples.");
int amount = getQuantity("apples","buy");
if (!buyApples(amount)){
System.out.println("You don't have enough money");
}
break;
case 4: //Sell apples
System.out.println("You have "+ appleInventory +" apples.");
int amountx = getQuantity("apples","sell");
if (!sellApples(amountx)){
System.out.println("You don't have enough apples.");
}
break;
case 5: //buy pears
System.out.println("You can afford to buy "+((int) (cash/pearPrice))+" pears.");
int amounty = getQuantity("pears","buy");
if (!buyPears(amounty)){
System.out.println("You don't have enough money");
}
break;
case 6: //sell pears
System.out.println("You have "+ pearInventory +" pears.");
int amountz = getQuantity("pears","sell");
if (!sellPears(amountz)){
System.out.println("You don't have enough pears");
}
break;
}
System.out.println();
} while (choice !=7);
}
System.out.println("You finished with:"+currencyFormatter(cash));
if (cash>INITIAL_CASH){
System.out.println("Congratulations, you increased your initial investment "+ (int)(cash/INITIAL_CASH) +" times");
}else{
System.out.println("Unfortunately you only have "+ (cash/INITIAL_CASH*100) +"% of your initial investment remaining.");
}
}
public static void printMenu(){
System.out.println("1. Print cash balance and inventory");
System.out.println("2. Print today's prices");
System.out.println("3. Buy apples");
System.out.println("4. Sell apples");
System.out.println("5. Buy pears");
System.out.println("6. Sell pears");
System.out.println("7. I am done for today");
}
public static int getChoice(){
Scanner keyboard = new Scanner(System.in);
int choice;
do{
System.out.print("Your choice: ");
choice = keyboard.nextInt();
} while (choice > 7 || choice <1);
return choice;
}
public static String currencyFormatter(double amount){
DecimalFormat myFormatter = new DecimalFormat("$###,###.00");
return myFormatter.format(amount);
}
public static double computePrice(double basePrice, double variation){
Random random = new Random();
double y = random.nextDouble()*variation;
if(random.nextBoolean()){
basePrice = basePrice+y;
}else{
basePrice = basePrice-y;
}
return (((int)(basePrice*100))/100.0);
}
public static int getQuantity(String product, String action){
System.out.print("How many "+ product+" do you want to "+action+"? ");
Scanner keyboard = new Scanner(System.in);
return keyboard.nextInt();
}
public static boolean sellApples(int amount){
if (amount>appleInventory){
return false;
}
cash += amount * applePrice;
appleInventory-= amount;
return true;
}
public static boolean sellPears(int amount){
if(amount>pearInventory){
return false;
}
cash += amount * pearPrice;
pearInventory-= amount;
return true;
}
public static boolean buyApples(int amount){
if(amount*applePrice<cash){
cash -= amount * applePrice;
appleInventory += amount;
return true;
}
return false;
}
public static boolean buyPears(int amount){
if (amount * pearPrice < cash){
cash -= amount * pearPrice;
pearInventory += amount;
return true;
}
return false;
}
public static void chooseCity(){
System.out.println("Cash: "+currencyFormatter(cash));
System.out.println("Apple inventory: " + appleInventory);
System.out.println("Pear inventory: "+pearInventory);
System.out.println("Price of apples in NY: "+currencyFormatter(nyApplePrice));
System.out.println("Price of pears in NY: "+currencyFormatter(nyPearPrice));
System.out.println("Price of apples in LA: "+currencyFormatter(laApplePrice));
System.out.println("Price of pears in LA: "+currencyFormatter(laPearPrice));
System.out.println("1. Go to New York");
System.out.println("2. Go to Los Angeles");
Scanner keyboard = new Scanner(System.in);
int choice;
do{
System.out.print("Your choice: ");
choice = keyboard.nextInt();
} while (choice > 2 || choice <1);
if(choice == 1){//if NY
applePrice = nyApplePrice;
pearPrice = nyPearPrice;
city = 1;
}else{//if LA
applePrice = laApplePrice;
pearPrice = laPearPrice;
city = 2;
}
}
}
Subscribe to:
Comments (Atom)