Lab 4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Advanced Programming

Lab 4

Name: D.M. Kulaj Mandara

Student ID = 104491255

Week 4- Lab
Task 4.1

package com.labs.week4;

import java.util.Scanner;

public class AccountDriver {


public static void main(String[] args) {
//creating an array for 5 acc objects
Account[] myAccounts = new Account[5];
Scanner sc = new Scanner(System.in);
int index = 0;

do {
//menu
System.out.println("1. Create Account");
System.out.println("2. Print All Accounts");
System.out.println("3. Deposit");
System.out.println("4. Withdraw");
System.out.println("5. Exit");

int choice = sc.nextInt();

switch(choice) {
case 1:
//create account
if(index < 5) {
System.out.println("Enter Account Details");
System.out.println("Enter Account ID");
sc.nextLine(); // clear buffer
String id = sc.nextLine();
System.out.println("Enter Account Type");
String type = sc.nextLine();
System.out.println("Enter Account Balance");
double bal = sc.nextDouble();

//account obj creation


Account acc = new Account(id, type, bal);
myAccounts[index] = acc;
index++;
} else {
System.out.println("maximun account count is
reached");
}
break;

case 2:
//printing details
for(Account a:myAccounts) {
if(a != null)
a.printDetails();
}
break;
case 3:
//depositing
System.out.println("Enter account ID to deposit : ");
sc.nextLine(); // clearing buffer
String id = sc.nextLine();

//searching for the right account


for(Account a:myAccounts) {
if(a != null && a.getAccountID().equals(id)) {
System.out.println("Enter amount to
deposit");
double amount = sc.nextDouble();
a.deposit(amount);
}
}
break;
case 4:
//withdrawing
boolean accountFound = false;
System.out.println("Enter account ID to withdraw : ");
sc.nextLine(); // clearing buffer
id = sc.nextLine();

//searching for the right account


for(Account a:myAccounts) {
if(a != null && a.getAccountID().equals(id)) {
accountFound = true;
System.out.println("Enter amount to
withdraw");
double amount = sc.nextDouble();

//check whether withdrawal is possible or not


boolean status = a.withdraw(amount);
if(status)
System.out.println("Withdrawal
Successful");
else
System.out.println("Insufficient
Balance!");

}
}
if(!accountFound)
System.out.println("Invalid account ID");

break;
case 5:
System.exit(0);
break;

}while(true);
}
}
Account class:

package com.labs.week4;

public class Account {


String accountID;
String accountType;
double accountBalance;

//parameterized constructor
public Account(String accountID, String accountType, double accountBalance) {
super();
this.accountID = accountID;
this.accountType = accountType;
this.accountBalance = accountBalance;
}

public String getAccountID() {


return accountID;
}

public String getAccountType() {


return accountType;
}

public double getAccountBalance() {


return accountBalance;
}

public void setAccountBalance(double accountBalance) {


this.accountBalance = accountBalance;
}

public void deposit (double amount) {


this.accountBalance = this.accountBalance + amount;
}

public boolean withdraw (double amount) {


if((this.accountBalance - amount) >= 0) {
this.accountBalance = this.accountBalance - amount;
return true;
}
else {

return false;
}
}

public void printDetails() {


System.out.print(" Account ID : "+ getAccountID());
System.out.print(" Type : "+ getAccountType());
System.out.printf(" Balance : %.2f\n", getAccountBalance());
}
}
Console:

Creating two accounts.

Printing those accounts.

Depositing 500 to second account.


Trying to withdraw through invalid account ID.

Withdrawing from valid account and printing.

Trying to withdraw higher amount than the balance.


Exiting.

UML class diagram for the Account class

Account

- AccountID: String
- AccountType: String
- AccountBalance: double

+ Account(AccountID: String,
AccountType: String,
AccountBalance: double)
+ getAccountID(): String
+ getAccountType(): String
+ getAccountBalance(): double
+ setAccountBalance(accountBalance: double): void
+ deposit(amount: double): void
+ withdraw(amount: double): boolean
+ toString(): String
Task 4.2

Code:

Customer class:

package com.labs.week4;

public class Customer {

// Member variables for customer details


private String FirstName;
private String LastName;
private String Address;

// Constructor to initialize customer details


public Customer(String firstName, String lastName, String address) {
this.FirstName = firstName;
this.LastName = lastName;
this.Address = address;
}

// Accessory methods (getters) to retrieve customer details


public String getFirstName() {
return FirstName;
}

public String getLastName() {


return LastName;
}

public String getAddress() {


return Address;
}

@Override
public String toString() {
return "Customer Details: \n" +
"Customer First Name: " + FirstName + "\n" +
"Last Name: " + LastName + "\n" +
"Address: " + Address+ "\n";
}
}

UML class diagram for the Customer class

Customer

- FirstName: String
- LastName: String
- Address: String

+ Customer(firstName: String,
lastName: String,
address: String)
+ getFirstName(): String
+ getLastName(): String
+ getAddress(): String
+ toString(): String
Lab 4_2 class:

package com.labs.week4;

import java.util.Scanner;

public class Lab4_2 {


public static void main(String[] args) {

Customer[] customerObjects = new Customer[3];// creating a array for saving


each customer object

Scanner sc = new Scanner(System.in);

// Loop for inputing details for each customer


for (int i = 0; i < 3; i++) {
System.out.println("Enter Customer Details");
System.out.println("Enter Customer First name");
String firstName = sc.nextLine();

System.out.println("Enter Customer last name");


String lastName = sc.nextLine();

System.out.println("Enter customer Address");


String address = sc.nextLine();

// Using the data supplied, create a new Customer object and save it in
the array.
customerObjects[i] = new Customer(firstName, lastName, address);
}

System.out.println("Customer Details are:");


for (int i = 0; i < 3; i++) {
System.out.println(customerObjects[i].toString()); // Using the
toString() method for each customer to display details
}
}
}

Console:

Entering data for three customer objects and displaying them.

You might also like