In: Computer Science
You can use any language. You have to provide all the data, output and input.
You are to use Linked Lists to do this Program.
The XYZ Widget store receives shipments of widgets at various costs. The store’s policy is to charge a 30% markup, and to sell widgets which were received earlier before widgets which were received later. This is called a FIFO policy.
Write a program using linked lists that reads in three types of input data and does the following:
A sales record which contains an “S” in column 1 and a quantity which represents the number of Widgets sold
A receipt record which contains an “R” in column 1 and a quantity and a price which represent the receipt of a quantity of widgets at the stated cost per widget.
A promotion card which contains a “P” in column 1 and a number such as 25 which would represent a 25% discount to the next 2 buying customers (the next 2 sales cards )
The program should
Print a message after each receipt record is read in with the price of the widgets received.
Print a message after each promotion card is read in with the amount of discount the next to customers will be receiving.
After a sales record is read in print a message stating the number sold and the price of each widget and total price of the order. For example if 200 widgets were sold and there were 50 widgets at $1.00 and 100 at $2.00 and 50 at $3.00 print (recall the 30% markup and the FIFO policy)
200 Widgets sold
50 at 1.30 each Sales: $ 65.00
100 at 2.60 each Sales: $ 260.00
50 at 3.90 each Sales: $ 195.00
Total Sale: $ 520.00
If there are an insufficient number of widgets in stock to fill an order sell as many as are available and then print
“ Remainder of xxx Widgets not available”
Do not forget the promotional discount.
At the end of the data before exiting the program print out under a separate heading the widgets still left in stock and their original purchase price.
Data for program
R/S/P # widgets or Price
discount%
R 150 $1.00
R 130 $2.00
S 145
R 50 $2.50
S 75
S 180
R 50 $4.00
R 30 $5.00
R 40 $5.50
P 30%
S 50
S 30
R 50 $6.00
R 265 $10.00
S 60
P 50%
S 100
S 70
S 175
R 40 $14.00
R 75 $15.00
S 110
R 30 $16.00
R 40 $18.00
CONSIDERING THE CONDITIONS AND PARMETERSFROM QUESTION WE SHOW THE WORK AS:
AVA CODE :
import java.util.*;
import java.io.*;
class Node{
char col1;
int col2;
double price;
Node next = null;
public Node(char col1,int col2,double price){
this.col1 = col1;
this.col2 = col2;
this.price = price;
}
public Node(char col1,int col2){
this.col1 = col1;
this.col2 = col2;
}
}
public class LinkedList{
Node head = null;
Node tail = null;
void addNode(char col1,int col2, double price){
Node newNode = new Node(col1,col2,price);
if(head==null){
head = newNode;
tail = newNode;
}else{
tail.next = newNode;
tail = newNode;
}
}
void addNode(char col1,int col2){
Node newNode = new Node(col1,col2);
if(head==null){
head = newNode;
tail = newNode;
}else{
tail.next = newNode;
tail = newNode;
}
}
Node removeNode(){
if(head==null){return null;}
Node newNode = head;
head = head.next;
return newNode;
}
void readFile(){
String filename = "data.txt";
try{
FileInputStream fstream = new FileInputStream(filename);
Scanner fileIn = new Scanner(fstream);
while(fileIn.hasNextLine()){
String line = fileIn.nextLine();
String[] arr = line.split("\\s+");
if(arr.length==3){
char col1 = arr[0].charAt(0);
int col2 = Integer.parseInt(arr[1]);
double price = Double.parseDouble(arr[2]);
addNode(col1,col2,price);
}else if(arr.length==2){
char col1 = arr[0].charAt(0);
int col2 = Integer.parseInt(arr[1]);
addNode(col1,col2);
}
}
}catch(FileNotFoundException e){
System.out.println(filename + " not found. Exiting the program.");
}
}
void loopList(){
Node curr = head,sale = head;
int numCustomers = 0;
double currDiscount = 0;
while(curr!=null){
if(curr.col1=='R'){
double totalPrice = curr.col2*curr.price;
System.out.println(curr.col2 + " widgets received at " + curr.price + " each.");
System.out.println("Total Received: $" + totalPrice);
}else if(curr.col1=='P'){
System.out.println("Next 2 customers will receive " + curr.col2 + "% discount.");
numCustomers = 2;
currDiscount = curr.col2;
}else{
Node tempSale = sale;
int totalWidgets = 0;
while(tempSale!=curr && totalWidgets<=curr.col2){
if(tempSale.col1=='R'){
totalWidgets += tempSale.col2;
}
tempSale = tempSale.next;
}
if(totalWidgets>=curr.col2){
totalWidgets = curr.col2;
}
System.out.println(totalWidgets + " Widgets sold");
double totalPrice = 0;
tempSale = sale;
while(tempSale!=curr && totalWidgets>0){
if(tempSale.col1=='R'){
double newPrice = tempSale.price*1.3;
if(tempSale.col2<=totalWidgets){
System.out.println(tempSale.col2 + " at " + newPrice + " each\tSales:$" + newPrice*tempSale.col2);
totalPrice += newPrice*tempSale.col2;
totalWidgets-=tempSale.col2;
tempSale.col2 = 0;
tempSale = tempSale.next;
}else{
System.out.println(totalWidgets + " at " + newPrice + " each\tSales:$" + newPrice*totalWidgets);
totalPrice += newPrice*totalWidgets;
tempSale.col2 -= totalWidgets;
totalWidgets = 0;
}
}else{
tempSale = tempSale.next;
}
}
sale = tempSale;
System.out.println("\t\tTotal Sales:$" + totalPrice);
if(numCustomers>0){
System.out.println("\t\tDiscount:$" + totalPrice*(currDiscount/100));
numCustomers--;
}
System.out.println("\tTotal Sales after Dicount:$" + totalPrice*(1-currDiscount/100));
}
curr = curr.next;
System.out.println();
}
}
public static void main(String[] args) {
LinkedList l = new LinkedList();
l.readFile();
l.loopList();
}
}
Input:-
R 150 1.00
R 130 2.00
S 145
R 50 2.50
S 75
S 180
R 50 4.00
R 30 5.00
R 40 5.50
P 30
S 50
S 30
R 50 6.00
R 265 10.00
S 60
P 50
S 100
S 70
S 175
R 40 14.00
R 75 15.00
S 110
R 30 16.00
R 40 18.00
Sample Output:-
150 widgets received at 1.0 each. Total Received: $158.0
130 widgets received at 2.0 each. Total Received: $260.0
145 Widgets sold 145 at 1.3 each Sales: $188.5 Total Sales: $188.5 Total Sales after Dicount:$188.5
50 widgets received at 2.5 each. Total Received: $125.0
Widgets sold 75 5 at 1.3 each Sales:$6.5 7θ at 2.6 each Sales: $182.0 Total Sales: $188.5 Total Sales after Dicount:$188.5
110 Widgets sold 60 at 2.6 each Sales:$156.0 50 at 3.25 each Sales: $162.5 Total Sales: $318.5 Total Sales after Dicount:$318.5
50 widgets received at 4.0 each. Total Received: $200.0
30 widgets received at 5.0 each. Total Received: 150.0
40 widgets received at 5.5 each. Total Received: $220.0
Next 2 custome rs will receive 30% discount.
50 Widgets sold 5θ at 5.2 each Sales: $268.0 Total Sales: $268. 0 Discount : $78. 0 Total Sales after Dicount: $182.0
30 Widgets sold
PLEASE UPVOTE ITS VERY NECESSARY FOR ME