I would like some help on this java program! Thank you! Part 1: Bank Account (50
ID: 3668842 • Letter: I
Question
I would like some help on this java program! Thank you!
Part 1: Bank Account (50 points)
Write a class to describe a BankAccount object. A bank account is described by:
the account owner's name
an account ID (stored as text)
the balance in the account
BankAccount Class (35 points)
In your class, include:
instance data variables
two constructors
one constructor takes an initial balance; the second constructor opens an account with no money
getters and setters
include appropriate value checks when applicable
a toString method
you can decide the output
a deposit method
include appropriate value checks
a withdrawal method
include appropriate value check
Driver Program (15 points)
Write a driver program to demonstrate your class, including:
creating bank account objects using both constructors
making deposits and withdrawals (with both valid and invalid values)
invoking other methods
Note: your driver program does not need to be interactive with the user. It only needs to create objects and invoke methods on those objects to demonstrate that your class works.
Extra Credit (5 points)
Include an interest rate as part of what describes a bank account. Update the BankAccount class as necessary. You should be able to adjust the interest rate and add interest. Add code to the driver program to demonstrate the interest rate functionality.
Notes
For full credit, follow good principles of programming and object-oriented design.
Part 2: Course Registration (50 points)
Write a Student and Course class that might be part of a course registration system.
A student is described by:
first name
last name
student ID (as text)
whether or not tuition is paid
A course is described by:
a name
the maximum number of students that can be enrolled in the class
a roster of students enrolled in the course (stored as a Student[])
Student Class (10 points)
In your class, include:
instance data variables
a constructor
getters and setters
a toString method
Course Class (40 points)
In your class, include
instance data variables
a constructor
a course is initially created with no students on the roster
getters and setters
this about which variables should have setters
include validity checks where appropriate
a toString method
an addStudent method
method header: public boolean addStudent(Student student)
if there is room for a student and the student has paid their tuition, add the student to the roster
a dropStudent method
method header: public boolean dropStudent(Student student)
if the student is on the roster, remove them
a toString method
include the name, number of students enrolled in the course, maximum number that can be enrolled, and a printed roster as part of the text representation
make sure that the roster does not print any "nulls"
Notes
You can include additional instance data variables if it is helpful. If you include additional variables, include additional getters and setters as appropriate.
I have provided a driver program you can use to test your code and sample output.
I also provided the sample output generated from the driver program.
You might add additional code to the driver to run additional tests to make sure your classes work.
For full credit, follow good principles of programming and object-oriented design.
Extra Credit (15 points)
Write a class called CourseAL. This class has the same methods as Course. Instead of using a Student[] to store the roster, use an ArrayList<Student>. For full credit, take advantage of the methods in the ArrayList class to implement the methods.
Note: If completing the extra credit, you must submit both the Course and CourseAL classes.
Explanation / Answer
main.java
import java.text.NumberFormat;
import java.util.Scanner;
public class HelloWorld
{
public static void main(String[] args)
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
//Make a Scanner object to get the input
Scanner scan = new Scanner(System.in);
double enteredAmount;
char command;
boolean flag;
String input;
// print intro message
System.out.println("Welcome to Canara BANK");
System.out.print("What is your name:");
String enteredName = scan.nextLine();
// Ask for id
System.out.print("What is your bank id? ");
String enteredID = scan.nextLine();
// Ask user for initial deposit
do
{
System.out.print("Initial deposit into Checking: ");
enteredAmount = scan.nextDouble();
// make sure they enter a positive number
if ( enteredAmount <= 0 )
{
System.out.println("Invalid choice (number must be positive).");
}
} while ( enteredAmount <= 0 );
BankAccount account1= new BankAccount(enteredName, enteredID, enteredAmount);
// print the menu
printMenu();
do
{
// ask a user to choose a command
System.out.println(" Please enter a command or type ?");
input = scan.next();
command = input.charAt(0);
switch (command)
{
case 'a': // Ask user for initial deposit
case 'A':
do
{
System.out.print("Amount to deposit: ");
enteredAmount = scan.nextDouble();
// make sure they enter a positive number
if ( enteredAmount <= 0 )
{
System.out.println("Invalid choice (number must be positive).");
}
} while ( enteredAmount <= 0 );
account1.deposit(enteredAmount);
System.out.println("You deposited " + fmt.format(enteredAmount) + " to Checking.");
//print a prompt after the deposit
break;
case 'b':
case 'B':
do
{
System.out.print("Amount to withdraw: ");
enteredAmount = scan.nextDouble();
// make sure amount is positive and less than current checking balance
if (enteredAmount <= 0)
System.out.println("Invalid choice (number must be positive).");
} while (enteredAmount <= 0);
// update balance and display message
flag = account1.withdraw(enteredAmount);
if (flag)
System.out.println("You withdrew " + fmt.format(enteredAmount));
else
System.out.println("Invalid choice (not sufficient fund)");
break;
case 'c': // print the info on each account
System.out.println(account1.toString());
break;
case 'd':
case 'D': // ask for id and instantiate a new account using the entered id
// but same balance from account1
// then use the method equals to check to see if the ids are the same or not!
System.out.print("What is your bank name: ");
enteredName = scan.next();
// Ask for id
System.out.print("What is your bank id? ");
enteredID = scan.next();
// Ask user for initial deposit
BankAccount account2= new BankAccount(enteredName,enteredID, enteredAmount);
flag = account1.equals(account2);
if (flag)
System.out.println("Same account");
else
System.out.println("Not the same account!");
break;
case 'e':
case 'E':
account1.addInterest();
System.out.println(account1.toString());
break;
case '?': // display the menu again
printMenu();
break;
case 'q':// quit the program
break;
default:
System.out.println("Invalid choice!");
break;
}
} while (command != 'q');
} // end of main
// this method prints out the menu to a user
public static void printMenu()
{
System.out.print(" Command Options "
+ "----------------------------------- "
+ "a: deposit "
+ "b: withdraw "
+ "c: display the balance "
+ "d: Check the account "
+ "e: add interest "
+ "?: display the menu again "
+ "q: quit this program ");
}
} // end class
BankAccount.java
import java.text.NumberFormat;
public class BankAccount
{
private String name;
private String ID;
private double Balance;
public BankAccount(String n,String initID, double initBalance)
{
name = n;
ID = initID;
Balance = initBalance;
}
public BankAccount()
{ ID = "???";
Balance = 0;
}
public BankAccount(String initID)
{ initID.equals(ID);
}
public String getID()
{ return ID;
}
public void setName(String n){ //changes initial name of an account
name = n;
}
public void setID(String name)
{ ID=name;
}
public double getBalance()
{ return Balance;
}
public void addInterest()
{
if (Balance < 1000)
{
Balance *= 1.025;
}else if (Balance >= 1000 && Balance < 5000)
{
Balance *= 1.035;
}else if (Balance >= 5000)
{
Balance *= 1.045;
}
}
public boolean withdraw(double withdrawAmount)
{ if(withdrawAmount>0 && withdrawAmount<Balance)
{ Balance -= withdrawAmount;
return true;
}else
return false;
}
public void deposit(double depositAmount)
{ if(depositAmount>=0)
{ Balance += depositAmount;
}
}
public String toString()
{ NumberFormat fmt = NumberFormat.getCurrencyInstance();
String result = "Name: " + name + "ID " + ID + " Balance: " + fmt.format(Balance);
return result;
}
public boolean equals(BankAccount otherAccount)
{ if(ID.equals(otherAccount.getID()))
{ return true;
}else
return false;
}
}
sample output
Welcome to Canara BANK
What is your name:Ram
What is your bank id? 123
Initial deposit into Checking: 5000
Course
main.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Scanner;
public class main {
private static Student s;
public static void main(String[] args) {
Student[] rosters = {new Student ("Adam", "Ant", "S925"),
new Student ("Bob", "Barker", "S713"),
new Student ("Chevy", "Chase", "S512"),
new Student ("Doris", "Day", "S513"),
new Student ("Emilio", "Estevez", "S516")
};
Course c1 = new Course("Media Studies", rosters);
Scanner input = new Scanner( System.in );
System.out.println (" Choose options 1 to add the student and 2 to drop the student");
String option = input.nextLine();
int num = Integer.parseInt(option);
if (num == 1)
{
System.out.println ("Enter Student First Name: ");
String firstName = input.nextLine();
System.out.println ("Enter Student Last Name: ");
String lastName = input.nextLine();
System.out.println ("Enter Student ID: ");
String studentID = input.nextLine();
s = new Student(firstName, lastName, studentID);
c1.addStudent(s);// i call the object of the Course by c1 which i initalize in line22
//System.out.println(s + " could not be added.");
}
else if (num ==2)
{
c1.displayRoster();
System.out.println ("Enter Student First Name: ");
String firstName = input.nextLine();
System.out.println ("Enter Student Last Name: ");
String lastName = input.nextLine();
System.out.println ("Enter Student ID: ");
String studentID = input.nextLine();
s = new Student(firstName, lastName, studentID);
if(!c1.dropStudent(s))
{System.out.println(s + " do not find in the system! ");
}
}
System.out.println(c1);
c1.displayRoster();
}
}
Course.java
import java.util.Iterator;
import java.util.Scanner;
import java.util.Arrays;
public class Course {
private String name;
private int size = 0, drop = 0;
private Student[]roster;
private int maxStudents = 5;
public Course(String name, Student[] studentRoster) {
setName(name);
//setMaxStudents(maxStudents);
roster = new Student [maxStudents];
for (int i =0; i< studentRoster.length; i++)
{
roster[i]= studentRoster[i];
size++;
}
}
public int groupSize()
{
return size;
}
public void displayRoster()
{
for (int i =0; i< roster.length; i++)
{
if (roster[i]!=null)
System.out.printf("%10s%s%s%s ", "",roster[i].lastName, ",", roster[i].firstName);
}
}
public int getMaxStudent() {
return maxStudents;
}
public void setMaxStudents(int maxStudents) {
if(maxStudents > 0) {
this.maxStudents = maxStudents;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean addStudent(Student s)
{
if (groupSize()<=maxStudents)
{
return false;
}
else
{roster[size]= s;
size++;
System.out.println("The student added successfully! ");
//displayRoster();
return true;
}
}
public boolean dropStudent(Student s)
{
for(int i = 0; i <roster.length; i++)
{
if (s.equals(roster[i]))
{
for (int j = i; j < roster.length; j++)
{
roster[i] = roster[j];
drop = 1;
}
size--;
System.out.println ("The student was successfully dropped! ");
}
}
if (drop == 1)
{
// displayRoster();
return true;
}
else
return false;
}
@Override
public String toString()
{
return String.format( "%s %8s%s %8s%s %14s ", name,size, " students enrolled.", maxStudents - size, " spaces available.", "Roster:");
// System.out.println(Arrays.toString(roster));
// System.out.println(java.util.Arrays.toString(roster));
}
}
Student.java
public class Student {
public String firstName, lastName, id;
private boolean tuitionPaid;
public Student(String firstName, String lastName, String id, boolean tuitionPaid) {
setFirstName(firstName);
setLastName(lastName);
setID(id);
setTuitionPaid(tuitionPaid);
}
public Student(String firstName, String lastName, String id) {
this(firstName, lastName, id, true);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public boolean isTuitionPaid() {
return tuitionPaid;
}
public void setTuitionPaid(boolean tuitionPaid) {
this.tuitionPaid = tuitionPaid;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student otherStudent = (Student) obj;
return id.equalsIgnoreCase(otherStudent.id);
} else {
return false;
}
}
@Override
public String toString() {
return firstName + " " + lastName + " (" + id + ")";
}
}
Sample output
Choose options 1 to add the student and 2 to drop the student
Course using ArrayList
main.java
public class CourseTester {
public static void main(String[] args) {
Student s1 = new Student("Jane Doe", "Q123", true);
Student s2 = new Student("Sam Smith", "A654", false);
Student s3 = new Student("Al Adams", "Z777", true);
Student s4 = new Student("Ray Jones", "K519", false);
Course c = new Course("Intro to Java", 5);
System.out.println(c);
c.addStudent(s1);
System.out.println(c);
c.addStudent(s2);
System.out.println(c);
c.addStudent(s3);
System.out.println(c);
c.addStudent(s4);
System.out.println(c);
c.dropStudent(s3);
Student s5 = new Student("Frank Fake", "F123", true);
c.dropStudent(s5);
System.out.println(c);
c.dropAllUnpaidStudents();
System.out.println(c);
}
}
Student.java
public class Student implements Comparable {
private String name, id;
private boolean tuitionPaid;
public Student(String name, String id, boolean tuitionPaid) {
this.name = name;
this.id = id;
this.tuitionPaid = tuitionPaid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public boolean isTuitionPaid() {
return tuitionPaid;
}
public void setTuitionPaid(boolean tuitionPaid) {
this.tuitionPaid = tuitionPaid;
}
@Override
public String toString() {
String s = "Name: " + name;
s += " ID: " + id;
s += (tuitionPaid ? " Paid" : " Unpaid");
return s;
}
@Override
public int compareTo(Object obj) {
if(obj instanceof Student) {
Student otherStudent = (Student) obj;
return name.compareTo(otherStudent.name);
} else {
return -1;
}
}
}
Course.java
import java.util.*;
public class Course {
private String name;
private int capacity;
private ArrayList<Student> studentList;
public Course(String name, int capacity) {
this.name = name;
this.capacity = capacity;
studentList = new ArrayList<Student>();
}
public boolean addStudent(Student s) {
int numberEnrolled = studentList.size();
if(numberEnrolled < capacity) {
// there is room- add the student
return studentList.add(s);
} else {
// no room!
return false;
}
}
public boolean dropStudent(Student s) {
return studentList.remove(s);
}
public int dropAllUnpaidStudents() {
// cycle through the list and remove only those
// students that haven't paid
int droppedCount = 0;
Iterator<Student> iterator = studentList.iterator();
while(iterator.hasNext()) {
Student s = iterator.next();
boolean paid = s.isTuitionPaid();
if(!paid) {
iterator.remove();
droppedCount++;
// whenever you are iterating through a list/collection and you need to remove
// an element, ONLY use the remove method of the iterator- NOT the underlying list/collection
}
}
return droppedCount;
}
public boolean isEnrolled(Student s) {
return studentList.contains(s);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
if(capacity > 0) {
this.capacity = capacity;
}
}
@Override
public String toString() {
String s = "Course " + name + " ";
Collections.sort(studentList);
Iterator<Student> iterator = studentList.iterator();
while(iterator.hasNext()) {
Student student = iterator.next();
s += student.toString() + " ";
}
return s;
}
}
Sample output
Course Intro to Java
Name: Al Adams ID: Z777 Paid