APPLICATION FOR A SHOP
CLASS WINE
namespace AppForAShop
{
class Wine : Article
{
private int year; //the year this wine was bottled
public Wine(int id, String name, double basicPrice, int nrInStock, int year)
: base(id, name, basicPrice, nrInStock)
{
this.year = year;
}
public int Year { get { return this.year; } }
public override double getSellingPrice()
{
return this.getBasicPrice() + (DateTime.Now.Year - this.year) * 2.0;
}
public override String ToString()
{
String holder;
holder = "WINE "+ base.ToString() + " year: " + this.year;
return holder;
}
}
}
CLASS WCARTICLE.cS
namespace AppForAShop
{
class WCArticle : Article
{
private bool onDiscount; //if true, then this article will be sold cheaper.
public WCArticle(int id, String name, double basicPrice, int nrInStock, bool
onDiscount)
: base(id, name, basicPrice, nrInStock)
{
this.onDiscount = onDiscount;
}
public override double getSellingPrice()
{
if(this.onDiscount)
{
return this.getBasicPrice();
}
else
{
return this.getBasicPrice() * 2;
}
}
public override String ToString()
{
String holder;
holder = "WCART " + base.ToString();
if (this.onDiscount) { holder += " is on discount."; }
return holder;
}
}
}
CLASS SHOP.CS
namespace AppForAShop
{
class Shop
{
private String name; //the name of this shop
private List<Article> theArticles; //the articles this shop is selling
public Shop(String name)
{
this.name = name;
this.theArticles = new List<Article>();
}
public String Name { get { return this.name; } }
/// <summary>
/// returns a list of all articles of this shop
/// </summary>
/// <returns></returns>
public List<Article> GetAllArticles()
{
return this.theArticles; ;
}
/// <summary>
/// if the list of this shop contains an article with identity equal to id,
/// then this article is returned
/// else null is returned
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public Article GetArticle(int id)
{
foreach(Article a in this.theArticles)
{
if (a.getID() == id) return a;
}
return null;
}
/// <summary>
/// if the list of this shop does not contain an article with
/// the same id as article a,
/// then article a is added to the list and returnvalue is true
/// else nothing is added to the list and returnvalue is false
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public bool AddArticle(Article a)
{
if (this.GetArticle(a.getID()) == null)
{
this.theArticles.Add(a);
return true;
}
else
{
return false;
}
}
/// <summary>
/// if the list of this shop contains an article with identity equal to idnr,
/// then for this article the number in stock should be lowered by amount
/// and the returnvalue is the total selling price for these amount of
articles
/// else -1 is returned
/// </summary>
/// <param name="idnr"></param>
/// <param name="amount"></param>
/// <returns></returns>
public double SellArticle(int idnr, int amount)
{
Article a = this.GetArticle(idnr);
if (a != null)
{
a.sellSome(amount);
return amount * a.getSellingPrice();
}
else
{
return -1;
}
}
//******************************************************
//IMPORTANT: No changes above this line.
//******************************************************
/// <summary>
/// returns a list of wine that is bottled in year yrr or earlier
/// </summary>
/// <param name="yrr"></param>
/// <returns></returns>
public List<Article> GetListOfOldWine(int yrr)
{
//todo, see assignment 2
List<Article> temp = new List<Article>();
foreach (Article a in this.theArticles)
{
if (a is Wine)
{
if (((Wine)a).Year <= yrr) { temp.Add(a); }
}
}
return temp;
}
public void clearArticleList()
{
this.theArticles.Clear();
}
}
}
CLASS SELLINGEXCEPTION.CS
namespace AppForAShop
{
class SellingException : Exception
{
public SellingException(): base() { }
public SellingException(String message) : base(message) { }
}
}
CLASS IARTICLE.CS
namespace AppForAShop
{
interface IArticle
{
int getID(); //returns the id of this article
double getBasicPrice(); //returns the basic price of this article
double getSellingPrice(); //returns the selling price of this article
void sellSome(int amount); //if the nrInStock >= amount, nrInStock is lowered
by amount
// otherwise an exception is thrown
}
}
CLASS DEPOSITARTICLE.CS
namespace AppForAShop
{
class DepositArticle : Article
{
private double deposit; //the deposit for this article
public DepositArticle(int id, String name, double basicPrice, int nrInStock,
double deposit)
: base(id, name, basicPrice, nrInStock)
{
this.deposit = deposit;
}
public override double getSellingPrice()
{
return this.getBasicPrice() + this.deposit;
}
public override String ToString()
{
String holder;
holder = "DEP " + base.ToString();
return holder;
}
}
}
CLASS ARTICLE.CS
namespace AppForAShop
{
abstract class Article : IArticle
{
private int id; //the unique id-number of this article
private String name; //the name of this article
private double basicPrice; //the basic price of this article
private int nrInStock; //the number in stock of this article
public Article(int id, String name, double basicPrice, int nrInStock)
{
this.id = id;
this.name = name;
this.basicPrice = basicPrice;
this.nrInStock = nrInStock;
}
public int getID() { return this.id; }
public double getBasicPrice() { return this.basicPrice; }
public abstract double getSellingPrice();
public void sellSome(int amount)
{
if (amount < 0) { throw new SellingException("amount should be positive");
}
if (this.nrInStock < amount) { throw new SellingException("not enough in
stock"); }
this.nrInStock -= amount;
}
public override String ToString()
{
String holder;
holder = this.name + " (" + this.id + ") in stock: " + this.nrInStock + "
selling price: " + this.getSellingPrice();
return holder;
}
}
}
FORM1.CS
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace AppForAShop
{
public partial class Form1 : Form
{
private Shop myShop;
public Form1()
{
InitializeComponent();
myShop = new Shop("Cinderella");
this.Text = myShop.Name + ", powered by Bert.";
this.addSomeTestingStuff2();
}
private void btnShowAllArticles_Click(object sender, EventArgs e)
{
this.listBox1.Items.Clear();
foreach (Article a in this.myShop.GetAllArticles())
{
this.listBox1.Items.Add(a);
}
}
private void btnAddArticle_Click(object sender, EventArgs e)
{
//todo, see assignment 3
int id = Convert.ToInt32(tbID.Text);
double bprice = Convert.ToDouble(tbBasicPrice.Text);
int inStock = Convert.ToInt32(tbInStock.Text);
Article a;
if (this.rbWine.Checked)
{
a = new Wine(id, tbName.Text, bprice, inStock,
Convert.ToInt32(tbYear.Text));
}
else if (this.rbDepositArticle.Checked)
{
a = new DepositArticle(id, tbName.Text, bprice, inStock,
Convert.ToDouble(tbDeposit.Text));
}
else
{//then it will be a wc-art
a = new WCArticle(id, tbName.Text, bprice, inStock,
cbDiscount.Checked);
}
if (this.myShop.AddArticle(a))
{
MessageBox.Show("succesfully added");
}
else
{
MessageBox.Show("not succesfully added");
}
}
private void btnSellSome_Click(object sender, EventArgs e)
{
//todo, see assignment 3
try
{
int idnr = Convert.ToInt32(this.tbID.Text);
int amount = Convert.ToInt32(this.tbAmount.Text);
double totalPrice = this.myShop.SellArticle(idnr, amount);
if (totalPrice != -1)
{
MessageBox.Show("total price is " + totalPrice);
}
else
{
MessageBox.Show("idnumber is not correct");
}
}
catch (FormatException) { MessageBox.Show("please fill in correct
numbers"); }
catch (SellingException se) { MessageBox.Show(se.Message); }
}
private void btnShowOldWine_Click(object sender, EventArgs e)
{
//todo, see assignment 3
this.listBox1.Items.Clear();
foreach (Article a in
this.myShop.GetListOfOldWine(Convert.ToInt32(this.tbYear.Text)))
{
this.listBox1.Items.Add(a);
}
}
private void btnLoadFromFile_Click(object sender, EventArgs e)
{
//todo, see assignment 3
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs = null;
StreamReader sr = null;
this.myShop.clearArticleList();
try
{
fs = new FileStream(this.openFileDialog1.FileName, FileMode.Open,
FileAccess.Read);
sr = new StreamReader(fs);
string s = sr.ReadLine();
while (s != null)
{
Article newArticle;
int id = Convert.ToInt32(sr.ReadLine());
string name = sr.ReadLine();
double basicPrice = Convert.ToDouble(sr.ReadLine());
int nrInStock = Convert.ToInt32(sr.ReadLine());
if (s == "wine")
{
int year = Convert.ToInt32(sr.ReadLine());
newArticle = new Wine(id, name, basicPrice, nrInStock,
year);
}
else if (s == "wcart")
{
String onDiscount = sr.ReadLine();
if (onDiscount == "discount yes")
newArticle = new WCArticle(id, name, basicPrice,
nrInStock, true);
else
newArticle = new WCArticle(id, name, basicPrice,
nrInStock, false);
}
else //now s == "dep"
{
double depositAmount = Convert.ToDouble(sr.ReadLine());
newArticle = new DepositArticle(id, name, basicPrice,
nrInStock, depositAmount);
}
this.myShop.AddArticle(newArticle);
s = sr.ReadLine();//for skipping the delimiter-line with =-
signs
s = sr.ReadLine();
}
}
catch (IOException) { MessageBox.Show("something wrong about file"); }
finally
{
if (sr!=null) sr.Close();
if (fs != null) fs.Close();
}
}
}
private void addSomeTestingStuff()
{
//this.myShop.AddArticle(new Article(11, "Chateaux Brie", 10, 20, 2014));
//this.myShop.AddArticle(new Article(22, "orange hat", 12.50, 100, true));
//this.myShop.AddArticle(new Article(93, "Gran Passione", 17.50, 12,
2006));
//this.myShop.AddArticle(new Article(44, "soundblaster", 49.95, 34,
false));
//this.myShop.AddArticle(new Article(17, "Hautes La Lande", 4.94, 31,
2016));
//this.myShop.AddArticle(new Article(34, "Vino Studente", 2.50, 95,
2012));
}
private void addSomeTestingStuff2()
{
this.myShop.AddArticle(new Wine(11, "Chateaux Brie", 10, 20, 2014));
this.myShop.AddArticle(new WCArticle(22, "orange hat", 12.50, 100, true));
this.myShop.AddArticle(new Wine(93, "Gran Passione", 17.50, 12, 2006));
this.myShop.AddArticle(new WCArticle(44, "soundblaster", 49.95, 34,
false));
this.myShop.AddArticle(new Wine(17, "Hautes La Lande", 4.94, 31, 2016));
this.myShop.AddArticle(new Wine(34, "Vino Studente", 2.50, 95, 2012));
this.myShop.AddArticle(new DepositArticle(77, "Bavaria beer", 2.50, 300,
0.25));
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}