COS2614 ASS1 (C-GPT)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Question 1

#include <QApplication>

#include <QInputDialog>

#include <QMessageBox>

#include <QString>

#include <QChar>

#include <QDebug>

#include <QTime>

#include <algorithm>

QString generateUniqueId(const QString &fullName) {

QString uniqueId;

QStringList nameParts = fullName.split(' ', Qt::SkipEmptyParts);

if (nameParts.size() >= 2) {

uniqueId += nameParts[0][0].toUpper(); // First name initial

uniqueId += nameParts.size() > 2 ? nameParts[1][0].toUpper() : nameParts.last()[0].toUpper(); //


Middle or last name initial

} else {

uniqueId = "NN"; // Default initials if less than 2 parts

QString nameWithoutSpaces = fullName;

nameWithoutSpaces.remove(' ');

QString nameLength = QString::number(nameWithoutSpaces.length());

uniqueId += nameLength.rightJustified(4, '0');

return uniqueId;

}
QString generateInitialKey(const QString &fullName) {

QString initialKey;

QString nameWithoutSpaces = fullName;

nameWithoutSpaces.remove(' ');

QTime time = QTime::currentTime();

qsrand(static_cast<uint>(time.msec()));

// Shuffle the characters in the name

std::random_shuffle(nameWithoutSpaces.begin(), nameWithoutSpaces.end());

// Ensure at least one vowel and one consonant

bool hasVowel = false;

bool hasConsonant = false;

for (const QChar &ch : nameWithoutSpaces) {

if (ch.isLetter()) {

if (ch.isLower()) {

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {

hasVowel = true;

} else {

hasConsonant = true;

initialKey += ch;

if (hasVowel && hasConsonant) {

break;

}
// If not enough characters, add random letters

while (initialKey.length() < 6) {

char randomChar = qrand() % ('z' - 'a' + 1) + 'a';

initialKey += QChar(randomChar);

return initialKey.toLower();

int main(int argc, char *argv[]) {

QApplication app(argc, argv);

// Prompt user for full name

QString fullName = QInputDialog::getText(nullptr, "Enter Your Full Name", "Full Name:");

if (!fullName.isEmpty()) {

// Generate unique ID and initial key

QString uniqueId = generateUniqueId(fullName);

QString initialKey = generateInitialKey(fullName);

// Display unique ID and initial key

QMessageBox::information(nullptr, "Generated Information", "Unique ID: " + uniqueId +


"\nInitial Key: " + initialKey);

return app.exec();

Question 2
#include <iostream>

#include <vector>

#include <string>
using namespace std;

class Vendor {

private:

string vendorID;

string name;

string address;

public:

Vendor(const string& vendorID, const string& name, const string& address)

: vendorID(vendorID), name(name), address(address) {}

string toString() const {

return "Vendor ID: " + vendorID + "\nName: " + name + "\nAddress: " + address;

string getName() const {

return name;

};

class Item {

private:

string itemID;

string name;

double price;

public:

Item(const string& itemID, const string& name, double price)

: itemID(itemID), name(name), price(price) {}


string toString() const {

return "Item ID: " + itemID + "\nName: " + name + "\nPrice: $" + to_string(price);

// Accessors

string getName() const {

return name;

double getPrice() const {

return price;

};

class StoreItem : public Item {

private:

Vendor* vendor;

public:

StoreItem(const string& itemID, const string& name, double price)

: Item(itemID, name, price), vendor(nullptr) {}

void setVendor(Vendor* vendor) {

this->vendor = vendor;

string toString(bool includeVendorDetails) const {

if (includeVendorDetails && vendor != nullptr) {

return Item::toString() + "\nVendor Details:\n" + vendor->toString();

} else {
return Item::toString();

string getVendorName() const {

if (vendor != nullptr) {

return vendor->getName();

} else {

return "Unknown";

};

int main() {

vector<StoreItem> items;

// Prompt user to enter item details

cout << "Enter item details (ID, Name, Price) or type 'done' to finish:\n";

string itemID, itemName;

double itemPrice;

while (true) {

cout << "Item ID: ";

cin >> itemID;

if (itemID == "done") {

break;

cout << "Item Name: ";

cin >> itemName;

cout << "Item Price: ";

cin >> itemPrice;

items.emplace_back(itemID, itemName, itemPrice);


}

// Prompt user to enter vendor details

cout << "\nEnter vendor details (ID, Name, Address) or type 'done' to finish:\n";

string vendorID, vendorName, vendorAddress;

vector<Vendor> vendors;

while (true) {

cout << "Vendor ID: ";

cin >> vendorID;

if (vendorID == "done") {

break;

cout << "Vendor Name: ";

cin >> vendorName;

cout << "Vendor Address: ";

cin.ignore(); // Clear input buffer

getline(cin, vendorAddress);

vendors.emplace_back(vendorID, vendorName, vendorAddress);

// Assign vendors to items

for (size_t i = 0; i < items.size(); ++i) {

cout << "\nAssign vendor for item " << i + 1 << " (0-" << vendors.size() << "): ";

int vendorIndex;

cin >> vendorIndex;

if (vendorIndex >= 0 && vendorIndex < vendors.size()) {

items[i].setVendor(&vendors[vendorIndex]);

// Display item details


cout << "\nItem Details:\n";

for (const auto& item : items) {

cout << item.toString(false) << endl;

// Display item details with vendor information

cout << "\nItem Details with Vendor Information:\n";

for (const auto& item : items) {

cout << item.toString(true) << endl;

// Display vendor name for each item

cout << "\nVendor Names for Each Item:\n";

for (const auto& item : items) {

cout << "Item: " << item.getName() << ", Vendor: " << item.getVendorName() << endl;

return 0;

You might also like