$25
What is this Lab about ?
This program is for practicing Object Oriented Programming.
You will need to implement a class Account and a simple program to Credit, Debit and Display Account information.
Getting Started
1. Create a class called Lab6. Use the same setup for setting up your class and main method as you did for the previous assignments. Be sure to name your file Lab6.java.
2. Create another class called Account. Be sure to name the file Account.java.
3. Remove the comments and insert correct expression according to the instructions.
4. Write comments about what you are thinking and explain yourselves.
When declaring a variable, you usually want to initialize it.
Use whitespace to make your program more readable.
Use comments after ending brace of classes, methods, and blocks to identify to which block it belongs.
Use Proper Indentation, 4 spaces or tabs.
Now lets begin with the assignment. You will find the instruction in line.
// import all and anything you need
//-->
//Declare the class Lab6
{
//Declare the main method
//-->
{
//Declare a scanner
Scanner scan = new Scanner(System.in);
//Declare a String username, String accountNumber, Double balance //-->
//Ask the user to Enter UserName
//-->
//Scan the input to UserName //-->
//Ask the user for Account Number //-->
//Scan the Input for Account Number
//-->
//Ask the user to Enter Initial Account Balance Amount //-->
System.out.println("Please Enter Initial Account Balance:"); balance = scan.nextDouble();
//Instantiate An Account using its constructor method //-->
//Declare constant Integers Display Balance = 0 , Modify_Name = 1, Deposit = 2,
Withdraw = 3 ,Quit = 4 final int DISPLAY_BALANCE = 0; final int MODIFY_NAME = 1; final int DEPOSIT = 2; final int WITHDRAW = 3; final int QUIT = 4;
//Create a choice variable
//-->
//Now Create A Do-While Loop which Exits if choice == QUIT
//--> Do Statement
{
//Print the Following Options
"This Program Does The Following :-"
"Press 0 to Display User Account Information."
"Press 1 to Modify User Name."
"Press 2 to Make a Deposit."
"Press 3 to Make a Withdrawal."
"Press 4 to Quit."
// Read the value user enters and store it in the choice integer variable. choice = scan.nextInt();
//Create a switch statement with choice variable as input for the 5 cases switch(//What should be the variable here??)
{ case ??? :
//Print the String returned by the toString() method of Account
System.out.println("User Account Information is :");
//Do note this Print statement works only if your variable is called account !!
System.out.println(account.toString());
break; // Don't forget to put breaks after each case ends case ??? :
//Print the Information, Please enter new Name
//Read the value entered by user into a temp variable using scanner : scan.next() !!
//Update the Name value using setName method of Account
//Don't forget what comes here !! case ??? :
// Print the Information, Please enter Deposit Amount //Read the value entered by user into a temp variable using scanner :
scan.nextDouble() !!
//Update the Balance using toCredit method of Account
//Don't forget what comes here !! case ??? :
// Print the Information, Please enter Withdrawal Amount
//Read the value entered by user into a temp variable using scanner : scan.nextDouble() !!
//Update the Balance using toDebit method of Account
//Don't forget what comes here !! case ??? :
//Remember How to Write the Quit Section from Previous Assignments ??
//Don't forget what comes here !!
default:
//Print "Please enter a valid Option."
//Don't forget what comes here !!
} }
while(//What should be the condition here??);
//Don't forget to close your Scanner object
}
}
Thats it for the file called Lab6.java. But we need another file which contains the methods for class Account.
// This file will contain Information about the class Account.
// It has 3 private variables Name, Account Number, Balance
// It has a toString(), setName(), toCredit() and toDebit() methods
// Now Define a public class named Account
//-->
{
//Declare the 3 private variables which are name, accountNumber and balance
//Declare the constructor for the Account class. It looks like public className(VariableType1 variableName1, VariableType2 variableName2 ... for all variables)
{
// remember to set the values of the private variables with the method passed variables
// like if private variable is privateName, and variableName1 is the method passed variable for name
// then privateName = variableName1; !!
}
// Declaring the toString method which returns a String representation of the object. public String toString()
{ return String.format("Name is \t: %s\nAccount Number is \t\t: %s\nBalance is \t\t:
$%.2f",name,accountNumber,balance);
}
// Declaring the setName method which sets the Name of the Account object
{
// Assign the value to the private variable
}
// Declaring the toCredit method which updates the balance the passed amount public void toCredit(Double depositAmount)
{
balance = balance+depositAmount; }
// Declaring the toDebit method which updates the balance the passed amount
{
// Looks similar to above toCredit !!
}
}
And thats it. Do look at the Sample Output below to know how your program execution should look like.
Sample Output
Please Enter Username:
Max
Please Enter Account Number:
ACC1001
Please Enter Initial Account Balance: 50.35
This Program Does The Following :-
Press 0 to Display User Account Information.
Press 1 to Modify User Name.
Press 2 to Make a Deposit.
Press 3 to Make a Withdrawal.
Press 4 to Quit.
0
User Account Information is :
Name is : Max
Account Number is : ACC1001
Balance is : $50.35
This Program Does The Following :-
Press 0 to Display User Account Information.
Press 1 to Modify User Name.
Press 2 to Make a Deposit.
Press 3 to Make a Withdrawal.
Press 4 to Quit.
1
Please enter new Name :
Tom
This Program Does The Following :-
Press 0 to Display User Account Information.
Press 1 to Modify User Name.
Press 2 to Make a Deposit.
Press 3 to Make a Withdrawal.
Press 4 to Quit.
2
Please enter Deposit Amount : 70.35
This Program Does The Following :-
Press 0 to Display User Account Information.
Press 1 to Modify User Name.
Press 2 to Make a Deposit.
Press 3 to Make a Withdrawal.
Press 4 to Quit.
0
User Account Information is :
Name is : Tom
Account Number is : ACC1001
Balance is : $120.70
This Program Does The Following :-
Press 0 to Display User Account Information.
Press 1 to Modify User Name.
Press 2 to Make a Deposit.
Press 3 to Make a Withdrawal.
Press 4 to Quit.
3
Please enter Withdrawal Amount : 44.75
This Program Does The Following :-
Press 0 to Display User Account Information.
Press 1 to Modify User Name.
Press 2 to Make a Deposit.
Press 3 to Make a Withdrawal.
Press 4 to Quit.
0
User Account Information is :
Name is : Tom
Account Number is : ACC1001
Balance is : $75.95 This Program Does The Following :-
Press 0 to Display User Account Information.
Press 1 to Modify User Name.
Press 2 to Make a Deposit.
Press 3 to Make a Withdrawal.
Press 4 to Quit.
4
Thank You. Have a nice day!