(c++) This program is to keep track of customers who owe your company money. The
ID: 3851641 • Letter: #
Question
(c++) This program is to keep track of customers who owe your company money. The data and each node should contain the customer’s name and the amount owed.
Data should be stored in a circular linked list. Your program should be able to create a list, add a customer to the list in sorted order by customerName, delete a customer from the list, output a display of the list, store the current updated list to a file and import a list from a file(data.txt). Lastly, the program should be able to list all customer who owe $50 or more.
The program should also have a try/throw exception handler. The handler should activate if a customer owed amount is a negative value.
The data.txt contains: JerryJones 400 MiraMartin -55.02 HarryTuna 302.6 RalphRobotics 402.2 KFC Abc Catapillar 300.10 Kaiser 1322.12 Dell 721.80 RelaysCo 159.0 Goupon 385. Gimini -65 Safeway 49.5
Explanation / Answer
The answer is as follows:
The code is as follows:
#include<iostream.h>
#include<string>
#include<exception>
struct node {
string name;
double amt;
node *next;
};
class amount_negative_exception : public exception{
public:
const char * what() const throw()
{
return "Amount negative Exception ";
}
};
class Customer_list
{
private:
node *last;
public:
customer_list()
{
last = NULL;
}
void add(string name, double amt){
struct node *temp, *p;
temp = new(struct node);
temp->name = name;
try {
if (amt < 0){
amount_negative_exception a;
throw a;
}
temp->amt = amt;
}
catch (Exception e)
{
cout <<e.what();
}
if (amt < 0)
return;
if (last == NULL)
{
last = temp;
temp->next = last;
}
else
{
if (last->next == last){
if (temp->name > last->name){
temp->next = last->next;
last->next = temp;
last = temp;
}
else {
temp->next = last;
last->next = temp;
}
}
else {
p = last->next;
while (p->next = last->next){
if (p->next->name > temp->name){
temp->next = p->next;
p->next = temp;
return;
}
}
temp->next = last->next;
last->next = temp;
last = temp;
}
}
}
void delete(string name){
struct node *p;
int found;
found = 1;
if (last != NULL){
if (last->next == last){
if (last->name == name){
last = NULL;
cout << "Node deleted"<< endl;
return;
}
}
else {
p = last;
while (p->next != last){
if (p->next->name == name){
p->next = p->next->next;
cout << "Node deleted"<< endl;
return;
}
}
cout << "Element not found" << endl;
}
}
else {
cout << "List is empty" << endl;
}
}
void display(double amt){
struct node *p;
if (last == NULL){
cout << "List is empty" << endl;
}
else {
if (last->next == last && last->amt >= amt){
cout << last->name << " " << last->amt << endl;
}
else {
p = last->next;
while (p->next != last->next){
if (p->amt >= amt)
cout << p->name << " " << p->amt << endl;
}
}
}
}
void writetofile(){
ofstream outfile;
struct node *p;
outfile.open(:output.txt");
if (last == NULL){
cout << "List is empty" << endl;
}
else {
if (last->next == last){
outfile << last->name << " " << last->amt << endl;
}
else {
p = last->next;
while (p->next != last->next){
outfile << p->name << " " << p->amt << endl;
}
}
}
outfile.close();
}
};
void main() {
int choice;
node *start, *temp, *p;
int n;
string name, filename;
double amt;
Customer_list list;
ifstream infile;
start = NULL;
do {
cout << "1.Create a list" << endl;
cout << "2.Add a Customer" << endl;
cout << "3.Delete a Customer" << endl;
cout << "4.Display the list" << endl;
cout << "5. Store the list in a file" << endl;
cout << "6.Import data from the file" << endl;
cout << "7.Dispaly customers who owe $50 or more" << endl;
cout << "8.Quit" << endl;
switch (choice) {
case 1:
cout << "Enter number of customers" << endl;
cin >> n;
for (int i = 0; i<n; i++){
temp = new node;
cout << "Enter name:" ;
cin >> temp->name;
cout << "Enter amt:" ;
cin >> temp->amt;
list.add(name, amt);
}
case 2:
cout << "Enter name:" ;
cin >> temp->name;
cout << "Enter amt:" ;
cin >> temp->amt;
list.add(name, amt);
case 3:
cout << "Enter name:" ;
cin >> temp->name;
list.delete(name);
case 4:
list.display(0);
case 5:
list.writetofile();
case 6:
cout << "Enter filename:";
cin >> filename;
infile.open(filename);
while (infile >> name >> amt){
list.add(name,amt);
}
case 7:
list.display(50);
}
} while (choice != 8)
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.