Hello World!
It's a brand new year, and that entails new beginnings once again.
Here's a bit of review of some codes I've done in the past year.
Happy New Year and Welcome to 2011! ^.^
----------------------------------------------------------------------------------------------------------
import java.util.Scanner;
import java.text.NumberFormat;
public class QC_Price {
/**
* This program is intended to approximate the total cost of items purchased after taxes.
* @author A. Wright
* @version 1.0, March 2010
*/
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
double PST=0.07875, GST=0.05;
double subtotal=0,total;
int op1, items;
double[] prices;
NumberFormat currencyFormatter;
System.out.println("Would you like to:"
+"\n1.Calculate sub-total AND total costs?"
+"\n2.Calculate ONLY total cost?");
op1=kb.nextInt();
while(op1<1||op1>2){
System.out.println("That is not a valid option.\nPlease enter a valid option:");
System.out.println("Would you like to:"
+"\n1.Calculate sub-total AND total costs?"
+"\n2.Calculate ONLY total cost?");
op1=kb.nextInt();
}
if(op1==1){
System.out.println("Please enter number of items to be inputed:");
items=kb.nextInt();
while(items<0){
System.out.println("That is not a valid number.\nPlease enter a positive value:");
System.out.println("Please enter number of items to be inputed:");
items=kb.nextInt();
}
if(items==0){
System.out.println("No item quantity was specified.\nSystem aborting operation.");
System.exit(0);
}
prices=new double[items];
System.out.println("Please enter item prices as advertized:");
for(int i=0;i<items;i++){
System.out.print((i+1)+". ");
prices[i]=kb.nextDouble();
subtotal += prices[i];
}
String currencySubtotal,currencyGST,currencyPST,currencyTotal;
currencyFormatter=NumberFormat.getCurrencyInstance();
currencySubtotal=currencyFormatter.format(subtotal);
currencyGST=currencyFormatter.format(((subtotal*GST)));
currencyPST=currencyFormatter.format(((subtotal*PST)));
System.out.println("The subtotal amount is: "+currencySubtotal+" CDN.");
total=subtotal+(subtotal*(GST+PST));
currencyTotal=currencyFormatter.format(total);
System.out.println("The total GST amount is: "+currencyGST+" CDN.");
System.out.println("The total PST amount is: "+currencyPST+" CDN.");
System.out.println("The total amount is: "+currencyTotal+" CDN.");
System.exit(0);
}
if(op1==2){
System.out.println("Please enter sub-total costs:");
subtotal=kb.nextDouble();
while(subtotal<0){
System.out.println("That is not a valid price.\nPlease enter a positive value:");
System.out.println("Please enter sub-total costs:");
subtotal=kb.nextDouble();
}
total=subtotal+(subtotal*(GST+PST));
String currencysubtotal,currencygst,currencypst,currencytotal;
currencyFormatter=NumberFormat.getCurrencyInstance();
currencysubtotal=currencyFormatter.format(subtotal);
currencygst=currencyFormatter.format(((subtotal*GST)));
currencypst=currencyFormatter.format(((subtotal*PST)));
currencytotal=currencyFormatter.format(total);
System.out.println("The subtotal amount is: "+currencysubtotal+" CDN.");
System.out.println("The total GST amount is: "+currencygst+" CDN.");
System.out.println("The total PST amount is: "+currencypst+" CDN.");
System.out.println("The total amount is: "+currencytotal+" CDN.");
System.exit(0);
}
}
}
----------------------------------------------------------------------------------------------------------
/*
* Author: A. Wright
* Date Written: November 12, 2010
* Purpose: To properly print out a visual of a month according to the Gregorian Calendar.
* Assignment: 3C
* Class: Programming 1
* Institution: CÉGEP Vanier College
*/
import java.util.Scanner;
public class MonthCalendar {
static Scanner kb = new Scanner(System.in); //Scanner object
static int yearInput, monthInput;
public static int readInt()
{
int inputInteger;
while(! kb.hasNextInt())
{
String lineInput = kb.nextLine();
System.out.println(lineInput+" is not a valid input. Try again...");
System.out.println("Please enter an integer value: ");
}
inputInteger = kb.nextInt();
return inputInteger;
}
public static int readMonth()
{
System.out.println("Enter a month's integer value between [1-12]: ");
monthInput = readInt();
while(monthInput < 1 || monthInput > 12)
{
System.out.println(monthInput+" is not a valid integer value, try again...");
System.out.println("Enter a month's integer value between [1-12]: ");
monthInput = readInt();
}
return monthInput;
}
public static int readYear()
{
System.out.println("Enter a year value greater than or equal to 1582: ");
yearInput = readInt();
while(yearInput < 1582)
{
System.out.println("That is not a valid year value, try again...");
System.out.println("Enter a year value greater than or equal to 1582: ");
yearInput = readInt();
}
return yearInput;
}
public static String getMonthName(int monthValue)
{
String nameOfMonth = null;
switch(monthValue)
{
case 1:
nameOfMonth = "January";
break;
case 2:
nameOfMonth = "February";
break;
case 3:
nameOfMonth = "March";
break;
case 4:
nameOfMonth = "April";
break;
case 5:
nameOfMonth = "May";
break;
case 6:
nameOfMonth = "June";
break;
case 7:
nameOfMonth = "July";
break;
case 8:
nameOfMonth = "August";
break;
case 9:
nameOfMonth = "September";
break;
case 10:
nameOfMonth = "October";
break;
case 11:
nameOfMonth = "November";
break;
case 12:
nameOfMonth = "December";
break;
}
return nameOfMonth;
}
public static boolean isLeapYear(int year)
{
boolean isLeapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
return isLeapYear;
}
public static int getDaysInMonth (int month,int year)
{
int daysInMonth = 0;
switch (month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12: daysInMonth = 31; break;
case 2:
if(isLeapYear(year))
{
daysInMonth = 29;
}
else
daysInMonth = 28;
break;
default : daysInMonth = 30; break;
}
return daysInMonth;
}
public static int zeller(int month, int day, int year)
{
int d = day;
int m = 0;
switch(month)
{
case 1:
m = 11;
break;
case 2:
m = 12;
break;
case 3:
m = 1;
break;
case 4:
m = 2;
break;
case 5:
m = 3;
break;
case 6:
m = 4;
break;
case 7:
m = 5;
break;
case 8:
m = 6;
break;
case 9:
m = 7;
break;
case 10:
m = 8;
break;
case 11:
m = 9;
break;
case 12:
m = 10;
break;
}
if(m == 11 || m == 12)
{
year -= 1;
}
int c = year / 100; //First 2 digits of the year
int y = year % 100; //Last 2 digits of the year
int z = (d + ((13 * m - 1) / 5) + y + (y / 4) + (c / 4) - (2 * c)) % 7; //Zeller's Formula
if(z < 0)
{
z += 7;
}
return z;
}
public static void printSpace(int position)
{
for(int k = 0; k <= position - 1; k++)
{
System.out.print(" ");
}
}
public static void printMonthAndYearCentered(String monthName, int year)
{
String calendarMonthYear = monthName+", "+year;
int startingSpaces = (33/2)-(calendarMonthYear.length()/2);
for(int k = 0; k < startingSpaces; k++)
{
System.out.print(" ");
}
System.out.println(calendarMonthYear);
}
public static void printGregorianCalendar(int month, int year)
{
int daysInMonth = getDaysInMonth(month, year);
int day = 1;
int firstDay = zeller(month, 1, year);
String monthName = getMonthName(month);
System.out.println();
System.out.print(" ");
printMonthAndYearCentered(monthName, year);
System.out.println("\n ---------------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
System.out.println(" --- --- --- --- --- --- ---");
printSpace(firstDay);
for(int w = 0 ;w <= 6 - firstDay; w++)
{
System.out.print(" "+ day++);
}
System.out.println();
for(int y = 0; y <= 6 ; y++)
{
for(int x = 0; x < 7; x++)
{
if(day / 10 ==0)
{
System.out.print(" " + day++);
}
else
{
System.out.print(" " + day++);
}
if(day > daysInMonth ) break;
}
if(day > daysInMonth ) break;
System.out.println();
}
System.out.println("\n ---------------------------------");
}
public static void main(String[] args)
{
int month = readMonth();
System.out.println("Input confirmed as valid. Continue...");
int year = readYear();
System.out.println("Input confirmed as valid. Retrieving Calendar archive...");
printGregorianCalendar(month, year);
}
}
-----------------------------------------------------------------------------------------------------------
@_@ ... ummm.... WHAT?!
ReplyDeleteHaha. I think what I might do from now on is post individual programs in seperate posts instead of allocating them into a big post. ^^;
ReplyDeleteOld, but for month names it's better to do final String[] monthArray = {"", "January", "February", (etc. until December)} then say monthName = monthArray[monthValue]. And for Zeller I just did m = month - 2, then if(m <= 0){m += 12}. Less clutter.
ReplyDeleteHmm, I may go with that. It's been awhile since I've used arrays, but I've been looking for the excuse to use them in my programming. Thanks Tabarnaco.
ReplyDelete