Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Using the for loop statement, define a method that reads the amount spent by each customer objects added in the ArrayList and deducts 20%

JAVA

Using the for loop statement, define a method that reads the amount spent by each customer objects added in the ArrayList and deducts 20% of the amount spent if a customer spent more than 100 euros.

Customer.java

public class Customer {

private String name;

private String gender;

private double amountSpent;

public Customer(String n) {

this.name = n;

}

public Customer(String n, String g) {

this.name = n;

this.gender = g;

}

public Customer(String n, String g, double a) {

this.name = n;

this.gender = g;

this.amountSpent = a;

}

public void Display() {

System.out.println("Name: " + name);

System.out.println("Gender: " + gender);

System.out.println("Amount spent: " + amountSpent);

}

@Override

public String toString() {

return "Customer [name=" + name + ", gender=" + gender + ", amountSpent=" + amountSpent + "]";

}

}

CustomerMain.java

import java.util.ArrayList;

import java.util.Scanner;

public class CustomerMain {

public static void main(String[] args) {

ArrayList myList = new ArrayList();

Scanner sc = new Scanner(System.in);

for (int i = 0; i < 3; i++) {

System.out.println("Eneter name: ");

String name = sc.next();

System.out.println("gender: ");

String gender = sc.next();

System.out.println("Eneter amount spent: ");

double amountSpent = sc.nextDouble();

Customer c = new Customer(name, gender, amountSpent);

myList.add(c);

}

sc.close();

Display(myList);

}

public static void Display(ArrayList display) {

for (int i = 0; i < display.size(); i++) {

System.out.println(display.get(i));

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions