Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io.FileInputStream; mport java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Order { private int orderId; private int itemId; private double amount; private String

import java.io.FileInputStream;

mport java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Order {

private int orderId;

private int itemId;

private double amount;

private String description;

public Order(int orderId, int itemId, double amount, String description) {

this.orderId = orderId;

this.itemId = itemId;

this.amount = amount;

this.description = description;

}

public int getOrderId() {

return orderId;

}

public void setOrderId(int orderId) {

this.orderId = orderId;

}

public int getItemId() {

return itemId;

}

public void setItemId(int itemId) {

this.itemId = itemId;

}

public double getAmount() {

return amount;

}

public void setAmount(double amount) {

this.amount = amount;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

@Override

public String toString() {

return "Order{" +

"orderId=" + orderId +

", itemId=" + itemId +

", amount=" + amount +

", description =" + description + '}';

}

}

\color{blue}\underline{Orders.java:} /*

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Orders {

private List orders;

public Orders(String filename) {

try {

Scanner fin = new Scanner(new FileInputStream(filename));

String words[];

orders = new ArrayList<>();

while(fin.hasNextLine()) {

words = fin.nextLine().split(", ");

orders.add(new Order(Integer.parseInt(words[0]),

Integer.parseInt(words[1]), Double.parseDouble(words[2]), words[3]));

}

fin.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

public double getTotal() {

double total = 0;

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

total += orders.get(i).getAmount();

}

return total;

}

public void printOrders() {

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

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

}

}

}

}

\color{blue}\underline{OrderMain.java:

public class OrderMain {

public static void main(String[] args) {

Orders orders = new Orders("orderRecs.csv");

orders.printOrders(); System.out.println("Total order amount: " + orders.getTotal());

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

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

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions