Application For A Shop
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; } }
/// <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;
}
}
}
CLASS SELLINGEXCEPTION.CS
namespace AppForAShop
{
class SellingException : Exception
{
public SellingException(): base() { }
}
}
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
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
this.nrInStock -= amount;
}
}
}
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();
}
}
}
}
}
}