In: Computer Science
2. Modify assignment 1 solution code I posted to do the following:
a. Change car class to bankAccount class and TestCar class to TestBank class
b. Change mileage to balance
c. Change car info (make, model, color, year, fuel efficiency) to customer first and last name and account balance
d. Change add gas to deposit (without limit)
e. Change drive to withdraw cash
f. Change test car menu to the following Bank Account Menu choices
1 - Deposit
2 - Withdraw
3 - Display account info
4 - Exit
public class Car {
//Attributes
private double fuelEfficiency, mileage, fuelCapacity,
fuelLevel;
private String make, model, color, year;
/**
* @return the make
*/
public String getMake() {
return make;
}
/**
* @param make the make to set
*/
public void setMake(String make) {
this.make = make;
}
/**
* @return the model
*/
public String getModel() {
return model;
}
/**
* @param model the model to set
*/
public void setModel(String model) {
this.model = model;
}
/**
* @return the color
*/
public String getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(String color) {
this.color = color;
}
/**
* @return the year
*/
public String getYear() {
return year;
}
/**
* @param year the year to set
*/
public void setYear(String year) {
this.year = year;
}
/**
* @return the fuelEfficiency
*/
public double getFuelEfficiency() {
return fuelEfficiency;
}
/**
* @param fuelEfficiency the fuelEfficiency to
set
*/
public void setFuelEfficiency(double fuelEfficiency)
{
this.fuelEfficiency =
fuelEfficiency;
}
/**
* @return the mileage
*/
public double getMileage() {
return mileage;
}
/**
* @param mileage the mileage to set
*/
public void setMileage(double mileage) {
this.mileage = mileage;
}
/**
* @return the fuelCapacity
*/
public double getFuelCapacity() {
return fuelCapacity;
}
/**
* @param fuelCapacity the fuelCapacity to set
*/
public void setFuelCapacity(double fuelCapacity)
{
this.fuelCapacity =
fuelCapacity;
}
/**
* @return the fuelLevel
*/
public double getFuelLevel() {
return fuelLevel;
}
/**
* @param fuelLevel the fuelLevel to set
*/
public void setFuelLevel(double fuelLevel) {
this.fuelLevel = fuelLevel;
}
public Car() {
fuelLevel = 0;
fuelCapacity = 0;
mileage = 0;
fuelEfficiency = 0;
}
public Car (double fuelEfficiency, double mileage,
double fuelCapacity) {
this.fuelEfficiency =
fuelEfficiency;
this.mileage = mileage;
this.fuelCapacity =
fuelCapacity;
}
public void drive(double distance) {
double gallons =
distance/fuelEfficiency;
if(fuelLevel <gallons){
System.out.println("Not enough gas!");
}
else {
mileage =
mileage + distance;
fuelLevel =
fuelLevel - gallons;
System.out.println("You have driven " +distance+ "
mile(s).");
System.out.println();
return;
}
}
public void addGas(double fuel) {
if (fuel + fuelLevel <
fuelCapacity){
fuelLevel =
fuelLevel + fuel;
System.out.println("You have added " +fuel+ " gallon(s) of
fuel.");
System.out.println();
return;
}
else {
System.out.println("Please enter an amount of fuel less than it's
capacity.");
}
}
public void displayCar(){
System.out.println();
System.out.println("Make: " +
getMake());
System.out.println("Model: " +
getModel());
System.out.println("Color: " +
getColor());
System.out.println("Year: " +
getYear());
System.out.println("You have driven
" + getMileage() + " miles.");
System.out.println("Your fuel level
is: " + getFuelLevel() + " gallon(s)");
System.out.println("Your fuel
efficiency is " + getFuelEfficiency() + " mpg.");
System.out.println("Your fuel tank
capacity is " + getFuelCapacity() + " gallons.");
System.out.println();
}
}
bankAccount.java:
Raw_code:
public class bankAccount {
//Attributes
private double balance, accountbalance;
private String firstname, lastname;
/**
* @return the name
*/
public String getName() {
return firstname + " "+ lastname;
}
public void setFirst(String first){
this.firstname = first;
}
public void setLast(String last){
this.lastname = last;
}
/**
* @param name the name to set
*/
public void setName(String first, String last) {
this.firstname = first;
this.lastname = last;
}
/**
* @return the balance
*/
public double getBalance() {
return this.balance;
}
/**
* @param balance the balance to set
*/
public void setBalance(double balance) {
this.balance = balance;
}
public bankAccount() {
balance = 0;
firstname = "";
lastname = "";
}
public bankAccount (String first, String last, double balance)
{
this.firstname = first;
this.lastname = last;
this.balance = balance;
}
public void deposit(double money) {
if (money > 0){
this.balance += money;
System.out.println("You have deposited " + money + "
dollors.");
System.out.println();
return;
}
}
public void withdraw(double money) {
if (this.balance > 0 && this.balance > money){
this.balance -= money;
System.out.println("You have withdraw " +money+ " dollors.");
System.out.println();
return;
}
else {
System.out.println("Insufficient balance in Account.");
}
}
public void displayAccount(){
System.out.println();
System.out.println("Name: " + getName());
System.out.println("Balance: " + getBalance());;
System.out.println();
}
}
TestBank.java:
Raw_code:
import java.util.Scanner;
public class TestBank{
public static void menu(){
System.out.println();
System.out.println("Account Menu : ");
System.out.println("1 - Deposit");
System.out.println("2 - Withdraw");
System.out.println("3 - Display Account Info");
System.out.println("4 - Exit");
System.out.println();
}
public static void main(String [] args){
Scanner snr = new Scanner(System.in);
int choice = 1;
double money = 0;
bankAccount b1 = new bankAccount();
System.out.print("Enter the FirstName : ");
b1.setFirst(snr.nextLine());
System.out.print("Enter the LastName : ");
b1.setFirst(snr.nextLine());
while(choice > 0){
menu();
System.out.print("Enter your choice : ");
choice = snr.nextInt();
while (choice < 0 && choice > 5){
System.out.println("Invalid Choice!.");
System.out.print("Enter your choice : ");
choice = snr.nextInt();
}
if (choice == 1){
System.out.print("Enter ammount : ");
money = snr.nextDouble();
b1.deposit(money);
}
else if (choice == 2){
System.out.print("Enter ammount : ");
money = snr.nextDouble();
b1.withdraw(money);
}
else if (choice == 3)
b1.displayAccount();
else if (choice == 4){
System.out.println("Thank you. Hava a nice day");
System.exit(0);
}
}
}
}
output: