Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is my main function: public class Main { public static void main(String[] a

ID: 3753900 • Letter: H

Question

Here is my main function:

public class Main {

public static void main(String[] args) {
ArrayList<Customer> customer = new ArrayList<Customer>();
try (Scanner scanner = new Scanner(new FileInputStream("user.dat"));) {
while (scanner.hasNextLine()) {
String id = scanner.next();
String fname = scanner.next();
String lname = scanner.next();
float money = scanner.nextFloat();
scanner.nextLine(); // clears newlines from the buffer
Customer u = new Customer(id, fname, lname, money);
customer.add(u);
}
} catch (IOException e) {
System.out.println("File not found");
}
}


}

I have the class:

package proj1;

public class Customer {

String id;
String fname, lname;
float moneySpent;

public Customer(String id, String fname, String lname, float moneySpent){
this.id = id;
this.fname = fname;
this.lname = lname;
this.moneySpent = moneySpent;
}

}

I want to make a new class PreferredCustomer (not the main class) that uses class Customer as the parent class. The new class PreferredCustomer has a new field that holds a discount that will be held in example[4]. This new class will be stored in a seperate 1D object array called PreferredCustomer.

Example of a preferred.dat file:

777789 Miles James 97.79 5% (parse off the %, convert it to .95)

12313 Rick Montes 100.00 12% (convert to .88)

743324234 Ana Holme 156.22 15% (convert to .85)

Explanation / Answer

class Main:

package proj1;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Scanner;

public class Main {

public Main() {

// TODO Auto-generated constructor stub

}

public static void main(String[] args) {

ArrayList<Customer> customer = new ArrayList<Customer>();

ArrayList<PreferredCustomer> preferredCustomer=new ArrayList<PreferredCustomer>();

try (Scanner scanner = new Scanner(new FileInputStream("user.dat"));) {

while (scanner.hasNextLine()) {

String id = scanner.next();

String fname = scanner.next();

String lname = scanner.next();

float money = scanner.nextFloat();

float discount=scanner.nextFloat();

scanner.nextLine(); // clears newlines from the buffer

Customer u = new Customer(id, fname, lname, money);

customer.add(u);

PreferredCustomer pc=new PreferredCustomer(lname, lname, lname, money, discount);

preferredCustomer.add(pc);

}

} catch (IOException e) {

System.out.println("File not found");

}

}

}

package proj1;

public class Customer {

String id;

String fname, lname;

float moneySpent;

class Customer:

public Customer(String id, String fname, String lname, float moneySpent){

this.id = id;

this.fname = fname;

this.lname = lname;

this.moneySpent = moneySpent;

}

}

package proj1;

New class PreferredCustomer:

package proj1;

public class PreferredCustomer extends Customer {

   int discount;

   public PreferredCustomer(String id, String fname, String lname, float moneySpent, float discountPercentage) {

       super(id, fname, lname, moneySpent);
       float discountMoney = (discountPercentage * moneySpent) / 100;
       this.discount = (int)(moneySpent - discountMoney);

   }

}

I have done with new class called PreferredCustomer which is subclass to Customer class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote