[go: up one dir, main page]

0% found this document useful (0 votes)
2 views17 pages

Java Ass2 Siva

The document outlines two programming assignments: an Inventory Management System and an Online Voting System, both developed in Java. The Inventory Management System tracks product stock, sales, and purchases, while the Online Voting System allows users to securely cast votes and view results. Each system includes algorithms, class structures, and main functionalities to manage inventory and voting processes effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Java Ass2 Siva

The document outlines two programming assignments: an Inventory Management System and an Online Voting System, both developed in Java. The Inventory Management System tracks product stock, sales, and purchases, while the Online Voting System allows users to securely cast votes and view results. Each system includes algorithms, class structures, and main functionalities to manage inventory and voting processes effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

SETHU INSTITUTE OF TECHNOLOGY

ARTIFICIAL INTELLIGENCE AND DATA SCIENCE

THINKING IN JAVA 21UAD603

ASSIGNMENT 2

DONE BY
B K SIVANANTHAN

B TECH AI&DS
Thinking in java 21UAD603
Inventory Management System
Aim:

To develop an Inventory Management System that tracks product stock, sales, and purchases,
ensuring efficient inventory control and management.

Algorithm:
1. Initialize Inventory: Load product details such as name, stock, price, and supplier.
2. Add/Update Products: Allow adding new products or updating existing ones.
3. Track Stock Levels: Maintain current stock levels and update them based on sales and
purchases.
4. Sales Management: Deduct stock when a sale occurs.
5. Purchase Management: Increase stock when new stock is purchased.
6. Low-Stock Alerts: Notify the user when stock falls below a set threshold.
7. Display Inventory: Show updated inventory after transactions.

Program
import java.util.*;

class Product {
int id;
String name;
int quantity;
double price;
public Product(int id, String name, int quantity, double price)
{
this.id = id;
this.name = name;
this.quantity = quantity;
this.price = price;
}
}

public class InventoryManagement {


static Scanner sc = new Scanner(System.in);
static List<Product> inventory = new ArrayList<>();

public static void main(String[] args) {


while (true) {
System.out.println("\nInventory Management System");
System.out.println("1. Add Product");
System.out.println("2. Sell Product");
System.out.println("3. Purchase Product");
System.out.println("4. Check Low Stock");
System.out.println("5. Display Inventory");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();

switch (choice) {
case 1:
addProduct();
break;
case 2:
sellProduct();
break;
case 3:
purchaseProduct();
break;
case 4:
checkLowStock();
break;
case 5:
displayInventory();
break;
case 6:
System.out.println("Exiting program...");
return;
default:
System.out.println("Invalid choice. Try again.");
}
}
}

static void addProduct() {


System.out.print("Enter Product ID: ");
int id = sc.nextInt();
sc.nextLine();
System.out.print("Enter Product Name: ");
String name = sc.nextLine();
System.out.print("Enter Quantity: ");
int quantity = sc.nextInt();
System.out.print("Enter Price: ");
double price = sc.nextDouble();

inventory.add(new Product(id, name, quantity, price));


System.out.println("Product added successfully!");
}

static void sellProduct() {


System.out.print("Enter Product ID to Sell: ");
int id = sc.nextInt();
for (Product p : inventory) {
if (p.id == id) {
System.out.print("Enter Quantity to Sell: ");
int sellQty = sc.nextInt();
if (sellQty > p.quantity) {
System.out.println("Not enough stock!");
} else {
p.quantity -= sellQty;
System.out.println("Product sold successfully!");
}
return;
}
}
System.out.println("Product not found!");
}

static void purchaseProduct() {


System.out.print("Enter Product ID to Purchase: ");
int id = sc.nextInt();
for (Product p : inventory) {
if (p.id == id) {
System.out.print("Enter Quantity to Add: ");
int addQty = sc.nextInt();
p.quantity += addQty;
System.out.println("Stock updated successfully!");
return;
}
}
System.out.println("Product not found!");
}

static void checkLowStock() {


System.out.println("Low Stock Products (Less than 5
units):");
for (Product p : inventory) {
if (p.quantity < 5) {
System.out.println(p.name + " - " + p.quantity + "
units left");
}
}
}

static void displayInventory() {


System.out.println("\nCurrent Inventory:");
System.out.println("ID\tName\tQuantity\tPrice");
for (Product p : inventory) {
System.out.println(p.id + "\t" + p.name + "\t" +
p.quantity + "\t\t" + p.price);
}
}
}

output

Result
Thus the above program was executed and the output is verified.
2. Online voting system
Aim
To develop an Online Voting System where users can securely cast votes for elections or
surveys. The system ensures authentication, real-time vote counting, and accurate result
generation.

Algorithm
1. Initialize the system with predefined candidates.
2. Authenticate users and check eligibility.
3. Display available candidates and allow voting.
4. Store votes securely and prevent multiple voting.
5. Count votes in real-time and declare results.

Program
import java.util.*;

class Voter {
String voterID;
boolean hasVoted;

public Voter(String voterID) {


this.voterID = voterID;
this.hasVoted = false;
}
}

class Candidate {
String name;
int votes;

public Candidate(String name) {


this.name = name;
this.votes = 0;
}
}

public class OnlineVotingSystem {


static Scanner sc = new Scanner(System.in);
static Map<String, Voter> voters = new HashMap<>();
static List<Candidate> candidates = new ArrayList<>();
public static void main(String[] args) {
initializeCandidates();
initializeVoters();

while (true) {
System.out.println("\n--- Online Voting System ---");
System.out.println("1. Vote");
System.out.println("2. Show Results");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();

switch (choice) {
case 1:
castVote();
break;
case 2:
showResults();
break;
case 3:
System.out.println("Exiting system... Thank you!");
return;
default:
System.out.println("Invalid choice. Try again.");
}
}
}

static void initializeCandidates() {


candidates.add(new Candidate("Alice"));
candidates.add(new Candidate("Bob"));
candidates.add(new Candidate("Charlie"));
}

static void initializeVoters() {


voters.put("V123", new Voter("V123"));
voters.put("V456", new Voter("V456"));
voters.put("V789", new Voter("V789"));
}

static void castVote() {


System.out.print("Enter your Voter ID: ");
String voterID = sc.next();

if (!voters.containsKey(voterID)) {
System.out.println("Invalid Voter ID! Access denied.");
return;
}

Voter voter = voters.get(voterID);


if (voter.hasVoted) {
System.out.println("You have already voted!");
return;
}

System.out.println("\nAvailable Candidates:");
for (int i = 0; i < candidates.size(); i++) {
System.out.println((i + 1) + ". " +
candidates.get(i).name);
}

System.out.print("Enter candidate number to vote: ");


int choice = sc.nextInt();

if (choice < 1 || choice > candidates.size()) {


System.out.println("Invalid choice! Try again.");
return;
}

candidates.get(choice - 1).votes++;
voter.hasVoted = true;
System.out.println("Vote cast successfully!");
}

static void showResults() {


System.out.println("\n--- Voting Results ---");
for (Candidate c : candidates) {
System.out.println(c.name + ": " + c.votes + " votes");
}
}
}
Output
Result
Thus the above program was executed and the output is verified

You might also like